Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rename field name #413

Merged
merged 1 commit into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
}
*/
}
Loading