Skip to content

Commit

Permalink
feat: exp code ref (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
HeyJavaBean authored Jul 8, 2024
1 parent efe48e8 commit f5bfc1e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
3 changes: 2 additions & 1 deletion generator/golang/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ type Features struct {
EnumAsINT32 bool `enum_as_int_32:"Generate enum type as int32"`
CodeRefSlim bool `code_ref_slim:"Generate code ref by given idl-ref.yaml with less refs to avoid conflict"`
CodeRef bool `code_ref:"Generate code ref by given idl-ref.yaml"`
KeepCodeRefName bool `keep_code_ref_name:"Generate code ref but still keep file name."`
ExpCodeRef bool `exp_code_ref:"Generate code ref by given idl-ref.yaml with less refs to avoid conflict, but remind some struct as local.( this is a exp feature )"`
KeepCodeRefName bool `keep_code_ref_name:"Generate code ref but still keep file name."`
TrimIDL bool `trim_idl:"Simplify IDL to the most concise form before generating code."`
EnableNestedStruct bool `enable_nested_struct:"Generate nested field when 'thrift.nested=\"true\"' annotation is set to field, valid only in 'slim and raw_struct template'"`
JSONStringer bool `json_stringer:"Generate the JSON marshal method in String() method."`
Expand Down
2 changes: 1 addition & 1 deletion generator/golang/scope_ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (s *Scope) GetFirstDescriptor() string {

func BuildRefScope(cu *CodeUtils, ast *parser.Thrift) (*Scope, *Scope, error) {
thriftRef := config.GetRef(ast.Filename)
enableCodeRef := cu.Features().CodeRef || cu.Features().CodeRefSlim
enableCodeRef := cu.Features().CodeRef || cu.Features().CodeRefSlim || cu.Features().ExpCodeRef
scope, err := BuildScope(cu, ast)
if err != nil {
return nil, nil, err
Expand Down
15 changes: 11 additions & 4 deletions generator/golang/templates/ref/ref_tpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@ package ref_tpl
var File = `// Code generated by thriftgo ({{Version}}). DO NOT EDIT.
{{InsertionPoint "bof"}}
package {{.FilePackage}}
{{- $RefPackage := .RefPackage}}
{{- $RefPackage := printf "ref_%s" .RefPackage }}
import (
{{InsertionPoint "imports"}}
{{define "Imports"}}
{{- if Features.ExpCodeRef }}
{{- range $path, $alias := .}}
{{$alias }}"{{$path}}"
{{- end}}
{{- end}}
{{end}}
{{.RefPackage}} "{{.RefPath}}"
{{$RefPackage}} "{{.RefPath}}"
)
` + constRef + `
Expand Down Expand Up @@ -115,7 +120,9 @@ var structRef = `
{{if SupportIsSet .Field}}
{{$DefaultVarName := printf "%s_%s_%s" $TypeName $FieldName "DEFAULT"}}
{{- if Features.CodeRefSlim }}
{{- else if Features.ExpCodeRef }}
var {{$DefaultVarName}} {{$DefaultVarTypeName}}
{{- if .Default}} = {{.DefaultValue}}{{- end}}
{{- else }}
var {{$DefaultVarName}} = {{$RefPackage}}.{{$DefaultVarName}}
{{- end }}
Expand All @@ -130,7 +137,7 @@ var enumRef = `
type {{$TypeName}}= {{$RefPackage}}.{{$TypeName}}
var {{$EnumType}}FromString = {{$RefPackage}}.{{$EnumType}}FromString
var {{$EnumType}}Ptr = {{$RefPackage}}.{{$EnumType}}Ptr
{{- if Features.CodeRefSlim }}
{{- if or Features.CodeRefSlim Features.ExpCodeRef }}
const (
{{- range .Values}}
{{- if and Features.ReserveComments .ReservedComments}}
Expand Down

0 comments on commit f5bfc1e

Please sign in to comment.