@sachitv/avro-typescript 0.4.1 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/avro_reader_sync.d.ts +48 -0
- package/esm/avro_reader_sync.js +79 -0
- package/esm/avro_writer_sync.d.ts +28 -0
- package/esm/avro_writer_sync.js +63 -0
- package/esm/internal/collections/circular_buffer.d.ts +4 -3
- package/esm/internal/collections/circular_buffer.js +8 -6
- package/esm/mod.d.ts +17 -0
- package/esm/mod.js +11 -0
- package/esm/schemas/base_type.d.ts +8 -0
- package/esm/schemas/base_type.js +21 -0
- package/esm/schemas/complex/array_type.d.ts +31 -10
- package/esm/schemas/complex/array_type.js +129 -39
- package/esm/schemas/complex/enum_type.d.ts +24 -7
- package/esm/schemas/complex/enum_type.js +61 -15
- package/esm/schemas/complex/fixed_type.d.ts +27 -12
- package/esm/schemas/complex/fixed_type.js +40 -27
- package/esm/schemas/complex/map_type.d.ts +31 -10
- package/esm/schemas/complex/map_type.js +88 -47
- package/esm/schemas/complex/named_type.d.ts +1 -1
- package/esm/schemas/complex/named_type.js +2 -2
- package/esm/schemas/complex/record_field.d.ts +62 -0
- package/esm/schemas/complex/record_field.js +112 -0
- package/esm/schemas/complex/record_resolver.d.ts +45 -0
- package/esm/schemas/complex/record_resolver.js +92 -0
- package/esm/schemas/complex/record_type.d.ts +45 -70
- package/esm/schemas/complex/record_type.js +158 -213
- package/esm/schemas/complex/record_writer_cache.d.ts +44 -0
- package/esm/schemas/complex/record_writer_cache.js +141 -0
- package/esm/schemas/complex/record_writer_strategy.d.ts +123 -0
- package/esm/schemas/complex/record_writer_strategy.js +309 -0
- package/esm/schemas/complex/union_type.d.ts +23 -11
- package/esm/schemas/complex/union_type.js +77 -29
- package/esm/schemas/logical/decimal_logical_type.d.ts +1 -1
- package/esm/schemas/logical/decimal_logical_type.js +2 -2
- package/esm/schemas/logical/duration_logical_type.d.ts +1 -1
- package/esm/schemas/logical/duration_logical_type.js +2 -2
- package/esm/schemas/logical/logical_type.d.ts +27 -9
- package/esm/schemas/logical/logical_type.js +50 -17
- package/esm/schemas/logical/temporal_logical_types.d.ts +9 -9
- package/esm/schemas/logical/temporal_logical_types.js +18 -18
- package/esm/schemas/logical/uuid_logical_type.d.ts +1 -1
- package/esm/schemas/logical/uuid_logical_type.js +2 -2
- package/esm/schemas/primitive/boolean_type.d.ts +15 -4
- package/esm/schemas/primitive/boolean_type.js +22 -7
- package/esm/schemas/primitive/bytes_type.d.ts +16 -5
- package/esm/schemas/primitive/bytes_type.js +31 -16
- package/esm/schemas/primitive/double_type.d.ts +13 -2
- package/esm/schemas/primitive/double_type.js +34 -5
- package/esm/schemas/primitive/fixed_size_base_type.d.ts +11 -7
- package/esm/schemas/primitive/fixed_size_base_type.js +14 -14
- package/esm/schemas/primitive/float_type.d.ts +11 -2
- package/esm/schemas/primitive/float_type.js +26 -5
- package/esm/schemas/primitive/int_type.d.ts +17 -6
- package/esm/schemas/primitive/int_type.js +26 -17
- package/esm/schemas/primitive/long_type.d.ts +16 -5
- package/esm/schemas/primitive/long_type.js +30 -16
- package/esm/schemas/primitive/null_type.d.ts +15 -4
- package/esm/schemas/primitive/null_type.js +20 -7
- package/esm/schemas/primitive/primitive_type.d.ts +2 -0
- package/esm/schemas/primitive/primitive_type.js +4 -0
- package/esm/schemas/primitive/string_type.d.ts +17 -6
- package/esm/schemas/primitive/string_type.js +33 -18
- package/esm/schemas/resolver.d.ts +6 -0
- package/esm/schemas/type.d.ts +64 -4
- package/esm/schemas/type.js +97 -0
- package/esm/serialization/avro_file_parser_sync.d.ts +34 -0
- package/esm/serialization/avro_file_parser_sync.js +160 -0
- package/esm/serialization/avro_file_writer_sync.d.ts +47 -0
- package/esm/serialization/avro_file_writer_sync.js +211 -0
- package/esm/serialization/buffers/blob_readable_buffer.d.ts +4 -3
- package/esm/serialization/buffers/blob_readable_buffer.js +20 -6
- package/esm/serialization/buffers/buffer.d.ts +6 -1
- package/esm/serialization/buffers/buffer.js +3 -0
- package/esm/serialization/buffers/buffer_error.d.ts +28 -0
- package/esm/serialization/buffers/buffer_error.js +70 -0
- package/esm/serialization/buffers/buffer_sync.d.ts +51 -0
- package/esm/serialization/buffers/buffer_sync.js +4 -0
- package/esm/serialization/buffers/in_memory_buffer.d.ts +1 -0
- package/esm/serialization/buffers/in_memory_buffer.js +7 -5
- package/esm/serialization/buffers/in_memory_buffer_sync.d.ts +133 -0
- package/esm/serialization/buffers/in_memory_buffer_sync.js +259 -0
- package/esm/serialization/counting_writable_tap.d.ts +45 -0
- package/esm/serialization/counting_writable_tap.js +90 -0
- package/esm/serialization/counting_writable_tap_sync.d.ts +46 -0
- package/esm/serialization/counting_writable_tap_sync.js +87 -0
- package/esm/serialization/decoders/decoder_null_sync.d.ts +12 -0
- package/esm/serialization/decoders/decoder_null_sync.js +13 -0
- package/esm/serialization/decoders/decoder_sync.d.ts +15 -0
- package/esm/serialization/decoders/decoder_sync.js +1 -0
- package/esm/serialization/encoders/encoder_null_sync.d.ts +12 -0
- package/esm/serialization/encoders/encoder_null_sync.js +13 -0
- package/esm/serialization/encoders/encoder_sync.d.ts +15 -0
- package/esm/serialization/encoders/encoder_sync.js +1 -0
- package/esm/serialization/streams/fixed_size_stream_readable_buffer_adapter.d.ts +3 -2
- package/esm/serialization/streams/fixed_size_stream_readable_buffer_adapter.js +19 -9
- package/esm/serialization/streams/fixed_size_stream_reader_sync.d.ts +25 -0
- package/esm/serialization/streams/fixed_size_stream_reader_sync.js +63 -0
- package/esm/serialization/streams/fixed_size_stream_writer_sync.d.ts +45 -0
- package/esm/serialization/streams/fixed_size_stream_writer_sync.js +98 -0
- package/esm/serialization/streams/forward_only_stream_readable_buffer_adapter.d.ts +5 -3
- package/esm/serialization/streams/forward_only_stream_readable_buffer_adapter.js +18 -8
- package/esm/serialization/streams/stream_readable_buffer_adapter.d.ts +3 -2
- package/esm/serialization/streams/stream_readable_buffer_adapter.js +19 -6
- package/esm/serialization/streams/stream_readable_buffer_adapter_sync.d.ts +36 -0
- package/esm/serialization/streams/stream_readable_buffer_adapter_sync.js +93 -0
- package/esm/serialization/streams/stream_writable_buffer_adapter_sync.d.ts +45 -0
- package/esm/serialization/streams/stream_writable_buffer_adapter_sync.js +78 -0
- package/esm/serialization/streams/streams_sync.d.ts +36 -0
- package/esm/serialization/streams/streams_sync.js +4 -0
- package/esm/serialization/tap.d.ts +15 -32
- package/esm/serialization/tap.js +45 -134
- package/esm/serialization/tap_sync.d.ts +240 -0
- package/esm/serialization/tap_sync.js +545 -0
- package/esm/serialization/text_encoding.d.ts +16 -0
- package/esm/serialization/text_encoding.js +48 -1
- package/esm/type/create_type.d.ts +20 -0
- package/esm/type/create_type.js +49 -28
- package/package.json +2 -2
- package/esm/serialization/manipulate_bytes.d.ts +0 -6
- package/esm/serialization/manipulate_bytes.js +0 -13
- package/esm/serialization/read_uint_le.d.ts +0 -4
- package/esm/serialization/read_uint_le.js +0 -14
|
@@ -1,14 +1,17 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { ReadableTapLike, WritableTapLike } from "../../serialization/tap.js";
|
|
2
|
+
import type { SyncReadableTapLike, SyncWritableTapLike } from "../../serialization/tap_sync.js";
|
|
2
3
|
import { BaseType } from "../base_type.js";
|
|
3
4
|
import { Resolver } from "../resolver.js";
|
|
4
5
|
import type { JSONType, Type } from "../type.js";
|
|
5
|
-
import {
|
|
6
|
+
import type { ErrorHook } from "../error.js";
|
|
6
7
|
/**
|
|
7
8
|
* Parameters for creating a MapType.
|
|
8
9
|
*/
|
|
9
10
|
export interface MapTypeParams<T> {
|
|
10
11
|
/** The type of values in the map. */
|
|
11
12
|
values: Type<T>;
|
|
13
|
+
/** Whether to validate during writes. Defaults to true. */
|
|
14
|
+
validate?: boolean;
|
|
12
15
|
}
|
|
13
16
|
/**
|
|
14
17
|
* Helper function to read a map from a tap.
|
|
@@ -17,6 +20,13 @@ export interface MapTypeParams<T> {
|
|
|
17
20
|
* @param collect Function to collect each key-value pair.
|
|
18
21
|
*/
|
|
19
22
|
export declare function readMapInto<T>(tap: ReadableTapLike, readValue: (tap: ReadableTapLike) => Promise<T>, collect: (key: string, value: T) => void): Promise<void>;
|
|
23
|
+
/**
|
|
24
|
+
* Synchronous helper function to read a map from a tap.
|
|
25
|
+
* @param tap The tap to read from.
|
|
26
|
+
* @param readValue Function to read a single value.
|
|
27
|
+
* @param collect Function to collect each key-value pair.
|
|
28
|
+
*/
|
|
29
|
+
export declare function readMapIntoSync<T>(tap: SyncReadableTapLike, readValue: (tap: SyncReadableTapLike) => T, collect: (key: string, value: T) => void): void;
|
|
20
30
|
/**
|
|
21
31
|
* Avro `map` type for string-keyed collections of values matching a schema.
|
|
22
32
|
*/
|
|
@@ -36,26 +46,33 @@ export declare class MapType<T = unknown> extends BaseType<Map<string, T>> {
|
|
|
36
46
|
*/
|
|
37
47
|
check(value: unknown, errorHook?: ErrorHook, path?: string[]): boolean;
|
|
38
48
|
/**
|
|
39
|
-
* Writes
|
|
40
|
-
|
|
41
|
-
|
|
49
|
+
* Writes map without validation.
|
|
50
|
+
*/
|
|
51
|
+
writeUnchecked(tap: WritableTapLike, value: Map<string, T>): Promise<void>;
|
|
52
|
+
/**
|
|
53
|
+
* Writes map without validation synchronously.
|
|
42
54
|
*/
|
|
43
|
-
|
|
55
|
+
writeSyncUnchecked(tap: SyncWritableTapLike, value: Map<string, T>): void;
|
|
44
56
|
/**
|
|
45
57
|
* Reads a map value from the provided tap.
|
|
46
58
|
*/
|
|
47
59
|
read(tap: ReadableTapLike): Promise<Map<string, T>>;
|
|
60
|
+
/**
|
|
61
|
+
* Reads the map from a sync tap without asynchronous operations.
|
|
62
|
+
*/
|
|
63
|
+
/**
|
|
64
|
+
* Resolves nested map entries synchronously using the value resolver.
|
|
65
|
+
*/
|
|
66
|
+
readSync(tap: SyncReadableTapLike): Map<string, T>;
|
|
48
67
|
/**
|
|
49
68
|
* Skips over a map value in the tap without reading it.
|
|
50
69
|
* @param tap The readable tap to skip from.
|
|
51
70
|
*/
|
|
52
71
|
skip(tap: ReadableTapLike): Promise<void>;
|
|
53
72
|
/**
|
|
54
|
-
*
|
|
55
|
-
* @param value The map value to serialize.
|
|
56
|
-
* @returns The serialized ArrayBuffer.
|
|
73
|
+
* Skips the encoded map blocks on the sync tap efficiently.
|
|
57
74
|
*/
|
|
58
|
-
|
|
75
|
+
skipSync(tap: SyncReadableTapLike): void;
|
|
59
76
|
/**
|
|
60
77
|
* Creates a deep clone of the map from the given value.
|
|
61
78
|
* @param value The value to clone, which can be a Map or a plain object.
|
|
@@ -88,6 +105,10 @@ export declare class MapType<T = unknown> extends BaseType<Map<string, T>> {
|
|
|
88
105
|
* @throws Always throws an error.
|
|
89
106
|
*/
|
|
90
107
|
match(_tap1: ReadableTapLike, _tap2: ReadableTapLike): Promise<number>;
|
|
108
|
+
/**
|
|
109
|
+
* Raises for maps because they cannot be compared even in sync mode.
|
|
110
|
+
*/
|
|
111
|
+
matchSync(_tap1: SyncReadableTapLike, _tap2: SyncReadableTapLike): number;
|
|
91
112
|
/**
|
|
92
113
|
* Creates a resolver for schema evolution from a writer type to this reader type.
|
|
93
114
|
* @param writerType The writer schema type.
|
|
@@ -10,13 +10,9 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
10
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
12
|
var _MapType_valuesType, _MapResolver_valueResolver;
|
|
13
|
-
import { WritableTap, } from "../../serialization/tap.js";
|
|
14
|
-
import { encode } from "../../serialization/text_encoding.js";
|
|
15
13
|
import { bigIntToSafeNumber } from "../../serialization/conversion.js";
|
|
16
14
|
import { BaseType } from "../base_type.js";
|
|
17
15
|
import { Resolver } from "../resolver.js";
|
|
18
|
-
import { throwInvalidError } from "../error.js";
|
|
19
|
-
import { calculateVarintSize } from "../../internal/varint.js";
|
|
20
16
|
/**
|
|
21
17
|
* Helper function to read a map from a tap.
|
|
22
18
|
* @param tap The tap to read from.
|
|
@@ -41,6 +37,33 @@ export async function readMapInto(tap, readValue, collect) {
|
|
|
41
37
|
}
|
|
42
38
|
}
|
|
43
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* Synchronous helper function to read a map from a tap.
|
|
42
|
+
* @param tap The tap to read from.
|
|
43
|
+
* @param readValue Function to read a single value.
|
|
44
|
+
* @param collect Function to collect each key-value pair.
|
|
45
|
+
*/
|
|
46
|
+
export function readMapIntoSync(tap, readValue, collect) {
|
|
47
|
+
/**
|
|
48
|
+
* Synchronously reads map blocks from the tap and populates the provided map.
|
|
49
|
+
*/
|
|
50
|
+
while (true) {
|
|
51
|
+
let rawCount = tap.readLong();
|
|
52
|
+
if (rawCount === 0n) {
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
if (rawCount < 0n) {
|
|
56
|
+
rawCount = -rawCount;
|
|
57
|
+
tap.skipLong();
|
|
58
|
+
}
|
|
59
|
+
const count = bigIntToSafeNumber(rawCount, "Map block length");
|
|
60
|
+
for (let i = 0; i < count; i++) {
|
|
61
|
+
const key = tap.readString();
|
|
62
|
+
const value = readValue(tap);
|
|
63
|
+
collect(key, value);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
44
67
|
/**
|
|
45
68
|
* Avro `map` type for string-keyed collections of values matching a schema.
|
|
46
69
|
*/
|
|
@@ -50,7 +73,7 @@ export class MapType extends BaseType {
|
|
|
50
73
|
* @param params The map type parameters.
|
|
51
74
|
*/
|
|
52
75
|
constructor(params) {
|
|
53
|
-
super();
|
|
76
|
+
super(params.validate ?? true);
|
|
54
77
|
_MapType_valuesType.set(this, void 0);
|
|
55
78
|
if (!params.values) {
|
|
56
79
|
throw new Error("MapType requires a values type.");
|
|
@@ -95,26 +118,31 @@ export class MapType extends BaseType {
|
|
|
95
118
|
return isValid;
|
|
96
119
|
}
|
|
97
120
|
/**
|
|
98
|
-
* Writes
|
|
99
|
-
* @param tap The writable tap to write to.
|
|
100
|
-
* @param value The map value to write.
|
|
121
|
+
* Writes map without validation.
|
|
101
122
|
*/
|
|
102
|
-
async
|
|
103
|
-
if (!(value instanceof Map)) {
|
|
104
|
-
throwInvalidError([], value, this);
|
|
105
|
-
}
|
|
123
|
+
async writeUnchecked(tap, value) {
|
|
106
124
|
if (value.size > 0) {
|
|
107
125
|
await tap.writeLong(BigInt(value.size));
|
|
108
126
|
for (const [key, entry] of value) {
|
|
109
|
-
if (typeof key !== "string") {
|
|
110
|
-
throwInvalidError([], value, this);
|
|
111
|
-
}
|
|
112
127
|
await tap.writeString(key);
|
|
113
|
-
await __classPrivateFieldGet(this, _MapType_valuesType, "f").
|
|
128
|
+
await __classPrivateFieldGet(this, _MapType_valuesType, "f").writeUnchecked(tap, entry);
|
|
114
129
|
}
|
|
115
130
|
}
|
|
116
131
|
await tap.writeLong(0n);
|
|
117
132
|
}
|
|
133
|
+
/**
|
|
134
|
+
* Writes map without validation synchronously.
|
|
135
|
+
*/
|
|
136
|
+
writeSyncUnchecked(tap, value) {
|
|
137
|
+
if (value.size > 0) {
|
|
138
|
+
tap.writeLong(BigInt(value.size));
|
|
139
|
+
for (const [key, entry] of value) {
|
|
140
|
+
tap.writeString(key);
|
|
141
|
+
__classPrivateFieldGet(this, _MapType_valuesType, "f").writeSyncUnchecked(tap, entry);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
tap.writeLong(0n);
|
|
145
|
+
}
|
|
118
146
|
/**
|
|
119
147
|
* Reads a map value from the provided tap.
|
|
120
148
|
*/
|
|
@@ -125,6 +153,19 @@ export class MapType extends BaseType {
|
|
|
125
153
|
});
|
|
126
154
|
return result;
|
|
127
155
|
}
|
|
156
|
+
/**
|
|
157
|
+
* Reads the map from a sync tap without asynchronous operations.
|
|
158
|
+
*/
|
|
159
|
+
/**
|
|
160
|
+
* Resolves nested map entries synchronously using the value resolver.
|
|
161
|
+
*/
|
|
162
|
+
readSync(tap) {
|
|
163
|
+
const result = new Map();
|
|
164
|
+
readMapIntoSync(tap, (innerTap) => __classPrivateFieldGet(this, _MapType_valuesType, "f").readSync(innerTap), (key, value) => {
|
|
165
|
+
result.set(key, value);
|
|
166
|
+
});
|
|
167
|
+
return result;
|
|
168
|
+
}
|
|
128
169
|
/**
|
|
129
170
|
* Skips over a map value in the tap without reading it.
|
|
130
171
|
* @param tap The readable tap to skip from.
|
|
@@ -151,41 +192,28 @@ export class MapType extends BaseType {
|
|
|
151
192
|
}
|
|
152
193
|
}
|
|
153
194
|
/**
|
|
154
|
-
*
|
|
155
|
-
* @param value The map value to serialize.
|
|
156
|
-
* @returns The serialized ArrayBuffer.
|
|
195
|
+
* Skips the encoded map blocks on the sync tap efficiently.
|
|
157
196
|
*/
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
let totalSize = 1; // final zero block terminator
|
|
164
|
-
for (const [key, entry] of value.entries()) {
|
|
165
|
-
if (typeof key !== "string") {
|
|
166
|
-
throwInvalidError([], value, this);
|
|
197
|
+
skipSync(tap) {
|
|
198
|
+
while (true) {
|
|
199
|
+
const rawCount = tap.readLong();
|
|
200
|
+
if (rawCount === 0n) {
|
|
201
|
+
break;
|
|
167
202
|
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
await tap.writeLong(BigInt(serializedEntries.length));
|
|
181
|
-
for (const { keyBytes, valueBytes } of serializedEntries) {
|
|
182
|
-
await tap.writeLong(BigInt(keyBytes.length));
|
|
183
|
-
await tap.writeFixed(keyBytes);
|
|
184
|
-
await tap.writeFixed(valueBytes);
|
|
203
|
+
if (rawCount < 0n) {
|
|
204
|
+
const blockSize = tap.readLong();
|
|
205
|
+
const size = bigIntToSafeNumber(blockSize, "Map block size");
|
|
206
|
+
if (size > 0) {
|
|
207
|
+
tap.skipFixed(size);
|
|
208
|
+
}
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
const count = bigIntToSafeNumber(rawCount, "Map block length");
|
|
212
|
+
for (let i = 0; i < count; i++) {
|
|
213
|
+
tap.skipString();
|
|
214
|
+
__classPrivateFieldGet(this, _MapType_valuesType, "f").skipSync(tap);
|
|
185
215
|
}
|
|
186
216
|
}
|
|
187
|
-
await tap.writeLong(0n);
|
|
188
|
-
return buffer;
|
|
189
217
|
}
|
|
190
218
|
/**
|
|
191
219
|
* Creates a deep clone of the map from the given value.
|
|
@@ -256,6 +284,12 @@ export class MapType extends BaseType {
|
|
|
256
284
|
async match(_tap1, _tap2) {
|
|
257
285
|
throw new Error("maps cannot be compared");
|
|
258
286
|
}
|
|
287
|
+
/**
|
|
288
|
+
* Raises for maps because they cannot be compared even in sync mode.
|
|
289
|
+
*/
|
|
290
|
+
matchSync(_tap1, _tap2) {
|
|
291
|
+
throw new Error("maps cannot be compared");
|
|
292
|
+
}
|
|
259
293
|
/**
|
|
260
294
|
* Creates a resolver for schema evolution from a writer type to this reader type.
|
|
261
295
|
* @param writerType The writer schema type.
|
|
@@ -286,5 +320,12 @@ class MapResolver extends Resolver {
|
|
|
286
320
|
});
|
|
287
321
|
return result;
|
|
288
322
|
}
|
|
323
|
+
readSync(tap) {
|
|
324
|
+
const result = new Map();
|
|
325
|
+
readMapIntoSync(tap, (innerTap) => __classPrivateFieldGet(this, _MapResolver_valueResolver, "f").readSync(innerTap), (key, value) => {
|
|
326
|
+
result.set(key, value);
|
|
327
|
+
});
|
|
328
|
+
return result;
|
|
329
|
+
}
|
|
289
330
|
}
|
|
290
331
|
_MapResolver_valueResolver = new WeakMap();
|
|
@@ -10,7 +10,7 @@ export declare abstract class NamedType<T = unknown> extends BaseType<T> {
|
|
|
10
10
|
* Creates a new NamedType.
|
|
11
11
|
* @param resolvedNames The resolved names (full name, namespace, aliases).
|
|
12
12
|
*/
|
|
13
|
-
protected constructor(resolvedNames: ResolvedNames);
|
|
13
|
+
protected constructor(resolvedNames: ResolvedNames, validate?: boolean);
|
|
14
14
|
/**
|
|
15
15
|
* Fully qualified schema name.
|
|
16
16
|
*/
|
|
@@ -20,8 +20,8 @@ export class NamedType extends BaseType {
|
|
|
20
20
|
* Creates a new NamedType.
|
|
21
21
|
* @param resolvedNames The resolved names (full name, namespace, aliases).
|
|
22
22
|
*/
|
|
23
|
-
constructor(resolvedNames) {
|
|
24
|
-
super();
|
|
23
|
+
constructor(resolvedNames, validate = true) {
|
|
24
|
+
super(validate);
|
|
25
25
|
_NamedType_fullName.set(this, void 0);
|
|
26
26
|
_NamedType_namespace.set(this, void 0);
|
|
27
27
|
_NamedType_aliases.set(this, void 0);
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { Type } from "../type.js";
|
|
2
|
+
/**
|
|
3
|
+
* Specifies the sort order for record fields.
|
|
4
|
+
*/
|
|
5
|
+
export type RecordFieldOrder = "ascending" | "descending" | "ignore";
|
|
6
|
+
/**
|
|
7
|
+
* Parameters for defining a record field.
|
|
8
|
+
*/
|
|
9
|
+
export interface RecordFieldParams {
|
|
10
|
+
/** The name of the field. */
|
|
11
|
+
name: string;
|
|
12
|
+
/** The type of the field. */
|
|
13
|
+
type: Type;
|
|
14
|
+
/** Optional aliases for the field. */
|
|
15
|
+
aliases?: string[];
|
|
16
|
+
/** Optional ordering for the field. */
|
|
17
|
+
order?: RecordFieldOrder;
|
|
18
|
+
/** Optional default value for the field. */
|
|
19
|
+
default?: unknown;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Represents a field in an Avro record type.
|
|
23
|
+
*/
|
|
24
|
+
export declare class RecordField {
|
|
25
|
+
#private;
|
|
26
|
+
/**
|
|
27
|
+
* Constructs a new RecordField instance.
|
|
28
|
+
*/
|
|
29
|
+
constructor(params: RecordFieldParams);
|
|
30
|
+
/**
|
|
31
|
+
* Gets the name of the field.
|
|
32
|
+
*/
|
|
33
|
+
getName(): string;
|
|
34
|
+
/**
|
|
35
|
+
* Gets the type of the field.
|
|
36
|
+
*/
|
|
37
|
+
getType(): Type;
|
|
38
|
+
/**
|
|
39
|
+
* Gets the aliases of the field.
|
|
40
|
+
*/
|
|
41
|
+
getAliases(): string[];
|
|
42
|
+
/**
|
|
43
|
+
* Gets the order of the field.
|
|
44
|
+
*/
|
|
45
|
+
getOrder(): RecordFieldOrder;
|
|
46
|
+
/**
|
|
47
|
+
* Returns true if the field has a default value.
|
|
48
|
+
*/
|
|
49
|
+
hasDefault(): boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Gets the default value for the field.
|
|
52
|
+
* @returns The default value.
|
|
53
|
+
* @throws Error if the field has no default.
|
|
54
|
+
*/
|
|
55
|
+
getDefault(): unknown;
|
|
56
|
+
/**
|
|
57
|
+
* Checks if the given name matches the field name or any of its aliases.
|
|
58
|
+
* @param name The name to check.
|
|
59
|
+
* @returns True if the name matches, false otherwise.
|
|
60
|
+
*/
|
|
61
|
+
nameMatches(name: string): boolean;
|
|
62
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
2
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
3
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
4
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
5
|
+
};
|
|
6
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
7
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
10
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
11
|
+
};
|
|
12
|
+
var _a, _RecordField_name, _RecordField_type, _RecordField_aliases, _RecordField_order, _RecordField_hasDefault, _RecordField_defaultValue, _RecordField_isValidOrder;
|
|
13
|
+
import { isValidName } from "./resolve_names.js";
|
|
14
|
+
import { Type } from "../type.js";
|
|
15
|
+
/**
|
|
16
|
+
* Represents a field in an Avro record type.
|
|
17
|
+
*/
|
|
18
|
+
export class RecordField {
|
|
19
|
+
/**
|
|
20
|
+
* Constructs a new RecordField instance.
|
|
21
|
+
*/
|
|
22
|
+
constructor(params) {
|
|
23
|
+
_RecordField_name.set(this, void 0);
|
|
24
|
+
_RecordField_type.set(this, void 0);
|
|
25
|
+
_RecordField_aliases.set(this, void 0);
|
|
26
|
+
_RecordField_order.set(this, void 0);
|
|
27
|
+
_RecordField_hasDefault.set(this, void 0);
|
|
28
|
+
_RecordField_defaultValue.set(this, void 0);
|
|
29
|
+
const { name, type, aliases = [], order = "ascending" } = params;
|
|
30
|
+
if (typeof name !== "string" || !isValidName(name)) {
|
|
31
|
+
throw new Error(`Invalid record field name: ${name}`);
|
|
32
|
+
}
|
|
33
|
+
if (!(type instanceof Type)) {
|
|
34
|
+
throw new Error(`Invalid field type for ${name}`);
|
|
35
|
+
}
|
|
36
|
+
if (!__classPrivateFieldGet(_a, _a, "m", _RecordField_isValidOrder).call(_a, order)) {
|
|
37
|
+
throw new Error(`Invalid record field order: ${order}`);
|
|
38
|
+
}
|
|
39
|
+
__classPrivateFieldSet(this, _RecordField_name, name, "f");
|
|
40
|
+
__classPrivateFieldSet(this, _RecordField_type, type, "f");
|
|
41
|
+
__classPrivateFieldSet(this, _RecordField_order, order, "f");
|
|
42
|
+
const aliasSet = new Set();
|
|
43
|
+
const resolvedAliases = [];
|
|
44
|
+
for (const alias of aliases) {
|
|
45
|
+
if (typeof alias !== "string" || !isValidName(alias)) {
|
|
46
|
+
throw new Error(`Invalid record field alias: ${alias}`);
|
|
47
|
+
}
|
|
48
|
+
if (!aliasSet.has(alias)) {
|
|
49
|
+
aliasSet.add(alias);
|
|
50
|
+
resolvedAliases.push(alias);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
__classPrivateFieldSet(this, _RecordField_aliases, resolvedAliases, "f");
|
|
54
|
+
__classPrivateFieldSet(this, _RecordField_hasDefault, Object.prototype.hasOwnProperty.call(params, "default"), "f");
|
|
55
|
+
if (__classPrivateFieldGet(this, _RecordField_hasDefault, "f")) {
|
|
56
|
+
__classPrivateFieldSet(this, _RecordField_defaultValue, __classPrivateFieldGet(this, _RecordField_type, "f").cloneFromValue(params.default), "f");
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Gets the name of the field.
|
|
61
|
+
*/
|
|
62
|
+
getName() {
|
|
63
|
+
return __classPrivateFieldGet(this, _RecordField_name, "f");
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Gets the type of the field.
|
|
67
|
+
*/
|
|
68
|
+
getType() {
|
|
69
|
+
return __classPrivateFieldGet(this, _RecordField_type, "f");
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Gets the aliases of the field.
|
|
73
|
+
*/
|
|
74
|
+
getAliases() {
|
|
75
|
+
return __classPrivateFieldGet(this, _RecordField_aliases, "f").slice();
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Gets the order of the field.
|
|
79
|
+
*/
|
|
80
|
+
getOrder() {
|
|
81
|
+
return __classPrivateFieldGet(this, _RecordField_order, "f");
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Returns true if the field has a default value.
|
|
85
|
+
*/
|
|
86
|
+
hasDefault() {
|
|
87
|
+
return __classPrivateFieldGet(this, _RecordField_hasDefault, "f");
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Gets the default value for the field.
|
|
91
|
+
* @returns The default value.
|
|
92
|
+
* @throws Error if the field has no default.
|
|
93
|
+
*/
|
|
94
|
+
getDefault() {
|
|
95
|
+
if (!__classPrivateFieldGet(this, _RecordField_hasDefault, "f")) {
|
|
96
|
+
throw new Error(`Field '${__classPrivateFieldGet(this, _RecordField_name, "f")}' has no default.`);
|
|
97
|
+
}
|
|
98
|
+
return __classPrivateFieldGet(this, _RecordField_type, "f").cloneFromValue(__classPrivateFieldGet(this, _RecordField_defaultValue, "f"));
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Checks if the given name matches the field name or any of its aliases.
|
|
102
|
+
* @param name The name to check.
|
|
103
|
+
* @returns True if the name matches, false otherwise.
|
|
104
|
+
*/
|
|
105
|
+
nameMatches(name) {
|
|
106
|
+
return name === __classPrivateFieldGet(this, _RecordField_name, "f") || __classPrivateFieldGet(this, _RecordField_aliases, "f").includes(name);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
_a = RecordField, _RecordField_name = new WeakMap(), _RecordField_type = new WeakMap(), _RecordField_aliases = new WeakMap(), _RecordField_order = new WeakMap(), _RecordField_hasDefault = new WeakMap(), _RecordField_defaultValue = new WeakMap(), _RecordField_isValidOrder = function _RecordField_isValidOrder(order) {
|
|
110
|
+
return order === "ascending" || order === "descending" ||
|
|
111
|
+
order === "ignore";
|
|
112
|
+
};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { ReadableTapLike } from "../../serialization/tap.js";
|
|
2
|
+
import type { SyncReadableTapLike } from "../../serialization/tap_sync.js";
|
|
3
|
+
import { Resolver } from "../resolver.js";
|
|
4
|
+
import type { RecordField } from "./record_field.js";
|
|
5
|
+
import type { RecordType } from "./record_type.js";
|
|
6
|
+
/**
|
|
7
|
+
* Mapping between writer and reader fields during schema evolution.
|
|
8
|
+
*/
|
|
9
|
+
export interface FieldMapping {
|
|
10
|
+
/** Index in the reader's field array, or -1 if the field should be skipped. */
|
|
11
|
+
readerIndex: number;
|
|
12
|
+
/** The writer field being mapped. */
|
|
13
|
+
writerField: RecordField;
|
|
14
|
+
/** Optional resolver for type promotion between writer and reader field types. */
|
|
15
|
+
resolver?: Resolver;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Resolver for record schema evolution.
|
|
19
|
+
*
|
|
20
|
+
* Handles reading data written with a writer schema into a reader schema,
|
|
21
|
+
* mapping fields by name/alias, skipping removed fields, and applying
|
|
22
|
+
* default values for new fields.
|
|
23
|
+
*/
|
|
24
|
+
export declare class RecordResolver extends Resolver<Record<string, unknown>> {
|
|
25
|
+
#private;
|
|
26
|
+
/**
|
|
27
|
+
* Creates a new RecordResolver.
|
|
28
|
+
* @param reader The reader record type.
|
|
29
|
+
* @param mappings Field mappings from writer to reader.
|
|
30
|
+
* @param readerFields The reader's field definitions.
|
|
31
|
+
*/
|
|
32
|
+
constructor(reader: RecordType, mappings: FieldMapping[], readerFields: RecordField[]);
|
|
33
|
+
/**
|
|
34
|
+
* Reads a record from the tap using the resolution mappings.
|
|
35
|
+
* @param tap The tap to read from.
|
|
36
|
+
* @returns The resolved record value.
|
|
37
|
+
*/
|
|
38
|
+
read(tap: ReadableTapLike): Promise<Record<string, unknown>>;
|
|
39
|
+
/**
|
|
40
|
+
* Resolves incoming data synchronously, applying missing defaults as needed.
|
|
41
|
+
* @param tap The tap to read from.
|
|
42
|
+
* @returns The resolved record value.
|
|
43
|
+
*/
|
|
44
|
+
readSync(tap: SyncReadableTapLike): Record<string, unknown>;
|
|
45
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _RecordResolver_mappings, _RecordResolver_readerFields;
|
|
13
|
+
import { Resolver } from "../resolver.js";
|
|
14
|
+
/**
|
|
15
|
+
* Resolver for record schema evolution.
|
|
16
|
+
*
|
|
17
|
+
* Handles reading data written with a writer schema into a reader schema,
|
|
18
|
+
* mapping fields by name/alias, skipping removed fields, and applying
|
|
19
|
+
* default values for new fields.
|
|
20
|
+
*/
|
|
21
|
+
export class RecordResolver extends Resolver {
|
|
22
|
+
/**
|
|
23
|
+
* Creates a new RecordResolver.
|
|
24
|
+
* @param reader The reader record type.
|
|
25
|
+
* @param mappings Field mappings from writer to reader.
|
|
26
|
+
* @param readerFields The reader's field definitions.
|
|
27
|
+
*/
|
|
28
|
+
constructor(reader, mappings, readerFields) {
|
|
29
|
+
super(reader);
|
|
30
|
+
_RecordResolver_mappings.set(this, void 0);
|
|
31
|
+
_RecordResolver_readerFields.set(this, void 0);
|
|
32
|
+
__classPrivateFieldSet(this, _RecordResolver_mappings, mappings, "f");
|
|
33
|
+
__classPrivateFieldSet(this, _RecordResolver_readerFields, readerFields, "f");
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Reads a record from the tap using the resolution mappings.
|
|
37
|
+
* @param tap The tap to read from.
|
|
38
|
+
* @returns The resolved record value.
|
|
39
|
+
*/
|
|
40
|
+
async read(tap) {
|
|
41
|
+
const result = {};
|
|
42
|
+
const seen = new Array(__classPrivateFieldGet(this, _RecordResolver_readerFields, "f").length).fill(false);
|
|
43
|
+
for (const mapping of __classPrivateFieldGet(this, _RecordResolver_mappings, "f")) {
|
|
44
|
+
if (mapping.readerIndex === -1) {
|
|
45
|
+
await mapping.writerField.getType().skip(tap);
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
const value = mapping.resolver
|
|
49
|
+
? await mapping.resolver.read(tap)
|
|
50
|
+
: await mapping.writerField.getType().read(tap);
|
|
51
|
+
const readerField = __classPrivateFieldGet(this, _RecordResolver_readerFields, "f")[mapping.readerIndex];
|
|
52
|
+
result[readerField.getName()] = value;
|
|
53
|
+
seen[mapping.readerIndex] = true;
|
|
54
|
+
}
|
|
55
|
+
for (let i = 0; i < __classPrivateFieldGet(this, _RecordResolver_readerFields, "f").length; i++) {
|
|
56
|
+
if (!seen[i]) {
|
|
57
|
+
const field = __classPrivateFieldGet(this, _RecordResolver_readerFields, "f")[i];
|
|
58
|
+
result[field.getName()] = field.getDefault();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return result;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Resolves incoming data synchronously, applying missing defaults as needed.
|
|
65
|
+
* @param tap The tap to read from.
|
|
66
|
+
* @returns The resolved record value.
|
|
67
|
+
*/
|
|
68
|
+
readSync(tap) {
|
|
69
|
+
const result = {};
|
|
70
|
+
const seen = new Array(__classPrivateFieldGet(this, _RecordResolver_readerFields, "f").length).fill(false);
|
|
71
|
+
for (const mapping of __classPrivateFieldGet(this, _RecordResolver_mappings, "f")) {
|
|
72
|
+
if (mapping.readerIndex === -1) {
|
|
73
|
+
mapping.writerField.getType().skipSync(tap);
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
const value = mapping.resolver
|
|
77
|
+
? mapping.resolver.readSync(tap)
|
|
78
|
+
: mapping.writerField.getType().readSync(tap);
|
|
79
|
+
const readerField = __classPrivateFieldGet(this, _RecordResolver_readerFields, "f")[mapping.readerIndex];
|
|
80
|
+
result[readerField.getName()] = value;
|
|
81
|
+
seen[mapping.readerIndex] = true;
|
|
82
|
+
}
|
|
83
|
+
for (let i = 0; i < __classPrivateFieldGet(this, _RecordResolver_readerFields, "f").length; i++) {
|
|
84
|
+
if (!seen[i]) {
|
|
85
|
+
const field = __classPrivateFieldGet(this, _RecordResolver_readerFields, "f")[i];
|
|
86
|
+
result[field.getName()] = field.getDefault();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return result;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
_RecordResolver_mappings = new WeakMap(), _RecordResolver_readerFields = new WeakMap();
|