From df66be90fe36e0679d3c688e242ee3d116b9465a Mon Sep 17 00:00:00 2001 From: Alexander Korelskiy Date: Fri, 9 Dec 2022 16:20:22 +0300 Subject: [PATCH] Add support for optional parameters in go client, add package setting for go client --- README.md | 9 +++++---- generator.go | 4 ++-- golang/go_client.go | 17 +++++++++++++---- golang/go_template.go | 8 ++++---- golang/rpcgen_test.go | 2 +- golang/schema.go | 5 +---- golang/testdata/catalogue_client.go | 22 +++++++++++----------- 7 files changed, 37 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 92edfde..ccb9d53 100644 --- a/README.md +++ b/README.md @@ -39,22 +39,23 @@ func main() { ### Generate in HTTP handler ```go -package main +package main import ( "net/http" - + "github.com/vmkteam/rpcgen/v2" + "github.com/vmkteam/rpcgen/v2/golang" "github.com/vmkteam/rpcgen/v2/swift" "github.com/vmkteam/zenrpc/v2" ) -func main () { +func main() { rpc := zenrpc.NewServer(zenrpc.Options{}) gen := rpcgen.FromSMD(rpc.SMD()) - http.HandleFunc("/client.go", rpcgen.Handler(gen.GoClient())) + http.HandleFunc("/client.go", rpcgen.Handler(gen.GoClient(golang.Settings{}))) http.HandleFunc("/client.ts", rpcgen.Handler(gen.TSClient(nil))) http.HandleFunc("/RpcClient.php", rpcgen.Handler(gen.PHPClient(""))) http.HandleFunc("/client.swift", rpcgen.Handler(gen.SwiftClient(swift.Settings{}))) diff --git a/generator.go b/generator.go index aaaa2d9..041f2df 100644 --- a/generator.go +++ b/generator.go @@ -22,8 +22,8 @@ type Generator interface { Generate() ([]byte, error) } -func (g RPCGen) GoClient() Generator { - return golang.NewClient(g.schema) +func (g RPCGen) GoClient(settings golang.Settings) Generator { + return golang.NewClient(g.schema, settings) } func (g RPCGen) PHPClient(phpNamespace string) Generator { diff --git a/golang/go_client.go b/golang/go_client.go index 8fa678f..555f107 100644 --- a/golang/go_client.go +++ b/golang/go_client.go @@ -11,21 +11,30 @@ import ( "github.com/vmkteam/zenrpc/v2/smd" ) +type Settings struct { + Package string +} + // Generator main package structure type Generator struct { - schema Schema + schema Schema + settings Settings } // NewClient create Generator from zenrpc/v2 SMD. -func NewClient(schema smd.Schema) *Generator { - return &Generator{schema: NewSchema(schema)} +func NewClient(schema smd.Schema, settings Settings) *Generator { + if settings.Package == "" { + settings.Package = "client" + } + return &Generator{schema: NewSchema(schema), settings: settings} } // Generate returns generated Go client. func (g *Generator) Generate() ([]byte, error) { g.schema.GeneratorData = gen.DefaultGeneratorData() + g.schema.Package = g.settings.Package - tmpl, err := template.New("").Funcs(templateFuncs).Parse(goTpl) + tmpl, err := template.New("golang client").Funcs(templateFuncs).Parse(goTpl) if err != nil { return nil, err } diff --git a/golang/go_template.go b/golang/go_template.go index 9ddbe1f..2392acd 100644 --- a/golang/go_template.go +++ b/golang/go_template.go @@ -3,7 +3,7 @@ package golang // goTpl contains template for Go client const goTpl = `// Code generated from jsonrpc schema by rpcgen v{{ .Version }}; DO NOT EDIT. -package client +package {{ .Package }} import ( "bytes" @@ -47,7 +47,7 @@ func NewClient(endpoint string, header http.Header, httpClient *http.Client) *Cl {{ range .Models }} type {{ .Name }} struct { {{ range .Fields }}{{ if ne .Description "" }}// {{ .Description }} - {{ end }}{{ title .Name }} {{ .GoType }} ` + "`json:\"{{ .Name }}{{if .Optional}},omitempty{{end}}\"`" + ` + {{ end }}{{ title .Name }} {{ if and .Optional (eq .ArrayItemType "")}}*{{ end }}{{ .GoType }} ` + "`json:\"{{ .Name }}{{if .Optional}},omitempty{{end}}\"`" + ` {{ end }} } {{ end }} @@ -75,9 +75,9 @@ Err{{ title $namespace }}{{ title $method.Name }}{{ .StringCode }} = zenrpc.NewE {{ end }} {{ $method.CommentDescription }} -func (c *svc{{ title $lTitle}}) {{ title . }}(ctx context.Context, {{ range $method.Params }}{{ .Name }} {{ .GoType }}, {{ end }}) ( {{ if $method.HasResult }} res {{ $method.Returns.GoType }}, {{ else }} {{end}} err error) { +func (c *svc{{ title $lTitle}}) {{ title . }}(ctx context.Context, {{ range $method.Params }}{{ .Name }} {{ if and .Optional (eq .ArrayItemType "")}}*{{ end }}{{ .GoType }}, {{ end }}) ( {{ if $method.HasResult }} res {{ if and $method.Returns.Optional (eq $method.Returns.ArrayItemType "")}}*{{ end }}{{ $method.Returns.GoType }}, {{ else }} {{end}} err error) { _req := struct { - {{ range $method.Params }}{{ title .Name }} {{ .GoType }} + {{ range $method.Params }}{{ title .Name }} {{ if and .Optional (eq .ArrayItemType "")}}*{{ end }}{{ .GoType }} {{ end }} } { {{ range $method.Params }}{{ title .Name }}: {{ .Name }}, {{ end }} diff --git a/golang/rpcgen_test.go b/golang/rpcgen_test.go index c591826..df6f489 100644 --- a/golang/rpcgen_test.go +++ b/golang/rpcgen_test.go @@ -16,7 +16,7 @@ func TestGenerateGoClient(t *testing.T) { rpc.Register("phonebook", testdata.PhoneBook{}) rpc.Register("arith", testdata.ArithService{}) - cl := NewClient(rpc.SMD()) + cl := NewClient(rpc.SMD(), Settings{}) generated, err := cl.Generate() if err != nil { diff --git a/golang/schema.go b/golang/schema.go index 8c3b136..044f4fe 100644 --- a/golang/schema.go +++ b/golang/schema.go @@ -30,6 +30,7 @@ func NewSchema(schema smd.Schema) Schema { type Schema struct { gen.GeneratorData + Package string Namespaces []Namespace } @@ -203,10 +204,6 @@ func (v *Value) GoType() string { return fmt.Sprintf("[]%s", simpleGoType(v.ArrayItemType)) } else if v.Type == smd.Object { - if v.Optional { - return fmt.Sprintf("*%s", v.LocalModelName()) - } - return v.LocalModelName() } diff --git a/golang/testdata/catalogue_client.go b/golang/testdata/catalogue_client.go index 1a224bb..3f650bb 100755 --- a/golang/testdata/catalogue_client.go +++ b/golang/testdata/catalogue_client.go @@ -87,16 +87,16 @@ type Person struct { Mobile []string `json:"Mobile"` // Phone is main phone Phone string `json:"Phone"` - WorkPhone string `json:"WorkPhone,omitempty"` + WorkPhone *string `json:"WorkPhone,omitempty"` Address *Address `json:"address,omitempty"` } type PersonSearch struct { ByAddress *Address `json:"ByAddress,omitempty"` // ByName is filter for searching person by first name or last name. - ByName string `json:"ByName,omitempty"` - ByPhone string `json:"ByPhone"` - ByType string `json:"ByType,omitempty"` + ByName *string `json:"ByName,omitempty"` + ByPhone string `json:"ByPhone"` + ByType *string `json:"ByType,omitempty"` } type Quotient struct { @@ -259,10 +259,10 @@ func (c *svcArith) Positive(ctx context.Context) (res bool, err error) { } // Pow returns x**y, the base-x exponential of y. If Exp is not set then default value is 2. -func (c *svcArith) Pow(ctx context.Context, base float64, exp float64) (res float64, err error) { +func (c *svcArith) Pow(ctx context.Context, base float64, exp *float64) (res float64, err error) { _req := struct { Base float64 - Exp float64 + Exp *float64 }{ Base: base, Exp: exp, } @@ -390,11 +390,11 @@ func (c *svcPhonebook) Delete(ctx context.Context, id int) (res bool, err error) } // Get returns all people from DB. -func (c *svcPhonebook) Get(ctx context.Context, search PersonSearch, page int, count int) (res []Person, err error) { +func (c *svcPhonebook) Get(ctx context.Context, search PersonSearch, page *int, count *int) (res []Person, err error) { _req := struct { Search PersonSearch - Page int - Count int + Page *int + Count *int }{ Search: search, Page: page, Count: count, } @@ -423,10 +423,10 @@ var ( ) // Save saves person to DB. -func (c *svcPhonebook) Save(ctx context.Context, p Person, replace bool) (res int, err error) { +func (c *svcPhonebook) Save(ctx context.Context, p Person, replace *bool) (res int, err error) { _req := struct { P Person - Replace bool + Replace *bool }{ P: p, Replace: replace, }