-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathschema-schema.ts
More file actions
303 lines (290 loc) · 8.5 KB
/
schema-schema.ts
File metadata and controls
303 lines (290 loc) · 8.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import { CID } from 'multiformats/cid'
export type KindBool = boolean
export type KindString = string
export type KindBytes = Uint8Array
export type KindInt = number | bigint
export type KindFloat = number
export type KindNull = null
export type KindMap = {}
export type KindList = []
export type KindLink = CID
export type KindUnion = {}
export type KindStruct = {}
export type KindEnum = {}
export namespace KindBool {
export function isKindBool(value: any): value is KindBool {
return typeof value === 'boolean'
}
}
export namespace KindString {
export function isKindString(value: any): value is KindString {
return typeof value === 'string'
}
}
export namespace KindBytes {
export function isKindBytes(value: any): value is KindBytes {
return value instanceof Uint8Array
}
}
export namespace KindInt {
export function isKindInt(value: any): value is KindInt {
return (typeof value === 'number' && Number.isInteger(value) && Number.isFinite(value)) || (typeof value === 'bigint')
}
}
export namespace KindFloat {
export function isKindFloat(value: any): value is KindFloat {
return typeof value === 'number' && Number.isFinite(value)
}
}
export namespace KindNull {
export function isKindNull(value: any): value is KindNull {
return value === null
}
}
export namespace KindMap {
export function isKindMap(value: any): value is KindMap {
return value !== null && typeof value === 'object' && value.asCID !== value && !Array.isArray(value) && !(value instanceof Uint8Array)
}
}
export namespace KindList {
export function isKindList(value: any): value is KindList {
return Array.isArray(value)
}
}
export namespace KindLink {
export function isKindLink(value: any): value is KindLink {
return value !== null && typeof value === 'object' && value.asCID === value
}
}
export type TypeDefn =
{ bool: TypeDefnBool }
| { string: TypeDefnString }
| { bytes: TypeDefnBytes }
| { int: TypeDefnInt }
| { float: TypeDefnFloat }
| { map: TypeDefnMap }
| { list: TypeDefnList }
| { link: TypeDefnLink }
| { union: TypeDefnUnion }
| { struct: TypeDefnStruct }
| { enum: TypeDefnEnum }
| { unit: TypeDefnUnit }
| { any: TypeDefnAny }
| { copy: TypeDefnCopy }
| { null: TypeDefnNull } // TODO: not in schema-schema, what do?
export type TypeDefnBool = {}
export type TypeDefnString = {
representation?: StringRepresentation // TODO: not in schema-schema
}
export type StringRepresentation = { advanced: AdvancedDataLayoutName } // TODO: does it need a {string}
export type TypeDefnBytes = {
representation?: BytesRepresentation // TODO: not optional in schema-schema
}
export type BytesRepresentation =
{ bytes: BytesRepresentation_Bytes } // TODO: does this make sense? or is default good enough?
| { advanced: AdvancedDataLayoutName }
export type BytesRepresentation_Bytes = {}
export type TypeDefnInt = {}
export type TypeDefnFloat = {}
export type TypeDefnNull = {} // TODO: not in schema-schema, what do?
export type TypeDefnMap = {
keyType: TypeName
valueType: TypeNameOrInlineDefn
valueNullable?: KindBool
representation?: MapRepresentation
}
export type MapRepresentation =
{ map: MapRepresentation_Map } // TODO: not in schema-schema - put it there
| { stringpairs: MapRepresentation_StringPairs }
| { listpairs: MapRepresentation_ListPairs }
| { advanced: AdvancedDataLayoutName }
export type MapRepresentation_Map = {}
export type MapRepresentation_StringPairs = {
innerDelim: KindString
entryDelim: KindString
}
export type MapRepresentation_ListPairs = {}
export type TypeDefnList = {
valueType: TypeNameOrInlineDefn
valueNullable?: KindBool
representation?: ListRepresentation
}
export type ListRepresentation =
{ list: ListRepresentation_List } // TODO: not in schema-schema - put it there?
| { advanced: AdvancedDataLayoutName }
export type ListRepresentation_List = {}
export type TypeDefnLink = {
expectedType?: KindString
}
export type TypeDefnUnion = {
members: UnionMember[]
representation: UnionRepresentation
}
export type UnionMember =
TypeName
| UnionMemberInlineDefn
export type UnionMemberInlineDefn =
{ link: TypeDefnLink }
export type UnionRepresentation =
{ kinded: UnionRepresentation_Kinded }
| { keyed: UnionRepresentation_Keyed }
| { envelope: UnionRepresentation_Envelope }
| { inline: UnionRepresentation_Inline }
| { stringprefix: UnionRepresentation_StringPrefix }
| { bytesprefix: UnionRepresentation_BytesPrefix }
// TODO: schema-schema has UnionMember instead of TypeName here but we need a name for the map
export type UnionRepresentation_Kinded = { [k in RepresentationKind]?: TypeName }
export type UnionRepresentation_Keyed = { [k in KindString]: UnionMember }
export type UnionRepresentation_Envelope = {
discriminantKey: KindString
contentKey: KindString
discriminantTable: { [ k in KindString]: UnionMember }
}
export type UnionRepresentation_Inline = {
discriminantKey: KindString
discriminantTable: { [ k in HexString]: TypeName }
}
export type HexString = string
export type UnionRepresentation_StringPrefix = {
prefixes: { [ k in KindString]: TypeName }
}
export type UnionRepresentation_BytesPrefix = {
prefixes: { [ k in KindString]: TypeName }
}
export type TypeDefnStruct = {
fields: { [ k in FieldName]: StructField }
representation?: StructRepresentation
annotations?: StructAnnotations
comments?: StructComments
}
export type FieldName = string
export type StructField = {
type: TypeNameOrInlineDefn
optional?: KindBool
nullable?: KindBool
}
export type TypeNameOrInlineDefn =
TypeName
| InlineDefn
export type InlineDefn =
{ map: TypeDefnMap }
| { list: TypeDefnList }
| { link: TypeDefnLink }
export type StructRepresentation =
{ map: StructRepresentation_Map }
| { tuple: StructRepresentation_Tuple }
| { stringpairs: StructRepresentation_StringPairs }
| { stringjoin: StructRepresentation_StringJoin }
| { listpairs: StructRepresentation_ListPairs }
| { advanced: AdvancedDataLayoutName } // TODO: not on schema-schema
export type StructRepresentation_Map = {
fields?: { [ k in FieldName]: StructRepresentation_Map_FieldDetails }
}
export type StructRepresentation_Map_FieldDetails = {
rename?: KindString
implicit?: AnyScalar
}
export type StructRepresentation_Tuple = {
fieldOrder?: FieldName[]
}
export type StructRepresentation_StringPairs = {
innerDelim: KindString
entryDelim: KindString
}
export type StructRepresentation_StringJoin = {
join: KindString
fieldOrder?: FieldName[]
}
export type StructRepresentation_ListPairs = {}
export type StructAnnotations = {
type?: { [ k in KindString]: KindString }[]
fields?: { [ k in FieldName]: { [ k in KindString]: KindString }[] }
}
export type StructComments = {
type?: {
precomments: KindString,
linecomment: KindString
}
fields?: {
[ k in FieldName]: {
precomments: KindString,
linecomment: KindString
}
}
}
export type TypeDefnEnum = {
members: EnumMember[]
representation: EnumRepresentation
annotations?: {
type?: { [ k in KindString]: KindString }[]
}
comments?: {
type?: {
precomments: KindString,
linecomment: KindString
}
}
}
export type EnumMember = string
export type EnumRepresentation =
{ string: EnumRepresentation_String }
| { int: EnumRepresentation_Int }
export type EnumRepresentation_String = { [ k in EnumMember]: KindString }
export type EnumRepresentation_Int = { [ k in EnumMember]: KindInt }
export type TypeDefnCopy = {
fromType: TypeName
annotations?: {
type?: { [ k in KindString]: KindString }[]
}
comments?: {
type?: {
precomments: KindString,
linecomment: KindString
}
}
}
export type TypeDefnUnit = {
representation: UnitRepresentation
}
export type UnitRepresentation =
"null"
| "true"
| "false"
| "emptymap"
export type TypeDefnAny = {}
export type TypeName = string
export type Schema = {
types: { [ k in TypeName]: TypeDefn }
advanced?: AdvancedDataLayoutMap
}
export type AdvancedDataLayoutName = string
export type AdvancedDataLayoutMap = { [ k in AdvancedDataLayoutName ]: AdvancedDataLayout }
export type AdvancedDataLayout = {}
export type TypeKind =
KindBool
| KindString
| KindBytes
| KindInt
| KindFloat
| KindMap
| KindList
| KindLink
| KindUnion
| KindStruct
| KindEnum
export type RepresentationKind =
"bool"
| "string"
| "bytes"
| "int"
| "float"
| "null" // TODO: not in schema-schema, wot do?
| "map"
| "list"
| "link"
export type AnyScalar =
KindBool
| KindString
| KindBytes
| KindInt
| KindFloat