Skip to content

Commit

Permalink
Merge pull request #16 from TruceRPC/optional-return
Browse files Browse the repository at this point in the history
Optional return value
  • Loading branch information
GeorgeMac committed Jan 31, 2021
2 parents 201152d + 8350e5f commit 1eea92c
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 12 deletions.
12 changes: 12 additions & 0 deletions example/service.cue
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,18 @@ specifications: {
}
}
}
"Delete\(resourceName)": {
arguments: [
{name: "id", type: "string"},
]
transports: http: {
path: "/\(strings.ToLower(resourceName))s/{id}"
method: "DELETE"
arguments: {
id: {from: "path", var: "id"}
}
}
}
}
}
types: {
Expand Down
2 changes: 1 addition & 1 deletion internal/generate/gocode/gotemplate/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func unexportedSignature(f truce.Function) string {
func signatureReturn(f truce.Function) string {
builder := &strings.Builder{}
builder.WriteString("(")
if rtn := f.Return; rtn.Name != "" {
if rtn := f.Return; rtn.Present && rtn.Name != "" {
fmt.Fprintf(builder, "%s, ", goType(rtn.Type))
}
builder.WriteString("error)")
Expand Down
19 changes: 16 additions & 3 deletions internal/generate/gocode/gotemplate/func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,26 @@ func TestSignature(t *testing.T) {
Arguments: []truce.Field{
{Name: "a", Type: "string"},
},
Return: truce.Field{
Name: "x",
Type: "string",
Return: truce.OptionalField{
Present: true,
Field: truce.Field{
Name: "x",
Type: "string",
},
},
},
out: "do(ctxt context.Context, a string) (string, error)",
},
{
name: "no return value",
in: truce.Function{
Name: "do",
Arguments: []truce.Field{
{Name: "a", Type: "string"},
},
},
out: "do(ctxt context.Context, a string) (error)",
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion internal/generate/gocode/gotemplate/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ func NewFunction(config *truce.HTTP, function truce.Function) (*Function, error)
}
}

if function.Return.Name != "" {
if function.Return.Present && function.Return.Name != "" {
b.HasReturn = true
b.ReturnType = string(function.Return.Type)

Expand Down
8 changes: 5 additions & 3 deletions openapi.cue
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,11 @@ openapi3: {
responses: {
"200": {
description: "\(fnDef.name) operation 200 response"
content: {
"application/json": {
_#schemaObj & {schema: {_type: fnDef.return.type}}
if fnDef.return.present {
content: {
"application/json": {
_#schemaObj & {schema: {_type: fnDef.return.type}}
}
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,15 @@ type HTTPError struct {
type Function struct {
Name string `cue:"name"`
Arguments []Field `cue:"arguments"`
Return Field `cue:"return"`
Return OptionalField `cue:"return"`
Transports FunctionTransport `cue:"transports"`
}

type OptionalField struct {
Field
Present bool `cur:"present"`
}

// FunctionTransport to be defined at a later date.
type FunctionTransport struct {
HTTP *HTTPFunction `cue:"http"`
Expand Down
7 changes: 6 additions & 1 deletion truce.cue
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ specifications: [n=string]: [v=string]: #API & {name: n, version: v}
#Function: {
name: =~"^[A-Z][a-zA-Z]*$" // function names are capitalised
arguments: [...#Field]
return: #Field
return: *({present: false}) | #OptionalField
transports?: http?: #HTTPFunction
}

Expand All @@ -72,6 +72,11 @@ specifications: [n=string]: [v=string]: #API & {name: n, version: v}
type: or(#all) | =~"map[[][*]?[A-Za-z]+[]][*]?[A-Za-z]+" | =~"^[*]?[A-Z][a-zA-Z]*$" | =~"^[[][]][*]?[A-Z][a-zA-Z]*?" // can be primitive, Custom, *Custom, []primitive, []Custom, []*Custom.
}

#OptionalField: {
present: bool | *true
#Field
}

#HTTPFunction: {
path: string
method: "GET" | "POST" | "PUT" | "PATCH" | "OPTIONS" | "DELETE" | "HEAD" | "CONNECT" | "TRACE"
Expand Down
4 changes: 2 additions & 2 deletions truce.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1eea92c

Please sign in to comment.