Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(goctl): fix nested struct generation #4281

Merged
merged 1 commit into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions tools/goctl/api/dartgen/gendata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions tools/goctl/api/gogen/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 13 additions & 3 deletions tools/goctl/api/spec/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions tools/goctl/api/spec/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tools/goctl/api/tsgen/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 6 additions & 8 deletions tools/goctl/pkg/parser/api/parser/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down