diff --git a/internal/gen/go.go b/internal/gen/go.go index fba49bc..f94cf76 100644 --- a/internal/gen/go.go +++ b/internal/gen/go.go @@ -3,6 +3,7 @@ package main import ( "fmt" + gogen "github.com/buildkite/buildkite-sdk/internal/gen/go" "github.com/buildkite/buildkite-sdk/internal/gen/types" "github.com/buildkite/buildkite-sdk/internal/gen/utils" ) @@ -28,15 +29,7 @@ func generateGoTypes( return fmt.Errorf("generating files contents for [%s]: %v", fileName, err) } - file := utils.NewGoFile( - "buildkite", - fileName, - []string{}, - utils.NewCodeBlock( - contents, - ), - ) - + file := gogen.NewGoFile("buildkite", fileName, contents) err = file.Write() if err != nil { return fmt.Errorf("writing file [%s]: %v", fileName, err) @@ -49,15 +42,7 @@ func generateGoTypes( } pipelineFileName := fmt.Sprintf("%s/pipeline.go", outDir) - file := utils.NewGoFile( - "buildkite", - pipelineFileName, - []string{}, - utils.NewCodeBlock( - pipelineSchemaString, - ), - ) - + file := gogen.NewGoFile("buildkite", pipelineFileName, pipelineSchemaString) err = file.Write() if err != nil { return fmt.Errorf("writing file [%s]: %v", pipelineFileName, err) diff --git a/internal/gen/go/comment.go b/internal/gen/go/comment.go new file mode 100644 index 0000000..3770128 --- /dev/null +++ b/internal/gen/go/comment.go @@ -0,0 +1,7 @@ +package gogen + +import "fmt" + +func NewGoComment(comment string) string { + return fmt.Sprintf("// %s", comment) +} diff --git a/internal/gen/go/constraint_interface.go b/internal/gen/go/constraint_interface.go new file mode 100644 index 0000000..12c3ab1 --- /dev/null +++ b/internal/gen/go/constraint_interface.go @@ -0,0 +1,41 @@ +package gogen + +import ( + "fmt" + "strings" + + "github.com/buildkite/buildkite-sdk/internal/gen/utils" +) + +type GoConstraintInterface struct { + Name string + Description string + Items []string +} + +func (g *GoConstraintInterface) AddItem(item string) { + g.Items = append(g.Items, item) +} + +func (g *GoConstraintInterface) Write() string { + contents := utils.NewCodeBlock() + + if g.Description != "" { + contents.AddLines(NewGoComment(g.Description)) + } + + contents.AddLines( + fmt.Sprintf("type %s interface {", g.Name), + fmt.Sprintf(" %s", strings.Join(g.Items, " | ")), + "}", + ) + + return contents.String() +} + +func NewGoConstraintInterface(name, description string) *GoConstraintInterface { + return &GoConstraintInterface{ + Name: name, + Description: description, + } +} diff --git a/internal/gen/go/enum_values.go b/internal/gen/go/enum_values.go new file mode 100644 index 0000000..0fec628 --- /dev/null +++ b/internal/gen/go/enum_values.go @@ -0,0 +1,44 @@ +package gogen + +import ( + "fmt" + + "github.com/buildkite/buildkite-sdk/internal/gen/utils" +) + +type GoEnumValues struct { + EnumName string + Description string + Values []any +} + +func (g GoEnumValues) Write() string { + lines := utils.NewCodeBlock( + fmt.Sprintf("type %s string", g.EnumName), + ) + + if g.Description != "" { + lines.AddLines(NewGoComment(g.Description)) + } + + lines.AddLines( + fmt.Sprintf("var %sValues = map[string]%s{", g.EnumName, g.EnumName), + ) + + for _, val := range g.Values { + lines.AddLinesWithIndent( + 4, + fmt.Sprintf("\"%s\": \"%v\",", val, val), + ) + } + + lines.AddLines("}") + return lines.String() +} + +func NewGoEnumValues(name string, values []any) GoEnumValues { + return GoEnumValues{ + EnumName: name, + Values: values, + } +} diff --git a/internal/gen/go/file.go b/internal/gen/go/file.go new file mode 100644 index 0000000..b1eed98 --- /dev/null +++ b/internal/gen/go/file.go @@ -0,0 +1,39 @@ +package gogen + +import ( + "fmt" + "os" + + "github.com/buildkite/buildkite-sdk/internal/gen/utils" +) + +type GoFile struct { + PackageName string + Name string + Contents string +} + +func (g GoFile) Write() error { + contents := utils.NewCodeBlock( + "// Code generated by the gen package. DO NOT EDIT.", + "// *** WARNING: Do not edit by hand unless you're certain you know what you are doing! ***", + "", + fmt.Sprintf("package %s", g.PackageName), + "", + g.Contents, + ).String() + + err := os.WriteFile(g.Name, []byte(contents), 0644) + if err != nil { + return fmt.Errorf("writing file [%s]: %v", g.Name, err) + } + return nil +} + +func NewGoFile(packageName, name, contents string) GoFile { + return GoFile{ + PackageName: packageName, + Name: name, + Contents: contents, + } +} diff --git a/internal/gen/go/struct.go b/internal/gen/go/struct.go new file mode 100644 index 0000000..58044b0 --- /dev/null +++ b/internal/gen/go/struct.go @@ -0,0 +1,111 @@ +package gogen + +import ( + "fmt" + "strings" + + "github.com/buildkite/buildkite-sdk/internal/gen/utils" +) + +type GoStructItem struct { + Name string + Description string + Value string + Pointer bool + TagName string +} + +type GoStruct struct { + Name string + Description string + Items *utils.OrderedMap[GoStructItem] +} + +func (g *GoStruct) AddItem(key, val, tagName, description string, isPointer bool) { + g.Items.Set(key, GoStructItem{ + Name: key, + Description: description, + Value: val, + Pointer: isPointer, + TagName: tagName, + }) +} + +func (g GoStruct) WriteConstraintInterface() string { + contents := utils.NewCodeBlock() + + keys := g.Items.Keys() + items := make([]string, len(keys)) + for i, key := range keys { + item, _ := g.Items.Get(key) + items[i] = item.Value + } + + contents.AddLines( + fmt.Sprintf("type %sValues interface {", g.Name), + fmt.Sprintf(" %s", strings.Join(items, " | ")), + "}", + ) + + return contents.String() +} + +func (g GoStruct) WriteMarshalFunction() string { + contents := utils.NewCodeBlock( + fmt.Sprintf("func (e %s) MarshalJSON() ([]byte, error) {", g.Name), + ) + + g.Items.SortKeys() + for _, key := range g.Items.Keys() { + item, _ := g.Items.Get(key) + contents.AddLinesWithIndent(4, fmt.Sprintf("if e.%s != nil {", item.Name)) + contents.AddLinesWithIndent(8, fmt.Sprintf("return json.Marshal(e.%s)", item.Name)) + contents.AddLinesWithIndent(4, "}") + } + + contents.AddLinesWithIndent(4, "return json.Marshal(nil)") + contents.AddLines("}") + return contents.String() +} + +func (g GoStruct) Write() string { + contents := utils.NewCodeBlock() + + if g.Description != "" { + contents.AddLines(NewGoComment(g.Description)) + } + + contents.AddLines(fmt.Sprintf("type %s struct {", g.Name)) + + g.Items.SortKeys() + for _, key := range g.Items.Keys() { + item, _ := g.Items.Get(key) + + if item.Description != "" { + contents.AddLines(fmt.Sprintf(" %s", NewGoComment(item.Description))) + } + + pointer := "" + if item.Pointer { + pointer = "*" + } + + tag := "" + if item.TagName != "" { + tag = fmt.Sprintf(" `json:\"%s,omitempty\"`", item.TagName) + } + + contents.AddLines(fmt.Sprintf(" %s %s%s%s", item.Name, pointer, item.Value, tag)) + } + + contents.AddLines("}") + return contents.String() +} + +func NewGoStruct(name, description string, items []GoStructItem) *GoStruct { + return &GoStruct{ + Name: name, + Description: description, + Items: utils.NewOrderedMap[GoStructItem](nil), + } +} diff --git a/internal/gen/go/type.go b/internal/gen/go/type.go new file mode 100644 index 0000000..6728d04 --- /dev/null +++ b/internal/gen/go/type.go @@ -0,0 +1,35 @@ +package gogen + +import ( + "fmt" + + "github.com/buildkite/buildkite-sdk/internal/gen/utils" +) + +type goType struct { + Name string + Description string + Value string +} + +func (g goType) String() string { + block := utils.NewCodeBlock() + + if g.Description != "" { + block.AddLines(NewGoComment(g.Description)) + } + + block.AddLines( + fmt.Sprintf("type %s = %s", g.Name, g.Value), + ) + + return block.String() +} + +func NewType(name, description, value string) goType { + return goType{ + Name: name, + Description: description, + Value: value, + } +} diff --git a/internal/gen/types/array.go b/internal/gen/types/array.go index a55fe80..4e67343 100644 --- a/internal/gen/types/array.go +++ b/internal/gen/types/array.go @@ -4,6 +4,7 @@ import ( "fmt" "strings" + gogen "github.com/buildkite/buildkite-sdk/internal/gen/go" "github.com/buildkite/buildkite-sdk/internal/gen/typescript" "github.com/buildkite/buildkite-sdk/internal/gen/utils" ) @@ -29,37 +30,25 @@ func (Array) IsPrimitive() bool { // Go func (a Array) GoStructType() string { - if a.IsReference() { + if a.IsReference() || isPrimitiveValue(a.Type) { return fmt.Sprintf("[]%s", a.Type.GoStructType()) } - switch a.Type.(type) { - case String: - return "[]string" - case Boolean: - return "[]bool" - case Number: - return "[]int" - case Union: - return fmt.Sprintf("[]%sItem", a.Name.ToTitleCase()) - default: - return fmt.Sprintf("[]%s", a.Name.ToTitleCase()) + typeValue := a.Name.ToTitleCase() + if isUnionValue(a.Type) { + typeValue = fmt.Sprintf("%sItem", typeValue) } + return fmt.Sprintf("[]%s", typeValue) } func (a Array) GoStructKey(isUnion bool) string { if isUnion { - switch a.Type.(type) { - case String: - return "StringArray" - case Boolean: - return "BoolArray" - case Number: - return "IntArray" - case Union: + if isPrimitiveValue(a.Type) { + return fmt.Sprintf("%sArray", utils.CamelCaseToTitleCase(a.Type.GoStructType())) + } + + if isUnionValue(a.Type) { return fmt.Sprintf("%sItem", a.Name.ToTitleCase()) - default: - return a.Name.ToTitleCase() } } @@ -67,11 +56,11 @@ func (a Array) GoStructKey(isUnion bool) string { } func (a Array) Go() (string, error) { - lines := utils.NewCodeBlock() - description := fmt.Sprintf("// %s", a.Description) + contents := utils.NewCodeBlock() + typeValue := a.GoStructType() - union, ok := a.Type.(Union) - if !a.IsReference() && ok { + if isUnionValue(a.Type) && !a.IsReference() { + union := a.Type.(Union) item := Union{ Name: NewPropertyName(fmt.Sprintf("%sItem", a.Name.Value)), Description: union.Description, @@ -83,22 +72,16 @@ func (a Array) Go() (string, error) { return "", fmt.Errorf("generating lines for union in array [%s]: %v", a.Name.Value, err) } - lines.AddLines(itemLines) - - if description != "" { - lines.AddLines(description) - } - - lines.AddLines(fmt.Sprintf("type %s = []%sItem", a.Name.ToTitleCase(), a.Type.GoStructType())) - return lines.String(), nil - } - - if description != "" { - lines.AddLines(description) + contents.AddLines(itemLines) } - lines.AddLines(fmt.Sprintf("type %s = []%s", a.Name.ToTitleCase(), a.Type.GoStructType())) - return lines.String(), nil + typ := gogen.NewType( + a.Name.ToTitleCase(), + a.Description, + typeValue, + ) + contents.AddLines(typ.String()) + return contents.String(), nil } // TypeScript diff --git a/internal/gen/types/boolean.go b/internal/gen/types/boolean.go index 250e237..42d0a51 100644 --- a/internal/gen/types/boolean.go +++ b/internal/gen/types/boolean.go @@ -3,6 +3,7 @@ package types import ( "fmt" + gogen "github.com/buildkite/buildkite-sdk/internal/gen/go" "github.com/buildkite/buildkite-sdk/internal/gen/typescript" "github.com/buildkite/buildkite-sdk/internal/gen/utils" ) @@ -38,13 +39,12 @@ func (b Boolean) GoStructKey(isUnion bool) string { } func (b Boolean) Go() (string, error) { - block := utils.NewCodeBlock() - if b.Description != "" { - block.AddLines(fmt.Sprintf("// %s", b.Description)) - } - - block.AddLines(fmt.Sprintf("type %s = string", b.Name.ToTitleCase())) - return block.String(), nil + typ := gogen.NewType( + b.Name.ToTitleCase(), + b.Description, + b.GoStructType(), + ) + return typ.String(), nil } // TypeScript diff --git a/internal/gen/types/enum.go b/internal/gen/types/enum.go index b0c7161..be9985e 100644 --- a/internal/gen/types/enum.go +++ b/internal/gen/types/enum.go @@ -2,39 +2,33 @@ package types import ( "fmt" - "sort" "strings" + gogen "github.com/buildkite/buildkite-sdk/internal/gen/go" "github.com/buildkite/buildkite-sdk/internal/gen/typescript" "github.com/buildkite/buildkite-sdk/internal/gen/utils" - "github.com/iancoleman/orderedmap" ) func parseEnumValue(val any) EnumValue { - if s, ok := val.(string); ok { - return EnumValue{ - Value: s, - Type: String{}, + var enumType Value + switch val.(type) { + case string: + enumType = String{} + case bool: + enumType = Boolean{} + case int: + enumType = Number{ + Name: NewPropertyName("Int"), } - } + default: + panic("enum type not implemented ") - if b, ok := val.(bool); ok { - return EnumValue{ - Value: b, - Type: Boolean{}, - } } - if n, ok := val.(int); ok { - return EnumValue{ - Value: n, - Type: Number{ - Name: NewPropertyName("Int"), - }, - } + return EnumValue{ + Value: val, + Type: enumType, } - - panic("not implemented enum type") } type EnumValue struct { @@ -71,79 +65,40 @@ func (e Enum) GoStructKey(isUnion bool) string { } func (e Enum) Go() (string, error) { - lines := utils.NewCodeBlock() - - displayName := e.Name.ToTitleCase() enumDefinitionName := e.GoStructType() + enumDefinition := gogen.NewGoStruct(enumDefinitionName, e.Description, nil) - enumInterface := utils.NewGoConstraintInterface(fmt.Sprintf("%sValues", enumDefinitionName), e.Description) - enumDefinition := utils.NewGoStruct(enumDefinitionName, e.Description, nil) - - enumMarshalFunction := utils.NewCodeBlock( - fmt.Sprintf("func (e %s) MarshalJSON() ([]byte, error) {", displayName), - ) - - enumTypes := orderedmap.New() + enumTypes := utils.NewOrderedMap[string](nil) for _, val := range e.Values { parsed := parseEnumValue(val) typ := parsed.Type.GoStructType() enumTypes.Set(typ, typ) } - enumTypes.SortKeys(sort.Strings) + + enumTypes.SortKeys() + enumKeys := enumTypes.Keys() // If there is only one type in the values. - if len(enumTypes.Keys()) == 1 { - for _, typ := range enumTypes.Keys() { - if typ != "string" { - panic("type not supported in single enum") - } - - lines.AddLines( - fmt.Sprintf("type %s %s", enumDefinitionName, typ), - fmt.Sprintf("// %s", e.Description), - fmt.Sprintf("var %sValues = map[string]%s{", enumDefinitionName, enumDefinitionName), - ) - - for _, val := range e.Values { - lines.AddLines( - fmt.Sprintf(" \"%s\": \"%v\",", val, val), - ) - } - - lines.AddLines("}") + if len(enumKeys) == 1 { + typ := enumKeys[0] + if typ != "string" { + panic("go enum values: only strings are supported") } - return lines.String(), nil + + enumValues := gogen.NewGoEnumValues(enumDefinitionName, e.Values) + return enumValues.Write(), nil } - for _, typ := range enumTypes.Keys() { + for _, typ := range enumKeys { titleCaseType := utils.CamelCaseToTitleCase(typ) - - enumInterface.AddItem(typ) enumDefinition.AddItem(titleCaseType, typ, "", "", true) - - enumMarshalFunction.AddLines( - fmt.Sprintf(" if e.%s != nil {\n return json.Marshal(e.%s)\n }", titleCaseType, titleCaseType), - ) } - enumMarshalFunction.AddLines(" return json.Marshal(nil)\n}") - - enumInterfaceString, err := enumInterface.Write() - if err != nil { - return "", fmt.Errorf("generating interface for [%s]: %v", e.Name.Value, err) - } - - enumDefinitionString, err := enumDefinition.Write() - if err != nil { - return "", fmt.Errorf("generating struct for [%s]: %v", e.Name.Value, err) - } - - lines.AddLines( - enumInterfaceString, - enumDefinitionString, - enumMarshalFunction.String(), + lines := utils.NewCodeBlock( + enumDefinition.WriteConstraintInterface(), + enumDefinition.Write(), + enumDefinition.WriteMarshalFunction(), ) - return lines.String(), nil } diff --git a/internal/gen/types/number.go b/internal/gen/types/number.go index 88b2d8e..8fd36bc 100644 --- a/internal/gen/types/number.go +++ b/internal/gen/types/number.go @@ -3,6 +3,7 @@ package types import ( "fmt" + gogen "github.com/buildkite/buildkite-sdk/internal/gen/go" "github.com/buildkite/buildkite-sdk/internal/gen/typescript" "github.com/buildkite/buildkite-sdk/internal/gen/utils" ) @@ -26,13 +27,12 @@ func (Number) IsPrimitive() bool { // Go func (n Number) Go() (string, error) { - block := utils.NewCodeBlock() - if n.Description != "" { - block.AddLines(fmt.Sprintf("// %s", n.Description)) - } - - block.AddLines(fmt.Sprintf("type %s = int", n.Name.ToTitleCase())) - return block.String(), nil + typ := gogen.NewType( + n.Name.ToTitleCase(), + n.Description, + "int", + ) + return typ.String(), nil } func (Number) GoStructType() string { diff --git a/internal/gen/types/object.go b/internal/gen/types/object.go index 6b84a73..46bbc03 100644 --- a/internal/gen/types/object.go +++ b/internal/gen/types/object.go @@ -4,6 +4,7 @@ import ( "fmt" "slices" + gogen "github.com/buildkite/buildkite-sdk/internal/gen/go" "github.com/buildkite/buildkite-sdk/internal/gen/schema" "github.com/buildkite/buildkite-sdk/internal/gen/typescript" "github.com/buildkite/buildkite-sdk/internal/gen/utils" @@ -44,25 +45,22 @@ func (o Object) GoStructKey(isUnion bool) string { func (o Object) Go() (string, error) { keys := o.Properties.Keys() if len(keys) == 0 { - block := utils.NewCodeBlock() - - if o.Description != "" { - block.AddLines(fmt.Sprintf("// %s", o.Description)) - } - + mapValueType := "interface{}" if o.AdditionalProperties != nil { - prop := *o.AdditionalProperties - block.AddLines(fmt.Sprintf("type %s = map[string]%s", o.Name.ToTitleCase(), prop.GoStructType())) - return block.String(), nil + mapValueType = (*o.AdditionalProperties).GoStructType() } - block.AddLines(fmt.Sprintf("type %s = map[string]interface{}", o.Name.ToTitleCase())) - return block.String(), nil + typ := gogen.NewType( + o.Name.ToTitleCase(), + o.Description, + fmt.Sprintf("map[string]%s", mapValueType), + ) + return typ.String(), nil } codeBlock := utils.NewCodeBlock() + objectStruct := gogen.NewGoStruct(o.Name.ToTitleCase(), o.Description, nil) - objectStruct := utils.NewGoStruct(o.Name.ToTitleCase(), o.Description, nil) for _, name := range keys { val, err := o.Properties.Get(name) if err != nil { @@ -74,79 +72,56 @@ func (o Object) Go() (string, error) { description := val.GetDescription() isPointer := true - // Array - if _, ok := val.(Array); ok { + var extraValue Value + switch typ := val.(type) { + case Array: isPointer = false structKey = utils.DashCaseToTitleCase(name) - } - - // Object - if obj, ok := val.(Object); ok { + case Object: structKey = utils.DashCaseToTitleCase(name) nestedObjName := NewPropertyName(fmt.Sprintf("%s%s", o.Name.ToTitleCase(), structKey)) - nestedObj := Object{ + extraValue = Object{ Name: nestedObjName, - Description: obj.Description, - Properties: obj.Properties, - AdditionalProperties: obj.AdditionalProperties, - } - - objLines, err := nestedObj.Go() - if err != nil { - return "", fmt.Errorf("generating nested object for [%s]: %v", o.Name.Value, err) + Description: typ.Description, + Properties: typ.Properties, + AdditionalProperties: typ.AdditionalProperties, } structType = nestedObjName.ToTitleCase() - codeBlock.AddLines(objLines) - } - - // Enum - if enum, ok := val.(Enum); ok { + case Enum: structKey = utils.DashCaseToTitleCase(name) - nestedEnum := Enum{ + extraValue = Enum{ Name: NewPropertyName(fmt.Sprintf("%s%s", o.Name.ToTitleCase(), structKey)), - Description: enum.Description, - Values: enum.Values, - Default: enum.Default, + Description: typ.Description, + Values: typ.Values, + Default: typ.Default, } - enumLines, err := nestedEnum.Go() - if err != nil { - return "", fmt.Errorf("generating enum lines for struct [%s]: %v", o.Name.Value, err) + structType = extraValue.GoStructType() + case Union: + extraValue = Union{ + Name: NewPropertyName(fmt.Sprintf("%s%s", o.Name.ToTitleCase(), utils.DashCaseToTitleCase(name))), + Description: typ.Description, + TypeIdentifiers: typ.TypeIdentifiers, } - structType = nestedEnum.GoStructType() - codeBlock.AddLines(enumLines) + structKey = utils.DashCaseToTitleCase(name) + structType = extraValue.GoStructType() } - // Union - if union, ok := val.(Union); ok { - nestedUnion := Union{ - Name: NewPropertyName(fmt.Sprintf("%s%s", o.Name.ToTitleCase(), utils.DashCaseToTitleCase(name))), - Description: union.Description, - TypeIdentifiers: union.TypeIdentifiers, - } - - unionLines, err := nestedUnion.Go() + if extraValue != nil { + extraLines, err := extraValue.Go() if err != nil { - return "", fmt.Errorf("generating union lines for struct [%s]: %v", o.Name.Value, err) + return "", err } - - structType = nestedUnion.GoStructType() - structKey = utils.DashCaseToTitleCase(name) - codeBlock.AddLines(unionLines) + codeBlock.AddLines(extraLines) } objectStruct.AddItem(structKey, structType, name, description, isPointer) } - structLines, err := objectStruct.Write() - if err != nil { - return "", fmt.Errorf("writing out object struct [%s]: %v", objectStruct.Name, err) - } - codeBlock.AddLines( - structLines, + objectStruct.Write(), ) return codeBlock.String(), nil diff --git a/internal/gen/types/string.go b/internal/gen/types/string.go index a5e75f0..a780209 100644 --- a/internal/gen/types/string.go +++ b/internal/gen/types/string.go @@ -3,6 +3,7 @@ package types import ( "fmt" + gogen "github.com/buildkite/buildkite-sdk/internal/gen/go" "github.com/buildkite/buildkite-sdk/internal/gen/typescript" "github.com/buildkite/buildkite-sdk/internal/gen/utils" ) @@ -26,13 +27,12 @@ func (String) IsPrimitive() bool { // Go func (s String) Go() (string, error) { - block := utils.NewCodeBlock() - if s.Description != "" { - block.AddLines(fmt.Sprintf("// %s", s.Description)) - } - - block.AddLines(fmt.Sprintf("type %s = string", s.Name.ToTitleCase())) - return block.String(), nil + typ := gogen.NewType( + s.Name.ToTitleCase(), + s.Description, + "string", + ) + return typ.String(), nil } func (s String) GoStructType() string { diff --git a/internal/gen/types/union.go b/internal/gen/types/union.go index d050962..2c0cbd6 100644 --- a/internal/gen/types/union.go +++ b/internal/gen/types/union.go @@ -4,6 +4,7 @@ import ( "fmt" "strings" + gogen "github.com/buildkite/buildkite-sdk/internal/gen/go" "github.com/buildkite/buildkite-sdk/internal/gen/typescript" "github.com/buildkite/buildkite-sdk/internal/gen/utils" ) @@ -37,90 +38,47 @@ func (u Union) GoStructType() string { func (u Union) Go() (string, error) { lines := utils.NewCodeBlock() - - displayName := u.Name.ToTitleCase() - unionValuesName := fmt.Sprintf("%sValues", displayName) - - unionInterface := utils.NewGoConstraintInterface(unionValuesName, u.Description) - unionDefinition := utils.NewGoStruct(displayName, u.Description, nil) - - unionMarshalFunction := utils.NewCodeBlock( - fmt.Sprintf("func (e %s) MarshalJSON() ([]byte, error) {", displayName), - ) + unionDefinition := gogen.NewGoStruct(u.Name.ToTitleCase(), u.Description, nil) for _, typ := range u.TypeIdentifiers { - titleCaseType := typ.GoStructType() - structKey := typ.GoStructKey(true) isPointer := true - - // Enum - if enum, ok := typ.(Enum); ok { - enumLines, err := enum.Go() - if err != nil { - return "", fmt.Errorf("generating enum lines for union [%s]: %v", u.Name.Value, err) - } - - lines.AddLines(enumLines) - } - - // Object - if obj, ok := typ.(Object); ok { - nestedObj := Object{ - Name: NewPropertyName(fmt.Sprintf("%sObject", obj.Name.Value)), - Properties: obj.Properties, - AdditionalProperties: obj.AdditionalProperties, + structKey := typ.GoStructKey(true) + structType := typ.GoStructType() + structTag := "" + + var extraDefinition Value + switch val := typ.(type) { + case Enum: + extraDefinition = typ + case Object: + extraDefinition = Object{ + Name: NewPropertyName(fmt.Sprintf("%sObject", val.Name.Value)), + Properties: val.Properties, + AdditionalProperties: val.AdditionalProperties, } - - objLines, err := nestedObj.Go() - if err != nil { - return "", fmt.Errorf("generating object lines for union [%s]: %v", u.Name.Value, err) + structType = extraDefinition.GoStructType() + case Array: + isPointer = false + if !isPrimitiveValue(val.Type) { + extraDefinition = val } - - titleCaseType = nestedObj.GoStructType() - lines.AddLines(objLines) } - // Array - if array, ok := typ.(Array); ok { - isPointer = false - - switch array.Type.(type) { - case String: - case Boolean: - case Number: - default: - arrayLines, err := array.Go() - if err != nil { - return "", fmt.Errorf("generating array lines for union [%s]: %v", u.Name.Value, err) - } - - lines.AddLines(arrayLines) + if extraDefinition != nil { + extraLines, err := extraDefinition.Go() + if err != nil { + return "", err } + lines.AddLines(extraLines) } - unionInterface.AddItem(titleCaseType) - unionDefinition.AddItem(structKey, titleCaseType, "", typ.GetDescription(), isPointer) - unionMarshalFunction.AddLines( - fmt.Sprintf(" if e.%s != nil {\n return json.Marshal(e.%s)\n }", structKey, structKey), - ) - } - - unionMarshalFunction.AddLines(" return json.Marshal(nil)\n}") - - unionInterfaceLines, err := unionInterface.Write() - if err != nil { - return "", fmt.Errorf("generating union interface [%s]: %v", u.Name.Value, err) - } - - unionDefinitionLines, err := unionDefinition.Write() - if err != nil { - return "", fmt.Errorf("generating union interface [%s]: %v", u.Name.Value, err) + unionDefinition.AddItem(structKey, structType, structTag, typ.GetDescription(), isPointer) } lines.AddLines( - unionInterfaceLines, - unionDefinitionLines, - unionMarshalFunction.String(), + unionDefinition.WriteConstraintInterface(), + unionDefinition.Write(), + unionDefinition.WriteMarshalFunction(), ) return lines.String(), nil diff --git a/internal/gen/types/value.go b/internal/gen/types/value.go index 625f1e6..6a8e09e 100644 --- a/internal/gen/types/value.go +++ b/internal/gen/types/value.go @@ -625,6 +625,21 @@ func (p PipelineSchemaGenerator) UnionDefinitionToUnionValue(propertyName Proper }, dependencies, nil } +// Value +func isPrimitiveValue(val Value) bool { + switch val.(type) { + case String, Number, Boolean: + return true + default: + return false + } +} + +func isUnionValue(val Value) bool { + _, ok := val.(Union) + return ok +} + type Value interface { GetDescription() string diff --git a/sdk/go/sdk/buildkite/agents.go b/sdk/go/sdk/buildkite/agents.go index 6037aef..321b102 100644 --- a/sdk/go/sdk/buildkite/agents.go +++ b/sdk/go/sdk/buildkite/agents.go @@ -16,11 +16,11 @@ type Agents struct { } func (e Agents) MarshalJSON() ([]byte, error) { - if e.AgentsObject != nil { - return json.Marshal(e.AgentsObject) - } if e.AgentsList != nil { return json.Marshal(e.AgentsList) } + if e.AgentsObject != nil { + return json.Marshal(e.AgentsObject) + } return json.Marshal(nil) } diff --git a/sdk/go/sdk/buildkite/allow_dependency_failure.go b/sdk/go/sdk/buildkite/allow_dependency_failure.go index 30022f1..c60220f 100644 --- a/sdk/go/sdk/buildkite/allow_dependency_failure.go +++ b/sdk/go/sdk/buildkite/allow_dependency_failure.go @@ -5,7 +5,6 @@ package buildkite import "encoding/json" -// Whether to proceed with this step and further steps if a step named in the depends_on attribute fails type AllowDependencyFailureValues interface { bool | string } diff --git a/sdk/go/sdk/buildkite/allowed_teams.go b/sdk/go/sdk/buildkite/allowed_teams.go index 885495e..8a16068 100644 --- a/sdk/go/sdk/buildkite/allowed_teams.go +++ b/sdk/go/sdk/buildkite/allowed_teams.go @@ -5,7 +5,6 @@ package buildkite import "encoding/json" -// A list of teams that are permitted to unblock this step, whose values are a list of one or more team slugs or IDs type AllowedTeamsValues interface { string | []string } diff --git a/sdk/go/sdk/buildkite/automatic_retry.go b/sdk/go/sdk/buildkite/automatic_retry.go index 1dee5a5..6382154 100644 --- a/sdk/go/sdk/buildkite/automatic_retry.go +++ b/sdk/go/sdk/buildkite/automatic_retry.go @@ -11,7 +11,6 @@ var AutomaticRetryExitStatusEnumValues = map[string]AutomaticRetryExitStatusEnum "*": "*", } -// The exit status number that will cause this job to retry type AutomaticRetryExitStatusValues interface { AutomaticRetryExitStatusEnum | int | []int } @@ -38,7 +37,6 @@ func (e AutomaticRetryExitStatus) MarshalJSON() ([]byte, error) { type AutomaticRetrySignalReason string -// The exit signal reason, if any, that may be retried var AutomaticRetrySignalReasonValues = map[string]AutomaticRetrySignalReason{ "*": "*", "none": "none", diff --git a/sdk/go/sdk/buildkite/block_step.go b/sdk/go/sdk/buildkite/block_step.go index bcf05a6..9f9dd8f 100644 --- a/sdk/go/sdk/buildkite/block_step.go +++ b/sdk/go/sdk/buildkite/block_step.go @@ -5,7 +5,6 @@ package buildkite type BlockStepBlockedState string -// The state that the build is set to when the build is blocked by this block step var BlockStepBlockedStateValues = map[string]BlockStepBlockedState{ "passed": "passed", "failed": "failed", diff --git a/sdk/go/sdk/buildkite/branches.go b/sdk/go/sdk/buildkite/branches.go index b356bb9..2ee6653 100644 --- a/sdk/go/sdk/buildkite/branches.go +++ b/sdk/go/sdk/buildkite/branches.go @@ -5,7 +5,6 @@ package buildkite import "encoding/json" -// Which branches will include this step in their builds type BranchesValues interface { string | []string } diff --git a/sdk/go/sdk/buildkite/build_notify.go b/sdk/go/sdk/buildkite/build_notify.go index 8545bc2..acca977 100644 --- a/sdk/go/sdk/buildkite/build_notify.go +++ b/sdk/go/sdk/buildkite/build_notify.go @@ -5,7 +5,6 @@ package buildkite import "encoding/json" -// Array of notification options for this step type BuildNotifyItemValues interface { NotifySimple | NotifyEmail | NotifyBasecamp | NotifySlack | NotifyWebhook | NotifyPagerduty | NotifyGithubCommitStatus | NotifyGithubCheck } @@ -23,29 +22,29 @@ type BuildNotifyItem struct { } func (e BuildNotifyItem) MarshalJSON() ([]byte, error) { - if e.NotifySimple != nil { - return json.Marshal(e.NotifySimple) + if e.NotifyBasecamp != nil { + return json.Marshal(e.NotifyBasecamp) } if e.NotifyEmail != nil { return json.Marshal(e.NotifyEmail) } - if e.NotifyBasecamp != nil { - return json.Marshal(e.NotifyBasecamp) - } - if e.NotifySlack != nil { - return json.Marshal(e.NotifySlack) + if e.NotifyGithubCheck != nil { + return json.Marshal(e.NotifyGithubCheck) } - if e.NotifyWebhook != nil { - return json.Marshal(e.NotifyWebhook) + if e.NotifyGithubCommitStatus != nil { + return json.Marshal(e.NotifyGithubCommitStatus) } if e.NotifyPagerduty != nil { return json.Marshal(e.NotifyPagerduty) } - if e.NotifyGithubCommitStatus != nil { - return json.Marshal(e.NotifyGithubCommitStatus) + if e.NotifySimple != nil { + return json.Marshal(e.NotifySimple) } - if e.NotifyGithubCheck != nil { - return json.Marshal(e.NotifyGithubCheck) + if e.NotifySlack != nil { + return json.Marshal(e.NotifySlack) + } + if e.NotifyWebhook != nil { + return json.Marshal(e.NotifyWebhook) } return json.Marshal(nil) } diff --git a/sdk/go/sdk/buildkite/cache.go b/sdk/go/sdk/buildkite/cache.go index d73c547..a700270 100644 --- a/sdk/go/sdk/buildkite/cache.go +++ b/sdk/go/sdk/buildkite/cache.go @@ -10,8 +10,6 @@ type CacheObject struct { Paths []string `json:"paths,omitempty"` Size *string `json:"size,omitempty"` } - -// The paths for the caches to be used in the step type CacheValues interface { string | []string | CacheObject } @@ -24,14 +22,14 @@ type Cache struct { } func (e Cache) MarshalJSON() ([]byte, error) { + if e.Cache != nil { + return json.Marshal(e.Cache) + } if e.String != nil { return json.Marshal(e.String) } if e.StringArray != nil { return json.Marshal(e.StringArray) } - if e.Cache != nil { - return json.Marshal(e.Cache) - } return json.Marshal(nil) } diff --git a/sdk/go/sdk/buildkite/cancel_on_build_failing.go b/sdk/go/sdk/buildkite/cancel_on_build_failing.go index fb39dd9..4ce63a0 100644 --- a/sdk/go/sdk/buildkite/cancel_on_build_failing.go +++ b/sdk/go/sdk/buildkite/cancel_on_build_failing.go @@ -5,7 +5,6 @@ package buildkite import "encoding/json" -// Whether to cancel the job as soon as the build is marked as failing type CancelOnBuildFailingValues interface { bool | string } diff --git a/sdk/go/sdk/buildkite/command_step.go b/sdk/go/sdk/buildkite/command_step.go index d45d8a2..f69bd6e 100644 --- a/sdk/go/sdk/buildkite/command_step.go +++ b/sdk/go/sdk/buildkite/command_step.go @@ -5,7 +5,6 @@ package buildkite import "encoding/json" -// The glob path/s of artifacts to upload once this step has finished running type CommandStepArtifactPathsValues interface { string | []string } @@ -28,7 +27,6 @@ func (e CommandStepArtifactPaths) MarshalJSON() ([]byte, error) { type CommandStepConcurrencyMethod string -// Control command order, allowed values are 'ordered' (default) and 'eager'. If you use this attribute, you must also define concurrency_group and concurrency. var CommandStepConcurrencyMethodValues = map[string]CommandStepConcurrencyMethod{ "ordered": "ordered", "eager": "eager", diff --git a/sdk/go/sdk/buildkite/command_step_automatic_retry.go b/sdk/go/sdk/buildkite/command_step_automatic_retry.go index 8535e62..290b234 100644 --- a/sdk/go/sdk/buildkite/command_step_automatic_retry.go +++ b/sdk/go/sdk/buildkite/command_step_automatic_retry.go @@ -23,7 +23,6 @@ func (e CommandStepAutomaticRetryEnum) MarshalJSON() ([]byte, error) { return json.Marshal(nil) } -// Whether to allow a job to retry automatically. If set to true, the retry conditions are set to the default value. type CommandStepAutomaticRetryValues interface { CommandStepAutomaticRetryEnum | AutomaticRetry | AutomaticRetryList } @@ -36,14 +35,14 @@ type CommandStepAutomaticRetry struct { } func (e CommandStepAutomaticRetry) MarshalJSON() ([]byte, error) { - if e.CommandStepAutomaticRetryEnum != nil { - return json.Marshal(e.CommandStepAutomaticRetryEnum) - } if e.AutomaticRetry != nil { return json.Marshal(e.AutomaticRetry) } if e.AutomaticRetryList != nil { return json.Marshal(e.AutomaticRetryList) } + if e.CommandStepAutomaticRetryEnum != nil { + return json.Marshal(e.CommandStepAutomaticRetryEnum) + } return json.Marshal(nil) } diff --git a/sdk/go/sdk/buildkite/command_step_command.go b/sdk/go/sdk/buildkite/command_step_command.go index 554dd83..a5124ae 100644 --- a/sdk/go/sdk/buildkite/command_step_command.go +++ b/sdk/go/sdk/buildkite/command_step_command.go @@ -5,7 +5,6 @@ package buildkite import "encoding/json" -// The commands to run on the agent type CommandStepCommandValues interface { []string | string } @@ -17,11 +16,11 @@ type CommandStepCommand struct { } func (e CommandStepCommand) MarshalJSON() ([]byte, error) { - if e.StringArray != nil { - return json.Marshal(e.StringArray) - } if e.String != nil { return json.Marshal(e.String) } + if e.StringArray != nil { + return json.Marshal(e.StringArray) + } return json.Marshal(nil) } diff --git a/sdk/go/sdk/buildkite/command_step_manual_retry.go b/sdk/go/sdk/buildkite/command_step_manual_retry.go index 51cac02..715132d 100644 --- a/sdk/go/sdk/buildkite/command_step_manual_retry.go +++ b/sdk/go/sdk/buildkite/command_step_manual_retry.go @@ -23,7 +23,6 @@ func (e CommandStepManualRetryEnum) MarshalJSON() ([]byte, error) { return json.Marshal(nil) } -// Whether to allow a job to be retried manually type CommandStepManualRetryValues interface { CommandStepManualRetryEnum | CommandStepManualRetryObject } diff --git a/sdk/go/sdk/buildkite/command_step_manual_retry_object.go b/sdk/go/sdk/buildkite/command_step_manual_retry_object.go index 751517d..4167cb8 100644 --- a/sdk/go/sdk/buildkite/command_step_manual_retry_object.go +++ b/sdk/go/sdk/buildkite/command_step_manual_retry_object.go @@ -5,7 +5,6 @@ package buildkite import "encoding/json" -// Whether or not this job can be retried manually type CommandStepManualRetryObjectAllowedValues interface { bool | string } @@ -26,7 +25,6 @@ func (e CommandStepManualRetryObjectAllowed) MarshalJSON() ([]byte, error) { return json.Marshal(nil) } -// Whether or not this job can be retried after it has passed type CommandStepManualRetryObjectPermitOnPassedValues interface { bool | string } diff --git a/sdk/go/sdk/buildkite/command_step_notify.go b/sdk/go/sdk/buildkite/command_step_notify.go index 07d05ad..ebd1793 100644 --- a/sdk/go/sdk/buildkite/command_step_notify.go +++ b/sdk/go/sdk/buildkite/command_step_notify.go @@ -5,7 +5,6 @@ package buildkite import "encoding/json" -// Array of notification options for this step type CommandStepNotifyItemValues interface { NotifySimple | NotifyBasecamp | NotifySlack | NotifyGithubCommitStatus | NotifyGithubCheck } @@ -20,20 +19,20 @@ type CommandStepNotifyItem struct { } func (e CommandStepNotifyItem) MarshalJSON() ([]byte, error) { - if e.NotifySimple != nil { - return json.Marshal(e.NotifySimple) - } if e.NotifyBasecamp != nil { return json.Marshal(e.NotifyBasecamp) } - if e.NotifySlack != nil { - return json.Marshal(e.NotifySlack) + if e.NotifyGithubCheck != nil { + return json.Marshal(e.NotifyGithubCheck) } if e.NotifyGithubCommitStatus != nil { return json.Marshal(e.NotifyGithubCommitStatus) } - if e.NotifyGithubCheck != nil { - return json.Marshal(e.NotifyGithubCheck) + if e.NotifySimple != nil { + return json.Marshal(e.NotifySimple) + } + if e.NotifySlack != nil { + return json.Marshal(e.NotifySlack) } return json.Marshal(nil) } diff --git a/sdk/go/sdk/buildkite/depends_on.go b/sdk/go/sdk/buildkite/depends_on.go index 04f0c00..4b6f8ac 100644 --- a/sdk/go/sdk/buildkite/depends_on.go +++ b/sdk/go/sdk/buildkite/depends_on.go @@ -5,7 +5,6 @@ package buildkite import "encoding/json" -// The step keys for a step to depend on type DependsOnValues interface { string | DependsOnList } @@ -17,11 +16,11 @@ type DependsOn struct { } func (e DependsOn) MarshalJSON() ([]byte, error) { - if e.String != nil { - return json.Marshal(e.String) - } if e.DependsOnList != nil { return json.Marshal(e.DependsOnList) } + if e.String != nil { + return json.Marshal(e.String) + } return json.Marshal(nil) } diff --git a/sdk/go/sdk/buildkite/depends_on_list.go b/sdk/go/sdk/buildkite/depends_on_list.go index d9e2676..f6aca2e 100644 --- a/sdk/go/sdk/buildkite/depends_on_list.go +++ b/sdk/go/sdk/buildkite/depends_on_list.go @@ -36,12 +36,12 @@ type DependsOnListItem struct { } func (e DependsOnListItem) MarshalJSON() ([]byte, error) { - if e.String != nil { - return json.Marshal(e.String) - } if e.DependsOnList != nil { return json.Marshal(e.DependsOnList) } + if e.String != nil { + return json.Marshal(e.String) + } return json.Marshal(nil) } diff --git a/sdk/go/sdk/buildkite/fields.go b/sdk/go/sdk/buildkite/fields.go index 5d42eb3..8166ddc 100644 --- a/sdk/go/sdk/buildkite/fields.go +++ b/sdk/go/sdk/buildkite/fields.go @@ -5,7 +5,6 @@ package buildkite import "encoding/json" -// A list of input fields required to be filled out before unblocking the step type FieldsItemValues interface { TextField | SelectField } @@ -17,12 +16,12 @@ type FieldsItem struct { } func (e FieldsItem) MarshalJSON() ([]byte, error) { - if e.TextField != nil { - return json.Marshal(e.TextField) - } if e.SelectField != nil { return json.Marshal(e.SelectField) } + if e.TextField != nil { + return json.Marshal(e.TextField) + } return json.Marshal(nil) } diff --git a/sdk/go/sdk/buildkite/group_steps.go b/sdk/go/sdk/buildkite/group_steps.go index 1cc4532..55b4eef 100644 --- a/sdk/go/sdk/buildkite/group_steps.go +++ b/sdk/go/sdk/buildkite/group_steps.go @@ -5,7 +5,6 @@ package buildkite import "encoding/json" -// A list of steps type GroupStepsItemValues interface { BlockStep | NestedBlockStep | StringBlockStep | InputStep | NestedInputStep | StringInputStep | CommandStep | NestedCommandStep | WaitStep | NestedWaitStep | StringWaitStep | TriggerStep | NestedTriggerStep } @@ -34,41 +33,41 @@ func (e GroupStepsItem) MarshalJSON() ([]byte, error) { if e.BlockStep != nil { return json.Marshal(e.BlockStep) } - if e.NestedBlockStep != nil { - return json.Marshal(e.NestedBlockStep) - } - if e.StringBlockStep != nil { - return json.Marshal(e.StringBlockStep) + if e.CommandStep != nil { + return json.Marshal(e.CommandStep) } if e.InputStep != nil { return json.Marshal(e.InputStep) } - if e.NestedInputStep != nil { - return json.Marshal(e.NestedInputStep) - } - if e.StringInputStep != nil { - return json.Marshal(e.StringInputStep) - } - if e.CommandStep != nil { - return json.Marshal(e.CommandStep) + if e.NestedBlockStep != nil { + return json.Marshal(e.NestedBlockStep) } if e.NestedCommandStep != nil { return json.Marshal(e.NestedCommandStep) } - if e.WaitStep != nil { - return json.Marshal(e.WaitStep) + if e.NestedInputStep != nil { + return json.Marshal(e.NestedInputStep) + } + if e.NestedTriggerStep != nil { + return json.Marshal(e.NestedTriggerStep) } if e.NestedWaitStep != nil { return json.Marshal(e.NestedWaitStep) } + if e.StringBlockStep != nil { + return json.Marshal(e.StringBlockStep) + } + if e.StringInputStep != nil { + return json.Marshal(e.StringInputStep) + } if e.StringWaitStep != nil { return json.Marshal(e.StringWaitStep) } if e.TriggerStep != nil { return json.Marshal(e.TriggerStep) } - if e.NestedTriggerStep != nil { - return json.Marshal(e.NestedTriggerStep) + if e.WaitStep != nil { + return json.Marshal(e.WaitStep) } return json.Marshal(nil) } diff --git a/sdk/go/sdk/buildkite/input_step.go b/sdk/go/sdk/buildkite/input_step.go index 446fba3..b3307c9 100644 --- a/sdk/go/sdk/buildkite/input_step.go +++ b/sdk/go/sdk/buildkite/input_step.go @@ -5,7 +5,6 @@ package buildkite type InputStepBlockedState string -// The state that the build is set to when the build is blocked by this input step var InputStepBlockedStateValues = map[string]InputStepBlockedState{ "passed": "passed", "failed": "failed", diff --git a/sdk/go/sdk/buildkite/matrix_adjustments.go b/sdk/go/sdk/buildkite/matrix_adjustments.go index 2087d51..fa25b9f 100644 --- a/sdk/go/sdk/buildkite/matrix_adjustments.go +++ b/sdk/go/sdk/buildkite/matrix_adjustments.go @@ -15,12 +15,12 @@ type MatrixAdjustmentsWith struct { } func (e MatrixAdjustmentsWith) MarshalJSON() ([]byte, error) { - if e.MatrixElementList != nil { - return json.Marshal(e.MatrixElementList) - } if e.MatrixAdjustmentsWithObject != nil { return json.Marshal(e.MatrixAdjustmentsWithObject) } + if e.MatrixElementList != nil { + return json.Marshal(e.MatrixElementList) + } return json.Marshal(nil) } diff --git a/sdk/go/sdk/buildkite/matrix_element.go b/sdk/go/sdk/buildkite/matrix_element.go index da9cdf3..e583902 100644 --- a/sdk/go/sdk/buildkite/matrix_element.go +++ b/sdk/go/sdk/buildkite/matrix_element.go @@ -15,14 +15,14 @@ type MatrixElement struct { } func (e MatrixElement) MarshalJSON() ([]byte, error) { - if e.String != nil { - return json.Marshal(e.String) + if e.Bool != nil { + return json.Marshal(e.Bool) } if e.Int != nil { return json.Marshal(e.Int) } - if e.Bool != nil { - return json.Marshal(e.Bool) + if e.String != nil { + return json.Marshal(e.String) } return json.Marshal(nil) } diff --git a/sdk/go/sdk/buildkite/notify_slack.go b/sdk/go/sdk/buildkite/notify_slack.go index e4df4ec..bf873bb 100644 --- a/sdk/go/sdk/buildkite/notify_slack.go +++ b/sdk/go/sdk/buildkite/notify_slack.go @@ -14,12 +14,12 @@ type NotifySlackSlack struct { } func (e NotifySlackSlack) MarshalJSON() ([]byte, error) { - if e.String != nil { - return json.Marshal(e.String) - } if e.NotifySlackObject != nil { return json.Marshal(e.NotifySlackObject) } + if e.String != nil { + return json.Marshal(e.String) + } return json.Marshal(nil) } diff --git a/sdk/go/sdk/buildkite/pipeline_steps.go b/sdk/go/sdk/buildkite/pipeline_steps.go index 68590ce..d29a274 100644 --- a/sdk/go/sdk/buildkite/pipeline_steps.go +++ b/sdk/go/sdk/buildkite/pipeline_steps.go @@ -5,7 +5,6 @@ package buildkite import "encoding/json" -// A list of steps type PipelineStepsItemValues interface { BlockStep | NestedBlockStep | StringBlockStep | InputStep | NestedInputStep | StringInputStep | CommandStep | NestedCommandStep | WaitStep | NestedWaitStep | StringWaitStep | TriggerStep | NestedTriggerStep | GroupStep } @@ -35,44 +34,44 @@ func (e PipelineStepsItem) MarshalJSON() ([]byte, error) { if e.BlockStep != nil { return json.Marshal(e.BlockStep) } - if e.NestedBlockStep != nil { - return json.Marshal(e.NestedBlockStep) + if e.CommandStep != nil { + return json.Marshal(e.CommandStep) } - if e.StringBlockStep != nil { - return json.Marshal(e.StringBlockStep) + if e.GroupStep != nil { + return json.Marshal(e.GroupStep) } if e.InputStep != nil { return json.Marshal(e.InputStep) } - if e.NestedInputStep != nil { - return json.Marshal(e.NestedInputStep) - } - if e.StringInputStep != nil { - return json.Marshal(e.StringInputStep) - } - if e.CommandStep != nil { - return json.Marshal(e.CommandStep) + if e.NestedBlockStep != nil { + return json.Marshal(e.NestedBlockStep) } if e.NestedCommandStep != nil { return json.Marshal(e.NestedCommandStep) } - if e.WaitStep != nil { - return json.Marshal(e.WaitStep) + if e.NestedInputStep != nil { + return json.Marshal(e.NestedInputStep) + } + if e.NestedTriggerStep != nil { + return json.Marshal(e.NestedTriggerStep) } if e.NestedWaitStep != nil { return json.Marshal(e.NestedWaitStep) } + if e.StringBlockStep != nil { + return json.Marshal(e.StringBlockStep) + } + if e.StringInputStep != nil { + return json.Marshal(e.StringInputStep) + } if e.StringWaitStep != nil { return json.Marshal(e.StringWaitStep) } if e.TriggerStep != nil { return json.Marshal(e.TriggerStep) } - if e.NestedTriggerStep != nil { - return json.Marshal(e.NestedTriggerStep) - } - if e.GroupStep != nil { - return json.Marshal(e.GroupStep) + if e.WaitStep != nil { + return json.Marshal(e.WaitStep) } return json.Marshal(nil) } diff --git a/sdk/go/sdk/buildkite/plugins_list.go b/sdk/go/sdk/buildkite/plugins_list.go index 463dcf2..7cff1fb 100644 --- a/sdk/go/sdk/buildkite/plugins_list.go +++ b/sdk/go/sdk/buildkite/plugins_list.go @@ -6,8 +6,6 @@ package buildkite import "encoding/json" type PluginsListObject = map[string]interface{} - -// Array of plugins for this step type PluginsListItemValues interface { string | PluginsListObject } @@ -19,12 +17,12 @@ type PluginsListItem struct { } func (e PluginsListItem) MarshalJSON() ([]byte, error) { - if e.String != nil { - return json.Marshal(e.String) - } if e.PluginsList != nil { return json.Marshal(e.PluginsList) } + if e.String != nil { + return json.Marshal(e.String) + } return json.Marshal(nil) } diff --git a/sdk/go/sdk/buildkite/secrets.go b/sdk/go/sdk/buildkite/secrets.go index 8945578..c2078a8 100644 --- a/sdk/go/sdk/buildkite/secrets.go +++ b/sdk/go/sdk/buildkite/secrets.go @@ -6,8 +6,6 @@ package buildkite import "encoding/json" type SecretsObject = map[string]string - -// A list of secret names or a mapping of environment variable names to secret names to be made available to the build or step type SecretsValues interface { []string | SecretsObject } @@ -19,11 +17,11 @@ type Secrets struct { } func (e Secrets) MarshalJSON() ([]byte, error) { - if e.StringArray != nil { - return json.Marshal(e.StringArray) - } if e.Secrets != nil { return json.Marshal(e.Secrets) } + if e.StringArray != nil { + return json.Marshal(e.StringArray) + } return json.Marshal(nil) } diff --git a/sdk/go/sdk/buildkite/select_field.go b/sdk/go/sdk/buildkite/select_field.go index e630d90..5d5b5aa 100644 --- a/sdk/go/sdk/buildkite/select_field.go +++ b/sdk/go/sdk/buildkite/select_field.go @@ -5,7 +5,6 @@ package buildkite import "encoding/json" -// The value of the option(s) that will be pre-selected in the dropdown type SelectFieldDefaultValues interface { string | []string } @@ -26,7 +25,6 @@ func (e SelectFieldDefault) MarshalJSON() ([]byte, error) { return json.Marshal(nil) } -// Whether more than one option may be selected type SelectFieldMultipleValues interface { bool | string } @@ -47,7 +45,6 @@ func (e SelectFieldMultiple) MarshalJSON() ([]byte, error) { return json.Marshal(nil) } -// Whether the field is required for form submission type SelectFieldRequiredValues interface { bool | string } diff --git a/sdk/go/sdk/buildkite/select_field_option.go b/sdk/go/sdk/buildkite/select_field_option.go index f615e99..830713f 100644 --- a/sdk/go/sdk/buildkite/select_field_option.go +++ b/sdk/go/sdk/buildkite/select_field_option.go @@ -5,7 +5,6 @@ package buildkite import "encoding/json" -// Whether the field is required for form submission type SelectFieldOptionRequiredValues interface { bool | string } diff --git a/sdk/go/sdk/buildkite/skip.go b/sdk/go/sdk/buildkite/skip.go index a448df9..ae45db3 100644 --- a/sdk/go/sdk/buildkite/skip.go +++ b/sdk/go/sdk/buildkite/skip.go @@ -5,7 +5,6 @@ package buildkite import "encoding/json" -// Whether this step should be skipped. Passing a string provides a reason for skipping this command type SkipValues interface { bool | string } diff --git a/sdk/go/sdk/buildkite/soft_fail.go b/sdk/go/sdk/buildkite/soft_fail.go index 18b3762..e06c04a 100644 --- a/sdk/go/sdk/buildkite/soft_fail.go +++ b/sdk/go/sdk/buildkite/soft_fail.go @@ -23,7 +23,6 @@ func (e SoftFailEnum) MarshalJSON() ([]byte, error) { return json.Marshal(nil) } -// The conditions for marking the step as a soft-fail. type SoftFailValues interface { SoftFailEnum | SoftFailList } diff --git a/sdk/go/sdk/buildkite/soft_fail_object.go b/sdk/go/sdk/buildkite/soft_fail_object.go index c9b02c3..5d00b6e 100644 --- a/sdk/go/sdk/buildkite/soft_fail_object.go +++ b/sdk/go/sdk/buildkite/soft_fail_object.go @@ -11,7 +11,6 @@ var SoftFailObjectExitStatusEnumValues = map[string]SoftFailObjectExitStatusEnum "*": "*", } -// The exit status number that will cause this job to soft-fail type SoftFailObjectExitStatusValues interface { SoftFailObjectExitStatusEnum | int } @@ -23,12 +22,12 @@ type SoftFailObjectExitStatus struct { } func (e SoftFailObjectExitStatus) MarshalJSON() ([]byte, error) { - if e.SoftFailObjectExitStatusEnum != nil { - return json.Marshal(e.SoftFailObjectExitStatusEnum) - } if e.Int != nil { return json.Marshal(e.Int) } + if e.SoftFailObjectExitStatusEnum != nil { + return json.Marshal(e.SoftFailObjectExitStatusEnum) + } return json.Marshal(nil) } diff --git a/sdk/go/sdk/buildkite/string_block_step.go b/sdk/go/sdk/buildkite/string_block_step.go index 1b69f84..e2c0296 100644 --- a/sdk/go/sdk/buildkite/string_block_step.go +++ b/sdk/go/sdk/buildkite/string_block_step.go @@ -5,7 +5,6 @@ package buildkite type StringBlockStep string -// Pauses the execution of a build and waits on a user to unblock it var StringBlockStepValues = map[string]StringBlockStep{ "block": "block", } diff --git a/sdk/go/sdk/buildkite/string_input_step.go b/sdk/go/sdk/buildkite/string_input_step.go index a61cba1..7f79c6c 100644 --- a/sdk/go/sdk/buildkite/string_input_step.go +++ b/sdk/go/sdk/buildkite/string_input_step.go @@ -5,7 +5,6 @@ package buildkite type StringInputStep string -// Pauses the execution of a build and waits on a user to unblock it var StringInputStepValues = map[string]StringInputStep{ "input": "input", } diff --git a/sdk/go/sdk/buildkite/string_wait_step.go b/sdk/go/sdk/buildkite/string_wait_step.go index 7cdbc97..c498424 100644 --- a/sdk/go/sdk/buildkite/string_wait_step.go +++ b/sdk/go/sdk/buildkite/string_wait_step.go @@ -5,7 +5,6 @@ package buildkite type StringWaitStep string -// Waits for previous steps to pass before continuing var StringWaitStepValues = map[string]StringWaitStep{ "wait": "wait", "waiter": "waiter", diff --git a/sdk/go/sdk/buildkite/text_field.go b/sdk/go/sdk/buildkite/text_field.go index 1f8ba8a..e750d9b 100644 --- a/sdk/go/sdk/buildkite/text_field.go +++ b/sdk/go/sdk/buildkite/text_field.go @@ -5,7 +5,6 @@ package buildkite import "encoding/json" -// Whether the field is required for form submission type TextFieldRequiredValues interface { bool | string } diff --git a/sdk/go/sdk/buildkite/trigger_step.go b/sdk/go/sdk/buildkite/trigger_step.go index 6fa8809..b410cd0 100644 --- a/sdk/go/sdk/buildkite/trigger_step.go +++ b/sdk/go/sdk/buildkite/trigger_step.go @@ -5,7 +5,6 @@ package buildkite import "encoding/json" -// Whether to continue the build without waiting for the triggered step to complete type TriggerStepAsyncValues interface { bool | string } diff --git a/sdk/go/sdk/buildkite/wait_step.go b/sdk/go/sdk/buildkite/wait_step.go index c774114..bc59ffc 100644 --- a/sdk/go/sdk/buildkite/wait_step.go +++ b/sdk/go/sdk/buildkite/wait_step.go @@ -5,7 +5,6 @@ package buildkite import "encoding/json" -// Continue to the next steps, even if the previous group of steps fail type WaitStepContinueOnFailureValues interface { bool | string }