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
90 changes: 90 additions & 0 deletions codegen/generator/functions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package generator

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

const (
QueryStructName = "Query"
QueryStructClientName = "Client"
)

// CustomScalar registers custom Dagger type.
// TODO: This may done it dynamically later instead of a static
// map.
var CustomScalar = map[string]string{
"ContainerID": "Container",
"FileID": "File",
"DirectoryID": "Directory",
"SecretID": "Secret",
"SocketID": "Socket",
"CacheID": "CacheVolume",
}

// FormatTypeFuncs is an interface to format any GraphQL type.
// Each generator has to implement this interface.
type FormatTypeFuncs interface {
FormatKindList(representation string) string
FormatKindScalarString(representation string) string
FormatKindScalarInt(representation string) string
FormatKindScalarFloat(representation string) string
FormatKindScalarBoolean(representation string) string
FormatKindScalarDefault(representation string, refName string, input bool) string
FormatKindObject(representation string, refName string) string
FormatKindInputObject(representation string, refName string) string
}

// CommonFunctions formatting function with global shared template functions.
type CommonFunctions struct {
formatTypeFuncs FormatTypeFuncs
}

func NewCommonFunctions(formatTypeFuncs FormatTypeFuncs) *CommonFunctions {
return &CommonFunctions{formatTypeFuncs: formatTypeFuncs}
}

// FormatInputType formats a GraphQL type into the SDK language input
//
// Example: `String` -> `string`
func (c *CommonFunctions) FormatInputType(r *introspection.TypeRef) string {
return c.formatType(r, true)
}

// FormatOutputType formats a GraphQL type into the SDK language output
//
// Example: `String` -> `string`
func (c *CommonFunctions) FormatOutputType(r *introspection.TypeRef) string {
return c.formatType(r, false)
}

// formatType loops through the type reference to transform it into its SDK language.
func (c *CommonFunctions) formatType(r *introspection.TypeRef, input bool) (representation string) {
for ref := r; ref != nil; ref = ref.OfType {
switch ref.Kind {
case introspection.TypeKindList:
// Handle this special case with defer to format array at the end of
// the loop.
// Since an SDK needs to insert it at the end, other at the beginning.
defer func() {
representation = c.formatTypeFuncs.FormatKindList(representation)
}()
case introspection.TypeKindScalar:
switch introspection.Scalar(ref.Name) {
case introspection.ScalarString:
return c.formatTypeFuncs.FormatKindScalarString(representation)
case introspection.ScalarInt:
return c.formatTypeFuncs.FormatKindScalarInt(representation)
case introspection.ScalarFloat:
return c.formatTypeFuncs.FormatKindScalarFloat(representation)
case introspection.ScalarBoolean:
return c.formatTypeFuncs.FormatKindScalarBoolean(representation)
default:
return c.formatTypeFuncs.FormatKindScalarDefault(representation, ref.Name, input)
}
case introspection.TypeKindObject:
return c.formatTypeFuncs.FormatKindObject(representation, ref.Name)
case introspection.TypeKindInputObject:
return c.formatTypeFuncs.FormatKindInputObject(representation, ref.Name)
}
}

panic(r)
}
57 changes: 57 additions & 0 deletions codegen/generator/go/templates/format.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package templates

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

// FormatTypeFunc is an implementation of generator.FormatTypeFuncs interface
// to format GraphQL type into Golang.
type FormatTypeFunc struct{}

func (f *FormatTypeFunc) FormatKindList(representation string) string {
representation = "[]" + representation
return representation
}

func (f *FormatTypeFunc) FormatKindScalarString(representation string) string {
representation += "string"
return representation
}

func (f *FormatTypeFunc) FormatKindScalarInt(representation string) string {
representation += "int"
return representation
}

func (f *FormatTypeFunc) FormatKindScalarFloat(representation string) string {
representation += "float"
return representation
}

func (f *FormatTypeFunc) FormatKindScalarBoolean(representation string) string {
representation += "bool"
return representation
}

func (f *FormatTypeFunc) FormatKindScalarDefault(representation string, refName string, input bool) string {
if alias, ok := generator.CustomScalar[refName]; ok && input {
representation += "*" + alias
} else {
representation += refName
}

return representation
}

func (f *FormatTypeFunc) FormatKindObject(representation string, refName string) string {
name := refName
if name == generator.QueryStructName {
name = generator.QueryStructClientName
}

representation += formatName(name)
return representation
}

func (f *FormatTypeFunc) FormatKindInputObject(representation string, refName string) string {
representation += formatName(refName)
return representation
}
92 changes: 12 additions & 80 deletions codegen/generator/go/templates/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ import (
"strings"
"text/template"

"github.com/dagger/dagger/codegen/generator"
"github.com/dagger/dagger/codegen/introspection"
)

