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

Commit

Permalink
Modified transform (#4)
Browse files Browse the repository at this point in the history
* Modified transform

- Capitalize first letter of body param names
- Modified router to use an external router pointer to wire routes
- Avoid trimming 'EXTERNAL' from non-generated types
- Generate default implementation
- Rename lib as restlib to match with package name
- Update transforms removing named imports
- Generate todos client and server with updated transforms
  • Loading branch information
Kasun Fernando authored Aug 7, 2019
1 parent 8aa2f97 commit e8f5b81
Show file tree
Hide file tree
Showing 12 changed files with 219 additions and 66 deletions.
2 changes: 1 addition & 1 deletion grammars/go.gen.g
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ InterfaceType : 'type' InterfaceName 'interface' '{\n' MethodSpec* '}\n\n
MethodSpec : '\t' MethodName Signature '\n' | InterfaceTypeName ;
MethodDecl: 'func' Receiver FunctionName Signature? Block? '\n\n';
Receiver: '(' ReceiverType ')';
AliasDecl: 'type' identifier Type? ';\n\n';
AliasDecl: 'type' identifier Type? '\n\n';

Block: '{\n' StatementList* '}\n';
StatementList: '\t' Statement '\n';
Expand Down
File renamed without changes.
File renamed without changes.
6 changes: 2 additions & 4 deletions todos/requestrouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

// Router interface for Todos
type Router interface {
Route() *chi.Mux
Route(router *chi.Mux)
}

// ServiceRouter for Todos API
Expand All @@ -24,11 +24,9 @@ func NewServiceRouter(svcHandler *ServiceHandler) *ServiceRouter {
}

// Route ...
func (r *ServiceRouter) Route() *chi.Mux {
router := chi.NewRouter()
func (r *ServiceRouter) Route(router *chi.Mux) {
router.Get("/comments", r.svcHandler.GetCommentsHandler)
router.Get("/posts", r.svcHandler.GetPostsHandler)
router.Get("/todos/{id}", r.svcHandler.GetTodosIDHandler)
router.Post("/comments", r.svcHandler.PostCommentsHandler)
return router
}
2 changes: 1 addition & 1 deletion todos/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"net/url"
"time"

restlib "github.com/anz-bank/syslgen-examples/lib"
"github.com/anz-bank/syslgen-examples/restlib"
"github.com/rickb777/date"
)

Expand Down
2 changes: 1 addition & 1 deletion todos/servicehandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"encoding/json"
"net/http"

restlib "github.com/anz-bank/syslgen-examples/lib"
"github.com/anz-bank/syslgen-examples/restlib"
)

// Handler interface for Todos
Expand Down
46 changes: 46 additions & 0 deletions todos/serviceinterface.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,19 @@ package todos
//

import (
"errors"
"net/http"
)

// DefaultTodosImpl ...
type DefaultTodosImpl struct {
}

// NewDefaultTodosImpl for Todos
func NewDefaultTodosImpl() *DefaultTodosImpl {
return &DefaultTodosImpl{}
}

// ServiceInterface for Todos
type ServiceInterface interface {
GetComments(PostID string) (int, map[string]string, *Posts)
Expand All @@ -17,3 +27,39 @@ type ServiceInterface interface {
IsAuthorized(r *http.Request, authHeader string) bool
GetErrorResponse(statusCode int, message string, errObj error) interface{}
}

// nolint:gocritic
// GetComments ...
func (d *DefaultTodosImpl) GetComments(PostID string) (int, map[string]string, *Posts) {
panic(errors.New("not implemented"))
}

// nolint:gocritic
// GetPosts ...
func (d *DefaultTodosImpl) GetPosts() (int, map[string]string, *Posts) {
panic(errors.New("not implemented"))
}

// nolint:gocritic
// GetTodosID ...
func (d *DefaultTodosImpl) GetTodosID(ID string) (int, map[string]string, *Todo) {
panic(errors.New("not implemented"))
}

// nolint:gocritic
// PostComments ...
func (d *DefaultTodosImpl) PostComments(newPost Post) (int, map[string]string, *Post) {
panic(errors.New("not implemented"))
}

// nolint:gocritic
// IsAuthorized ...
func (d *DefaultTodosImpl) IsAuthorized(r *http.Request, authHeader string) bool {
panic(errors.New("not implemented"))
}

// nolint:gocritic
// GetErrorResponse ...
func (d *DefaultTodosImpl) GetErrorResponse(statusCode int, message string, errObj error) interface{} {
panic(errors.New("not implemented"))
}
4 changes: 3 additions & 1 deletion todos_server/main-server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"github.com/go-chi/chi"
"log"
"net/http"

Expand All @@ -13,7 +14,8 @@ func main() {
serviceImpl := impl.NewServiceImpl()
svcHandler := todos.NewServiceHandler(serviceImpl)
serviceRouter := todos.NewServiceRouter(svcHandler)
router := serviceRouter.Route()
router := chi.NewRouter()
serviceRouter.Route(router)

log.Fatal(http.ListenAndServe(":8080", router))
}
12 changes: 3 additions & 9 deletions transforms/svc_handler.sysl
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ CodeGenTransform:
isOptional = true
)
let bodyParams = ep.value.params where("body" in .attrs.patterns) -> <BodyParams> (param:
paramType = param.type_ref
paramType = GoName(param.type_ref).out
paramName = param.name
varName = param.name
)
Expand Down Expand Up @@ -395,16 +395,10 @@ CodeGenTransform:
)

