type.go 3.69 KB
Newer Older
Eric Myhre's avatar
Eric Myhre committed
1
package schema
Eric Myhre's avatar
Eric Myhre committed
2

3
import (
4
	ipld "github.com/ipld/go-ipld-prime"
5 6
)

Eric Myhre's avatar
Eric Myhre committed
7
type TypeName string // = ast.TypeName
8

9 10 11 12 13 14 15 16 17 18 19 20 21 22
// typesystem.Type is an union interface; each of the `Type*` concrete types
// in this package are one of its members.
//
// Specifically,
//
// 	TypeBool
// 	TypeString
// 	TypeBytes
// 	TypeInt
// 	TypeFloat
// 	TypeMap
// 	TypeList
// 	TypeLink
// 	TypeUnion
Eric Myhre's avatar
Eric Myhre committed
23
// 	TypeStruct
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
// 	TypeEnum
//
// are all of the kinds of Type.
//
// This is a closed union; you can switch upon the above members without
// including a default case.  The membership is closed by the unexported
// '_Type' method; you may use the BurntSushi/go-sumtype tool to check
// your switches for completeness.
//
// Many interesting properties of each Type are only defined for that specific
// type, so it's typical to use a type switch to handle each type of Type.
// (Your humble author is truly sorry for the word-mash that results from
// attempting to describe the types that describe the typesystem.Type.)
//
// For example, to inspect the kind of fields in a struct: you might
// cast a `Type` interface into `TypeStruct`, and then the `Fields()` on
// that `TypeStruct` can be inspected.  (`Fields()` isn't defined for any
// other kind of Type.)
type Type interface {
	// Unexported marker method to force the union closed.
	_Type()
45

46
	// Returns a pointer to the TypeSystem this Type is a member of.
Eric Myhre's avatar
Eric Myhre committed
47
	TypeSystem() *TypeSystem
48

49 50 51 52 53
	// Returns the string name of the Type.  This name is unique within the
	// universe this type is a member of, *unless* this type is Anonymous,
	// in which case a string describing the type will still be returned, but
	// that string will not be required to be unique.
	Name() TypeName
Eric Myhre's avatar
Eric Myhre committed
54

55 56 57 58 59 60 61
	// Returns the Representation Kind in the IPLD Data Model that this type
	// is expected to be serialized as.
	//
	// Note that in one case, this will return `ipld.ReprKind_Invalid` --
	// TypeUnion with Style=Kinded may be serialized as different kinds
	// depending on their value, so we can't say from the type definition
	// alone what kind we expect.
62
	ReprKind() ipld.ReprKind
Eric Myhre's avatar
Eric Myhre committed
63 64
}

65 66 67 68 69 70 71 72 73 74
var (
	_ Type = TypeBool{}
	_ Type = TypeString{}
	_ Type = TypeBytes{}
	_ Type = TypeInt{}
	_ Type = TypeFloat{}
	_ Type = TypeMap{}
	_ Type = TypeList{}
	_ Type = TypeLink{}
	_ Type = TypeUnion{}
Eric Myhre's avatar
Eric Myhre committed
75
	_ Type = TypeStruct{}
76 77 78
	_ Type = TypeEnum{}
)

79 80
type anyType struct {
	name     TypeName
Eric Myhre's avatar
Eric Myhre committed
81
	universe *TypeSystem
82 83
}

Eric Myhre's avatar
Eric Myhre committed
84
type TypeBool struct {
85
	anyType
Eric Myhre's avatar
Eric Myhre committed
86
}
87

Eric Myhre's avatar
Eric Myhre committed
88
type TypeString struct {
89
	anyType
Eric Myhre's avatar
Eric Myhre committed
90
}
91

Eric Myhre's avatar
Eric Myhre committed
92
type TypeBytes struct {
93
	anyType
Eric Myhre's avatar
Eric Myhre committed
94
}
95

Eric Myhre's avatar
Eric Myhre committed
96
type TypeInt struct {
97
	anyType
Eric Myhre's avatar
Eric Myhre committed
98
}
99

Eric Myhre's avatar
Eric Myhre committed
100
type TypeFloat struct {
101
	anyType
Eric Myhre's avatar
Eric Myhre committed
102
}
103

Eric Myhre's avatar
Eric Myhre committed
104
type TypeMap struct {
105 106 107 108 109
	anyType
	anonymous     bool
	keyType       Type // must be ReprKind==string (e.g. Type==String|Enum).
	valueType     Type
	valueNullable bool
Eric Myhre's avatar
Eric Myhre committed
110
}
111

Eric Myhre's avatar
Eric Myhre committed
112
type TypeList struct {
113 114 115 116
	anyType
	anonymous     bool
	valueType     Type
	valueNullable bool
Eric Myhre's avatar
Eric Myhre committed
117
}
118

Eric Myhre's avatar
Eric Myhre committed
119
type TypeLink struct {
120
	anyType
Eric Myhre's avatar
Eric Myhre committed
121 122 123 124
	// ...?
}

type TypeUnion struct {
125 126 127
	anyType
	style        UnionStyle
	valuesKinded map[ipld.ReprKind]Type // for Style==Kinded
128
	values       map[string]Type        // for Style!=Kinded (note, key is freetext, not necessarily TypeName of the value)
129 130
	typeHintKey  string                 // for Style==Envelope|Inline
	contentKey   string                 // for Style==Envelope
Eric Myhre's avatar
Eric Myhre committed
131
}
132

Eric Myhre's avatar
Eric Myhre committed
133 134 135 136 137 138 139 140 141
type UnionStyle struct{ x string }

var (
	UnionStyle_Kinded   = UnionStyle{"kinded"}
	UnionStyle_Keyed    = UnionStyle{"keyed"}
	UnionStyle_Envelope = UnionStyle{"envelope"}
	UnionStyle_Inline   = UnionStyle{"inline"}
)

Eric Myhre's avatar
Eric Myhre committed
142
type TypeStruct struct {
143 144
	anyType
	tupleStyle bool // if true, ReprKind=Array instead of map (and optional fields are invalid!)
Eric Myhre's avatar
Eric Myhre committed
145
	fields     []StructField
Eric Myhre's avatar
Eric Myhre committed
146
}
Eric Myhre's avatar
Eric Myhre committed
147
type StructField struct {
148 149 150 151
	name     string
	typ      Type
	optional bool
	nullable bool
Eric Myhre's avatar
Eric Myhre committed
152 153 154
}

type TypeEnum struct {
155 156
	anyType
	members []string
Eric Myhre's avatar
Eric Myhre committed
157
}