type.go 7.12 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
func (tn TypeName) String() string { return string(tn) }

11 12 13 14 15 16 17 18 19 20 21 22 23 24
// 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
25
// 	TypeStruct
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
// 	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.
46 47
	// Also used to set the internal pointer back to the universe its part of.
	_Type(*TypeSystem)
48

49
	// Returns a pointer to the TypeSystem this Type is a member of.
Eric Myhre's avatar
Eric Myhre committed
50
	TypeSystem() *TypeSystem
51

52 53 54 55 56
	// 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
57

58
	// Returns the Kind of this Type.
59
	//
60 61 62 63 64
	// The returned value is a 1:1 association with which of the concrete
	// "schema.Type*" structs this interface can be cast to.
	//
	// Note that a schema.Kind is a different enum than ipld.ReprKind;
	// and furthermore, there's no strict relationship between them.
65
	// schema.TypedNode values can be described by *two* distinct ReprKinds:
66 67 68 69 70
	// one which describes how the Node itself will act,
	// and another which describes how the Node presents for serialization.
	// For some combinations of Type and representation strategy, one or both
	// of the ReprKinds can be determined statically; but not always:
	// it can sometimes be necessary to inspect the value quite concretely
71
	// (e.g., `schema.TypedNode{}.Representation().ReprKind()`) in order to find
72 73 74 75
	// out exactly how a node will be serialized!  This is because some types
	// can vary in representation kind based on their value (specifically,
	// kinded-representation unions have this property).
	Kind() Kind
Eric Myhre's avatar
Eric Myhre committed
76 77
}

78
var (
79 80 81 82 83 84 85 86 87 88 89
	_ Type = &TypeBool{}
	_ Type = &TypeString{}
	_ Type = &TypeBytes{}
	_ Type = &TypeInt{}
	_ Type = &TypeFloat{}
	_ Type = &TypeMap{}
	_ Type = &TypeList{}
	_ Type = &TypeLink{}
	_ Type = &TypeUnion{}
	_ Type = &TypeStruct{}
	_ Type = &TypeEnum{}
90 91
)

92
type typeBase struct {
93
	name     TypeName
Eric Myhre's avatar
Eric Myhre committed
94
	universe *TypeSystem
95 96
}

Eric Myhre's avatar
Eric Myhre committed
97
type TypeBool struct {
98
	typeBase
Eric Myhre's avatar
Eric Myhre committed
99
}
100

Eric Myhre's avatar
Eric Myhre committed
101
type TypeString struct {
102
	typeBase
Eric Myhre's avatar
Eric Myhre committed
103
}
104

Eric Myhre's avatar
Eric Myhre committed
105
type TypeBytes struct {
106
	typeBase
Eric Myhre's avatar
Eric Myhre committed
107
}
108

Eric Myhre's avatar
Eric Myhre committed
109
type TypeInt struct {
110
	typeBase
Eric Myhre's avatar
Eric Myhre committed
111
}
112

Eric Myhre's avatar
Eric Myhre committed
113
type TypeFloat struct {
114
	typeBase
Eric Myhre's avatar
Eric Myhre committed
115
}
116

Eric Myhre's avatar
Eric Myhre committed
117
type TypeMap struct {
118
	typeBase
119
	anonymous     bool
120 121
	keyType       TypeName // must be ReprKind==string (e.g. Type==String|Enum).
	valueType     TypeName
122
	valueNullable bool
Eric Myhre's avatar
Eric Myhre committed
123
}
124

Eric Myhre's avatar
Eric Myhre committed
125
type TypeList struct {
126
	typeBase
127
	anonymous     bool
128
	valueType     TypeName
129
	valueNullable bool
Eric Myhre's avatar
Eric Myhre committed
130
}
131

Eric Myhre's avatar
Eric Myhre committed
132
type TypeLink struct {
133
	typeBase
134
	referencedType    TypeName
135
	hasReferencedType bool
Eric Myhre's avatar
Eric Myhre committed
136 137 138 139
	// ...?
}

