Skip to content

Commit

Permalink
fix: fix some bug for ref_tpl (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
HeyJavaBean authored Apr 11, 2024
1 parent ffc9568 commit 11c5538
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
22 changes: 22 additions & 0 deletions generator/golang/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,32 @@ func ToReflectionRefFilename(keepName bool, filename string) string {
return strings.TrimSuffix(filename, ".go") + "-reflection-ref.go"
}

func (s *Scope) IsEmpty() bool {
if len(s.constants) == 0 &&
len(s.typedefs) == 0 &&
len(s.enums) == 0 &&
len(s.structs) == 0 &&
len(s.unions) == 0 &&
len(s.exceptions) == 0 &&
len(s.services) == 0 &&
len(s.synthesized) == 0 {
return true
}
return false
}

func (g *GoBackend) renderByTemplate(scope *Scope, executeTpl *template.Template, filename string) error {

if scope == nil {
return nil
}
// if scope has no content, just skip and don't generate this file
if g.utils.Features().SkipEmpty {
if scope.IsEmpty() {
return nil
}
}

var buf strings.Builder
g.utils.SetRootScope(scope)
err := executeTpl.ExecuteTemplate(&buf, executeTpl.Name(), scope)
Expand Down
3 changes: 2 additions & 1 deletion generator/golang/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ type Features struct {
EnableRefInterface bool `enable_ref_interface:"Generate Interface field without pointer type when 'thrift.is_interface=\"true\"' annotation is set to types in referred thrift."`
UseOption bool `use_option:"Parse specific Thrift annotations into struct-style option fields. If key not match, thriftgo will just ignore it."`
// ForceUseOption bool `use_option:"Forcefully parse all Thrift annotations into struct-style option fields. If parsing is not possible, an error will be thrown."`
NoFmt bool `no_fmt:"To achieve faster generation speed, skipping the formatting of Golang code can improve performance by approximately 50%."`
NoFmt bool `no_fmt:"To achieve faster generation speed, skipping the formatting of Golang code can improve performance by approximately 50%."`
SkipEmpty bool `skip_empty:"If there's not content in file, just skip it. Later this feature will be a default feature."`
}

var defaultFeatures = Features{
Expand Down
15 changes: 0 additions & 15 deletions generator/golang/templates/ref/ref_tpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,6 @@ var processorRef = `
type {{$ServiceName}} = {{$RefPackage}}.{{$ServiceName}}
{{- $ClientName := printf "%s%s" $ServiceName "Client"}}
type {{$ClientName}} = {{$RefPackage}}.{{$ClientName}}
var New{{$ClientName}}Factory = {{$RefPackage}}.New{{$ClientName}}Factory
var New{{$ClientName}}Protocol = {{$RefPackage}}.New{{$ClientName}}Protocol
var New{{$ClientName}} = {{$RefPackage}}.New{{$ClientName}}
{{- $ProcessorName := printf "%s%s" $ServiceName "Processor"}}
type {{$ProcessorName}} = {{$RefPackage}}.{{$ProcessorName}}
var New{{$ProcessorName}} = {{$RefPackage}}.New{{$ProcessorName}}
{{- range .Functions}}
{{$ArgsType := .ArgType.GoName}}
type {{$ArgsType}} = {{$RefPackage}}.{{$ArgsType}}
Expand Down

0 comments on commit 11c5538

Please sign in to comment.