Skip to content

Commit

Permalink
Merge branch 'cloudwego:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
HeyJavaBean authored Jul 12, 2024
2 parents 9cb11bd + 43f56c1 commit bb4172a
Show file tree
Hide file tree
Showing 13 changed files with 154 additions and 145 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
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
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ func init() {

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
109 changes: 0 additions & 109 deletions reflection/FileDescriptor.go

This file was deleted.

18 changes: 12 additions & 6 deletions tool/trimmer/trim/trimmer.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,24 +109,30 @@ func doTrimAST(ast *parser.Thrift, trimMethods []string, forceTrimming bool, mat
}
}
trimmer.trimMethods[i], err = regexp2.Compile(trimMethods[i], 0)
check(err)
if err != nil {
return 0, 0, err
}
}
trimmer.preservedStructs = preservedStructs
trimmer.countStructs(ast)
trimmer.markAST(ast)
trimmer.traversal(ast, ast.Filename)
if path := parser.CircleDetect(ast); len(path) > 0 {
check(fmt.Errorf("found include circle:\n\t%s", path))
return 0, 0, fmt.Errorf("found include circle:\n\t%s", path)
}
checker := semantic.NewChecker(semantic.Options{FixWarnings: true})
_, err = checker.CheckAll(ast)
check(err)
check(semantic.ResolveSymbols(ast))
if err != nil {
return 0, 0, err
}
err = semantic.ResolveSymbols(ast)
if err != nil {
return 0, 0, err
}

for i, method := range trimMethods {
if !trimmer.trimMethodValid[i] {
println("err: method", method, "not found!")
os.Exit(2)
return 0, 0, fmt.Errorf("err: method %s not found!\n", method)
}
}

Expand Down
Loading

0 comments on commit bb4172a

Please sign in to comment.