ImportDecl = app -> <ImportDecl>(:
let spec = ["encoding/json", "net/http"] -> <ImportSpec> (importPath:
let spec = ["encoding/json", "net/http", "github.com/anz-bank/syslgen-examples/restlib"] -> <ImportSpec> (importPath:
Import = if importPath == "" then true else '"' + importPath + '"'
)
let named = app -> <ImportDecl>(:
NamedImport = app -> <NamedImport>(:
Name = "restlib"
Import = '"' + "github.com/anz-bank/syslgen-examples/lib" + '"'
)
)
ImportSpec = spec | [named]
ImportSpec = spec
)

let serviceImplVarname = "serviceInterface"
Expand Down
159 changes: 153 additions & 6 deletions transforms/svc_interface.sysl
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,29 @@ CodeGenTransform:
methodName = GoName(method + Join(methodPostfix flatten(.out), "")).out
)

!view panicBlock(methodName <: string) -> StatementList:
methodName -> (:
StatementList = [methodName] -> <StatementList> (:
Statement = methodName -> <Statement> (:
FunctionCall = methodName -> <FunctionCall> (:
FunctionName = "panic"
FunctionArgs = methodName -> <FunctionArgs> (:
Expression = methodName -> <Expression> (:
FunctionCall = methodName -> <FunctionCall> (:
FunctionName = "errors.New"
FunctionArgs = methodName -> <FunctionArgs> (:
Expression = methodName -> <Expression> (:
ValueExpr = '"not implemented"'
)
)
)
)
)
)
)
)
)

!view getAllParams(ep <: sysl.Endpoint) -> sequence of AllParams:
ep -> (:
let queryParams = ep.value.queryvars -> <QueryParams> (param:
Expand All @@ -73,7 +96,7 @@ CodeGenTransform:
paramName = GoName(param.name).out
)
let bodyParams = ep.value.params where("body" in .attrs.patterns) -> <BodyParams> (param:
arg = param.name + " " + param.type_ref
arg = param.name + " " + GoName(param.type_ref).out
)
AllParams = queryParams flatten(.arg) | pathParams flatten(.arg) | bodyParams flatten(.arg) | reqHeaderParams flatten(.paramName) | optHeaderParams flatten(.paramName)
)
Expand All @@ -90,14 +113,44 @@ CodeGenTransform:
)
Result = ep -> <Result>(:
let resultTypes = Split(ep.value.ret.payload, ', ') -> <sequence of string> (:
typeName = '*' + .
typeName = '*' + GoName(.).out
)

TypeName = if ep.value.ret.payload == "" then "int, map[string]string" else "(int, map[string]string," + Join(resultTypes flatten(.typeName),", ") + ")"
)
)
)


!view epHandlerMethodDecl(eps <: set of sysl.Endpoints, receiverType <: string) -> TopLevelDecl:
eps -> (ep:
let allParams = getAllParams(ep)
let methodName = methodDef(ep).methodName
Comment = "// nolint:gocritic\n// " + methodName + " ..."
MethodDecl = ep -> <MethodDecl>(:
Receiver = ep -> <Receiver>(:
ReceiverType = "d *" + receiverType
)
FunctionName = methodName

Signature = ep -> <Signature> (:
Parameters = ep -> <Parameters>(:
ParameterList = ep -> <ParameterList>(:
ParameterDecl = if ep.value.params where("header" in .attrs.patterns) count > 0 then paramDecl(Join(allParams.AllParams, ", "), "string").ParameterDecl else paramDecl(Join(allParams.AllParams, ", "), "").ParameterDecl
)
)
Result = ep -> <Result>(:
let resultTypes = Split(ep.value.ret.payload, ', ') -> <sequence of string> (:
typeName = '*' + GoName(.).out
)

TypeName = if ep.value.ret.payload == "" then "int, map[string]string" else "(int, map[string]string," + Join(resultTypes flatten(.typeName),", ") + ")"
)
)

Block = panicBlock(methodName)
)
)

