diff --git a/example/service.cue b/example/service.cue index d8c21ae..95dcc38 100644 --- a/example/service.cue +++ b/example/service.cue @@ -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: { diff --git a/internal/generate/gocode/gotemplate/func.go b/internal/generate/gocode/gotemplate/func.go index 743a3fd..727d3f4 100644 --- a/internal/generate/gocode/gotemplate/func.go +++ b/internal/generate/gocode/gotemplate/func.go @@ -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)") diff --git a/internal/generate/gocode/gotemplate/func_test.go b/internal/generate/gocode/gotemplate/func_test.go index 36b93fb..5b59047 100644 --- a/internal/generate/gocode/gotemplate/func_test.go +++ b/internal/generate/gocode/gotemplate/func_test.go @@ -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) { diff --git a/internal/generate/gocode/gotemplate/http.go b/internal/generate/gocode/gotemplate/http.go index 65a0193..e426ce3 100644 --- a/internal/generate/gocode/gotemplate/http.go +++ b/internal/generate/gocode/gotemplate/http.go @@ -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) diff --git a/openapi.cue b/openapi.cue index 0e2d802..18f5790 100644 --- a/openapi.cue +++ b/openapi.cue @@ -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}} + } } } } diff --git a/spec.go b/spec.go index c2f0ae7..9acbbf5 100644 --- a/spec.go +++ b/spec.go @@ -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"` diff --git a/truce.cue b/truce.cue index 37b869c..9aa0b00 100644 --- a/truce.cue +++ b/truce.cue @@ -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 } @@ -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" diff --git a/truce.go b/truce.go index 85e6edd..ebe620b 100644 --- a/truce.go +++ b/truce.go @@ -41,5 +41,5 @@ func cuegenMake(name string, x interface{}) cue.Value { return v } -// Data size: 1988 bytes. -var cuegenInstanceData = []byte("\x01\x1f\x8b\b\x00\x00\x00\x00\x00\x00\xff\xbcY\xffo\u06f8\x15\xb7\xd2\x0e\x98\x84\xdb~\xdf\x01\x03\x18\xba(\xec\xd4Q\xb2f_0a\xb9\xc0K\xdck\x80[c\xb4\xde\xfd0E-\x18\x99v\xd8\u04f7\xa3\xa8\\\xd3\xd6\al\xbb\xdd\xf6W_\x86GR\x12%\xdbI\xd3\x06\xd7_B?\xbe\xf7y\x1f\xbe\xf7H>\xb1\xbf\xba\xfa\uf1b5q\xf5\xbf\x8eu\xf5\xafN\xe7OW\xff\xbcgY\x9f\xb1$\x17$\t\xe9\x11\x11\x04\xe4\xd6=\xeb\xfe\xf34\x15\xd6F\u01fa?&\xe2\xdc\xfa\xacc\xfd\xe2\t\x8bhn]\xfd\xd8\xe9t~{\xf5\x9f\r\xcb\xfa\xb5\x1f\x84\x05ug,\u0496?v\xac\xab\x1f:\x9d\xde\u057f\xefY\xd6/k\xf9\x0f\x1dk\u00fa\xff\x8c\xc4\x14\x80\xeeK\xa1\xd3\xe9t~\xfa<\x04&\x96e\xfdf\xce\xc4yq\xe6\x86i\xbc3\xe1EH\x9f\x8f\x0fw\x04\f,\u02f2\x8b$&`h\xde\x1a\xa5\xa2_\u0099d\x97\xc9,q\x01\x15U\u02f2bq\u0164,p=\xbb\xe4s\x15\xfd\x0fgoF}\xe1,\x9c4\xa3\t\xc9\u061e\x8c\xe3,\xe5\x88d\f\xba\x95\x01\f\xbe\xa6iF}V$a]{\u057a\x13I\x96\x93$\x87f8w\u03c5\xc8\\\x80m\xa6\x02\x9f\xd6\xf7Y\xfaU\xfa\x1d\xe5kLc*\xce\xd3i\xbf\x95HH\x97\xac\xfc\u3a67\x19\u0593\xaf2\xc2IL\x05\u5e47|$\xb7\x05\x9fk\xf6\xca\v\xe1\xf3\"\x86\xbak\x84\x9d\xcd\xd0j\x12\x95\xba\xaf\x80\u0704\xc44\x80\x83\")\xa2\b\xba\x80\xdb\u06393\x9e\xc6\xfa\x94\xf9h\u04f3tz\x89\x9bE\x03\x1ar\x0f\xd6\x06\u038aB\xfb\x84j\xd4\u0237\xafE\x9b%\xe5E\x88\xd0\xedWl\"Mi\x1er\x96\ty\xae\xc0\x99U+\xf7\x91\f\u03fa*\xbc\xc6A\x1f7\xc8\u07ae\x0eTR\xa0\xab\x82:o%\xc5\xe6\xf4\u06c2q:\xf5\xaaNjU\xd4\xcc\u034a\x02\xa3\x96\x934\x19\u0159\xb8\x1c/\xd5t\x06\xe5l\xd4:b3\x14\u0464\x97\xf5\xd1\x17h\x17\xbdC\x19jBA\xc9,\x9fo\x1f\xb65~\u07bd\xb1\xbf\xba\xc0\xed\xeb*w]\xe9\xdeT\xbb\xcd\xe2m\x95\xf2M\u01e9\xcdi\x9e\xa5I\u07be8\xf0\xe3\xdd]\u0722\xd2.\\\x15\x16U\xb7\xd5y\x86\x1e\xef\xee\xa2\x12\xb4Q\x94a\x9a\b\x9a\x88\xf6\xfa0\u0272H\xb7\x01;\xaf\xf34i\xbb\xbd!jk\xc3V\xdd@\x92&\xa7\xa2\xe0\u024a\xe8\xb5\xc2\xf7i\xd1\xd4\x15\xbc\xa2\xe8uM\x1b\b\xe6!\xbf\xc2\xe0Z|\xa8\xadeD\u06294\x17\u007f]\xda&\x1f\x98:m\x8fd\xe5~d\xea$\xb5[\x05\xed\xfa\x1b\xbd\xdd_6\xdbF\xf5\n\x95\xec7\u07a1\xfc\x8b\xfaww8>.k\xa6\xbc^\x90\xec\xe1\xaa\xd6\xee\xc2h^\v\x91\x15\xe2f\xd4&\\\xfd\xf0T\xb6\x9a\a\x1e\xea\x9ed4\x19\x8e\x8fO$d\xc3!\xda\xff\x1e\xbf\xf4w\xb7\xff\x1c\xa7\u00b1s\xca/(\aV\x93\u02ccNKq\x181\x9a\x88\xb6x\xe14\xb9KL\xb8\x18\x8cw7\xf4\x1emaw'\xff\x8e\xcc\u753b2\xf7NMm\x0f@\x14\x9cam\x98\xfe%\x17\u04f4\x10_`\xc7\u03be\x99{\rXI\x1fK\x84\x9aU\xb5\xb0\x06\xcc\v\xb9.\xec\xd8\u075a\xfbp|,\x95\xab\xa8\x95\u0669\xd85\xc3/\xdd\xe9\x14\x1b)\x97\u039b\x19H\u0280\xdbU\xeb\xb8\xc2\ue25e[g[_#\a\xca\x1a.\x13\xc8\xc1\xd3\xc9d\xac\xd3\tC9\xa99\u00d5\u9eaev\xe2\u0619|=\xf1\xaa\xa5Q\xceS^\x92\t\r2\x004\x82\u0252M.\x88(\xf2\xc3tJ=\x14\x1a\x05T\xb26B'\xc34\xdc\xfeG\xe0\x93\xed\xb7\xf0W>\n\\K\xbfD\x91\v\xad\xeeGE\xbe\xfb\x04\xbe\x04\x02\xc7V'1D\n\x04e\x9eov\xac>}\xe4R\xb1\xcc~.x!\xbf\x82\xd4'\u032a\\\xc0\u011aD\xe80\xcb\xe8HS34\x92\xc2\xef\xb6\xff\x10\xc8:y\xf7xQ3@\u6587\u0201\x8f6{\xb2\xfdv\x15\xfb\x94\xf7\xba$\x8a\xfa\xe8=\xa8\xa9\xc7\x1b\u007f+8\x80\u0542\xc9#?h\xfe\xc6J\xf3e)4A\xcb)\xf9\xf0\xb4\xacp\x80\xcb%6r[n\xe4\xb2t\xd4\u7247\xf0\x97\xa3\t@\xe2\xf1\xc9\v=\xf8\xbb\xfe;\x9c\x1c>\x95\xa3\x93\xf1\xe4\xf8\xe4\xd9\v9>\x1a}5\x9a\x8c\xe4\xf0\xe9hx$\a\x87'\u03de\x8d\x0e\x95\xd5\xe4\xf9\xf0p\x84\x1bU\xb0\x94\x9e\xa1\x9e[\x9f\xa1\x8c\xb3\x98\tv\x01;\xd4W\x0f\xaa\x03\xf5D;\xa8\x1fE\a\xfa\x05rP=\xec\r\xaaO\xe4\x81\xf9\xda\x198\xddT\x1f\xe0\\\fII\x94v\u05ce\x1b\xa3\xf9b\xeep\xe67?\xce\f\xc9\x11\xf3\xb3\xab\u007f\xdes\xee]\xfd\xab\xe3\\\xfd\xad\xd3\xf9\xdd\xd5_?q\x9cO\x19\xcf%\xe1\x13zH$\x01\xb9\xf3\x89s\xffy\x9aJ\xe7^\u01f9?\"\xf2\xdc\xf9\xb4\xe3\xfc\xe4\t\x8bi\xee\\\xfd\xd0\xe9t~y\xf5\x8f{\x8e\xf3\xf30\x9a\x14\u051f\xb1\xd8X\xfe\xd0q\xae\xbe\xeftzW\u007f\xff\xc4q~Z\u02ff\xef8\xf7\x9c\xfb\xcfHB\x01\xe8\xbe\x12z\x9dN\xe7\xc7\xcf\xfe\x03L\x1c\xc7\xf9\u015c\xc9\xf3\xe2\u031f\xa4\xc9\xf6X\x14\x13\xfa|t\xb0-a\xe08\x8e[\xf0\x84\x88\xfc\x9c\xc4\u038f\x9f\xfd;#\x93\xaf\u025c\"5\xeby,\xc9R!Q\xcfs\xb1\xa0s\xfa&\u00de\x8bs)\x18\x9f\xe7\xd8\xeb{\u07abn>9\xa7\t99{\x1d\xa0w\x9e\x1b\xea\xc9H\xfdp_\xc9\u02cc\x06\b!\xa4\xc5 b\xf9\x9fH\x0625\x89\xf6\xbeC\xf8eB\xb20\x8cz\xe1\xcb(\xeao\x86Q\xd4\xf37\xfb\x0f\xb0\xd6\x1e\nA.\x03t\x96\xa6\xb1\x16\x8c\x04K\x02T\t\xceH\xdev1KEBd`\x89\xd8\u030cs\u007f,X2\x12t\xc6\xde\xf4V\x88\xf0iO\xf1\xea\xe3\x01\xc2a\x84\xfb\x03\x847q\x1f\xed\xed!\u0338\xc4jY\xb5S\x90\xd19\x15XIK\xbf \xfd\xed\xafA\xb6\xb8+\u07f38%\x80\xd9\xf6\u03cb\xe4l\u027dR\xbeS\xf7\x10\xec%\xdf \xa4\x84\xb7\x9c\u07e9_\xc9\x12\x9aK\x92dK\xce5P\xcb\xf7\x94H\xba\x056wFb\xa3\xcc\xfb\u00c7w\x80\xa4\xc3x'PUA\xdc\t\x9a\x15\xe7\xbbY\u8964\xed\x94}\x14\xea\xda\x1a3\xa7\xc8\x1e(\xdb^\xab\x83cF\xe2\x9c\x1a\x919:\xa4(h\x83\xda\xearB\x18\xe6\x1b\x1bY{\xdbX\xef\xcd:\xd4\xc20\n\xa3\xc8\xdf|\x80\x9b\xde-\x95^\x18\x81N\u007f\xbf\x17nF\xfd\xfd\x90l\xbd-\rJ\x87\xea\xb84^\x12\x92\x8d\x88\x90y\x80\x90>\x8d\xfd'\x8cO\x87q\xfc\xa28K\x88\x9c\x9c\xf7\xd6\x1d\xa5\x03\xedu\x80v\xfb\x86\xccW$\xd6|*\xd4\xf0qt#+\xb7:\xd0!h\xe9\xd9k:Q\a\x8dK\xa6S&Y\xca\x015\u0368\x90\x8c\xe6\xfa\x0e(\x97a\x1c\x1a\x99\x01\xb2\x9d+\xf9\xa2\xb4\xd8X6y \xe8,@\xb8\xbb=I\x93,\xe5\x94\xcb|[\xdf>\xf9\xf6i\u03c6\xea\xe3\x1alQ\as\u00ce\xa6f\xa5\xd2f\x1chJ\x98\x80H\x030I\x93j\x19\xc6\xc2fT\xae\x02\xeaDK\x16\x95\xeaF[\xf7\x06\xfa\x80ax\x1b\x94\x8a~\tg\x93]&\xb3\xc4\x05Tt-\xab\x8a\xc5\x15\x93\xb2\xc0\xcd\xec\x92\xcfU\xf4?\x9c\xbd\x1d\xf5\x85\xb7\xf0\u048cr\x92\xb1]\x15\xc7Y*\x10\xc9\x18t+\x03\x18|EE\xceR\x9e#\xc6Q\x9e\xd1\t\x9b\xb1\t\x91J\x02\x8e\x8d\xba\xd1R\x16\x87t\x06\u02b6\xad\xa2\x88O{\x06\xb8\x8f\u02d4i\x99\u046b\u016e\xa1\x14 \xbc\xeb\xef\xf8\xbb&\xea\x8c\xcf\u04a0N-\x93\xb1*u\x83j\xc4\x17\x1a,\xb0\b42_\x87\xa7\x862q\xaa\x05j]\x90/\x1d\a\x18\xd5\xcb:\xa43\x1f$y\xad\x0e\v)\u056deXEkmE\xfd/[\u0686\xb5\xe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xad\xef*\x9b\xder\x9c\u06d1^\xfa\xfdQ\x890\u017fb\xbf\x98\xed`!\xd8\xf7\xc3\n\x83k\xf1\xa1,\x97\x11a\x93\xd3\\\xfeqi\x87}`\u058d=REo\x9b\xaf\xc9\xe7\xaat*j\xb7\n\xda\xf5\xcd@\xbb5mv\x9c\xfa\x01\x8b\xef5\x9e\xb0\u008b\xfaww8:.\u02e8\xbc\x99\x90j\xff\xaa\xae\xf0\xc2\xea{\v\x99\x15\xf2f\xd4&\\\xfdfUv\xa9\xfb\x01\xea\x9ed\x94\x0fG\xc7'\n\xb2\xe1\x10\xed}\x87_\x86;[\xbf\x8f\x1e\xe9/\xb4y\xbao\xee\xa5\xee\x17ie\xb0\xf8p\u0795\x99\xa2\xa6:Q@\uc389\x98S\xe9\xb99\x15\x17T\x00\xab\xf1eF\xa7\xa5x\x123\xcae[\xbc\xf0\x9a\xdc\x15&\xdc)\u0593\x1dz\x8f6\xb1\xbf\x9d\u007fK\xe6s*|\x95{\xaf\xa6\xb6\v \x1a\u03b2\xb6L\xff\x90\xcbiZ\xc8\u03f1\xe7f_\u03c3\x06\xac\xa2\x8f\x15B\u036aZX\x03\xe6\x85Z\x17\xf6\xdcn\xcd}8:V\xcaU\xd4\xca\xecT\xec\x9a\xe1W\xeeL\x8a\xad\x94+\xe7\xcd\f\xf02\xe0n\xd5u\xae\xb0{b\xe6\xd6\xd9\xd67\u043e\xb6\x86{\br\xf0t<\x1e\x99t\xc2PM\x1a\xcep\xdb\xfa\xbeo\x9cxn\xa6\x1e^\x82jiT\x88T\x94d&\x16\x19\x00:\x82\u0252M.\x89,\xf2\x83tJ\x034\xb1\n\xa8dm\x85N\x85i\xb8\xf5\x97($[o\xe1\xafzO\xb8\x96~\x89\xa2\x16Z]\xad\x9a|\xf7\t|DD\x9e\xab\x8f\xe6\x00m\x82\xb5\xb9?\xaa7\x9f\x05z\x0f[G\xbfK(\x8b\xb2\x10nf\xa6?\xabT,\xb0*\x8f\\\x8aB}a\xe9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