Skip to content

Commit

Permalink
Rename ReturnField to OptionalField
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeMac committed Jan 30, 2021
1 parent 4ee5eed commit 92f130a
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 17 deletions.
2 changes: 1 addition & 1 deletion internal/generate/gocode/gotemplate/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func signature(f truce.Function) string {
}

builder.WriteString(") (")
if rtn := f.Return; !rtn.NoReturn && rtn.Name != "" {
if rtn := f.Return; rtn.Present && rtn.Name != "" {
fmt.Fprintf(builder, "%s, ", goType(rtn.Type))
}
builder.WriteString("error)")
Expand Down
9 changes: 6 additions & 3 deletions internal/generate/gocode/gotemplate/func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,12 @@ 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)",
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 @@ -270,7 +270,7 @@ func NewFunction(config *truce.HTTP, function truce.Function) (*Function, error)
}
}

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

Expand Down
2 changes: 1 addition & 1 deletion openapi.cue
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ openapi3: {
responses: {
"200": {
description: "\(fnDef.name) operation 200 response"
if !fnDef.return.noReturn {
if fnDef.return.present {
content: {
"application/json": {
_#schemaObj & {schema: {_type: fnDef.return.type}}
Expand Down
14 changes: 8 additions & 6 deletions spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,17 @@ type HTTPError struct {
}

type Function struct {
Name string `cue:"name"`
Arguments []Field `cue:"arguments"`
Return struct {
Field
NoReturn bool `cue:"noReturn"`
} `cue:"return"`
Name string `cue:"name"`
Arguments []Field `cue:"arguments"`
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
6 changes: 3 additions & 3 deletions 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: *({noReturn: true}) | #ReturnField
return: *({present: false}) | #OptionalField
transports?: http?: #HTTPFunction
}

Expand All @@ -72,8 +72,8 @@ 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.
}

#ReturnField: {
noReturn: bool | *false
#OptionalField: {
present: bool | *true
#Field
}

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 92f130a

Please sign in to comment.