Skip to content

Commit

Permalink
Merge branch 'main' into feat/trimmer-compose
Browse files Browse the repository at this point in the history
  • Loading branch information
DMwangnima authored Jul 17, 2024
2 parents 9b32575 + 43f56c1 commit 8fbd6a1
Show file tree
Hide file tree
Showing 15 changed files with 164 additions and 155 deletions.
13 changes: 6 additions & 7 deletions args/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,12 @@ Options:
Available generators (and options): go
`)
//// print backend options
//for _, b := range g.AllBackend() {
// name, lang := b.Name(), b.Lang()
// println(fmt.Sprintf(" %s (%s):", name, lang))
// println(align(b.Options()))
//}
println()
// print backend options
b := new(golang.GoBackend)
name, lang := b.Name(), b.Lang()
println(fmt.Sprintf(" %s (%s):", name, lang))
println(align(b.Options()))

}

// align the help strings for plugin options.
Expand Down
9 changes: 9 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ import (
"gopkg.in/yaml.v3"
)

func init() {
err := LoadConfig()
if err != nil {
panic(err)
}
}

type RawConfig struct {
Ref map[string]interface{} `yaml:"ref"`
Debug bool `yaml:"debug"`
Expand Down Expand Up @@ -101,6 +108,8 @@ func GetRef(name string) *RefConfig {
return refConfig
}

// LoadConfig by default, config will load only once when the program is invoked, also the same for each plugin
// but for sdk mode, config should be reloaded each time when the sdk is called. so we provide this api and manually call this in sdk mode.
func LoadConfig() error {
config, err := initConfig()
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions generator/golang/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ type Features struct {
AlwaysGenerateJSONTag bool `always_gen_json_tag:"Always generate 'json' tag even if go.tag is provided (Disabled by default)"`
SnakeTyleJSONTag bool `snake_style_json_tag:"Generate snake style json tag"`
LowerCamelCaseJSONTag bool `lower_camel_style_json_tag:"Generate lower camel case style json tag"`
GenerateReflectionInfo bool `generate_reflection_info:"This option is no longer used. Please use with_reflection instead."`
WithReflection bool `with_reflection:"Generate reflection info"`
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 Expand Up @@ -97,7 +97,6 @@ var defaultFeatures = Features{
AlwaysGenerateJSONTag: false,
SnakeTyleJSONTag: false,
LowerCamelCaseJSONTag: false,
GenerateReflectionInfo: false,
ThriftStreaming: false,
EnumAsINT32: false,
TrimIDL: false,
Expand Down
5 changes: 0 additions & 5 deletions generator/golang/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/cloudwego/thriftgo/generator/golang/streaming"
"github.com/cloudwego/thriftgo/parser"
"github.com/cloudwego/thriftgo/pkg/namespace"
"github.com/cloudwego/thriftgo/reflection"
"github.com/cloudwego/thriftgo/thrift_reflection"
)

Expand Down Expand Up @@ -145,10 +144,6 @@ func (s *Scope) FilePackage() string {
return s.importPackage
}

func (s *Scope) IDLMeta() string {
return reflection.Encode(s.ast)
}

func (s *Scope) IDLName() string {
idlName := strings.TrimSuffix(s.ast.Filename, ".thrift")
arr := strings.Split(idlName, string(filepath.Separator))
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
7 changes: 0 additions & 7 deletions generator/golang/templates/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package {{.FilePackage}}
import (
{{InsertionPoint "imports"}}
{{- if Features.GenerateReflectionInfo}}thriftreflection "github.com/cloudwego/kitex/pkg/reflection/thrift"{{end}}
)
{{template "Constant" .}}
Expand Down Expand Up @@ -68,11 +67,5 @@ var (
{{- end}}
{{- end}}
{{- if Features.GenerateReflectionInfo}}
var file_{{.IDLName}}_rawDesc = {{.IDLMeta}}
func init(){
thriftreflection.RegisterIDL(file_{{.IDLName}}_rawDesc)
}
{{end}}
{{- InsertionPoint "eof"}}
`
1 change: 0 additions & 1 deletion generator/golang/templates/raw_struct/raw_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ package {{.FilePackage}}
import (
{{InsertionPoint "imports"}}
{{- if Features.GenerateReflectionInfo}}thriftreflection "github.com/cloudwego/kitex/pkg/reflection/thrift"{{end}}
)
{{template "Constant" .}}
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
10 changes: 5 additions & 5 deletions generator/golang/templates/slim/slim.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ func New{{$TypeName}}() *{{$TypeName}} {
}
}
{{if Features.FrugalTag}}
func (p *{{$TypeName}}) InitDefault() {
*p = {{$TypeName}}{
{{template "StructLikeDefault" .}}
}
{{- range .Fields}}
{{- if .IsSetDefault}}
p.{{.GoName}} = {{.DefaultValue}}
{{- end}}
{{- end}}
}
{{end}}{{/* if Features.FrugalTag */}}
{{template "FieldGetOrSet" .}}
Expand Down
13 changes: 8 additions & 5 deletions generator/golang/templates/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ func New{{$TypeName}}() *{{$TypeName}} {
}
}
{{if Features.FrugalTag}}
func (p *{{$TypeName}}) InitDefault() {
*p = {{$TypeName}}{
{{template "StructLikeDefault" .}}
}
{{- range .Fields}}
{{- if .IsSetDefault}}
p.{{.GoName}} = {{.DefaultValue}}
{{- end}}
{{- end}}
}
{{end}}{{/* if Features.FrugalTag */}}
{{template "FieldGetOrSet" .}}
Expand Down Expand Up @@ -650,6 +650,7 @@ var FieldReadMap = `
{{- $ctx := (.ValCtx.WithTarget $val).WithFieldMask $curFieldMask}}
{{- if $isStructVal}}
{{$val}} := &values[i]
{{$val}}.InitDefault()
{{- else}}
{{- $ctx = $ctx.WithDecl}}
{{- end}}
Expand Down Expand Up @@ -698,6 +699,7 @@ var FieldReadSet = `
{{- $ctx := (.ValCtx.WithTarget $val).WithFieldMask $curFieldMask}}
{{- if $isStructVal}}
{{$val}} := &values[i]
{{$val}}.InitDefault()
{{- else}}
{{- $ctx = $ctx.WithDecl}}
{{- end}}
Expand Down Expand Up @@ -746,6 +748,7 @@ var FieldReadList = `
{{- $ctx := (.ValCtx.WithTarget $val).WithFieldMask $curFieldMask}}
{{- if $isStructVal}}
{{$val}} := &values[i]
{{$val}}.InitDefault()
{{- else}}
{{- $ctx = $ctx.WithDecl}}
{{- end}}
Expand Down
10 changes: 3 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
"runtime/debug"
"runtime/pprof"

"github.com/cloudwego/thriftgo/config"

"time"

"github.com/cloudwego/thriftgo/args"
Expand All @@ -41,18 +39,16 @@ var (
var debugMode bool

func init() {
err := config.LoadConfig()
if err != nil {
panic(err)
}
_ = g.RegisterBackend(new(golang.GoBackend))
// export THRIFTGO_DEBUG=1
debugMode = os.Getenv("THRIFTGO_DEBUG") == "1"
}

func check(err error) {
if err != nil {
println(err.Error())
if err.Error() != "flag: help requested" {
println(err.Error())
}
os.Exit(2)
}
}
Expand Down
35 changes: 35 additions & 0 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,41 @@ func search(file, dir string, includeDirs []string) (string, error) {
return file, &os.PathError{Op: "search", Path: file, Err: os.ErrNotExist}
}

// ParseBatchString parses a group of string content and returns an AST.
// IDLContent is a map, which's key is IDLPath and value is IDL content.
func ParseBatchString(mainIDLFilePath string, IDLFileContentMap map[string]string, includeDirs []string) (*Thrift, error) {
thriftMap := make(map[string]*Thrift)
return parseBatchStringRecursively(mainIDLFilePath, includeDirs, thriftMap, IDLFileContentMap)
}

// doParseBatchString
func parseBatchStringRecursively(path string, includeDirs []string, thriftMap map[string]*Thrift, IDLFileContentMap map[string]string) (*Thrift, error) {

bs, ok := IDLFileContentMap[path]
if !ok {
return nil, fmt.Errorf("no idl found for: %s\n", path)
}
if t, ok := thriftMap[path]; ok {
return t, nil
}

t, err := parseString(path, bs, includeDirs)
if err != nil {
return nil, fmt.Errorf("parse %s err: %w\n", path, err)
}
thriftMap[path] = t
dir := filepath.Dir(path)
for _, inc := range t.Includes {
incPath := filepath.Join(dir, inc.Path)
t, err := parseBatchStringRecursively(incPath, includeDirs, thriftMap, IDLFileContentMap)
if err != nil {
return nil, err
}
inc.Reference = t
}
return t, nil
}

// ParseFile parses a thrift file and returns an AST.
// If recursive is true, then the include IDLs are parsed recursively as well.
func ParseFile(path string, includeDirs []string, recursive bool) (*Thrift, error) {
Expand Down
Loading

0 comments on commit 8fbd6a1

Please sign in to comment.