Skip to content

Commit 66b7ab9

Browse files
authored
Add support for any (#32)
1 parent 222f51e commit 66b7ab9

File tree

7 files changed

+126
-12
lines changed

7 files changed

+126
-12
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.24.0
55
require (
66
github.com/vmkteam/meta-schema/v2 v2.0.1
77
github.com/vmkteam/zenrpc v1.1.1
8-
github.com/vmkteam/zenrpc/v2 v2.3.0
8+
github.com/vmkteam/zenrpc/v2 v2.3.1
99
golang.org/x/text v0.29.0
1010
)
1111

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ github.com/vmkteam/meta-schema/v2 v2.0.1 h1:7eoImKpnCs2wiCcBB8AUCtfVVhGa6L6DDqih
3434
github.com/vmkteam/meta-schema/v2 v2.0.1/go.mod h1:GQzU4Rid0Q9dDIz/OeSDyL/aB/QG7tD0gOUt5nQ4JMk=
3535
github.com/vmkteam/zenrpc v1.1.1 h1:WmModRVCwgs7XEkeJcVvmDOwEtAQk0TuYV91wdpAaCw=
3636
github.com/vmkteam/zenrpc v1.1.1/go.mod h1:x3fCkb9HHOpqwqC7TKXf8pyXWdPxEt2Zrndl50YxexM=
37-
github.com/vmkteam/zenrpc/v2 v2.3.0 h1:C3jBi0FkseKmVVh2WTvdMuG0THdL84jOut4Xsqx5NeM=
38-
github.com/vmkteam/zenrpc/v2 v2.3.0/go.mod h1:HSnsZXbtiRDdnga3YjG8x2lQsQha7Jy/pZSFnNN+XeM=
37+
github.com/vmkteam/zenrpc/v2 v2.3.1 h1:JbQXzlYJWNdMXL2xClItf5bK0aLZFozTbqKXJZRyfGo=
38+
github.com/vmkteam/zenrpc/v2 v2.3.1/go.mod h1:HSnsZXbtiRDdnga3YjG8x2lQsQha7Jy/pZSFnNN+XeM=
3939
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
4040
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
4141
go.yaml.in/yaml/v2 v2.4.3 h1:6gvOSjQoTB3vt1l+CU+tSyi/HOjfOjRLJ4YwYZGwRO0=

golang/go_client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
const (
15-
version = "1.0.0"
15+
version = "1.1.0"
1616
lang = "golang"
1717
)
1818

golang/rpcgen_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func TestGenerateGoClient(t *testing.T) {
1919
rpc.Register("catalogue", testdata.CatalogueService{})
2020
rpc.Register("phonebook", testdata.PhoneBook{})
2121
rpc.Register("arith", testdata.ArithService{})
22+
rpc.Register("print", testdata.PrintService{})
2223

2324
cl := NewClient(rpc.SMD(), Settings{})
2425

golang/schema.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,10 @@ func (v *Value) GoType() string {
204204
}
205205

206206
return fmt.Sprintf("[]%s", simpleGoType(v.ArrayItemType))
207-
} else if v.Type == smd.Object {
207+
} else if v.Type == smd.Object && v.ModelName != "" {
208208
return v.LocalModelName()
209+
} else if v.Type == smd.Object {
210+
return "any"
209211
}
210212

211213
return simpleGoType(v.Type)
@@ -293,11 +295,6 @@ func newMethod(service smd.Service, namespace, methodName string) Method {
293295

294296
if service.Returns.Type != "" {
295297
method.Returns = newValuePointer(service.Returns, namespace, methodName)
296-
297-
// fix return model name
298-
if method.Returns.Type == smd.Object && method.Returns.ModelName == "" {
299-
method.Returns.ModelName = fmt.Sprintf("%s%sResponse", titleFirstLetter(namespace), titleFirstLetter(methodName))
300-
}
301298
}
302299

303300
return method
@@ -327,6 +324,8 @@ func newValue(in smd.JSONSchema, namespace, methodName string, isParam, isReturn
327324
value.ModelName = in.TypeName
328325
} else if in.Description != "" && smd.IsSMDTypeName(in.Description, in.Type) {
329326
value.ModelName = in.Description
327+
} else if len(in.Properties) == 0 && len(in.Definitions) == 0 && len(in.Items) == 0 {
328+
value.ModelName = ""
330329
} else if isParam {
331330
value.ModelName = fmt.Sprintf("%s%s%sParam", titleFirstLetter(namespace), titleFirstLetter(methodName), titleFirstLetter(in.Name))
332331
} else if isReturn {
@@ -400,7 +399,9 @@ func cleanModelList(models []Model, namespace, methodName string) (res []Model)
400399
for _, model := range models {
401400
// fix empty model names
402401
if model.Name == "" {
403-
if model.IsParamModel {
402+
if len(model.Fields) == 0 {
403+
continue
404+
} else if model.IsParamModel {
404405
model.Name = fmt.Sprintf("%s%s%sParam", titleFirstLetter(namespace), titleFirstLetter(methodName), titleFirstLetter(model.ParamName))
405406
} else if model.IsReturnModel {
406407
model.Name = fmt.Sprintf("%s%sResponse", titleFirstLetter(namespace), titleFirstLetter(methodName))

golang/schema_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,46 @@ func TestConvertJSONSchema(t *testing.T) {
379379
},
380380
},
381381
},
382+
// object param with no properties/definitions should result in "any"
383+
{
384+
in: smd.Schema{
385+
Services: map[string]smd.Service{
386+
"simple.anyParam": {
387+
Parameters: []smd.JSONSchema{
388+
{
389+
Name: "data",
390+
Type: "object",
391+
},
392+
},
393+
Returns: smd.JSONSchema{
394+
Type: smd.Object,
395+
},
396+
},
397+
},
398+
},
399+
out: Schema{
400+
Namespaces: []Namespace{
401+
{
402+
Name: "simple",
403+
Methods: []Method{
404+
{
405+
Name: "anyParam",
406+
Params: []Value{
407+
{
408+
Name: "data",
409+
Type: "object",
410+
ModelName: "",
411+
},
412+
},
413+
Returns: &Value{
414+
Type: "object",
415+
},
416+
},
417+
},
418+
},
419+
},
420+
},
421+
},
382422
}
383423

384424
for _, tt := range tc {

golang/testdata/catalogue_client.go.test

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Code generated from jsonrpc schema by rpcgen v2.5.x with golang v1.0.0; DO NOT EDIT.
1+
// Code generated from jsonrpc schema by rpcgen v2.5.x with golang v1.1.0; DO NOT EDIT.
22

33
package client
44

@@ -30,6 +30,7 @@ type Client struct {
3030
Arith *svcArith
3131
Catalogue *svcCatalogue
3232
Phonebook *svcPhonebook
33+
Print *svcPrint
3334
}
3435

3536
func NewClient(endpoint string, httpClient *http.Client) *Client {
@@ -43,6 +44,7 @@ func NewClient(endpoint string, httpClient *http.Client) *Client {
4344
c.Arith = newClientArith(c.rpcClient)
4445
c.Catalogue = newClientCatalogue(c.rpcClient)
4546
c.Phonebook = newClientPhonebook(c.rpcClient)
47+
c.Print = newClientPrint(c.rpcClient)
4648

4749
return c
4850
}
@@ -462,6 +464,76 @@ func (c *svcPhonebook) ValidateSearch(ctx context.Context, search *PersonSearch)
462464
return
463465
}
464466

467+
type svcPrint struct {
468+
client *rpcClient
469+
}
470+
471+
func newClientPrint(client *rpcClient) *svcPrint {
472+
return &svcPrint{
473+
client: client,
474+
}
475+
}
476+
477+
func (c *svcPrint) PrintAnything(ctx context.Context, s any) (res any, err error) {
478+
_req := struct {
479+
S any
480+
}{
481+
S: s,
482+
}
483+
484+
err = c.client.call(ctx, "print.PrintAnything", _req, &res)
485+
486+
return
487+
}
488+
489+
func (c *svcPrint) PrintOptional(ctx context.Context, s *string) (res string, err error) {
490+
_req := struct {
491+
S *string
492+
}{
493+
S: s,
494+
}
495+
496+
err = c.client.call(ctx, "print.PrintOptional", _req, &res)
497+
498+
return
499+
}
500+
501+
func (c *svcPrint) PrintOptionalWithDefault(ctx context.Context, s *string) (res string, err error) {
502+
_req := struct {
503+
S *string
504+
}{
505+
S: s,
506+
}
507+
508+
err = c.client.call(ctx, "print.PrintOptionalWithDefault", _req, &res)
509+
510+
return
511+
}
512+
513+
func (c *svcPrint) PrintRequired(ctx context.Context, s string) (res string, err error) {
514+
_req := struct {
515+
S string
516+
}{
517+
S: s,
518+
}
519+
520+
err = c.client.call(ctx, "print.PrintRequired", _req, &res)
521+
522+
return
523+
}
524+
525+
func (c *svcPrint) PrintRequiredDefault(ctx context.Context, s *string) (res string, err error) {
526+
_req := struct {
527+
S *string
528+
}{
529+
S: s,
530+
}
531+
532+
err = c.client.call(ctx, "print.PrintRequiredDefault", _req, &res)
533+
534+
return
535+
}
536+
465537
type rpcClient struct {
466538
endpoint string
467539
cl *http.Client

0 commit comments

Comments
 (0)