/
module_types.go
248 lines (208 loc) · 6.59 KB
/
module_types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package templates
import (
"fmt"
"go/types"
. "github.com/dave/jennifer/jen" //nolint:stylecheck
"github.com/iancoleman/strcase"
)
// A Go type that has been parsed and can be registered with the dagger API
type ParsedType interface {
// Generated code for registering the type with the dagger API
TypeDefCode() (*Statement, error)
// The underlying go type that ParsedType wraps
GoType() types.Type
// Go types referred to by this type
GoSubTypes() []types.Type
}
type NamedParsedType interface {
ParsedType
Name() string
ModuleName() string
}
func loadFromIDGQLFieldName(spec NamedParsedType) string {
// NOTE: unfortunately we currently need to account for namespacing here
return fmt.Sprintf("load%s%sFromID", strcase.ToCamel(spec.ModuleName()), spec.Name())
}
// parseGoTypeReference parses a Go type and returns a TypeSpec for the type *reference* only.
// So if the type is a struct or interface, the returned TypeSpec will not have all the fields,
// only the type name and kind.
// This is so that the typedef can be referenced as the type of an arg, return value or field
// without needing to duplicate the full type definition every time it occurs.
func (ps *parseState) parseGoTypeReference(typ types.Type, named *types.Named, isPtr bool) (ParsedType, error) {
switch t := typ.(type) {
case *types.Named:
// Named types are any types declared like `type Foo <...>`
typeSpec, err := ps.parseGoTypeReference(t.Underlying(), t, isPtr)
if err != nil {
return nil, fmt.Errorf("failed to parse named type: %w", err)
}
return typeSpec, nil
case *types.Pointer:
typeSpec, err := ps.parseGoTypeReference(t.Elem(), named, true)
if err != nil {
return nil, fmt.Errorf("failed to parse pointer type: %w", err)
}
return typeSpec, nil
case *types.Slice:
elemTypeSpec, err := ps.parseGoTypeReference(t.Elem(), nil, isPtr)
if err != nil {
return nil, fmt.Errorf("failed to parse slice element type: %w", err)
}
return &parsedSliceType{
goType: t,
underlying: elemTypeSpec,
}, nil
case *types.Basic:
if t.Kind() == types.Invalid {
return nil, fmt.Errorf("invalid type: %+v", t)
}
parsedType := &parsedPrimitiveType{goType: t, isPtr: isPtr}
if named != nil {
parsedType.alias = named.Obj().Name()
}
return parsedType, nil
case *types.Struct:
if named == nil {
return nil, fmt.Errorf("struct types must be named")
}
typeName := named.Obj().Name()
if typeName == "" {
return nil, fmt.Errorf("struct types must be named")
}
moduleName := ""
if !ps.isDaggerGenerated(named.Obj()) {
moduleName = ps.moduleName
}
return &parsedObjectTypeReference{
name: typeName,
moduleName: moduleName,
isPtr: isPtr,
goType: named,
}, nil
case *types.Interface:
if named == nil {
return nil, fmt.Errorf("interface types must be named")
}
typeName := named.Obj().Name()
if typeName == "" {
return nil, fmt.Errorf("interface types must be named")
}
moduleName := ""
if !ps.isDaggerGenerated(named.Obj()) {
moduleName = ps.moduleName
}
return &parsedIfaceTypeReference{
name: typeName,
moduleName: moduleName,
goType: named,
}, nil
default:
return nil, fmt.Errorf("unsupported type for named type reference %T", t)
}
}
// parsedPrimitiveType is a parsed type that is a primitive type like string, int, bool, etc.
type parsedPrimitiveType struct {
goType *types.Basic
isPtr bool
// if this is something like `type Foo string`, then alias will be "Foo"
alias string
}
var _ ParsedType = &parsedPrimitiveType{}
func (spec *parsedPrimitiveType) TypeDefCode() (*Statement, error) {
var kind Code
switch spec.goType.Info() {
case types.IsString:
kind = Id("StringKind")
case types.IsInteger:
kind = Id("IntegerKind")
case types.IsBoolean:
kind = Id("BooleanKind")
default:
return nil, fmt.Errorf("unsupported basic type: %+v", spec.goType)
}
def := Qual("dag", "TypeDef").Call().Dot("WithKind").Call(
kind,
)
if spec.isPtr {
def = def.Dot("WithOptional").Call(Lit(true))
}
return def, nil
}
func (spec *parsedPrimitiveType) GoType() types.Type {
return spec.goType
}
func (spec *parsedPrimitiveType) GoSubTypes() []types.Type {
return nil
}
// parsedSliceType is a parsed type that is a slice of other types
type parsedSliceType struct {
goType *types.Slice
underlying ParsedType // the element TypeSpec
}
var _ ParsedType = &parsedSliceType{}
func (spec *parsedSliceType) TypeDefCode() (*Statement, error) {
underlyingCode, err := spec.underlying.TypeDefCode()
if err != nil {
return nil, fmt.Errorf("failed to generate underlying type code: %w", err)
}
return Qual("dag", "TypeDef").Call().Dot("WithListOf").Call(underlyingCode), nil
}
func (spec *parsedSliceType) GoType() types.Type {
return spec.goType
}
func (spec *parsedSliceType) GoSubTypes() []types.Type {
return spec.underlying.GoSubTypes()
}
// parsedObjectTypeReference is a parsed object type that is referred to just by name rather
// than with the full type definition
type parsedObjectTypeReference struct {
name string
moduleName string
isPtr bool
goType types.Type
}
var _ NamedParsedType = &parsedObjectTypeReference{}
func (spec *parsedObjectTypeReference) TypeDefCode() (*Statement, error) {
return Qual("dag", "TypeDef").Call().Dot("WithObject").Call(
Lit(spec.name),
), nil
}
func (spec *parsedObjectTypeReference) GoType() types.Type {
return spec.goType
}
func (spec *parsedObjectTypeReference) GoSubTypes() []types.Type {
// because this is a *reference* to a named type, we return the goType itself as a subtype too
return []types.Type{spec.goType}
}
func (spec *parsedObjectTypeReference) Name() string {
return spec.name
}
func (spec *parsedObjectTypeReference) ModuleName() string {
return spec.moduleName
}
// parsedIfaceTypeReference is a parsed object type that is referred to just by name rather
// than with the full type definition
type parsedIfaceTypeReference struct {
name string
moduleName string
goType types.Type
}
var _ NamedParsedType = &parsedIfaceTypeReference{}
func (spec *parsedIfaceTypeReference) TypeDefCode() (*Statement, error) {
return Qual("dag", "TypeDef").Call().Dot("WithInterface").Call(
Lit(spec.name),
), nil
}
func (spec *parsedIfaceTypeReference) GoType() types.Type {
return spec.goType
}
func (spec *parsedIfaceTypeReference) GoSubTypes() []types.Type {
// because this is a *reference* to a named type, we return the goType itself as a subtype too
return []types.Type{spec.goType}
}
func (spec *parsedIfaceTypeReference) Name() string {
return spec.name
}
func (spec *parsedIfaceTypeReference) ModuleName() string {
return spec.moduleName
}