Skip to content

Commit

Permalink
Add support multiline comment for model field in go client (#24)
Browse files Browse the repository at this point in the history
* Add support multiline comment for model field

* Don't compare first lines of files in tests
  • Loading branch information
zhuharev authored Dec 16, 2022
1 parent e113b21 commit cd53383
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 31 deletions.
2 changes: 1 addition & 1 deletion gen/gen.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package gen

const version = "2.4.0"
const version = "2.4.1"

const DefinitionsPrefix = "#/definitions/"

Expand Down
2 changes: 1 addition & 1 deletion golang/go_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
}
Expand Down
9 changes: 4 additions & 5 deletions golang/rpcgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"os"
"testing"

"github.com/vmkteam/rpcgen/v2/gen"
"github.com/vmkteam/zenrpc/v2"
"github.com/vmkteam/zenrpc/v2/testdata"
)
Expand All @@ -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")
}
}
20 changes: 15 additions & 5 deletions golang/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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), ".", "")
}
Expand Down Expand Up @@ -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// ")
}
12 changes: 7 additions & 5 deletions php/php_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"os"
"testing"

"github.com/vmkteam/rpcgen/v2/gen"
"github.com/vmkteam/zenrpc/v2"
"github.com/vmkteam/zenrpc/v2/testdata"
)
Expand All @@ -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("<?php\n"))
testData = bytes.TrimPrefix(testData, []byte("<?php\n"))

if !bytes.Equal(generated, testData) {
_, generatedBody, _ := bytes.Cut(generated, []byte{'\n'})
_, testDataBody, _ := bytes.Cut(testData, []byte{'\n'})

if !bytes.Equal(generatedBody, testDataBody) {
t.Fatalf("bad generator output")
}
}
9 changes: 4 additions & 5 deletions swift/swift_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"os"
"testing"

"github.com/vmkteam/rpcgen/v2/gen"
"github.com/vmkteam/zenrpc/v2"
"github.com/vmkteam/zenrpc/v2/testdata"
)
Expand All @@ -26,11 +25,11 @@ func TestGenerateSwiftClient(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")
}
}
17 changes: 8 additions & 9 deletions typescript/rpcgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"os"
"testing"

"github.com/vmkteam/rpcgen/v2/gen"
"github.com/vmkteam/zenrpc/v2"
"github.com/vmkteam/zenrpc/v2/testdata"
)
Expand All @@ -26,11 +25,11 @@ func TestGenerateTypeScriptClient(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")
}
}
Expand All @@ -51,11 +50,11 @@ func TestGenerateTypeScriptClasses(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")
}
}

0 comments on commit cd53383

Please sign in to comment.