var (
funcMap = template.FuncMap{
commonFunc = generator.NewCommonFunctions(&FormatTypeFunc{})
funcMap = template.FuncMap{
"Comment": comment,
"FormatDeprecation": formatDeprecation,
"FormatInputType": formatInputType,
"FormatOutputType": formatOutputType,
"FormatInputType": commonFunc.FormatInputType,
"FormatOutputType": commonFunc.FormatOutputType,
"FormatName": formatName,
"FieldOptionsStructName": fieldOptionsStructName,
"FieldFunction": fieldFunction,
}
)

const queryStructName = "Query"

// comments out a string
// Example: `hello\nworld` -> `// hello\n// world\n`
func comment(s string) string {
Expand Down Expand Up @@ -50,74 +50,6 @@ func formatDeprecation(s string) string {
return comment("Deprecated: " + s)
}

// formatType formats a GraphQL type into Go
// Example: `String` -> `string`
func formatInputType(r *introspection.TypeRef) string {
return formatType(r, true)
}

func formatOutputType(r *introspection.TypeRef) string {
return formatType(r, false)
}

// formatType formats a GraphQL type into Go
// Example: `String` -> `string`
func formatType(r *introspection.TypeRef, input bool) string {
var representation string
for ref := r; ref != nil; ref = ref.OfType {
switch ref.Kind {
case introspection.TypeKindList:
representation += "[]"
case introspection.TypeKindScalar:
switch introspection.Scalar(ref.Name) {
case introspection.ScalarString:
representation += "string"
return representation
case introspection.ScalarInt:
representation += "int"
return representation
case introspection.ScalarBoolean:
representation += "bool"
return representation
case introspection.ScalarFloat:
representation += "float"
return representation
default:
// Custom scalar

// When used as an input, we're going to use objects rather than ID scalars (e.g. `*Container` rather than `ContainerID`)
// FIXME: do this dynamically rather than a hardcoded map.
rewrite := map[string]string{
"ContainerID": "Container",
"FileID": "File",
"DirectoryID": "Directory",
"SecretID": "Secret",
"SocketID": "Socket",
"CacheID": "CacheVolume",
}
if alias, ok := rewrite[ref.Name]; ok && input {
representation += "*" + alias
} else {
representation += ref.Name
}
return representation
}
case introspection.TypeKindObject:
name := ref.Name
if name == queryStructName {
name = "Client"
}
representation += formatName(name)
return representation
case introspection.TypeKindInputObject:
representation += formatName(ref.Name)
return representation
}
}

panic(r)
}

// formatName formats a GraphQL name (e.g. object, field, arg) into a Go equivalent
// Example: `fooId` -> `FooID`
func formatName(s string) string {
Expand All @@ -134,7 +66,7 @@ func fieldOptionsStructName(f introspection.Field) string {
// `ContainerOpts` rather than `QueryContainerOpts`
// The structure name will not clash with others since everybody else
// is prefixed by object name.
if f.ParentObject.Name == queryStructName {
if f.ParentObject.Name == generator.QueryStructName {
return formatName(f.Name) + "Opts"
}
return formatName(f.ParentObject.Name) + formatName(f.Name) + "Opts"
Expand All @@ -144,7 +76,7 @@ func fieldOptionsStructName(f introspection.Field) string {
// Example: `contents: String!` -> `func (r *File) Contents(ctx context.Context) (string, error)`
func fieldFunction(f introspection.Field) string {
structName := formatName(f.ParentObject.Name)
if structName == queryStructName {
if structName == generator.QueryStructName {
structName = "Client"
}
signature := fmt.Sprintf(`func (r *%s) %s`,
Expand All @@ -162,10 +94,10 @@ func fieldFunction(f introspection.Field) string {

// FIXME: For top-level queries (e.g. File, Directory) if the field is named `id` then keep it as a
// scalar (DirectoryID) rather than an object (*Directory).
if f.ParentObject.Name == queryStructName && arg.Name == "id" {
args = append(args, fmt.Sprintf("%s %s", arg.Name, formatOutputType(arg.TypeRef)))
if f.ParentObject.Name == generator.QueryStructName && arg.Name == "id" {
args = append(args, fmt.Sprintf("%s %s", arg.Name, commonFunc.FormatOutputType(arg.TypeRef)))
} else {
args = append(args, fmt.Sprintf("%s %s", arg.Name, formatInputType(arg.TypeRef)))
args = append(args, fmt.Sprintf("%s %s", arg.Name, commonFunc.FormatInputType(arg.TypeRef)))
}
}
// Options (e.g. DirectoryContentsOptions -> <Object><Field>Options)
Expand All @@ -179,9 +111,9 @@ func fieldFunction(f introspection.Field) string {

retType := ""
if f.TypeRef.IsScalar() || f.TypeRef.IsList() {
retType = fmt.Sprintf("(%s, error)", formatOutputType(f.TypeRef))
retType = fmt.Sprintf("(%s, error)", commonFunc.FormatOutputType(f.TypeRef))
} else {
retType = "*" + formatOutputType(f.TypeRef)
retType = "*" + commonFunc.FormatOutputType(f.TypeRef)
}
signature += " " + retType

Expand Down
59 changes: 59 additions & 0 deletions codegen/generator/nodejs/templates/format.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package templates

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

// FormatTypeFunc is an implementation of generator.FormatTypeFuncs interface
// to format GraphQL type into Typescript.
type FormatTypeFunc struct{}

func (f *FormatTypeFunc) FormatKindList(representation string) string {
representation += "[]"
return representation
}

func (f *FormatTypeFunc) FormatKindScalarString(representation string) string {
representation += "string"
return representation
}

func (f *FormatTypeFunc) FormatKindScalarInt(representation string) string {
representation += "number"
return representation
}

func (f *FormatTypeFunc) FormatKindScalarFloat(representation string) string {
representation += "number"
return representation
}

func (f *FormatTypeFunc) FormatKindScalarBoolean(representation string) string {
representation += "boolean"
return representation
}

func (f *FormatTypeFunc) FormatKindScalarDefault(representation string, refName string, input bool) string {
if alias, ok := generator.CustomScalar[refName]; ok && input {
representation += alias
} else {
representation += refName
}

return representation
}

func (f *FormatTypeFunc) FormatKindObject(representation string, refName string) string {
name := refName
if name == generator.QueryStructName {
name = generator.QueryStructClientName
}

representation += formatName(name)
return representation
}

func (f *FormatTypeFunc) FormatKindInputObject(representation string, refName string) string {
representation += formatName(refName)
return representation
}
Loading