type.go 5.14 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
	// Returns the Kind of this Type.
56
	//
57 58 59 60 61
	// 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.
62
	// schema.TypedNode values can be described by *two* distinct ReprKinds:
63 64 65 66 67
	// 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
68
	// (e.g., `schema.TypedNode{}.Representation().ReprKind()`) in order to find
69 70 71 72
	// 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
73 74
}

75 76 77 78 79 80 81 82 83 84
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
85
	_ Type = TypeStruct{}
86 87 88
	_ Type = TypeEnum{}
)

89 90
type anyType struct {
	name     TypeName
Eric Myhre's avatar
Eric Myhre committed
91
	universe *TypeSystem
92 93
}

Eric Myhre's avatar
Eric Myhre committed
94
type TypeBool struct {
95
	anyType
Eric Myhre's avatar
Eric Myhre committed
96
}
97

Eric Myhre's avatar
Eric Myhre committed
98
type TypeString struct {
99
	anyType
Eric Myhre's avatar
Eric Myhre committed
100
}
101

Eric Myhre's avatar
Eric Myhre committed
102
type TypeBytes struct {
103
	anyType
Eric Myhre's avatar
Eric Myhre committed
104
}
105

Eric Myhre's avatar
Eric Myhre committed
106
type TypeInt struct {
107
	anyType
Eric Myhre's avatar
Eric Myhre committed
108
}
109

Eric Myhre's avatar
Eric Myhre committed
110
type TypeFloat struct {
111
	anyType
Eric Myhre's avatar
Eric Myhre committed
112
}
113

Eric Myhre's avatar
Eric Myhre committed
114
type TypeMap struct {
115 116 117 118 119
	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
120
}
121

Eric Myhre's avatar
Eric Myhre committed
122
type TypeList struct {
123 124 125 126
	anyType
	anonymous     bool
	valueType     Type
	valueNullable bool
Eric Myhre's avatar
Eric Myhre committed
127
}
128

Eric Myhre's avatar
Eric Myhre committed
129
type TypeLink struct {
130
	anyType
131 132
	referencedType    Type
	hasReferencedType bool
Eric Myhre's avatar
Eric Myhre committed
133 134 135 136
	// ...?
}

type TypeUnion struct {
137 138 139
	anyType
	style        UnionStyle
	valuesKinded map[ipld.ReprKind]Type // for Style==Kinded
140
	values       map[string]Type        // for Style!=Kinded (note, key is freetext, not necessarily TypeName of the value)
141 142
	typeHintKey  string                 // for Style==Envelope|Inline
	contentKey   string                 // for Style==Envelope
Eric Myhre's avatar
Eric Myhre committed
143
}
144

Eric Myhre's avatar
Eric Myhre committed
145 146 147 148 149 150 151 152 153
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
154
type TypeStruct struct {
155
	anyType
156 157 158 159
	// 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
160
	fieldsMap      map[string]StructField // same content, indexed for lookup.
161
	representation StructRepresentation
Eric Myhre's avatar
Eric Myhre committed
162
}
Eric Myhre's avatar
Eric Myhre committed
163
type StructField struct {
164 165 166 167
	name     string
	typ      Type
	optional bool
	nullable bool
Eric Myhre's avatar
Eric Myhre committed
168 169
}

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
type StructRepresentation interface{ _StructRepresentation() }

func (StructRepresentation_Map) _StructRepresentation()         {}
func (StructRepresentation_Tuple) _StructRepresentation()       {}
func (StructRepresentation_StringPairs) _StructRepresentation() {}
func (StructRepresentation_StringJoin) _StructRepresentation()  {}

type StructRepresentation_Map struct {
	renames   map[string]string
	implicits map[string]interface{}
}
type StructRepresentation_Tuple struct{}
type StructRepresentation_StringPairs struct{ sep1, sep2 string }
type StructRepresentation_StringJoin struct{ sep string }

Eric Myhre's avatar
Eric Myhre committed
185
type TypeEnum struct {
186 187
	anyType
	members []string
Eric Myhre's avatar
Eric Myhre committed
188
}