Skip to content

Commit

Permalink
generate unions (#505)
Browse files Browse the repository at this point in the history
* generate unions

* drop unused TagOwner enums

* union tpl clean up
  • Loading branch information
davidbloss authored Jan 7, 2025
1 parent 58c303b commit 94e2dbd
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 40 deletions.
5 changes: 0 additions & 5 deletions check.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ import (
"github.com/relvacode/iso8601"
)

type CheckOwner struct {
Team TeamId `graphql:"... on Team"`
// User User `graphql:"... on User"` // TODO: will this be public?
}

type CheckInputConstructor func() any

var CheckCreateConstructors = map[CheckType]CheckInputConstructor{
Expand Down
5 changes: 0 additions & 5 deletions document.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package opslevel

type ServiceDocumentSource struct {
IntegrationId `graphql:"... on ApiDocIntegration"`
ServiceRepository `graphql:"... on ServiceRepository"`
}

type ServiceDocument struct {
Id ID `graphql:"id" json:"id"`
HtmlURL string `graphql:"htmlUrl" json:"htmUrl,omitempty"`
Expand Down
42 changes: 23 additions & 19 deletions gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ const (
mutationFile string = "mutation.go"
payloadFile string = "payload.go"
// scalarFile string = "scalar.go" // NOTE: probably not useful
// unionFile string = "union.go" // NOTE: probably not useful
)

var knownTypeIsName = []string{
Expand Down Expand Up @@ -285,6 +284,7 @@ func main() {
panic(fmt.Errorf("Unknown GraphQL type: %v", v))
}
}
genUnions(unions)
genObjects(objects)
genEnums(schemaAst.Enums)
genInputObjects(inputObjects)
Expand All @@ -310,6 +310,28 @@ func sortedMapKeys[T any](schemaMap map[string]T) []string {
return sortedNames
}

func genUnions(unions map[string]*types.Union) {
var buf bytes.Buffer

buf.WriteString(header + "\n\n")

tmpl := template.New("unions")
tmpl.Funcs(sprig.TxtFuncMap())
tmpl.Funcs(templFuncMap)
template.Must(tmpl.ParseFiles("./templates/unions.tpl"))
for _, union := range sortedMapKeys(unions) {
if err := tmpl.ExecuteTemplate(&buf, "union", unions[union]); err != nil {
panic(err)
}
}

fmt.Println("writing union.go")
err := os.WriteFile("union.go", buf.Bytes(), 0o644)
if err != nil {
panic(err)
}
}

func genEnums(schemaEnums []*types.EnumTypeDefinition) {
var buf bytes.Buffer

Expand Down Expand Up @@ -472,8 +494,6 @@ func run() error {
subSchema = objectSchema
// case scalarFile:
// subSchema = scalarSchema
// case unionFile:
// subSchema = unionSchema
default:
panic("Unknown file: " + filename)
}
Expand Down Expand Up @@ -867,21 +887,6 @@ var templates = map[string]*template.Template{
// func NewString(value string) *string {
// return &value
// }`),
// unionFile: t(header + `
// {{range .Types | sortByName}}{{if and (eq .Kind "UNION") (not (internal .Name))}}
// {{template "union_object" .}}
// {{end}}{{end}}

// {{- define "union_object" -}}
// // Union{{.Name}} {{ template "description" . }}
// type Union{{.Name}} interface { {{range .PossibleTypes }}
//
// {{.Name}}Fragment() {{.Name}}Fragment{{end}}
// }
//
// {{- end -}}
//
// `),
}

func t(text string) *template.Template {
Expand Down Expand Up @@ -1252,7 +1257,6 @@ func firstCharLowered(s string) string {
var templFuncMap = template.FuncMap{
"internal": func(s string) bool { return strings.HasPrefix(s, "__") },
"quote": strconv.Quote,
"join": strings.Join,
"check_fragments": fragmentsForCheck,
"custom_actions_ext_action_fragments": fragmentsForCustomActionsExtAction,
"integration_fragments": fragmentsForIntegration,
Expand Down
4 changes: 0 additions & 4 deletions owner.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ type EntityOwnerTeam struct {
Id ID `json:"id"`
}

type EntityOwner struct {
OnTeam EntityOwnerTeam `graphql:"... on Team"`
}

func (entityOwner *EntityOwner) Alias() string {
return entityOwner.OnTeam.Alias
}
Expand Down
7 changes: 0 additions & 7 deletions tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ import (
"slices"
)

type TagOwner string

const (
TagOwnerService TagOwner = "Service"
TagOwnerRepository TagOwner = "Repository"
)

type TaggableResourceInterface interface {
GetTags(*Client, *PayloadVariables) (*TagConnection, error)
ResourceId() ID
Expand Down
40 changes: 40 additions & 0 deletions templates/unions.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{{- define "union" }}
// {{ title .Name }} {{.Desc | clean | endSentence}}
{{- if eq "CheckOwner" .Name }}
{{ template "check_owner" }}
{{- else if eq "EntityOwner" .Name }}
{{ template "entity_owner" }}
{{- else if eq "ServiceDocumentSource" .Name }}
{{ template "service_document_source" }}
{{- else }}
type {{.Name}} struct {
{{ range .TypeNames }}
{{- if not (contains "Group" . ) }}
{{.}} {{. -}}
{{- if not (contains . (list "ApiDocIntegration" "InfrastructureResource" "ServiceRepository" | join " " )) -}}Id
{{- end }} `graphql:"... on {{.}}"`
{{- end }}
{{- end }}
}
{{- end }}
{{- end -}}

{{- define "check_owner" -}}
type CheckOwner struct {
Team TeamId `graphql:"... on Team"`
// User UserId `graphql:"... on User"` // TODO: will this be public?
}
{{ end }}

{{- define "entity_owner" -}}
type EntityOwner struct {
OnTeam EntityOwnerTeam `graphql:"... on Team"`
}
{{ end }}

{{- define "service_document_source" -}}
type ServiceDocumentSource struct {
IntegrationId `graphql:"... on ApiDocIntegration"`
ServiceRepository `graphql:"... on ServiceRepository"`
}
{{ end }}
56 changes: 56 additions & 0 deletions union.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 94e2dbd

Please sign in to comment.