type TypeUnion struct {
140
	typeBase
141 142 143 144 145
	// Members are listed in the order they appear in the schema.
	// To find the discriminant info, you must look inside the representation; they all contain a 'table' of some kind in which the member types are the values.
	// Note that multiple appearances of the same type as distinct members of the union is not possible.
	//  While we could do this... A: that's... odd, and nearly never called for; B: not possible with kinded mode; C: imagine the golang-native type switch!  it's impossible.
	//  We rely on this clarity in many ways: most visibly, the type-level Node implementation for a union always uses the type names as if they were map keys!  This behavior is consistent for all union representations.
146
	members        []TypeName
147
	representation UnionRepresentation
Eric Myhre's avatar
Eric Myhre committed
148
}
149

150
type UnionRepresentation interface{ _UnionRepresentation() }
Eric Myhre's avatar
Eric Myhre committed
151

152 153 154 155 156
func (UnionRepresentation_Keyed) _UnionRepresentation()    {}
func (UnionRepresentation_Kinded) _UnionRepresentation()   {}
func (UnionRepresentation_Envelope) _UnionRepresentation() {}
func (UnionRepresentation_Inline) _UnionRepresentation()   {}

157 158 159 160 161
// A bunch of these tables in union representation might be easier to use if flipped;
//  we almost always index into them by type (since that's what we have an ordered list of);
//  and they're unique in both directions, so it's equally valid either way.
//  The order they're currently written in matches the serial form in the schema AST.

162
type UnionRepresentation_Keyed struct {
163
	table map[string]TypeName // key is user-defined freetext
164 165
}
type UnionRepresentation_Kinded struct {
166
	table map[ipld.ReprKind]TypeName
167 168 169 170
}
type UnionRepresentation_Envelope struct {
	discriminantKey string
	contentKey      string
171
	table           map[string]TypeName // key is user-defined freetext
172 173 174
}
type UnionRepresentation_Inline struct {
	discriminantKey string
175
	table           map[string]TypeName // key is user-defined freetext
176
}
Eric Myhre's avatar
Eric Myhre committed
177

Eric Myhre's avatar
Eric Myhre committed
178
type TypeStruct struct {
179
	typeBase
180 181 182 183
	// n.b. `Fields` is an (order-preserving!) map in the schema-schema;
	//  but it's a list here, with the keys denormalized into the value,
	//   because that's typically how we use it.
	fields         []StructField
184
	fieldsMap      map[string]StructField // same content, indexed for lookup.
185
	representation StructRepresentation
Eric Myhre's avatar
Eric Myhre committed
186
}
Eric Myhre's avatar
Eric Myhre committed
187
type StructField struct {
188
	parent   *TypeStruct
189
	name     string
190
	typ      TypeName
191 192
	optional bool
	nullable bool
Eric Myhre's avatar
Eric Myhre committed
193 194
}

195 196 197 198 199
type StructRepresentation interface{ _StructRepresentation() }

func (StructRepresentation_Map) _StructRepresentation()         {}
func (StructRepresentation_Tuple) _StructRepresentation()       {}
func (StructRepresentation_StringPairs) _StructRepresentation() {}
200
func (StructRepresentation_Stringjoin) _StructRepresentation()  {}
201 202 203

type StructRepresentation_Map struct {
	renames   map[string]string
204
	implicits map[string]ImplicitValue
205 206 207
}
type StructRepresentation_Tuple struct{}
type StructRepresentation_StringPairs struct{ sep1, sep2 string }
208
type StructRepresentation_Stringjoin struct{ sep string }
209

Eric Myhre's avatar
Eric Myhre committed
210
type TypeEnum struct {
211
	typeBase
212
	members []string
Eric Myhre's avatar
Eric Myhre committed
213
}
214 215 216 217 218 219 220 221 222 223 224

// ImplicitValue is an sum type holding values that are implicits.
// It's not an 'Any' value because it can't be recursive
// (or to be slightly more specific, it can be one of the recursive kinds,
// but if so, only its empty value is valid here).
type ImplicitValue interface{ _ImplicitValue() }

type ImplicitValue_EmptyList struct{}
type ImplicitValue_EmptyMap struct{}
type ImplicitValue_String struct{ x string }
type ImplicitValue_Int struct{ x int }