From cd533833b4a57ead90004c8b852b13929faed69b Mon Sep 17 00:00:00 2001 From: Kirill Zhuharev Date: Fri, 16 Dec 2022 14:48:40 +0300 Subject: [PATCH] Add support multiline comment for model field in go client (#24) * Add support multiline comment for model field * Don't compare first lines of files in tests --- gen/gen.go | 2 +- golang/go_template.go | 2 +- golang/rpcgen_test.go | 9 ++++----- golang/schema.go | 20 +++++++++++++++----- php/php_client_test.go | 12 +++++++----- swift/swift_client_test.go | 9 ++++----- typescript/rpcgen_test.go | 17 ++++++++--------- 7 files changed, 40 insertions(+), 31 deletions(-) diff --git a/gen/gen.go b/gen/gen.go index c2df488..b343d6f 100644 --- a/gen/gen.go +++ b/gen/gen.go @@ -1,6 +1,6 @@ package gen -const version = "2.4.0" +const version = "2.4.1" const DefinitionsPrefix = "#/definitions/" diff --git a/golang/go_template.go b/golang/go_template.go index 2392acd..039f128 100644 --- a/golang/go_template.go +++ b/golang/go_template.go @@ -46,7 +46,7 @@ func NewClient(endpoint string, header http.Header, httpClient *http.Client) *Cl {{ range .Models }} type {{ .Name }} struct { - {{ range .Fields }}{{ if ne .Description "" }}// {{ .Description }} + {{ range .Fields }}{{ if ne .Description "" }} {{ .CommentDescription }} {{ end }}{{ title .Name }} {{ if and .Optional (eq .ArrayItemType "")}}*{{ end }}{{ .GoType }} ` + "`json:\"{{ .Name }}{{if .Optional}},omitempty{{end}}\"`" + ` {{ end }} } diff --git a/golang/rpcgen_test.go b/golang/rpcgen_test.go index df6f489..d454139 100644 --- a/golang/rpcgen_test.go +++ b/golang/rpcgen_test.go @@ -5,7 +5,6 @@ import ( "os" "testing" - "github.com/vmkteam/rpcgen/v2/gen" "github.com/vmkteam/zenrpc/v2" "github.com/vmkteam/zenrpc/v2/testdata" ) @@ -28,11 +27,11 @@ func TestGenerateGoClient(t *testing.T) { t.Fatalf("open test data file: %v", err) } - // cut version from comparsion - generated = bytes.ReplaceAll(generated, []byte("v"+gen.DefaultGeneratorData().Version), []byte("")) - testData = bytes.ReplaceAll(testData, []byte("v"+gen.DefaultGeneratorData().Version), []byte("")) + // cut first line with version from comparsion + _, generatedBody, _ := bytes.Cut(generated, []byte{'\n'}) + _, testDataBody, _ := bytes.Cut(testData, []byte{'\n'}) - if !bytes.Equal(generated, testData) { + if !bytes.Equal(generatedBody, testDataBody) { t.Fatalf("bad generator output") } } diff --git a/golang/schema.go b/golang/schema.go index 044f4fe..ba50be0 100644 --- a/golang/schema.go +++ b/golang/schema.go @@ -138,11 +138,7 @@ func (m Method) HasErrors() bool { // CommentDescription add to head of all lines two slashes func (m Method) CommentDescription() string { - if len(m.Description) == 0 { - return "" - } - - return "// " + strings.ReplaceAll(m.Description, "\n", "\n// ") + return commentText(m.Description) } type Error struct { @@ -187,6 +183,11 @@ func (v Value) LocalModelName() string { return localModelName(v.ModelName) } +// CommentDescription add to head of all lines two slashes +func (v Value) CommentDescription() string { + return commentText(v.Description) +} + func localModelName(name string) string { return strings.ReplaceAll(titleFirstLetter(name), ".", "") } @@ -491,3 +492,12 @@ func convertDefinitionToModel(def smd.Definition, name string) Model { return model } + +// commentText add to head of all lines two slashes +func commentText(text string) string { + if text == "" { + return "" + } + + return "// " + strings.ReplaceAll(text, "\n", "\n// ") +} diff --git a/php/php_client_test.go b/php/php_client_test.go index 0efed4d..a775660 100644 --- a/php/php_client_test.go +++ b/php/php_client_test.go @@ -5,7 +5,6 @@ import ( "os" "testing" - "github.com/vmkteam/rpcgen/v2/gen" "github.com/vmkteam/zenrpc/v2" "github.com/vmkteam/zenrpc/v2/testdata" ) @@ -26,11 +25,14 @@ func TestGeneratePHPClient(t *testing.T) { t.Fatalf("open test data file: %v", err) } - // cut version from comparsion - generated = bytes.ReplaceAll(generated, []byte("v"+gen.DefaultGeneratorData().Version), []byte("")) - testData = bytes.ReplaceAll(testData, []byte("v"+gen.DefaultGeneratorData().Version), []byte("")) + // cut first two lines with version from comparsion + generated = bytes.TrimPrefix(generated, []byte("