kind.go 2.83 KB
Newer Older
1 2 3 4 5 6
package schema

import (
	ipld "github.com/ipld/go-ipld-prime"
)

7
// TypeKind is an enum of kind in the IPLD Schema system.
8
//
9
// Note that schema.TypeKind is distinct from ipld.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 64
		return "Enum"
	default:
		panic("invalid enumeration value!")
	}
}

65 66
// ActsLike returns a constant from the ipld.Kind enum describing what
// 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).
77
func (k TypeKind) ActsLike() ipld.Kind {
78
	switch k {
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
	case TypeKind_Invalid:
		return ipld.Kind_Invalid
	case TypeKind_Map:
		return ipld.Kind_Map
	case TypeKind_List:
		return ipld.Kind_List
	case TypeKind_Unit:
		return ipld.Kind_Bool // maps to 'true'.
	case TypeKind_Bool:
		return ipld.Kind_Bool
	case TypeKind_Int:
		return ipld.Kind_Int
	case TypeKind_Float:
		return ipld.Kind_Float
	case TypeKind_String:
		return ipld.Kind_String
	case TypeKind_Bytes:
		return ipld.Kind_Bytes
	case TypeKind_Link:
		return ipld.Kind_Link
	case TypeKind_Struct:
		return ipld.Kind_Map // clear enough: fields are keys.
	case TypeKind_Union:
		return ipld.Kind_Map // REVIEW: unions are tricky.
	case TypeKind_Enum:
		return ipld.Kind_String // 'AsString' is the one clear thing to define.
105 106 107 108
	default:
		panic("invalid enumeration value!")
	}
}