Skip to content

Commit c67c276

Browse files
committed
feat: 🎸 add readAny() to decoder interface
1 parent 2530816 commit c67c276

16 files changed

+82
-65
lines changed

src/__bench__/bench.shallow-read.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ const benchmark = {
8282
const decoder = new MsgPackDecoder();
8383
return () => {
8484
decoder.reader.reset(doc);
85-
return decoder.findIndex(5).findKey('value').findKey('json').findKey('tags').findIndex(1).val();
85+
return decoder.findIndex(5).findKey('value').findKey('json').findKey('tags').findIndex(1).readAny();
8686
};
8787
},
8888
},
@@ -94,7 +94,7 @@ const benchmark = {
9494
const decoder = new MsgPackDecoder();
9595
return () => {
9696
decoder.reader.reset(doc);
97-
return decoder.find([5, 'value', 'json', 'tags', 1]).val();
97+
return decoder.find([5, 'value', 'json', 'tags', 1]).readAny();
9898
};
9999
},
100100
},
@@ -108,7 +108,7 @@ const benchmark = {
108108
return () => {
109109
decoder.reader.reset(doc);
110110
fn(decoder);
111-
return decoder.val();
111+
return decoder.readAny();
112112
};
113113
},
114114
},

src/__demos__/cbor.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ console.log(decoded);
4242
console.log('--------------------------------------------------');
4343
console.log('Retrieving values without parsing:');
4444
decoder.reader.reset(encoded);
45-
const id = decoder.find(['id']).val();
45+
const id = decoder.find(['id']).readAny();
4646
decoder.reader.reset(encoded);
47-
const foo = decoder.find(['foo']).val();
47+
const foo = decoder.find(['foo']).readAny();
4848
decoder.reader.reset(encoded);
49-
const secondTag = decoder.find(['tags', 1]).val();
49+
const secondTag = decoder.find(['tags', 1]).readAny();
5050
decoder.reader.reset(encoded);
51-
const nested = decoder.find(['nested', 'level2', 'c']).val();
51+
const nested = decoder.find(['nested', 'level2', 'c']).readAny();
5252
console.log('id:', id, 'foo:', foo, 'secondTag:', secondTag, 'nested:', nested);
5353

5454
console.log('--------------------------------------------------');

src/__demos__/msgpack.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ console.log(encoded);
3535
console.log('--------------------------------------------------');
3636
console.log('Retrieving values without parsing:');
3737
decoder.reader.reset(encoded);
38-
const id = decoder.find(['id']).val();
38+
const id = decoder.find(['id']).readAny();
3939
decoder.reader.reset(encoded);
40-
const foo = decoder.find(['foo']).val();
40+
const foo = decoder.find(['foo']).readAny();
4141
decoder.reader.reset(encoded);
42-
const secondTag = decoder.find(['tags', 1]).val();
42+
const secondTag = decoder.find(['tags', 1]).readAny();
4343
decoder.reader.reset(encoded);
44-
const nested = decoder.find(['nested', 'level2', 'c']).val();
44+
const nested = decoder.find(['nested', 'level2', 'c']).readAny();
4545
console.log('id:', id, 'foo:', foo, 'secondTag:', secondTag, 'nested:', nested);

src/bson/BsonDecoder.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ export class BsonDecoder implements BinaryJsonDecoder {
2929
return this.readDocument();
3030
}
3131

