From e75e1198ddbe57bd482760ea0381d82c4e94e413 Mon Sep 17 00:00:00 2001 From: steeling Date: Fri, 29 Nov 2024 21:59:19 -0500 Subject: [PATCH] rename field name (#413) add test case --- pkg/tools/gen/gengo.go | 23 +++++++++---- pkg/tools/gen/gengo_test.go | 65 +++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 7 deletions(-) diff --git a/pkg/tools/gen/gengo.go b/pkg/tools/gen/gengo.go index 2cfeb6b3..a4bc57c6 100644 --- a/pkg/tools/gen/gengo.go +++ b/pkg/tools/gen/gengo.go @@ -15,9 +15,10 @@ const goAnyType = "interface{}" var _ Generator = &goGenerator{} type GenGoOptions struct { - Package string - AnyType string - UseValue bool + Package string + AnyType string + UseValue bool + RenameFunc func(string) string } // GenGo translate kcl schema type to go struct. @@ -26,7 +27,8 @@ func GenGo(w io.Writer, filename string, src interface{}, opts *GenGoOptions) er } type goGenerator struct { - opts *GenGoOptions + opts *GenGoOptions + renameFunc func(string) string } func newGoGenerator(opts *GenGoOptions) *goGenerator { @@ -35,9 +37,16 @@ func newGoGenerator(opts *GenGoOptions) *goGenerator { AnyType: goAnyType, } } - return &goGenerator{ - opts: opts, + generator := &goGenerator{ + opts: opts, + renameFunc: opts.RenameFunc, } + if generator.renameFunc == nil { + generator.renameFunc = func(name string) string { + return name + } + } + return generator } 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) { goTagInfo := fmt.Sprintf(`kcl:"name=%s,type=%s"`, fieldName, g.GetFieldTag(fieldType)) goFieldDefines = append(goFieldDefines, - fmt.Sprintf("%s %s %s", fieldName, goFieldType, "`"+goTagInfo+"`"), + fmt.Sprintf("%s %s %s", g.renameFunc(fieldName), goFieldType, "`"+goTagInfo+"`"), ) goFieldDocs = append(goFieldDocs, fmt.Sprintf("// kcl-type: %s", kclFieldType), diff --git a/pkg/tools/gen/gengo_test.go b/pkg/tools/gen/gengo_test.go index 9b33210b..b2957d04 100644 --- a/pkg/tools/gen/gengo_test.go +++ b/pkg/tools/gen/gengo_test.go @@ -8,6 +8,7 @@ import ( "log" "testing" + "github.com/iancoleman/strcase" "kcl-lang.io/kcl-go/pkg/tools/gen" ) @@ -72,3 +73,67 @@ schema Company: } */ } + +func TestGenGoWithRename(t *testing.T) { + const code = ` +import units + +type NumberMultiplier = units.NumberMultiplier + +schema Person: + """Person Example""" + name: str = "kcl" + age: int = 2 + friends?: [str] = None + movies?: {str: Movie} = None + +schema Movie: + desc: str = "" + size: NumberMultiplier = 2M + kind?: "Superhero" | "War" | "Unknown" + unknown1?: int | str = None + unknown2?: any = None + +schema employee(Person): + bankCard: int + nationality: str + +schema Company: + name: str + employees: [employee] + persons: Person +` + var buf bytes.Buffer + err := gen.GenGo(&buf, "hello.k", code, &gen.GenGoOptions{ + RenameFunc: func(name string) string { + return strcase.ToCamel(name) + }, + }) + if err != nil { + log.Fatal(err) + } + goCode := buf.String() + fmt.Println(goCode) + /* + expectedGoCode := ` + // Person Example + type Person struct { + name string // kcl-type: str + age int // kcl-type: int + friends []string // kcl-type: [str] + movies map[string]*Movie // kcl-type: {str:Movie} + } + + type Movie struct { + desc string // kcl-type: str + size int // kcl-type: units.NumberMultiplier + kind string // kcl-type: "Superhero"|"War"|"Unknown" + unknown1 interface{} // kcl-type: int|str + unknown2 interface{} // kcl-type: any + } + ` + if goCode != expectedGoCode { + panic(fmt.Sprintf("test failed, expected %s got %s", expectedGoCode, goCode)) + } + */ +}