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.
- Loading branch information
0 parents
commit c08d1d0
Showing
7 changed files
with
536 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
.PHONY: all | ||
all: todos | ||
|
||
SYSLGEN=$(PWD)/syslgen | ||
REST_TRANSFORM = transforms/svc_interface.gen.sysl | ||
TYPES_TRANSFORM = transforms/svc_types.gen.sysl | ||
GRAMMAR = grammars/go.gen.g | ||
|
||
REST_TRANSFORM_INPUT = $(REST_TRANSFORM) $(GRAMMAR) | ||
TYPES_TRANSFORM_INPUT = $(TYPES_TRANSFORM) $(GRAMMAR) | ||
|
||
todos: todos/service.go | ||
|
||
%/service.go: examples/%.sysl $(TYPES_TRANSFORM_INPUT) %/main.go | ||
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 | ||
cd $*/; go build -o $*; cd - |
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,44 @@ | ||
# SYSLGEN Examples | ||
|
||
Examples of rest api code generation using `syslgen`. | ||
|
||
See `examples/todos.sysl` for the rest api description that has been created for the following service : `http://jsonplaceholder.typicode.com/` | ||
|
||
## How to build code | ||
|
||
### Get syslgen binary | ||
|
||
`syslgen` is expected to be available as a download in near future. | ||
|
||
Currently the only way to get syslgen binary is by building it like so: | ||
|
||
Assumes you have Go installed. | ||
``` | ||
export GOPATH=$HOME/gopath | ||
export SYSLBASE=$HOME/gopath/src/github.com/anz-bank | ||
mkdir -p $SYSLBASE | ||
cd $SYSLBASE | ||
git clone https://github.com/anz-bank/sysl/ | ||
cd $SYSLBASE/sysl/sysl2 | ||
go get -t -v github.com/anz-bank/sysl/sysl2/sysl | ||
go install -v | ||
ln -s $GOPATH/bin/sysl syslgen | ||
``` | ||
### Build code | ||
|
||
Checkout the code and run the following commands in the root of the repository. | ||
|
||
```bash | ||
make | ||
``` | ||
The make command will generate `todos/service.go` and build the binary `todos/todos` | ||
|
||
Try to run the example like so: | ||
|
||
``` | ||
./todos/todos | ||
``` | ||
|
||
This client calls the generated service method `GET_todos_id`. | ||
|
||
|
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,31 @@ | ||
Todos: | ||
!type Todo: | ||
userId <: int | ||
id <: int | ||
title <: string | ||
completed <: bool | ||
|
||
!type Post: | ||
userId <: int | ||
id <: int | ||
title <: string | ||
body <: string | ||
|
||
!alias Posts: | ||
sequence of Post | ||
|
||
/todos: | ||
/{id<:int}: | ||
GET: | ||
return Todo | ||
|
||
/posts: | ||
GET: | ||
return Posts | ||
|
||
/comments: | ||
GET ?postId=int: | ||
return Posts | ||
|
||
POST (newPost <: Post): | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
goFile: PackageClause '\n' ImportDecl? '\n' TopLevelDecl+ '\n'; | ||
PackageClause: 'package' PackageName ';\n'; | ||
|
||
ImportDecl: 'import' '(\n' ImportSpec* '\n)\n'; | ||
ImportSpec: Import '\n'; | ||
TopLevelDecl: Comment '\n' (Declaration | FunctionDecl | MethodDecl); | ||
Declaration: VarDecl | 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' IdentifierList TypeName; | ||
ConstDecl: 'const' '(\n' ConstSpec '\n)\n'; | ||
ConstSpec: VarName TypeName '=' ConstValue '\n'; | ||
|
||
FunctionDecl : 'func' FunctionName Signature? Block? ; | ||
Signature: Parameters Result?; | ||
Parameters: '(' ParameterList? ')'; | ||
Result : ReturnTypes | TypeName; | ||
ReturnTypes: '(' ResultTypeList 'error' ')'; | ||
ResultTypeList: TypeName ','; | ||
TypeList: TypeName; | ||
ParameterList : ParameterDecl ParameterDeclC*; | ||
ParameterDecl : Identifier TypeName; | ||
ParameterDeclC: ',' ParameterDecl; | ||
|
||
InterfaceType : 'type' InterfaceName 'interface' '{\n' MethodSpec* '}\n\n' MethodDecl*; | ||
MethodSpec : '\t' MethodName Signature '\n' | InterfaceTypeName ; | ||
MethodDecl: 'func' Receiver FunctionName Signature? Block? '\n'; | ||
Receiver: '(' ReceiverType ')'; | ||
|
||
Block: '{\n' StatementList* '}\n\n'; | ||
StatementList: Statement ';\n'; | ||
Statement: ReturnStmt | FunctionCall | DeclareAndAssignStmt | NewStruct; | ||
ReturnStmt: 'return' (PayLoad | FunctionCall); | ||
FunctionCall: FunctionName '(' FunctionArgs? ')'; | ||
DeclareAndAssignStmt: Variables ':=' Statement; | ||
NewStruct: StructName '{}'; | ||
AliasDecl: 'type' identifier Type? ';\n\n'; | ||
|
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,42 @@ | ||
package restlib | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
) | ||
|
||
func interpretResponse(resp *http.Response, res interface{}) error { | ||
defer resp.Body.Close() | ||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
panic(err) | ||
} | ||
err = json.Unmarshal(body, res) | ||
if err != nil { | ||
fmt.Printf("Error: %s\n", err.Error()) | ||
} | ||
return err | ||
} | ||
|
||
// DoPost does the http POST call | ||
func DoPost(url string, request interface{}, response interface{}) error { | ||
reqJson, err := json.Marshal(request) | ||
reader := bytes.NewReader(reqJson) | ||
resp, err := http.Post(url, "application/json", reader) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return interpretResponse(resp, response) | ||
} | ||
|
||
// DoGet does the http GET call | ||
func DoGet(url string, res interface{}) error { | ||
resp, err := http.Get(url) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return interpretResponse(resp, res) | ||
} |
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,20 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
func main() { | ||
httpClient := http.Client{} | ||
client := MakeTodosClient(&httpClient, "http://jsonplaceholder.typicode.com") | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
res, err := client.GET_todos_id(ctx, 1) | ||
if err != nil { | ||
fmt.Printf("Error: %s\n", err.Error()) | ||
} else { | ||
fmt.Printf("Value: \n%+v\n", res) | ||
} | ||
} |
Oops, something went wrong.