32+
public readAny(): unknown {
33+
return this.readDocument();
34+
}
35+
3236
public readDocument(): Record<string, unknown> {
3337
const reader = this.reader;
3438
const documentSize = reader.view.getInt32(reader.x, true); // true = little-endian

src/cbor/CborDecoder.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ export class CborDecoder<
3030
public readMapRaw(length: number): Map<unknown, unknown> {
3131
const map: Map<unknown, unknown> = new Map();
3232
for (let i = 0; i < length; i++) {
33-
const key = this.val();
34-
const value = this.val();
33+
const key = this.readAny();
34+
const value = this.readAny();
3535
map.set(key, value);
3636
}
3737
return map;
@@ -40,9 +40,9 @@ export class CborDecoder<
4040
public readMapIndef(): Map<unknown, unknown> {
4141
const map: Map<unknown, unknown> = new Map();
4242
while (this.reader.peak() !== CONST.END) {
43-
const key = this.val();
43+
const key = this.readAny();
4444
if (this.reader.peak() === CONST.END) throw ERROR.UNEXPECTED_OBJ_BREAK;
45-
const value = this.val();
45+
const value = this.readAny();
4646
map.set(key, value);
4747
}
4848
this.reader.x++;
@@ -282,7 +282,7 @@ export class CborDecoder<
282282
case MAJOR.MAP:
283283
return this.readAsValue();
284284
default:
285-
return this.val();
285+
return this.readAny();
286286
}
287287
}
288288

src/cbor/CborDecoderBase.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,21 @@ export class CborDecoderBase<R extends IReader & IReaderResettable = IReader & I
1818

1919
public read(uint8: Uint8Array): PackValue {
2020
this.reader.reset(uint8);
21-
return this.val() as PackValue;
21+
return this.readAny() as PackValue;
2222
}
2323

2424
public decode(uint8: Uint8Array): unknown {
2525
this.reader.reset(uint8);
26-
return this.val();
26+
return this.readAny();
2727
}
2828

2929
// -------------------------------------------------------- Any value reading
3030

3131
public val(): unknown {
32+
return this.readAny();
33+
}
34+
35+
public readAny(): unknown {
3236
const reader = this.reader;
3337
const octet = reader.u8();
3438
const major = octet >> 5;
@@ -218,13 +222,13 @@ export class CborDecoderBase<R extends IReader & IReaderResettable = IReader & I
218222

219223
public readArrRaw(length: number): unknown[] {
220224
const arr: unknown[] = [];
221-
for (let i = 0; i < length; i++) arr.push(this.val());
225+
for (let i = 0; i < length; i++) arr.push(this.readAny());
222226
return arr;
223227
}
224228

225229
public readArrIndef(): unknown[] {
226230
const arr: unknown[] = [];
227-
while (this.reader.peak() !== CONST.END) arr.push(this.val());
231+
while (this.reader.peak() !== CONST.END) arr.push(this.readAny());
228232
this.reader.x++;
229233
return arr;
230234
}
@@ -252,7 +256,7 @@ export class CborDecoderBase<R extends IReader & IReaderResettable = IReader & I
252256
for (let i = 0; i < length; i++) {
253257
const key = this.key();
254258
if (key === '__proto__') throw ERROR.UNEXPECTED_OBJ_KEY;
255-
const value = this.val();
259+
const value = this.readAny();
256260
obj[key] = value;
257261
}
258262
return obj;
@@ -265,7 +269,7 @@ export class CborDecoderBase<R extends IReader & IReaderResettable = IReader & I
265269
const obj: Record<string, unknown> = {};
266270
for (let i = 0; i < length; i++) {
267271
const key = this.key();
268-
const value = this.val();
272+
const value = this.readAny();
269273
obj[key] = value;
270274
}
271275
return obj;
@@ -276,7 +280,7 @@ export class CborDecoderBase<R extends IReader & IReaderResettable = IReader & I
276280
while (this.reader.peak() !== CONST.END) {
277281
const key = this.key();
278282
if (this.reader.peak() === CONST.END) throw ERROR.UNEXPECTED_OBJ_BREAK;
279-
const value = this.val();
283+
const value = this.readAny();
280284
obj[key] = value;
281285
}
282286
this.reader.x++;
@@ -314,7 +318,7 @@ export class CborDecoderBase<R extends IReader & IReaderResettable = IReader & I
314318
}
315319

316320
public readTagRaw(tag: number): JsonPackExtension<unknown> | unknown {
317-
return new JsonPackExtension<unknown>(tag, this.val());
321+
return new JsonPackExtension<unknown>(tag, this.readAny());
318322
}
319323

320324
// ------------------------------------------------------------ Token reading

src/cbor/CborDecoderDag.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {CborDecoder} from './CborDecoder';
33

44
export class CborDecoderDag extends CborDecoder {
55
public readTagRaw(tag: number): JsonPackExtension<unknown> | unknown {
6-
const value = this.val();
6+
const value = this.readAny();
77
return tag === 42 ? new JsonPackExtension<unknown>(tag, value) : value;
88
}
99
}

src/cbor/__tests__/CborDecoder.shallow-reading.spec.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,21 @@ describe('shallow reading values, without parsing the document', () => {
5656
test('can find object key', () => {
5757
const encoded = encoder.encode({foo: 'bar'});
5858
decoder.reader.reset(encoded);
59-
const decoded = decoder.findKey('foo').val();
59+
const decoded = decoder.findKey('foo').readAny();
6060
expect(decoded).toBe('bar');
6161
});
6262

6363
test('can find object key in the middle of the object', () => {
6464
const encoded = encoder.encode({x: 123, y: 0, z: -1});
6565
decoder.reader.reset(encoded);
66-
const decoded = decoder.findKey('y').val();
66+
const decoded = decoder.findKey('y').readAny();
6767
expect(decoded).toBe(0);
6868
});
6969

7070
test('can find object key at the end of the object', () => {
7171
const encoded = encoder.encode({x: 123, y: 0, z: -1});
7272
decoder.reader.reset(encoded);
73-
const decoded = decoder.findKey('z').val();
73+
const decoded = decoder.findKey('z').readAny();
7474
expect(decoded).toBe(-1);
7575
});
7676
});
@@ -108,34 +108,34 @@ describe('shallow reading values, without parsing the document', () => {
108108
test('can find value at beginning of array', () => {
109109
const encoded = encoder.encode(['foobar']);
110110
decoder.reader.reset(encoded);
111-
const decoded = decoder.findIndex(0).val();
111+
const decoded = decoder.findIndex(0).readAny();
112112
expect(decoded).toBe('foobar');
113113
});
114114

115115
test('can find value in the middle of array', () => {
116116
const encoded = encoder.encode([1, 2, 3]);
117117
decoder.reader.reset(encoded);
118-
const decoded = decoder.findIndex(1).val();
118+
const decoded = decoder.findIndex(1).readAny();
119119
expect(decoded).toBe(2);
120120
});
121121

122122
test('can find value at the end of array', () => {
123123
const encoded = encoder.encode([1, 2, 3]);
124124
decoder.reader.reset(encoded);
125-
const decoded = decoder.findIndex(2).val();
125+
const decoded = decoder.findIndex(2).readAny();
126126
expect(decoded).toBe(3);
127127
});
128128

129129
test('throws if array index is out of bounds', () => {
130130
const encoded = encoder.encode([1, 2, 3]);
131131
decoder.reader.reset(encoded);
132-
expect(() => decoder.findIndex(3).val()).toThrowError();
132+
expect(() => decoder.findIndex(3).readAny()).toThrowError();
133133
});
134134

135135
test('throws when reading value from an empty array', () => {
136136
const encoded = encoder.encode([]);
137137
decoder.reader.reset(encoded);
138-
expect(() => decoder.findIndex(0).val()).toThrowError();
138+
expect(() => decoder.findIndex(0).readAny()).toThrowError();
139139
});
140140
});
141141

@@ -160,23 +160,23 @@ describe('shallow reading values, without parsing the document', () => {
160160
const encoded = encoder.encode(doc);
161161

162162
decoder.reader.reset(encoded);
163-
const decoded1 = decoder.findKey('a').findKey('b').findKey('c').findKey('d').findKey('e').val();
163+
const decoded1 = decoder.findKey('a').findKey('b').findKey('c').findKey('d').findKey('e').readAny();
164164
expect(decoded1).toStrictEqual([1, 2, 3]);
165165

166166
decoder.reader.reset(encoded);
167-
const decoded2 = decoder.findKey('a').findKey('b').findKey('c').findKey('d').findKey('e').findIndex(1).val();
167+
const decoded2 = decoder.findKey('a').findKey('b').findKey('c').findKey('d').findKey('e').findIndex(1).readAny();
168168
expect(decoded2).toBe(2);
169169

170170
decoder.reader.reset(encoded);
171-
const decoded3 = decoder.findKey('a').findKey('b').findKey('c').findKey('hmm').findIndex(0).findKey('foo').val();
171+
const decoded3 = decoder.findKey('a').findKey('b').findKey('c').findKey('hmm').findIndex(0).findKey('foo').readAny();
172172
expect(decoded3).toBe('bar');
173173
});
174174

175175
describe('.find()', () => {
176176
test('can find deeply nested value', () => {
177177
const encoded = encoder.encode(doc);
178178
decoder.reader.reset(encoded);
179-
const decoded1 = decoder.find(['a', 'b', 'c', 'd', 'e', 1]).val();
179+
const decoded1 = decoder.find(['a', 'b', 'c', 'd', 'e', 1]).readAny();
180180
expect(decoded1).toStrictEqual(2);
181181
});
182182
});

src/cbor/__tests__/CborEncoderDag.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ describe('only extension = 42 is permitted', () => {
6767

6868
class IpfsCborDecoder extends CborDecoderDag {
6969
public readTagRaw(tag: number): CID | unknown {
70-
if (tag === 42) return new CID(this.val() as any);
70+
if (tag === 42) return new CID(this.readAny() as any);
7171
throw new Error('UNKNOWN_TAG');
7272
}
7373
}

src/msgpack/MsgPackDecoder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export class MsgPackDecoder extends MsgPackDecoderFast<Reader> {
133133
for (let i = 0; i < size; i++) arr.push(this.primitive());
134134
return arr;
135135
}
136-
return this.val();
136+
return this.readAny();
137137
}
138138

139139
/**
@@ -151,7 +151,7 @@ export class MsgPackDecoder extends MsgPackDecoderFast<Reader> {
151151
const buf = reader.buf(length);
152152
return new JsonPackValue(buf);
153153
}
154-
return this.val();
154+
return this.readAny();
155155
}
156156

157157
protected skip(length: number): number {

0 commit comments

Comments
 (0)