Skip to content

Commit

Permalink
Merge pull request #260 from wubin1989/main
Browse files Browse the repository at this point in the history
v2.4.2
  • Loading branch information
wubin1989 authored Sep 18, 2024
2 parents 67f49e1 + 0a7294f commit 919fbfb
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
20 changes: 18 additions & 2 deletions cmd/internal/svc/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -44,14 +55,15 @@ 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 {
panic(err)
}
sc := astutils.NewStructCollector(ExprStringP)
ast.Walk(sc, root)
sc.GlobalStructs = globalStructs
structs := sc.DocFlatEmbed()
var ret []v3.Schema
for _, item := range structs {
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
9 changes: 7 additions & 2 deletions toolkit/astutils/structcollector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions toolkit/protobuf/v3/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

type ProtoGenerator struct {
fieldNamingFunc func(string) string
Structs []astutils.StructMeta
}

type ProtoGeneratorOption func(*ProtoGenerator)
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion version/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package version

const Release = "v2.4.1"
const Release = "v2.4.2"

0 comments on commit 919fbfb

Please sign in to comment.