Skip to content
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: 3 additions & 1 deletion codegen/generator/functions.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package generator

import "github.com/dagger/dagger/codegen/introspection"
import (
"github.com/dagger/dagger/codegen/introspection"
)

const (
QueryStructName = "Query"
Expand Down
2 changes: 2 additions & 0 deletions codegen/generator/go/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ type GoGenerator struct {
}

func (g *GoGenerator) Generate(_ context.Context, schema *introspection.Schema) ([]byte, error) {
generator.SetSchema(schema)

headerData := struct {
Package string
Schema *introspection.Schema
Expand Down
75 changes: 65 additions & 10 deletions codegen/generator/go/templates/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"sort"
"strings"
"text/template"
"unicode"

"github.com/dagger/dagger/codegen/generator"
"github.com/dagger/dagger/codegen/introspection"
Expand All @@ -15,16 +16,22 @@ import (
var (
commonFunc = generator.NewCommonFunctions(&FormatTypeFunc{})
funcMap = template.FuncMap{
"Comment": comment,
"FormatDeprecation": formatDeprecation,
"FormatInputType": commonFunc.FormatInputType,
"FormatOutputType": commonFunc.FormatOutputType,
"FormatName": formatName,
"FormatEnum": formatEnum,
"SortEnumFields": sortEnumFields,
"FieldOptionsStructName": fieldOptionsStructName,
"FieldFunction": fieldFunction,
"IsEnum": isEnum,
"Comment": comment,
"FormatDeprecation": formatDeprecation,
"FormatInputType": commonFunc.FormatInputType,
"FormatOutputType": commonFunc.FormatOutputType,
"FormatName": formatName,
"FormatEnum": formatEnum,
"SortEnumFields": sortEnumFields,
"FieldOptionsStructName": fieldOptionsStructName,
"FieldFunction": fieldFunction,
"IsEnum": isEnum,
"GetArrayField": getArrayField,
"IsListOfObject": isListOfObject,
"ToLowerCase": toLowerCase,
"ToUpperCase": toUpperCase,
"FormatArrayField": formatArrayField,
"FormatArrayToSingleType": formatArrayToSingleType,
}
)

Expand Down Expand Up @@ -86,6 +93,54 @@ func sortEnumFields(s []introspection.EnumValue) []introspection.EnumValue {
return s
}

func isListOfObject(t *introspection.TypeRef) bool {
return t.OfType.OfType.IsObject()
}

func getArrayField(f *introspection.Field) []*introspection.Field {
schema := generator.GetSchema()
var fieldType string

if f.TypeRef.OfType.OfType.OfType != nil {
fieldType = f.TypeRef.OfType.OfType.OfType.Name
} else {
fieldType = f.ParentObject.Name
}

var fields []*introspection.Field
// Only include scalar fields for now
// TODO: include subtype too
for _, typeField := range schema.Types.Get(fieldType).Fields {
if typeField.TypeRef.IsScalar() {
fields = append(fields, typeField)
}
}

return fields
}

func formatArrayField(fields []*introspection.Field) string {
result := []string{}

for _, f := range fields {
result = append(result, fmt.Sprintf("%s: &field.%s", f.Name, toUpperCase(f.Name)))
}

return strings.Join(result, ", ")
}

func formatArrayToSingleType(arrType string) string {
return arrType[2:]
}

func toLowerCase(s string) string {
return fmt.Sprintf("%c%s", unicode.ToLower(rune(s[0])), s[1:])
}

func toUpperCase(s string) string {
return fmt.Sprintf("%c%s", unicode.ToUpper(rune(s[0])), s[1:])
}

// fieldOptionsStructName returns the options struct name for a given field
func fieldOptionsStructName(f introspection.Field) string {
// Exception: `Query` option structs are not prefixed by `Query`.
Expand Down
50 changes: 50 additions & 0 deletions codegen/generator/go/templates/src/object.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
type {{ .Name | FormatName }} struct {
q *querybuilder.Selection
c graphql.Client

{{ range $field := .Fields }}
{{- if $field.TypeRef.IsScalar }}
{{ $field.Name }} *{{ $field.TypeRef | FormatOutputType }}
{{- end }}
{{- end }}
}
{{- end }}
{{ range $field := .Fields }}
Expand All @@ -29,7 +35,13 @@ type {{ $field | FieldOptionsStructName }} struct {
{{ $field.DeprecationReason | FormatDeprecation }}
{{- end }}
{{ $field | FieldFunction }} {
{{- if and ($field.TypeRef.IsScalar) (ne $field.ParentObject.Name "Query") }}
if r.{{ $field.Name }} != nil {
return *r.{{ $field.Name }}, nil
}
{{- end }}
q := r.q.Select("{{ $field.Name }}")

{{- range $arg := $field.Args }}
{{- if $arg.TypeRef.IsOptional }}
// `{{ $arg.Name }}` optional argument
Expand All @@ -49,12 +61,50 @@ type {{ $field | FieldOptionsStructName }} struct {
q: q,
c: r.c,
}

{{- else if or $field.TypeRef.IsScalar $field.TypeRef.IsList }}
{{- if and $field.TypeRef.IsList (IsListOfObject $field.TypeRef) }}
q = q.Select("{{ range $i, $v := $field | GetArrayField }}{{ if $i }} {{ end }}{{ $v.Name }}{{ end }}")

type {{ $field.Name | ToLowerCase }} struct {
{{ range $v := $field | GetArrayField }}
{{ $v.Name | ToUpperCase }} {{ $v.TypeRef | FormatOutputType }}
{{- end }}
}

convert := func(fields []{{ $field.Name | ToLowerCase }}) {{ $field.TypeRef | FormatOutputType }} {
out := {{ $field.TypeRef | FormatOutputType }}{}

for _, field := range fields {
out = append(out, {{ $field.TypeRef | FormatOutputType | FormatArrayToSingleType }}{{"{"}}{{ $field | GetArrayField | FormatArrayField }}{{"}"}})
}

return out
}

{{- end }}


{{- if and $field.TypeRef.IsList (IsListOfObject $field.TypeRef) }}
var response []{{ $field.Name | ToLowerCase }}
{{- else }}
var response {{ $field.TypeRef | FormatOutputType }}
{{- end }}

q = q.Bind(&response)
{{- $typeName := $field.TypeRef | FormatOutputType }}
{{- if ne $typeName "Client" }}
{{- if and $field.TypeRef.IsList (IsListOfObject $field.TypeRef) }}

err := q.Execute(ctx, r.c)
if err != nil {
return nil, err
}

return convert(response), nil
{{- else }}
return response, q.Execute(ctx, r.c)
{{- end }}
{{- else }}
return response, q.Execute(ctx, r.gql)
{{- end }}
Expand Down
13 changes: 13 additions & 0 deletions codegen/generator/schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package generator

import "github.com/dagger/dagger/codegen/introspection"

var _schema *introspection.Schema

func SetSchema(schema *introspection.Schema) {
_schema = schema
}

func GetSchema() *introspection.Schema {
return _schema
}
Loading