-
Notifications
You must be signed in to change notification settings - Fork 231
/
validate_types.go
53 lines (47 loc) · 1.44 KB
/
validate_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
package app
import (
"go/ast"
"encr.dev/pkg/errors"
"encr.dev/v2/internals/parsectx"
"encr.dev/v2/internals/schema"
"encr.dev/v2/internals/schema/schemautil"
"encr.dev/v2/parser/apis/api/apienc"
)
// validateType validates the type of a field can be marshalled.
// according to Encore's requirements.
//
// This walks the type recursively and validates the whole thing
func (d *Desc) validateType(pc *parsectx.Context, usedAt ast.Node, typ schema.Type) {
// Convert generic types to their concrete types
typ = schemautil.ConcretizeGenericType(pc.Errs, typ)
// Walk the type recursively
schemautil.Walk(typ, func(t schema.Type) bool {
switch t := t.(type) {
case schema.StructType:
for _, field := range t.Fields {
if field.IsAnonymous() {
// We don't support anonymous fields anywhere within
// Encore types that we need to marshal.
pc.Errs.Add(
apienc.ErrAnonymousFieldsNotSupported.
AtGoNode(field.AST, errors.AsError("defined here")).
AtGoNode(usedAt, errors.AsHelp("used here")),
)
}
}
case schema.FuncType:
pc.Errs.Add(
apienc.ErrFuncNotSupported.
AtGoNode(t.ASTExpr(), errors.AsError("defined here")).
AtGoNode(usedAt, errors.AsHelp("used here")),
)
case schema.InterfaceType:
pc.Errs.Add(
apienc.ErrInterfaceNotSupported.
AtGoNode(t.ASTExpr(), errors.AsError("defined here")).
AtGoNode(usedAt, errors.AsHelp("used here")),
)
}
return true
})
}