Skip to content
Draft
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
21 changes: 3 additions & 18 deletions internal/gen/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions internal/gen/go/comment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package gogen

import "fmt"

func NewGoComment(comment string) string {
return fmt.Sprintf("// %s", comment)
}
41 changes: 41 additions & 0 deletions internal/gen/go/constraint_interface.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
44 changes: 44 additions & 0 deletions internal/gen/go/enum_values.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
39 changes: 39 additions & 0 deletions internal/gen/go/file.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
111 changes: 111 additions & 0 deletions internal/gen/go/struct.go
Original file line number Diff line number Diff line change
@@ -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),
}
}
35 changes: 35 additions & 0 deletions internal/gen/go/type.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
Loading