From 2ba1628c52f38a4097419d34fe098755a2d089f0 Mon Sep 17 00:00:00 2001 From: anqiansong Date: Sun, 28 Jul 2024 22:08:45 +0800 Subject: [PATCH] fix nested struct generation --- tools/goctl/api/dartgen/gendata.go | 4 ++-- tools/goctl/api/gogen/util.go | 4 ++-- tools/goctl/api/spec/name.go | 16 +++++++++++++--- tools/goctl/api/spec/spec.go | 7 +++++++ tools/goctl/api/tsgen/util.go | 4 ++-- tools/goctl/pkg/parser/api/parser/analyzer.go | 14 ++++++-------- 6 files changed, 32 insertions(+), 17 deletions(-) diff --git a/tools/goctl/api/dartgen/gendata.go b/tools/goctl/api/dartgen/gendata.go index 0e01447b257c..9723b0ededc7 100644 --- a/tools/goctl/api/dartgen/gendata.go +++ b/tools/goctl/api/dartgen/gendata.go @@ -157,8 +157,8 @@ func convertDataType(api *spec.ApiSpec, isLegacy bool) (error, *DartSpec) { defineStruct, ok := ty.(spec.DefineStruct) if ok { for index, member := range defineStruct.Members { - structMember, ok := member.Type.(spec.DefineStruct) - if ok && structMember.IsNestedStruct() { + structMember, ok := member.Type.(spec.NestedStruct) + if ok { defineStruct.Members[index].Type = spec.PrimitiveType{RawName: member.Name} t := template.New("dataTemplate") t = t.Funcs(funcMap) diff --git a/tools/goctl/api/gogen/util.go b/tools/goctl/api/gogen/util.go index 847197bf5e0b..566aa1c7bd53 100644 --- a/tools/goctl/api/gogen/util.go +++ b/tools/goctl/api/gogen/util.go @@ -63,8 +63,8 @@ func writeProperty(writer io.Writer, name, tag, comment string, tp spec.Type, in err error isNestedStruct bool ) - structType, ok := tp.(spec.DefineStruct) - if ok && structType.IsNestedStruct() { + structType, ok := tp.(spec.NestedStruct) + if ok { isNestedStruct = true } if len(comment) > 0 { diff --git a/tools/goctl/api/spec/name.go b/tools/goctl/api/spec/name.go index 6c0c855766f3..9fe9995314af 100644 --- a/tools/goctl/api/spec/name.go +++ b/tools/goctl/api/spec/name.go @@ -30,9 +30,19 @@ func (t DefineStruct) Documents() []string { return t.Docs } -// IsNestedStruct returns whether the structure is nested. -func (t DefineStruct) IsNestedStruct() bool { - return len(t.Members) > 0 +// Name returns a structure string, such as User +func (t NestedStruct) Name() string { + return t.RawName +} + +// Comments returns the comments of struct +func (t NestedStruct) Comments() []string { + return nil +} + +// Documents returns the documents of struct +func (t NestedStruct) Documents() []string { + return t.Docs } // Name returns a map string, such as map[string]int diff --git a/tools/goctl/api/spec/spec.go b/tools/goctl/api/spec/spec.go index 7127a81f4c8a..8249c66f8195 100644 --- a/tools/goctl/api/spec/spec.go +++ b/tools/goctl/api/spec/spec.go @@ -105,6 +105,13 @@ type ( Docs Doc } + // NestedStruct describes a structure nested in structure. + NestedStruct struct { + RawName string + Members []Member + Docs Doc + } + // PrimitiveType describes the basic golang type, such as bool,int32,int64, ... PrimitiveType struct { RawName string diff --git a/tools/goctl/api/tsgen/util.go b/tools/goctl/api/tsgen/util.go index 328d871543ec..d105eea70b5f 100644 --- a/tools/goctl/api/tsgen/util.go +++ b/tools/goctl/api/tsgen/util.go @@ -54,8 +54,8 @@ func writeIndent(writer io.Writer, indent int) { } func genTsType(m spec.Member, indent int) (ty string, err error) { - v, ok := m.Type.(spec.DefineStruct) - if ok && v.IsNestedStruct() { + v, ok := m.Type.(spec.NestedStruct) + if ok { writer := bytes.NewBuffer(nil) _, err := fmt.Fprintf(writer, "{\n") if err != nil { diff --git a/tools/goctl/pkg/parser/api/parser/analyzer.go b/tools/goctl/pkg/parser/api/parser/analyzer.go index 2b1c302a108e..5561fd1e2da7 100644 --- a/tools/goctl/pkg/parser/api/parser/analyzer.go +++ b/tools/goctl/pkg/parser/api/parser/analyzer.go @@ -54,7 +54,7 @@ func (a *Analyzer) astTypeToSpec(in ast.DataType) (spec.Type, error) { return nil, ast.SyntaxError(v.Pos(), "unsupported empty struct") } - return spec.DefineStruct{ + return spec.NestedStruct{ RawName: v.RawText(), Members: members, }, nil @@ -347,14 +347,12 @@ func (a *Analyzer) fillTypes() error { for _, member := range v.Members { switch v := member.Type.(type) { case spec.DefineStruct: - if !v.IsNestedStruct() { - tp, err := a.findDefinedType(v.RawName) - if err != nil { - return err - } - - member.Type = tp + tp, err := a.findDefinedType(v.RawName) + if err != nil { + return err } + + member.Type = tp } members = append(members, member) }