| 1 | package schema |
| 2 | |
| 3 | type SchemaTypeKind int |
| 4 | |
| 5 | type SchemaType interface { |
| 6 | Name() string |
| 7 | } |
| 8 | |
| 9 | type UserDefinedType struct { |
| 10 | name string |
| 11 | type_ Type |
| 12 | } |
| 13 | |
| 14 | func (udt *UserDefinedType) Name() string { |
| 15 | return udt.name |
| 16 | } |
| 17 | |
| 18 | func (udt *UserDefinedType) Type() Type { |
| 19 | return udt.type_ |
| 20 | } |
| 21 | |
| 22 | type UserDefinedEnum struct { |
| 23 | name string |
| 24 | kind TypeKind |
| 25 | values []EnumValue |
| 26 | } |
| 27 | |
| 28 | func (ude *UserDefinedEnum) Name() string { |
| 29 | return ude.name |
| 30 | } |
| 31 | |
| 32 | func (ude *UserDefinedEnum) Kind() TypeKind { |
| 33 | return ude.kind |
| 34 | } |
| 35 | |
| 36 | func (ude *UserDefinedEnum) Values() []EnumValue { |
| 37 | return ude.values |
| 38 | } |
| 39 | |
| 40 | type EnumValue struct { |
| 41 | name string |
| 42 | value uint |
| 43 | } |
| 44 | |
| 45 | func (ev *EnumValue) Name() string { |
| 46 | return ev.name |
| 47 | } |
| 48 | |
| 49 | func (ev *EnumValue) Value() uint { |
| 50 | return ev.value |
| 51 | } |
| 52 | |
| 53 | type TypeKind int |
| 54 | |
| 55 | const ( |
| 56 | UINT TypeKind = iota |
| 57 | U8 |
| 58 | U16 |
| 59 | U32 |
| 60 | U64 |
| 61 | INT |
| 62 | I8 |
| 63 | I16 |
| 64 | I32 |
| 65 | I64 |
| 66 | F32 |
| 67 | F64 |
| 68 | Bool |
| 69 | String |
| 70 | Void |
| 71 | // data |
| 72 | Data |
| 73 | // data<length> |
| 74 | DataFixed |
| 75 | // [len]type |
| 76 | Array |
| 77 | // []type |
| 78 | Slice |
| 79 | // optional<type> |
| 80 | Optional |
| 81 | // data<len> |
| 82 | DataArray |
| 83 | // data |
| 84 | DataSlice |
| 85 | // map[type]type |
| 86 | Map |
| 87 | // (type | type | ...) |
| 88 | Union |
| 89 | // { fields... } |
| 90 | Struct |
| 91 | // Named user type |
| 92 | UserType |
| 93 | ) |
| 94 | |
| 95 | func (tk TypeKind) String() string { |
| 96 | switch tk { |
| 97 | case UINT: |
| 98 | return "UINT"; |
| 99 | case U8: |
| 100 | return "U8"; |
| 101 | case U16: |
| 102 | return "U16"; |
| 103 | case U32: |
| 104 | return "U32"; |
| 105 | case U64: |
| 106 | return "U64"; |
| 107 | case INT: |
| 108 | return "INT"; |
| 109 | case I8: |
| 110 | return "I8"; |
| 111 | case I16: |
| 112 | return "I16"; |
| 113 | case I32: |
| 114 | return "I32"; |
| 115 | case I64: |
| 116 | return "I64"; |
| 117 | case F32: |
| 118 | return "F32"; |
| 119 | case F64: |
| 120 | return "F64"; |
| 121 | case Bool: |
| 122 | return "Bool"; |
| 123 | case String: |
| 124 | return "String"; |
| 125 | case Void: |
| 126 | return "Void"; |
| 127 | case Data: |
| 128 | return "Data"; |
| 129 | case DataFixed: |
| 130 | return "DataFixed"; |
| 131 | case Array: |
| 132 | return "Array"; |
| 133 | case Slice: |
| 134 | return "Slice"; |
| 135 | case Optional: |
| 136 | return "Optional"; |
| 137 | case DataArray: |
| 138 | return "DataArray"; |
| 139 | case DataSlice: |
| 140 | return "DataSlice"; |
| 141 | case Map: |
| 142 | return "Map"; |
| 143 | case Union: |
| 144 | return "Union"; |
| 145 | case Struct: |
| 146 | return "Struct"; |
| 147 | case UserType: |
| 148 | return "UserType"; |
| 149 | default: |
| 150 | return "?" |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | type Type interface { |
| 155 | Kind() TypeKind |
| 156 | } |
| 157 | |
| 158 | type PrimitiveType struct { |
| 159 | kind TypeKind |
| 160 | } |
| 161 | |
| 162 | func (pt *PrimitiveType) Kind() TypeKind { |
| 163 | return pt.kind |
| 164 | } |
| 165 | |
| 166 | type OptionalType struct { |
| 167 | subtype Type |
| 168 | } |
| 169 | |
| 170 | func (ot *OptionalType) Kind() TypeKind { |
| 171 | return Optional |
| 172 | } |
| 173 | |
| 174 | func (ot *OptionalType) Subtype() Type { |
| 175 | return ot.subtype |
| 176 | } |
| 177 | |
| 178 | type DataType struct { |
| 179 | length uint |
| 180 | } |
| 181 | |
| 182 | func (dt *DataType) Kind() TypeKind { |
| 183 | if dt.length == 0 { |
| 184 | return DataSlice |
| 185 | } |
| 186 | return DataArray |
| 187 | } |
| 188 | |
| 189 | func (dt *DataType) Length() uint { |
| 190 | return dt.length |
| 191 | } |
| 192 | |
| 193 | type MapType struct { |
| 194 | key Type |
| 195 | value Type |
| 196 | } |
| 197 | |
| 198 | func (mt *MapType) Kind() TypeKind { |
| 199 | return Map |
| 200 | } |
| 201 | |
| 202 | func (mt *MapType) Key() Type { |
| 203 | return mt.key |
| 204 | } |
| 205 | |
| 206 | func (mt *MapType) Value() Type { |
| 207 | return mt.value |
| 208 | } |
| 209 | |
| 210 | type ArrayType struct { |
| 211 | member Type |
| 212 | length uint |
| 213 | } |
| 214 | |
| 215 | func (at *ArrayType) Kind() TypeKind { |
| 216 | if at.length == 0 { |
| 217 | return Slice |
| 218 | } |
| 219 | return Array |
| 220 | } |
| 221 | |
| 222 | func (at *ArrayType) Member() Type { |
| 223 | return at.member |
| 224 | } |
| 225 | |
| 226 | func (at *ArrayType) Length() uint { |
| 227 | return at.length |
| 228 | } |
| 229 | |
| 230 | type UnionType struct { |
| 231 | types []UnionSubtype |
| 232 | } |
| 233 | |
| 234 | func (ut *UnionType) Kind() TypeKind { |
| 235 | return Union |
| 236 | } |
| 237 | |
| 238 | func (ut *UnionType) Types() []UnionSubtype { |
| 239 | return ut.types |
| 240 | } |
| 241 | |
| 242 | type UnionSubtype struct { |
| 243 | subtype Type |
| 244 | tag uint64 |
| 245 | } |
| 246 | |
| 247 | func (ust *UnionSubtype) Type() Type { |
| 248 | return ust.subtype |
| 249 | } |
| 250 | |
| 251 | func (ust *UnionSubtype) Tag() uint64 { |
| 252 | return ust.tag |
| 253 | } |
| 254 | |
| 255 | type StructType struct { |
| 256 | fields []StructField |
| 257 | } |
| 258 | |
| 259 | func (st *StructType) Kind() TypeKind { |
| 260 | return Struct |
| 261 | } |
| 262 | |
| 263 | func (st *StructType) Fields() []StructField { |
| 264 | return st.fields |
| 265 | } |
| 266 | |
| 267 | type StructField struct { |
| 268 | name string |
| 269 | type_ Type |
| 270 | } |
| 271 | |
| 272 | func (sf *StructField) Name() string { |
| 273 | return sf.name |
| 274 | } |
| 275 | |
| 276 | func (sf *StructField) Type() Type { |
| 277 | return sf.type_ |
| 278 | } |
| 279 | |
| 280 | // This has not been compared with the list of user-defined types and is not |
| 281 | // guaranteed to actually exist; the consumer of this type must perform this |
| 282 | // lookup itself. |
| 283 | type NamedUserType struct { |
| 284 | name string |
| 285 | } |
| 286 | |
| 287 | func (nut *NamedUserType) Kind() TypeKind { |
| 288 | return UserType |
| 289 | } |
| 290 | |
| 291 | func (nut *NamedUserType) Name() string { |
| 292 | return nut.name |
| 293 | } |