Skip to content
This repository has been archived by the owner on Apr 15, 2020. It is now read-only.

Commit

Permalink
Sysl transformation for generating server (#3)
Browse files Browse the repository at this point in the history
* Add transform for router, handler, and service interface
	* Make minor modifications to grammar
	* Modify svc_types.gen.sysl according to grammer changes
	* Add library code to read params

* Generate todos server components

* Move manually written files to impl dir

* Modify makefile

* Modify structure of generated code

* Add sysl version of grammar and clean up transforms (#1)

* Add sysl version of grammar
* Validate code and fix validation failures

* Update grammar
  • Loading branch information
Kasun Fernando authored Jul 30, 2019
1 parent 24c4987 commit 0640634
Show file tree
Hide file tree
Showing 19 changed files with 1,457 additions and 66 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ linters-settings:
misspell:
locale: UK
lll:
line-length: 190
line-length: 300
gocritic:
enabled-tags:
- performance
Expand Down
52 changes: 42 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,20 +1,52 @@
.PHONY: all
all: todos
all: clean/todos mkdir/todos generate/todos gotools/todos compile

SYSLGEN=$(PWD)/syslgen
.PHONY: clean
clean: clean/todos

.PHONY: mkdir
mkdir: mkdir/todos

.PHONY: gotools
gotools: gotools/todos

.PHONY: generate
generate: generate/todos

.PHONY: compile
compile: todos_client/todos todos_server/todos

SYSLGEN=$(GOPATH)/bin/syslgen
TYPES_TRANSFORM = transforms/svc_types.gen.sysl
INTERFACE_TRANSFORM = transforms/svc_interface.sysl
HANDLER_TRANSFORM = transforms/svc_handler.sysl
ROUTER_TRANSFORM = transforms/svc_router.sysl
GRAMMAR = grammars/go.gen.g
TYPES_TRANSFORM_INPUT = $(TYPES_TRANSFORM) $(GRAMMAR)

todos: todos_client/todos
gen = $(SYSLGEN) -root-model . -root-transform . -transform $(1) -model examples/$(2).sysl -grammar $(GRAMMAR) -start goFile -outdir $(2)

todos_client/%: %/service.go %_client/main.go
-rm $*/$*
cd $*_client; go build -o $*; cd -
clean/%:
-rm $*_client/$*-client
-rm $*_server/$*-server

%/service.go: examples/%.sysl $(TYPES_TRANSFORM_INPUT)
todos_client/%:
cd $*_client; go build -o $*-client; cd -

todos_server/%:
cd $*_server; go build -o $*-server; cd -

mkdir/%:
echo "creating output dir" $*
-mkdir $*
$(SYSLGEN) -root-model . -root-transform . -transform $(TYPES_TRANSFORM) -model examples/$*.sysl -grammar $(GRAMMAR) -start goFile -outdir $*
gofmt -w $*/service.go
-mkdir -p $*

gotools/%:
gofmt -w $*/
goimports -w $*/
golangci-lint run $*

generate/%:
$(call gen,$(TYPES_TRANSFORM),$*)
$(call gen,$(INTERFACE_TRANSFORM),$*)
$(call gen,$(HANDLER_TRANSFORM),$*)
$(call gen,$(ROUTER_TRANSFORM),$*)
2 changes: 1 addition & 1 deletion examples/todos.sysl
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ Todos:
GET ?postId=int:
return Posts

POST (newPost <: Post):
POST (newPost <: Post [~body]):
return Post
17 changes: 11 additions & 6 deletions grammars/go.gen.g
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
goFile: PackageClause Comment? '\n' ImportDecl? '\n' TopLevelDecl+ '\n';
PackageClause: 'package' PackageName ';\n';
PackageClause: 'package' PackageName '\n';

ImportDecl: 'import' '(\n' ImportSpec* '\n)\n';
ImportSpec: (Import | NamedImport) '\n';
NamedImport: Name Import;
TopLevelDecl: Comment '\n' (Declaration | FunctionDecl | MethodDecl);
Declaration: VarDecl | ConstDecl | StructType | InterfaceType | AliasDecl;
Declaration: VarDecl | VarDeclWithVal | ConstDecl | StructType | InterfaceType | AliasDecl;
StructType : 'type' StructName 'struct' '{\n' FieldDecl* '}\n\n';
FieldDecl: '\t' identifier Type? Tag? '\n';
IdentifierList: identifier IdentifierListC*;
IdentifierListC: ',' identifier;

VarDecl: 'var' identifier '=' TypeName '\n';
VarDeclWithVal: 'var' identifier '=' Expression '\n';
VarDecl: 'var' identifier TypeName '\n';
ConstDecl: 'const' '(\n' ConstSpec '\n)\n';
ConstSpec: VarName TypeName '=' ConstValue '\n';

Expand All @@ -32,21 +33,25 @@ MethodDecl: 'func' Receiver FunctionName Signature? Block? '\n\n';
Receiver: '(' ReceiverType ')';
AliasDecl: 'type' identifier Type? ';\n\n';

Block: '{\n' StatementList* '}';
Block: '{\n' StatementList* '}\n';
StatementList: '\t' Statement '\n';
Statement: ReturnStmt | DeclareAndAssignStmt | AssignStmt | IfElseStmt | IncrementVarByStmt | FunctionCall;
Statement: ReturnStmt | DeclareAndAssignStmt | AssignStmt | IfElseStmt | IncrementVarByStmt | FunctionCall | VarDecl | ForLoop;

AssignStmt: Variables '=' Expression;
IfElseStmt: 'if' Expression Block;
IncrementVarByStmt: Variables '+=' Expression;
ReturnStmt: 'return' (PayLoad | Expression);
DeclareAndAssignStmt: Variables ':=' Expression;

Expression: FunctionCall | NewStruct | GetArg | ValueExpr | NewSlice;
Expression: FunctionCall | NewStruct | GetArg | ValueExpr | NewSlice | Map;

GetArg: LHS '.' RHS;
NewSlice: '[]' TypeName '{' SliceValues? '}';
Map: 'map[' KeyType ']' ValType '{\n' KeyValuePairs? '\n}';
KeyValuePairs: KeyValuePair*;
KeyValuePair: Key ':' Value ',\n';
FunctionCall: FunctionName '(' FunctionArgs? ')';
FunctionArgs: Expression FuncArgsRest*;
FuncArgsRest: ',' Expression;
NewStruct: StructName '{}';
ForLoop: '\nfor' LoopVar ':=' 'range' Variable Block;
245 changes: 245 additions & 0 deletions grammars/go.gen.sysl
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
Go:
!type goFile:
PackageClause <: PackageClause
Comment <: string?
ImportDecl <: ImportDecl?
TopLevelDecl(1..) <: TopLevelDecl

!type PackageClause:
PackageName <: string

!type NamedImport:
Name <: string
Import <: string

!union __Choice_ImportSpec:
Import
NamedImport

!type ImportDecl:
ImportSpec <: sequence of ImportSpec?

!type ImportSpec:
__Choice_ImportSpec <: __Choice_ImportSpec

!union __Choice_TopLevelDecl:
Declaration
FunctionDecl
MethodDecl

!type TopLevelDecl:
Comment <: string
__Choice_TopLevelDecl <: __Choice_TopLevelDecl

!union __Choice_Declaration:
VarDecl
VarDeclWithVal
ConstDecl
StructType
InterfaceType
AliasDecl

!type Declaration:
__Choice_Declaration <: __Choice_Declaration

!type StructType:
StructName <: string
FieldDecl <: sequence of FieldDecl?

!type FieldDecl:
identifier <: string
Type <: string?
Tag <: string?

!type IdentifierList:
identifier <: string
IdentifierListC <: sequence of IdentifierListC?

!type IdentifierListC:
identifier <: string

!type VarDeclWithVal:
identifier <: string
Expression <: Expression

!type VarDecl:
identifier <: string
TypeName <: string

!type ConstDecl:
ConstSpec <: ConstSpec

!type ConstSpec:
VarName <: string
TypeName <: string
ConstValue <: string

!type FunctionDecl:
FunctionName <: string
Signature <: Signature?
Block <: Block

!type Signature:
Parameters <: Parameters
Result <: Result?

!type Parameters:
ParameterList <: ParameterList?

!union __Choice_Result:
ReturnTypes
TypeName

!type Result:
__Choice_Result <: __Choice_Result

!type ReturnTypes:
TypeName <: string
ResultTypeList <: sequence of ResultTypeList?

!type ResultTypeList:
TypeName <: string

!type TypeList:
TypeName <: string

!type ParameterList:
ParameterDecl <: ParameterDecl
ParameterDeclC <: sequence of ParameterDeclC?

!type ParameterDecl:
Identifier <: string
TypeName <: string

!type ParameterDeclC:
ParameterDecl <: ParameterDecl

!type InterfaceType:
InterfaceName <: string
MethodSpec <: sequence of MethodSpec?
MethodDecl <: sequence of MethodDecl?

!alias InterfaceTypeName:
string

!type __Choice_Combination_MethodSpec_MethodName_Signature:
MethodName <: string
Signature <: Signature

!union __Choice_MethodSpec:
__Choice_Combination_MethodSpec_MethodName_Signature
InterfaceTypeName

!type MethodSpec:
__Choice_MethodSpec <: __Choice_MethodSpec

!type MethodDecl:
Receiver <: Receiver
FunctionName <: string
Signature <: Signature?
Block <: Block?

!type Receiver:
ReceiverType <: string

!type AliasDecl:
identifier <: string
Type <: string?

!type Block:
StatementList <: sequence of StatementList?

!type StatementList:
Statement <: Statement

!union __Choice_Statement:
ReturnStmt
DeclareAndAssignStmt
AssignStmt
IfElseStmt
IncrementVarByStmt
FunctionCall
VarDecl
ForLoop

!type Statement:
__Choice_Statement <: __Choice_Statement

!type AssignStmt:
Variables <: string
Expression <: Expression

!type IfElseStmt:
Expression <: Expression
Block <: Block

!type IncrementVarByStmt:
Variables <: string
Expression <: Expression

!alias PayLoad:
string

!union __Choice_ReturnStmt:
PayLoad
Expression

!type ReturnStmt:
__Choice_ReturnStmt <: __Choice_ReturnStmt

!type DeclareAndAssignStmt:
Variables <: string
Expression <: Expression

!alias ValueExpr:
string

!union __Choice_Expression:
FunctionCall
NewStruct
GetArg
ValueExpr
NewSlice
Map

!type Expression:
__Choice_Expression <: __Choice_Expression

!type GetArg:
LHS <: string
RHS <: string

!type NewSlice:
TypeName <: string
SliceValues <: string?

!type Map:
KeyType <: string
ValType <: string
KeyValuePairs <: KeyValuePairs?

!type KeyValuePairs:
KeyValuePair <: sequence of KeyValuePair?

!type KeyValuePair:
Key <: string
Value <: string

!type FunctionCall:
FunctionName <: string
FunctionArgs <: FunctionArgs?

!type FunctionArgs:
Expression <: Expression
FuncArgsRest <: sequence of FuncArgsRest?

!type FuncArgsRest:
Expression <: Expression

!type NewStruct:
StructName <: string

!type ForLoop:
LoopVar <: string
Variable <: string
Block <: Block
18 changes: 18 additions & 0 deletions lib/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package restlib

import (
"net/http"

"github.com/go-chi/chi"
)

func GetURLParam(r *http.Request, key string) string {
return chi.URLParam(r, key)
}
func GetQueryParam(r *http.Request, key string) string {
return r.URL.Query().Get(key)
}

func GetHeaderParam(r *http.Request, key string) string {
return r.Header.Get(key)
}
Loading

0 comments on commit 0640634

Please sign in to comment.