Skip to content

Commit

Permalink
rename field name (#413)
Browse files Browse the repository at this point in the history
add test case
  • Loading branch information
steeling authored Nov 30, 2024
1 parent 5b62a0a commit e75e119
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 7 deletions.
23 changes: 16 additions & 7 deletions pkg/tools/gen/gengo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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),
Expand Down
65 changes: 65 additions & 0 deletions pkg/tools/gen/gengo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"testing"

"github.com/iancoleman/strcase"
"kcl-lang.io/kcl-go/pkg/tools/gen"
)

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

0 comments on commit e75e119

Please sign in to comment.