Skip to content

Commit cb0234e

Browse files
author
Zack Chase
committed
Clean up object generation [go]
1 parent b6da72d commit cb0234e

File tree

1 file changed

+36
-61
lines changed

1 file changed

+36
-61
lines changed

internal/gen/types/object.go

Lines changed: 36 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"slices"
66

7+
gogen "github.com/buildkite/buildkite-sdk/internal/gen/go"
78
"github.com/buildkite/buildkite-sdk/internal/gen/schema"
89
"github.com/buildkite/buildkite-sdk/internal/gen/typescript"
910
"github.com/buildkite/buildkite-sdk/internal/gen/utils"
@@ -44,25 +45,22 @@ func (o Object) GoStructKey(isUnion bool) string {
4445
func (o Object) Go() (string, error) {
4546
keys := o.Properties.Keys()
4647
if len(keys) == 0 {
47-
block := utils.NewCodeBlock()
48-
49-
if o.Description != "" {
50-
block.AddLines(fmt.Sprintf("// %s", o.Description))
51-
}
52-
48+
mapValueType := "interface{}"
5349
if o.AdditionalProperties != nil {
54-
prop := *o.AdditionalProperties
55-
block.AddLines(fmt.Sprintf("type %s = map[string]%s", o.Name.ToTitleCase(), prop.GoStructType()))
56-
return block.String(), nil
50+
mapValueType = (*o.AdditionalProperties).GoStructType()
5751
}
5852

59-
block.AddLines(fmt.Sprintf("type %s = map[string]interface{}", o.Name.ToTitleCase()))
60-
return block.String(), nil
53+
typ := gogen.NewType(
54+
o.Name.ToTitleCase(),
55+
o.Description,
56+
fmt.Sprintf("map[string]%s", mapValueType),
57+
)
58+
return typ.String(), nil
6159
}
6260

6361
codeBlock := utils.NewCodeBlock()
62+
objectStruct := gogen.NewGoStruct(o.Name.ToTitleCase(), o.Description, nil)
6463

65-
objectStruct := utils.NewGoStruct(o.Name.ToTitleCase(), o.Description, nil)
6664
for _, name := range keys {
6765
val, err := o.Properties.Get(name)
6866
if err != nil {
@@ -74,79 +72,56 @@ func (o Object) Go() (string, error) {
7472
description := val.GetDescription()
7573
isPointer := true
7674

77-
// Array
78-
if _, ok := val.(Array); ok {
75+
var extraValue Value
76+
switch typ := val.(type) {
77+
case Array:
7978
isPointer = false
8079
structKey = utils.DashCaseToTitleCase(name)
81-
}
82-
83-
// Object
84-
if obj, ok := val.(Object); ok {
80+
case Object:
8581
structKey = utils.DashCaseToTitleCase(name)
8682
nestedObjName := NewPropertyName(fmt.Sprintf("%s%s", o.Name.ToTitleCase(), structKey))
87-
nestedObj := Object{
83+
extraValue = Object{
8884
Name: nestedObjName,
89-
Description: obj.Description,
90-
Properties: obj.Properties,
91-
AdditionalProperties: obj.AdditionalProperties,
92-
}
93-
94-
objLines, err := nestedObj.Go()
95-
if err != nil {
96-
return "", fmt.Errorf("generating nested object for [%s]: %v", o.Name.Value, err)
85+
Description: typ.Description,
86+
Properties: typ.Properties,
87+
AdditionalProperties: typ.AdditionalProperties,
9788
}
9889

9990
structType = nestedObjName.ToTitleCase()
100-
codeBlock.AddLines(objLines)
101-
}
102-
103-
// Enum
104-
if enum, ok := val.(Enum); ok {
91+
case Enum:
10592
structKey = utils.DashCaseToTitleCase(name)
106-
nestedEnum := Enum{
93+
extraValue = Enum{
10794
Name: NewPropertyName(fmt.Sprintf("%s%s", o.Name.ToTitleCase(), structKey)),
108-
Description: enum.Description,
109-
Values: enum.Values,
110-
Default: enum.Default,
95+
Description: typ.Description,
96+
Values: typ.Values,
97+
Default: typ.Default,
11198
}
11299

113-
enumLines, err := nestedEnum.Go()
114-
if err != nil {
115-
return "", fmt.Errorf("generating enum lines for struct [%s]: %v", o.Name.Value, err)
100+
structType = extraValue.GoStructType()
101+
case Union:
102+
extraValue = Union{
103+
Name: NewPropertyName(fmt.Sprintf("%s%s", o.Name.ToTitleCase(), utils.DashCaseToTitleCase(name))),
104+
Description: typ.Description,
105+
TypeIdentifiers: typ.TypeIdentifiers,
116106
}
117107

118-
structType = nestedEnum.GoStructType()
119-
codeBlock.AddLines(enumLines)
108+
structKey = utils.DashCaseToTitleCase(name)
109+
structType = extraValue.GoStructType()
120110
}
121111

122-
// Union
123-
if union, ok := val.(Union); ok {
124-
nestedUnion := Union{
125-
Name: NewPropertyName(fmt.Sprintf("%s%s", o.Name.ToTitleCase(), utils.DashCaseToTitleCase(name))),
126-
Description: union.Description,
127-
TypeIdentifiers: union.TypeIdentifiers,
128-
}
129-
130-
unionLines, err := nestedUnion.Go()
112+
if extraValue != nil {
113+
extraLines, err := extraValue.Go()
131114
if err != nil {
132-
return "", fmt.Errorf("generating union lines for struct [%s]: %v", o.Name.Value, err)
115+
return "", err
133116
}
134-
135-
structType = nestedUnion.GoStructType()
136-
structKey = utils.DashCaseToTitleCase(name)
137-
codeBlock.AddLines(unionLines)
117+
codeBlock.AddLines(extraLines)
138118
}
139119

140120
objectStruct.AddItem(structKey, structType, name, description, isPointer)
141121
}
142122

143-
structLines, err := objectStruct.Write()
144-
if err != nil {
145-
return "", fmt.Errorf("writing out object struct [%s]: %v", objectStruct.Name, err)
146-
}
147-
148123
codeBlock.AddLines(
149-
structLines,
124+
objectStruct.Write(),
150125
)
151126

152127
return codeBlock.String(), nil

0 commit comments

Comments
 (0)