Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional return value #16

Merged
merged 6 commits into from
Jan 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.