Skip to content

Commit

Permalink
Merge pull request vmkteam#23 from sas1024/master
Browse files Browse the repository at this point in the history
Add support for optional parameters in go client, add package setting…
  • Loading branch information
sas1024 authored Dec 9, 2022
2 parents 19ff0d5 + df66be9 commit e113b21
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 30 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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{})))
Expand Down
4 changes: 2 additions & 2 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 13 additions & 4 deletions golang/go_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions golang/go_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 }}
Expand Down Expand Up @@ -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 }}
Expand Down
2 changes: 1 addition & 1 deletion golang/rpcgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 1 addition & 4 deletions golang/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func NewSchema(schema smd.Schema) Schema {

type Schema struct {
gen.GeneratorData
Package string
Namespaces []Namespace
}

Expand Down Expand Up @@ -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()
}

Expand Down
22 changes: 11 additions & 11 deletions golang/testdata/catalogue_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e113b21

Please sign in to comment.