This repository has been archived by the owner on Apr 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sysl transformation for generating server (#3)
* 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
Showing
19 changed files
with
1,457 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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),$*) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,5 +27,5 @@ Todos: | |
GET ?postId=int: | ||
return Posts | ||
|
||
POST (newPost <: Post): | ||
POST (newPost <: Post [~body]): | ||
return Post |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.