!view authHeaderMethodSpec(methodName <: string) -> MethodSpec:
methodName -> (:
MethodName = methodName
Expand All @@ -115,6 +168,32 @@ CodeGenTransform:
)
)

!view authHeaderMethodDecl(methodName <: string, receiverType <: string) -> TopLevelDecl:
methodName -> (:
Comment = "// nolint:gocritic\n// " + methodName + " ..."
MethodDecl = methodName -> <MethodDecl>(:
Receiver = methodName -> <Receiver>(:
ReceiverType = "d *" + receiverType
)
FunctionName = methodName

Signature = methodName -> <Signature> (:
Parameters = methodName -> <Parameters>(:
ParameterList = methodName -> <ParameterList>(:
ParameterDecl = paramDecl("r", "*http.Request").ParameterDecl
ParameterDeclC = [paramDecl("authHeader", "string")]
)
)

Result = methodName -> <Result>(:
TypeName = "bool"
)
)

Block = panicBlock(methodName)
)
)

!view errResponseMethodSpec(methodName <: string) -> MethodSpec:
methodName -> (:
MethodName = methodName
Expand All @@ -132,6 +211,31 @@ CodeGenTransform:
)
)

!view errResponseMethodDecl(methodName <: string, receiverType <: string) -> TopLevelDecl:
methodName -> (:
Comment = "// nolint:gocritic\n// " + methodName + " ..."
MethodDecl = methodName -> <MethodDecl>(:
Receiver = methodName -> <Receiver>(:
ReceiverType = "d *" + receiverType
)
FunctionName = methodName
Signature = methodName -> <Signature> (:
Parameters = methodName -> <Parameters>(:
ParameterList = methodName -> <ParameterList>(:
ParameterDecl = paramDecl("statusCode", "int").ParameterDecl
ParameterDeclC = [paramDecl("message", "string"), paramDecl("errObj", "error")]
)
)

Result = methodName -> <Result> (:
TypeName = "interface{}"
)
)

Block = panicBlock(methodName)
)
)

!view goFile(app <: sysl.App) -> goFile:
app -> (:

Expand All @@ -141,7 +245,7 @@ CodeGenTransform:
)

ImportDecl = app -> <ImportDecl>(:
let spec = ["net/http"] -> <ImportSpec> (importPath:
let spec = ["net/http", "errors"] -> <ImportSpec> (importPath:
Import = if importPath == "" then true else '"' + importPath + '"'
)
ImportSpec = spec
Expand All @@ -157,8 +261,51 @@ CodeGenTransform:
)
)
)

let structName = "Default" + GoName(.name).out + "Impl "

let defaultImplStruct = [.name] -> <TopLevelDecl> (name:
Comment = "// " + structName + " ..."
Declaration = name -> <Declaration>(:
StructType = name -> <StructType>(:
StructName = structName
)
)
)

let newDefaultImpl = [.name] -> <TopLevelDecl> (name:
let structName = "Default" + GoName(name).out + "Impl"
let funcName = "New" + structName
Comment = "// " + funcName + " for " + name
FunctionDecl = name -> <FunctionDecl>(:
FunctionName = funcName
Signature = name -> <Signature> (:
Parameters = name -> <Parameters>(:
ParameterList = name -> <ParameterList>(:
ParameterDecl = name -> <ParameterDecl>(:
Identifier = ""
TypeName = ""
)
)
)

Result = name -> <Result> (:
TypeName = "*"+structName
)
)
Block = name -> <Block>(item:
StatementList = [item] -> <StatementList>(item:
Statement = item -> <Statement> (:
ReturnStmt = item -> <ReturnStmt> (:
PayLoad = "&" + structName + "{}"
)
)
)
)
)
)

Comment = "//\n// THIS IS AUTOGENERATED BY syslgen \n//\n"
TopLevelDecl = svcImplInterface

TopLevelDecl = defaultImplStruct | newDefaultImpl | svcImplInterface | epHandlerMethodDecl(app.endpoints, structName) | [authHeaderMethodDecl("IsAuthorized", structName), errResponseMethodDecl("GetErrorResponse", structName)]
)
Loading

0 comments on commit e8f5b81

Please sign in to comment.