Skip to content

Commit

Permalink
optimize: add an option to control code ref file as old name to avoid…
Browse files Browse the repository at this point in the history
… confliction
  • Loading branch information
HeyJavaBean committed Nov 14, 2023
1 parent 9aa9581 commit 82a21cd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
15 changes: 11 additions & 4 deletions generator/golang/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ func (g *GoBackend) executeTemplates() {
}

func (g *GoBackend) renderOneFile(ast *parser.Thrift) error {
keepName := g.utils.Features().KeepCodeRefName
path := g.utils.CombineOutputPath(g.req.OutputPath, ast)
filename := filepath.Join(path, g.utils.GetFilename(ast))
localScope, refScope, err := BuildRefScope(g.utils, ast)
Expand All @@ -180,12 +181,12 @@ func (g *GoBackend) renderOneFile(ast *parser.Thrift) error {
if err != nil {
return err
}
err = g.renderByTemplate(refScope, g.refTpl, ToRefFilename(filename))
err = g.renderByTemplate(refScope, g.refTpl, ToRefFilename(keepName, filename))
if err != nil {
return err
}
if g.utils.Features().WithReflection {
err = g.renderByTemplate(refScope, g.reflectionRefTpl, ToReflectionRefFilename(filename))
err = g.renderByTemplate(refScope, g.reflectionRefTpl, ToReflectionRefFilename(keepName, filename))
if err != nil {
return err
}
Expand All @@ -194,15 +195,21 @@ func (g *GoBackend) renderOneFile(ast *parser.Thrift) error {
return nil
}

func ToRefFilename(filename string) string {
func ToRefFilename(keepName bool, filename string) string {
if keepName {
return filename
}
return strings.TrimSuffix(filename, ".go") + "-ref.go"
}

func ToReflectionFilename(filename string) string {
return strings.TrimSuffix(filename, ".go") + "-reflection.go"
}

func ToReflectionRefFilename(filename string) string {
func ToReflectionRefFilename(keepName bool, filename string) string {
if keepName {
return ToReflectionFilename(filename)
}
return strings.TrimSuffix(filename, ".go") + "-reflection-ref.go"
}

Expand Down
1 change: 1 addition & 0 deletions generator/golang/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type Features struct {
EnumAsINT32 bool `enum_as_int_32:"Generate enum type as int32"`
CodeRefSlim bool `code_ref_slim:"Genenerate code ref by given idl-ref.yaml with less refs to avoid conflict"`
CodeRef bool `code_ref:"Genenerate code ref by given idl-ref.yaml"`
KeepCodeRefName bool `keep_code_ref_name:"Genenerate code ref but still keep file name."`
TrimIDL bool `trim_idl:"Simplify IDL to the most concise form before generating code."`
}

Expand Down
2 changes: 1 addition & 1 deletion version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@

package version

const ThriftgoVersion = "0.3.2"
const ThriftgoVersion = "0.3.3"

0 comments on commit 82a21cd

Please sign in to comment.