github.com/ipld/go-ipld-prime@v0.21.0/schema/kind.go (about) 1 package schema 2 3 import ( 4 "github.com/ipld/go-ipld-prime/datamodel" 5 ) 6 7 // TypeKind is an enum of kind in the IPLD Schema system. 8 // 9 // Note that schema.TypeKind is distinct from datamodel.Kind! 10 // Schema kinds include concepts such as "struct" and "enum", which are 11 // concepts only introduced by the Schema layer, and not present in the 12 // Data Model layer. 13 type TypeKind uint8 14 15 const ( 16 TypeKind_Invalid TypeKind = 0 17 TypeKind_Map TypeKind = '{' 18 TypeKind_List TypeKind = '[' 19 TypeKind_Unit TypeKind = '1' 20 TypeKind_Bool TypeKind = 'b' 21 TypeKind_Int TypeKind = 'i' 22 TypeKind_Float TypeKind = 'f' 23 TypeKind_String TypeKind = 's' 24 TypeKind_Bytes TypeKind = 'x' 25 TypeKind_Link TypeKind = '/' 26 TypeKind_Struct TypeKind = '$' 27 TypeKind_Union TypeKind = '^' 28 TypeKind_Enum TypeKind = '%' 29 TypeKind_Any TypeKind = '?' 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_Any: 39 return "any" 40 case TypeKind_List: 41 return "list" 42 case TypeKind_Unit: 43 return "unit" 44 case TypeKind_Bool: 45 return "bool" 46 case TypeKind_Int: 47 return "int" 48 case TypeKind_Float: 49 return "float" 50 case TypeKind_String: 51 return "string" 52 case TypeKind_Bytes: 53 return "bytes" 54 case TypeKind_Link: 55 return "link" 56 case TypeKind_Struct: 57 return "struct" 58 case TypeKind_Union: 59 return "union" 60 case TypeKind_Enum: 61 return "enum" 62 default: 63 panic("invalid enumeration value!") 64 } 65 } 66 67 // ActsLike returns a constant from the datamodel.Kind enum describing what 68 // this schema.TypeKind acts like at the Data Model layer. 69 // 70 // Things with similar names are generally conserved 71 // (e.g. "map" acts like "map"); 72 // concepts added by the schema layer have to be mapped onto something 73 // (e.g. "struct" acts like "map"). 74 // 75 // Note that this mapping describes how a typed Node will *act*, programmatically; 76 // it does not necessarily describe how it will be *serialized* 77 // (for example, a struct will always act like a map, even if it has a tuple 78 // representation strategy and thus becomes a list when serialized). 79 func (k TypeKind) ActsLike() datamodel.Kind { 80 switch k { 81 case TypeKind_Invalid: 82 return datamodel.Kind_Invalid 83 case TypeKind_Map: 84 return datamodel.Kind_Map 85 case TypeKind_List: 86 return datamodel.Kind_List 87 case TypeKind_Unit: 88 return datamodel.Kind_Bool // maps to 'true'. // REVIEW: odd that this doesn't map to 'null'? // TODO this should be standardized in the specs, in a table. 89 case TypeKind_Bool: 90 return datamodel.Kind_Bool 91 case TypeKind_Int: 92 return datamodel.Kind_Int 93 case TypeKind_Float: 94 return datamodel.Kind_Float 95 case TypeKind_String: 96 return datamodel.Kind_String 97 case TypeKind_Bytes: 98 return datamodel.Kind_Bytes 99 case TypeKind_Link: 100 return datamodel.Kind_Link 101 case TypeKind_Struct: 102 return datamodel.Kind_Map // clear enough: fields are keys. 103 case TypeKind_Union: 104 return datamodel.Kind_Map 105 case TypeKind_Enum: 106 return datamodel.Kind_String // 'AsString' is the one clear thing to define. 107 case TypeKind_Any: 108 return datamodel.Kind_Invalid // TODO: maybe ActsLike should return (Kind, bool) 109 default: 110 panic("invalid enumeration value!") 111 } 112 }