-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.ts
185 lines (167 loc) · 5.23 KB
/
index.ts
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
import { ArrayAvroSchema, MapAvroSchema, EnumAvroSchema, FixedAvroSchema, RecordAvroSchema, FieldAvroSchema, BaseAvroSchema } from './types'
import BaseSchema, { pick } from './base'
export { BaseSchema }
export * from './types'
export { default as templater, avscToDefinerCode, parseSchema } from './templater'
export class SchemaFactory extends BaseSchema {
/**
* Primitive type. Corresponds to no value
*
* @example { "type": "null" }
*
* @reference https://avro.apache.org/docs/1.9.2/spec.html#schema_primitive
*/
null () {
return new BaseSchema<null>({ type: 'null' }).copyWith(this)
}
/**
* Primitive type. Corresponds to a binary value
*
* @example { "type": "boolean" }
*
* @reference https://avro.apache.org/docs/1.9.2/spec.html#schema_primitive
*/
boolean () {
return new BaseSchema<boolean>({ type: 'boolean' }).copyWith(this)
}
/**
* Primitive type. Corresponds to 32-bit signed integer
*
* @example { "type": "int" }
*
* @reference https://avro.apache.org/docs/1.9.2/spec.html#schema_primitive
*/
int () {
return new BaseSchema<number>({ type: 'int' }).copyWith(this)
}
/**
* Primitive type. Corresponds to 64-bit signed integer
*
* @example { "type": "long" }
*
* @reference https://avro.apache.org/docs/1.9.2/spec.html#schema_primitive
*/
long () {
return new BaseSchema<number>({ type: 'long' }).copyWith(this)
}
/**
* Primitive type. Corresponds to single precision (32-bit) IEEE 754 floating-point number
*
* @example { "type": "float" }
*
* @reference https://avro.apache.org/docs/1.9.2/spec.html#schema_primitive
*/
float () {
return new BaseSchema<number>({ type: 'float' }).copyWith(this)
}
/**
* Primitive type. Corresponds to double precision (64-bit) IEEE 754 floating-point number
*
* @example { "type": "double" }
*
* @reference https://avro.apache.org/docs/1.9.2/spec.html#schema_primitive
*/
double () {
return new BaseSchema<number>({ type: 'double' }).copyWith(this)
}
/**
* Primitive type. Corresponds to sequence of 8-bit unsigned bytes
*
* @example { "type": "bytes" }
*
* @reference https://avro.apache.org/docs/1.9.2/spec.html#schema_primitive
*/
bytes () {
return new BaseSchema<Buffer>({ type: 'bytes' }).copyWith(this)
}
/**
* Primitive type. Corresponds to unicode character sequence
*
* @example { "type": "string" }
*
* @reference https://avro.apache.org/docs/1.9.2/spec.html#schema_primitive
*/
string () {
return new BaseSchema<string>({ type: 'string' }).copyWith(this)
}
/**
* Complex type. Correspond to record of values keyed by strings
*
* @example { "type": "record", "fields": [ { "name": "value", "type": "long" } ] }
*
* @reference http://avro.apache.org/docs/1.9.2/spec.html#schema_record
*/
record <T extends Record<string, BaseSchema<any, any>>> (record: T) {
const fields: FieldAvroSchema[] = []
for (const name in record) {
const schema = record[name]
fields.push({ ...pick(schema.context)('doc', 'default', 'order', 'aliases'), name, type: schema.valueOf() })
}
return new BaseSchema<{ [K in keyof T]: T[K]['type'] }, RecordAvroSchema>(
{ type: 'record', fields },
['name', 'namespace', 'aliases', 'doc']
).copyWith(this)
}
/**
* Complex type. Correspond to enumerated type of strings
*
* @example { "type": "enum", "symbols": [ "some", "any" ] }
*
* @reference http://avro.apache.org/docs/1.9.2/spec.html#Enums
*/
enum <T extends string[]> (...symbols: T) {
return new BaseSchema<T[number], EnumAvroSchema>(
{ type: 'enum', symbols },
['name', 'namespace', 'aliases', 'default', 'doc']
).copyWith(this)
}
/**
* Complex type. Correspond to array of specified type
*
* @example { "type": "array", "items": { type: "string" } }
*
* @reference http://avro.apache.org/docs/1.9.2/spec.html#Arrays
*/
array <T extends BaseSchema> (items: T) {
return new BaseSchema<T['type'][], ArrayAvroSchema>(
{ type: 'array', items: items.valueOf() }
).copyWith(this)
}
/**
* Complex type. Correspond to map of specified type
*
* @example { "type": "map", "values": { type: "string" } }
*
* @reference http://avro.apache.org/docs/1.9.2/spec.html#Maps
*/
map <T extends BaseSchema> (values: T) {
return new BaseSchema<Record<string, T['type']>, MapAvroSchema>(
{ type: 'map', values: values.valueOf() }
).copyWith(this)
}
/**
* Complex type. Correspond to the given number of bytes per value
*
* @example { "type": "fixed", "size": 16 }
*
* @reference http://avro.apache.org/docs/1.9.2/spec.html#Fixed
*/
fixed (size: number) {
return new BaseSchema<Buffer, FixedAvroSchema>(
{ type: 'fixed', size },
['name', 'namespace', 'aliases']
).copyWith(this)
}
/**
* Complex type. Correspond to the value which may be one of given types
*
* @example ["null", "string"]
*
* @reference http://avro.apache.org/docs/1.9.2/spec.html#Unions
*/
union <T extends BaseSchema[]> (...args: T) {
return new BaseSchema<T[number]['type'], BaseAvroSchema[]>(args.map(arg => arg.valueOf()))
}
}
const A = new SchemaFactory()
export default A