Skip to content

Commit a09db3f

Browse files
Copilotstreamich
andcommitted
Implement Apache Avro schema interface types
Co-authored-by: streamich <[email protected]>
1 parent 4b962df commit a09db3f

File tree

1 file changed

+268
-0
lines changed

1 file changed

+268
-0
lines changed

src/avro/types.ts

Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
/**
2+
* Apache Avro schema type definitions based on Avro 1.12.0 specification.
3+
* Specification: https://avro.apache.org/docs/1.12.0/specification/
4+
*/
5+
6+
// Base schema interface with common properties
7+
export interface AvroBaseSchema {
8+
/** The schema type */
9+
type: string;
10+
/** Optional documentation for the schema */
11+
doc?: string;
12+
/** Optional JSON object of string-valued properties */
13+
[key: string]: any;
14+
}
15+
16+
// Primitive type schemas
17+
export interface AvroNullSchema extends AvroBaseSchema {
18+
type: 'null';
19+
}
20+
21+
export interface AvroBooleanSchema extends AvroBaseSchema {
22+
type: 'boolean';
23+
}
24+
25+
export interface AvroIntSchema extends AvroBaseSchema {
26+
type: 'int';
27+
}
28+
29+
export interface AvroLongSchema extends AvroBaseSchema {
30+
type: 'long';
31+
}
32+
33+
export interface AvroFloatSchema extends AvroBaseSchema {
34+
type: 'float';
35+
}
36+
37+
export interface AvroDoubleSchema extends AvroBaseSchema {
38+
type: 'double';
39+
}
40+
41+
export interface AvroBytesSchema extends AvroBaseSchema {
42+
type: 'bytes';
43+
}
44+
45+
export interface AvroStringSchema extends AvroBaseSchema {
46+
type: 'string';
47+
}
48+
49+
// Complex type schemas
50+
51+
export interface AvroRecordField {
52+
/** Name of the field */
53+
name: string;
54+
/** Schema of the field */
55+
type: AvroSchema;
56+
/** Optional documentation for the field */
57+
doc?: string;
58+
/** Optional default value for the field */
59+
default?: any;
60+
/** Optional ordering for the field */
61+
order?: 'ascending' | 'descending' | 'ignore';
62+
/** Optional aliases for the field */
63+
aliases?: string[];
64+
}
65+
66+
export interface AvroRecordSchema extends AvroBaseSchema {
67+
type: 'record';
68+
/** Name of the record schema */
69+
name: string;
70+
/** Optional namespace for the record */
71+
namespace?: string;
72+
/** Array of field definitions */
73+
fields: AvroRecordField[];
74+
/** Optional aliases for the record */
75+
aliases?: string[];
76+
}
77+
78+
export interface AvroEnumSchema extends AvroBaseSchema {
79+
type: 'enum';
80+
/** Name of the enum schema */
81+
name: string;
82+
/** Optional namespace for the enum */
83+
namespace?: string;
84+
/** Array of symbols in the enum */
85+
symbols: string[];
86+
/** Optional default symbol */
87+
default?: string;
88+
/** Optional aliases for the enum */
89+
aliases?: string[];
90+
}
91+
92+
export interface AvroArraySchema extends AvroBaseSchema {
93+
type: 'array';
94+
/** Schema of the array items */
95+
items: AvroSchema;
96+
}
97+
98+
export interface AvroMapSchema extends AvroBaseSchema {
99+
type: 'map';
100+
/** Schema of the map values */
101+
values: AvroSchema;
102+
}
103+
104+
export interface AvroUnionSchema extends Array<AvroSchema> {
105+
/** Union schemas are represented as JSON arrays */
106+
}
107+
108+
export interface AvroFixedSchema extends AvroBaseSchema {
109+
type: 'fixed';
110+
/** Name of the fixed schema */
111+
name: string;
112+
/** Optional namespace for the fixed */
113+
namespace?: string;
114+
/** Size of the fixed-length data in bytes */
115+
size: number;
116+
/** Optional aliases for the fixed */
117+
aliases?: string[];
118+
}
119+
120+
// Union of all primitive schemas
121+
export type AvroPrimitiveSchema =
122+
| AvroNullSchema
123+
| AvroBooleanSchema
124+
| AvroIntSchema
125+
| AvroLongSchema
126+
| AvroFloatSchema
127+
| AvroDoubleSchema
128+
| AvroBytesSchema
129+
| AvroStringSchema;
130+
131+
// Union of all complex schemas
132+
export type AvroComplexSchema =
133+
| AvroRecordSchema
134+
| AvroEnumSchema
135+
| AvroArraySchema
136+
| AvroMapSchema
137+
| AvroUnionSchema
138+
| AvroFixedSchema;
139+
140+
// Union of all schema types
141+
export type AvroSchema = AvroPrimitiveSchema | AvroComplexSchema | string;
142+
143+
// Named schemas (record, enum, fixed)
144+
export type AvroNamedSchema = AvroRecordSchema | AvroEnumSchema | AvroFixedSchema;
145+
146+
// Logical types - extensions to primitive types
147+
export interface AvroLogicalTypeSchema extends AvroBaseSchema {
148+
/** The logical type name */
149+
logicalType: string;
150+
}
151+
152+
export interface AvroDecimalLogicalType extends AvroLogicalTypeSchema {
153+
logicalType: 'decimal';
154+
/** The maximum number of digits in the decimal */
155+
precision: number;
156+
/** The number of digits to the right of the decimal point */
157+
scale?: number;
158+
}
159+
160+
export interface AvroUuidLogicalType extends AvroStringSchema {
161+
logicalType: 'uuid';
162+
}
163+
164+
export interface AvroDateLogicalType extends AvroIntSchema {
165+
logicalType: 'date';
166+
}
167+
168+
export interface AvroTimeMillisLogicalType extends AvroIntSchema {
169+
logicalType: 'time-millis';
170+
}
171+
172+
export interface AvroTimeMicrosLogicalType extends AvroLongSchema {
173+
logicalType: 'time-micros';
174+
}
175+
176+
export interface AvroTimestampMillisLogicalType extends AvroLongSchema {
177+
logicalType: 'timestamp-millis';
178+
}
179+
180+
export interface AvroTimestampMicrosLogicalType extends AvroLongSchema {
181+
logicalType: 'timestamp-micros';
182+
}
183+
184+
export interface AvroLocalTimestampMillisLogicalType extends AvroLongSchema {
185+
logicalType: 'local-timestamp-millis';
186+
}
187+
188+
export interface AvroLocalTimestampMicrosLogicalType extends AvroLongSchema {
189+
logicalType: 'local-timestamp-micros';
190+
}
191+
192+
export interface AvroDurationLogicalType extends AvroFixedSchema {
193+
logicalType: 'duration';
194+
size: 12;
195+
}
196+
197+
// Type guard functions for schema type checking
198+
export function isAvroNullSchema(schema: AvroSchema): schema is AvroNullSchema {
199+
return typeof schema === 'object' && !Array.isArray(schema) && schema.type === 'null';
200+
}
201+
202+
export function isAvroBooleanSchema(schema: AvroSchema): schema is AvroBooleanSchema {
203+
return typeof schema === 'object' && !Array.isArray(schema) && schema.type === 'boolean';
204+
}
205+
206+
export function isAvroIntSchema(schema: AvroSchema): schema is AvroIntSchema {
207+
return typeof schema === 'object' && !Array.isArray(schema) && schema.type === 'int';
208+
}
209+
210+
export function isAvroLongSchema(schema: AvroSchema): schema is AvroLongSchema {
211+
return typeof schema === 'object' && !Array.isArray(schema) && schema.type === 'long';
212+
}
213+
214+
export function isAvroFloatSchema(schema: AvroSchema): schema is AvroFloatSchema {
215+
return typeof schema === 'object' && !Array.isArray(schema) && schema.type === 'float';
216+
}
217+
218+
export function isAvroDoubleSchema(schema: AvroSchema): schema is AvroDoubleSchema {
219+
return typeof schema === 'object' && !Array.isArray(schema) && schema.type === 'double';
220+
}
221+
222+
export function isAvroBytesSchema(schema: AvroSchema): schema is AvroBytesSchema {
223+
return typeof schema === 'object' && !Array.isArray(schema) && schema.type === 'bytes';
224+
}
225+
226+
export function isAvroStringSchema(schema: AvroSchema): schema is AvroStringSchema {
227+
return typeof schema === 'object' && !Array.isArray(schema) && schema.type === 'string';
228+
}
229+
230+
export function isAvroRecordSchema(schema: AvroSchema): schema is AvroRecordSchema {
231+
return typeof schema === 'object' && !Array.isArray(schema) && schema.type === 'record';
232+
}
233+
234+
export function isAvroEnumSchema(schema: AvroSchema): schema is AvroEnumSchema {
235+
return typeof schema === 'object' && !Array.isArray(schema) && schema.type === 'enum';
236+
}
237+
238+
export function isAvroArraySchema(schema: AvroSchema): schema is AvroArraySchema {
239+
return typeof schema === 'object' && !Array.isArray(schema) && schema.type === 'array';
240+
}
241+
242+
export function isAvroMapSchema(schema: AvroSchema): schema is AvroMapSchema {
243+
return typeof schema === 'object' && !Array.isArray(schema) && schema.type === 'map';
244+
}
245+
246+
export function isAvroUnionSchema(schema: AvroSchema): schema is AvroUnionSchema {
247+
return Array.isArray(schema);
248+
}
249+
250+
export function isAvroFixedSchema(schema: AvroSchema): schema is AvroFixedSchema {
251+
return typeof schema === 'object' && !Array.isArray(schema) && schema.type === 'fixed';
252+
}
253+
254+
export function isAvroPrimitiveSchema(schema: AvroSchema): schema is AvroPrimitiveSchema {
255+
if (typeof schema === 'string') return true;
256+
if (typeof schema !== 'object' || Array.isArray(schema)) return false;
257+
return ['null', 'boolean', 'int', 'long', 'float', 'double', 'bytes', 'string'].includes(schema.type);
258+
}
259+
260+
export function isAvroComplexSchema(schema: AvroSchema): schema is AvroComplexSchema {
261+
if (Array.isArray(schema)) return true;
262+
if (typeof schema !== 'object') return false;
263+
return ['record', 'enum', 'array', 'map', 'fixed'].includes(schema.type);
264+
}
265+
266+
export function isAvroNamedSchema(schema: AvroSchema): schema is AvroNamedSchema {
267+
return typeof schema === 'object' && !Array.isArray(schema) && ['record', 'enum', 'fixed'].includes(schema.type);
268+
}

0 commit comments

Comments
 (0)