kind.go 2.79 KB
Newer Older
1 2
package schema

3
import (
tavit ohanian's avatar
tavit ohanian committed
4
	ld "gitlab.dms3.io/ld/go-ld-prime"
5 6
)

7
// TypeKind is an enum of kind in the LD Schema system.
8
//
tavit ohanian's avatar
tavit ohanian committed
9
// Note that schema.TypeKind is distinct from ld.Kind!
10 11 12
// Schema kinds include concepts such as "struct" and "enum", which are
// concepts only introduced by the Schema layer, and not present in the
// Data Model layer.
13
type TypeKind uint8
14 15

const (
16 17 18 19 20 21 22 23 24 25 26 27 28 29
	TypeKind_Invalid TypeKind = 0
	TypeKind_Map     TypeKind = '{'
	TypeKind_List    TypeKind = '['
	TypeKind_Unit    TypeKind = '1'
	TypeKind_Bool    TypeKind = 'b'
	TypeKind_Int     TypeKind = 'i'
	TypeKind_Float   TypeKind = 'f'
	TypeKind_String  TypeKind = 's'
	TypeKind_Bytes   TypeKind = 'x'
	TypeKind_Link    TypeKind = '/'
	TypeKind_Struct  TypeKind = '$'
	TypeKind_Union   TypeKind = '^'
	TypeKind_Enum    TypeKind = '%'
	// FUTURE: TypeKind_Any = '?'?
30 31
)

32
func (k TypeKind) String() string {
33
	switch k {
34
	case TypeKind_Invalid:
35
		return "Invalid"
36
	case TypeKind_Map:
37
		return "Map"
38
	case TypeKind_List:
39
		return "List"
40
	case TypeKind_Unit:
41
		return "Unit"
42
	case TypeKind_Bool:
43
		return "Bool"
44
	case TypeKind_Int:
45
		return "Int"
46
	case TypeKind_Float:
47
		return "Float"
48
	case TypeKind_String:
49
		return "String"
50
	case TypeKind_Bytes:
51
		return "Bytes"
52
	case TypeKind_Link:
53
		return "Link"
54
	case TypeKind_Struct:
55
		return "Struct"
56
	case TypeKind_Union:
57
		return "Union"
58
	case TypeKind_Enum:
59 60 61 62 63
		return "Enum"
	default:
		panic("invalid enumeration value!")
	}
}
64

tavit ohanian's avatar
tavit ohanian committed
65
// ActsLike returns a constant from the ld.Kind enum describing what
66
// this schema.TypeKind acts like at the Data Model layer.
67 68 69 70 71 72 73 74 75 76
//
// Things with similar names are generally conserved
// (e.g. "map" acts like "map");
// concepts added by the schema layer have to be mapped onto something
// (e.g. "struct" acts like "map").
//
// Note that this mapping describes how a typed Node will *act*, programmatically;
// it does not necessarily describe how it will be *serialized*
// (for example, a struct will always act like a map, even if it has a tuple
// representation strategy and thus becomes a list when serialized).
tavit ohanian's avatar
tavit ohanian committed
77
func (k TypeKind) ActsLike() ld.Kind {
78
	switch k {
79
	case TypeKind_Invalid:
tavit ohanian's avatar
tavit ohanian committed
80
		return ld.Kind_Invalid
81
	case TypeKind_Map:
tavit ohanian's avatar
tavit ohanian committed
82
		return ld.Kind_Map
83
	case TypeKind_List:
tavit ohanian's avatar
tavit ohanian committed
84
		return ld.Kind_List
85
	case TypeKind_Unit:
tavit ohanian's avatar
tavit ohanian committed
86
		return ld.Kind_Bool // maps to 'true'.
87
	case TypeKind_Bool:
tavit ohanian's avatar
tavit ohanian committed
88
		return ld.Kind_Bool
89
	case TypeKind_Int:
tavit ohanian's avatar
tavit ohanian committed
90
		return ld.Kind_Int
91
	case TypeKind_Float:
tavit ohanian's avatar
tavit ohanian committed
92
		return ld.Kind_Float
93
	case TypeKind_String:
tavit ohanian's avatar
tavit ohanian committed
94
		return ld.Kind_String
95
	case TypeKind_Bytes:
tavit ohanian's avatar
tavit ohanian committed
96
		return ld.Kind_Bytes
97
	case TypeKind_Link:
tavit ohanian's avatar
tavit ohanian committed
98
		return ld.Kind_Link
99
	case TypeKind_Struct:
tavit ohanian's avatar
tavit ohanian committed
100
		return ld.Kind_Map // clear enough: fields are keys.
101
	case TypeKind_Union:
tavit ohanian's avatar
tavit ohanian committed
102
		return ld.Kind_Map // REVIEW: unions are tricky.
103
	case TypeKind_Enum:
tavit ohanian's avatar
tavit ohanian committed
104
		return ld.Kind_String // 'AsString' is the one clear thing to define.
105 106 107 108
	default:
		panic("invalid enumeration value!")
	}
}