From 0a7294f88c0f491a10737eb0f1388524bac1a7ea Mon Sep 17 00:00:00 2001 From: wubin1989 <328454505@qq.com> Date: Wed, 18 Sep 2024 11:18:32 +0800 Subject: [PATCH] v2.4.2 --- cmd/internal/svc/parser/parser.go | 20 ++++++++++++++++++-- toolkit/astutils/structcollector.go | 9 +++++++-- toolkit/protobuf/v3/service.go | 2 ++ version/version.go | 2 +- 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/cmd/internal/svc/parser/parser.go b/cmd/internal/svc/parser/parser.go index 6d423dae..66923570 100644 --- a/cmd/internal/svc/parser/parser.go +++ b/cmd/internal/svc/parser/parser.go @@ -26,6 +26,17 @@ import ( "github.com/unionj-cloud/go-doudou/v2/toolkit/stringutils" ) +func getStructs(vofile string) []astutils.StructMeta { + fset := token.NewFileSet() + root, err := parser.ParseFile(fset, vofile, nil, parser.ParseComments) + if err != nil { + panic(err) + } + sc := astutils.NewStructCollector(ExprStringP) + ast.Walk(sc, root) + return sc.Structs +} + func getSchemaNames(vofile string) []string { fset := token.NewFileSet() root, err := parser.ParseFile(fset, vofile, nil, parser.ParseComments) @@ -44,7 +55,7 @@ func getSchemaNames(vofile string) []string { return ret } -func schemasOf(vofile string) []v3.Schema { +func schemasOf(vofile string, globalStructs []astutils.StructMeta) []v3.Schema { fset := token.NewFileSet() root, err := parser.ParseFile(fset, vofile, nil, parser.ParseComments) if err != nil { @@ -52,6 +63,7 @@ func schemasOf(vofile string) []v3.Schema { } sc := astutils.NewStructCollector(ExprStringP) ast.Walk(sc, root) + sc.GlobalStructs = globalStructs structs := sc.DocFlatEmbed() var ret []v3.Schema for _, item := range structs { @@ -463,8 +475,10 @@ func ParseDto(dir string, dtoDir string) { if err != nil { panic(err) } + globalStructs := make([]astutils.StructMeta, 0) for _, file := range files { v3.SchemaNames = append(v3.SchemaNames, getSchemaNames(file)...) + globalStructs = append(globalStructs, getStructs(file)...) } allMethods = make(map[string][]astutils.MethodMeta) allConsts = make(map[string][]string) @@ -486,7 +500,7 @@ func ParseDto(dir string, dtoDir string) { } } for _, file := range files { - vos = append(vos, schemasOf(file)...) + vos = append(vos, schemasOf(file, globalStructs)...) } for _, item := range vos { v3.Schemas[item.Title] = item @@ -592,6 +606,7 @@ func messagesOf(vofile string, p protov3.ProtoGenerator) []protov3.Message { } sc := astutils.NewStructCollector(ExprStringP) ast.Walk(sc, root) + sc.GlobalStructs = p.Structs structs := sc.DocFlatEmbed() var ret []protov3.Message for _, item := range structs { @@ -618,6 +633,7 @@ func ParseDtoGrpc(dir string, p protov3.ProtoGenerator, dtoDir string) { } for _, file := range files { protov3.MessageNames = append(protov3.MessageNames, getSchemaNames(file)...) + p.Structs = append(p.Structs, getStructs(file)...) } allMethods = make(map[string][]astutils.MethodMeta) allConsts = make(map[string][]string) diff --git a/toolkit/astutils/structcollector.go b/toolkit/astutils/structcollector.go index caf3367e..436d9b93 100644 --- a/toolkit/astutils/structcollector.go +++ b/toolkit/astutils/structcollector.go @@ -19,6 +19,7 @@ type StructCollector struct { NonStructTypeMap map[string]ast.Expr exprString func(ast.Expr) string enums map[string]EnumMeta + GlobalStructs []StructMeta } // Visit traverse each node from source code @@ -75,15 +76,19 @@ func (sc *StructCollector) Collect(n ast.Node) ast.Visitor { // DocFlatEmbed flatten embed struct fields func (sc *StructCollector) DocFlatEmbed() []StructMeta { + structs := sc.GlobalStructs + if len(structs) == 0 { + structs = sc.Structs + } structMap := make(map[string]StructMeta) - for _, structMeta := range sc.Structs { + for _, structMeta := range structs { if _, exists := structMap[structMeta.Name]; !exists { structMap[structMeta.Name] = structMeta } } var exStructs []StructMeta - for _, structMeta := range sc.Structs { + for _, structMeta := range structs { if !structMeta.IsExport { continue } diff --git a/toolkit/protobuf/v3/service.go b/toolkit/protobuf/v3/service.go index 816bed63..f8db9c98 100644 --- a/toolkit/protobuf/v3/service.go +++ b/toolkit/protobuf/v3/service.go @@ -14,6 +14,7 @@ import ( type ProtoGenerator struct { fieldNamingFunc func(string) string + Structs []astutils.StructMeta } type ProtoGeneratorOption func(*ProtoGenerator) @@ -26,6 +27,7 @@ func WithFieldNamingFunc(fn func(string) string) ProtoGeneratorOption { func NewProtoGenerator(options ...ProtoGeneratorOption) ProtoGenerator { var p ProtoGenerator + p.Structs = make([]astutils.StructMeta, 0) for _, opt := range options { opt(&p) } diff --git a/version/version.go b/version/version.go index 61128d99..a3e54bfb 100644 --- a/version/version.go +++ b/version/version.go @@ -1,3 +1,3 @@ package version -const Release = "v2.4.1" +const Release = "v2.4.2"