Skip to content

Commit e75e119

Browse files
authored
rename field name (#413)
add test case
1 parent 5b62a0a commit e75e119

File tree

2 files changed

+81
-7
lines changed

2 files changed

+81
-7
lines changed

pkg/tools/gen/gengo.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ const goAnyType = "interface{}"
1515
var _ Generator = &goGenerator{}
1616

1717
type GenGoOptions struct {
18-
Package string
19-
AnyType string
20-
UseValue bool
18+
Package string
19+
AnyType string
20+
UseValue bool
21+
RenameFunc func(string) string
2122
}
2223

2324
// GenGo translate kcl schema type to go struct.
@@ -26,7 +27,8 @@ func GenGo(w io.Writer, filename string, src interface{}, opts *GenGoOptions) er
2627
}
2728

2829
type goGenerator struct {
29-
opts *GenGoOptions
30+
opts *GenGoOptions
31+
renameFunc func(string) string
3032
}
3133

3234
func newGoGenerator(opts *GenGoOptions) *goGenerator {
@@ -35,9 +37,16 @@ func newGoGenerator(opts *GenGoOptions) *goGenerator {
3537
AnyType: goAnyType,
3638
}
3739
}
38-
return &goGenerator{
39-
opts: opts,
40+
generator := &goGenerator{
41+
opts: opts,
42+
renameFunc: opts.RenameFunc,
4043
}
44+
if generator.renameFunc == nil {
45+
generator.renameFunc = func(name string) string {
46+
return name
47+
}
48+
}
49+
return generator
4150
}
4251

4352
func (g *goGenerator) GenFromSource(w io.Writer, filename string, src interface{}) error {
@@ -94,7 +103,7 @@ func (g *goGenerator) GenSchema(w io.Writer, typ *pb.KclType) {
94103

95104
goTagInfo := fmt.Sprintf(`kcl:"name=%s,type=%s"`, fieldName, g.GetFieldTag(fieldType))
96105
goFieldDefines = append(goFieldDefines,
97-
fmt.Sprintf("%s %s %s", fieldName, goFieldType, "`"+goTagInfo+"`"),
106+
fmt.Sprintf("%s %s %s", g.renameFunc(fieldName), goFieldType, "`"+goTagInfo+"`"),
98107
)
99108
goFieldDocs = append(goFieldDocs,
100109
fmt.Sprintf("// kcl-type: %s", kclFieldType),

pkg/tools/gen/gengo_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"log"
99
"testing"
1010

11+
"github.com/iancoleman/strcase"
1112
"kcl-lang.io/kcl-go/pkg/tools/gen"
1213
)
1314

@@ -72,3 +73,67 @@ schema Company:
7273
}
7374
*/
7475
}
76+
77+
func TestGenGoWithRename(t *testing.T) {
78+
const code = `
79+
import units
80+
81+
type NumberMultiplier = units.NumberMultiplier
82+
83+
schema Person:
84+
"""Person Example"""
85+
name: str = "kcl"
86+
age: int = 2
87+
friends?: [str] = None
88+
movies?: {str: Movie} = None
89+
90+
schema Movie:
91+
desc: str = ""
92+
size: NumberMultiplier = 2M
93+
kind?: "Superhero" | "War" | "Unknown"
94+
unknown1?: int | str = None
95+
unknown2?: any = None
96+
97+
schema employee(Person):
98+
bankCard: int
99+
nationality: str
100+
101+
schema Company:
102+
name: str
103+
employees: [employee]
104+
persons: Person
105+
`
106+
var buf bytes.Buffer
107+
err := gen.GenGo(&buf, "hello.k", code, &gen.GenGoOptions{
108+
RenameFunc: func(name string) string {
109+
return strcase.ToCamel(name)
110+
},
111+
})
112+
if err != nil {
113+
log.Fatal(err)
114+
}
115+
goCode := buf.String()
116+
fmt.Println(goCode)
117+
/*
118+
expectedGoCode := `
119+
// Person Example
120+
type Person struct {
121+
name string // kcl-type: str
122+
age int // kcl-type: int
123+
friends []string // kcl-type: [str]
124+
movies map[string]*Movie // kcl-type: {str:Movie}
125+
}
126+
127+
type Movie struct {
128+
desc string // kcl-type: str
129+
size int // kcl-type: units.NumberMultiplier
130+
kind string // kcl-type: "Superhero"|"War"|"Unknown"
131+
unknown1 interface{} // kcl-type: int|str
132+
unknown2 interface{} // kcl-type: any
133+
}
134+
`
135+
if goCode != expectedGoCode {
136+
panic(fmt.Sprintf("test failed, expected %s got %s", expectedGoCode, goCode))
137+
}
138+
*/
139+
}

0 commit comments

Comments
 (0)