kind.go 2.31 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
package ipld

// ReprKind represents the primitive kind in the IPLD data model.
// All of these kinds map directly onto serializable data.
//
// Note that ReprKind contains the concept of "map", but not "struct"
// or "object" -- those are a concepts that could be introduced in a
// type system layers, but are *not* present in the data model layer,
// and therefore they aren't included in the ReprKind enum.
type ReprKind uint8

const (
13 14 15 16 17 18 19 20 21 22
	ReprKind_Invalid ReprKind = 0
	ReprKind_Map     ReprKind = '{'
	ReprKind_List    ReprKind = '['
	ReprKind_Null    ReprKind = '0'
	ReprKind_Bool    ReprKind = 'b'
	ReprKind_Int     ReprKind = 'i'
	ReprKind_Float   ReprKind = 'f'
	ReprKind_String  ReprKind = 's'
	ReprKind_Bytes   ReprKind = 'x'
	ReprKind_Link    ReprKind = '/'
23
)
Eric Myhre's avatar
Eric Myhre committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

func (k ReprKind) String() string {
	switch k {
	case ReprKind_Invalid:
		return "Invalid"
	case ReprKind_Map:
		return "Map"
	case ReprKind_List:
		return "List"
	case ReprKind_Null:
		return "Null"
	case ReprKind_Bool:
		return "Bool"
	case ReprKind_Int:
		return "Int"
	case ReprKind_Float:
		return "Float"
	case ReprKind_String:
		return "String"
	case ReprKind_Bytes:
		return "Bytes"
	case ReprKind_Link:
		return "Link"
	default:
		panic("invalid enumeration value!")
	}
}
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

// ReprKindSet is a type with a few enumerated consts that are commonly used
// (mostly, in error messages).
type ReprKindSet []ReprKind

var (
	ReprKindSet_Recursive = ReprKindSet{ReprKind_Map, ReprKind_List}
	ReprKindSet_Scalar    = ReprKindSet{ReprKind_Null, ReprKind_Bool, ReprKind_Int, ReprKind_Float, ReprKind_String, ReprKind_Bytes, ReprKind_Link}

	ReprKindSet_JustMap    = ReprKindSet{ReprKind_Map}
	ReprKindSet_JustList   = ReprKindSet{ReprKind_List}
	ReprKindSet_JustNull   = ReprKindSet{ReprKind_Null}
	ReprKindSet_JustBool   = ReprKindSet{ReprKind_Bool}
	ReprKindSet_JustInt    = ReprKindSet{ReprKind_Int}
	ReprKindSet_JustFloat  = ReprKindSet{ReprKind_Float}
	ReprKindSet_JustString = ReprKindSet{ReprKind_String}
	ReprKindSet_JustBytes  = ReprKindSet{ReprKind_Bytes}
	ReprKindSet_JustLink   = ReprKindSet{ReprKind_Link}
)

func (x ReprKindSet) String() string {
	s := ""
	for i := 0; i < len(x)-1; i++ {
		s += x[i].String() + " or "
	}
	s += x[len(x)-1].String()
	return s
}
79 80 81 82 83 84 85 86 87

func (x ReprKindSet) Contains(e ReprKind) bool {
	for _, v := range x {
		if v == e {
			return true
		}
	}
	return false
}