Skip to content

Commit 8a081e1

Browse files
committed
Bytes and VectorValue to/from JSON
1 parent 2a07791 commit 8a081e1

File tree

9 files changed

+272
-8
lines changed

9 files changed

+272
-8
lines changed

common/api-review/firestore-lite.api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,8 +468,10 @@ export function vector(values?: number[]): VectorValue;
468468
// @public
469469
export class VectorValue {
470470
/* Excluded from this release type: __constructor */
471+
static fromJSON(json: object): VectorValue;
471472
isEqual(other: VectorValue): boolean;
472473
toArray(): number[];
474+
toJSON(): object;
473475
}
474476

475477
// @public

common/api-review/firestore.api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,8 +797,10 @@ export function vector(values?: number[]): VectorValue;
797797
// @public
798798
export class VectorValue {
799799
/* Excluded from this release type: __constructor */
800+
static fromJSON(json: object): VectorValue;
800801
isEqual(other: VectorValue): boolean;
801802
toArray(): number[];
803+
toJSON(): object;
802804
}
803805

804806
// @public

docs-devsite/firestore_.bytes.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ export declare class Bytes
2323
| Method | Modifiers | Description |
2424
| --- | --- | --- |
2525
| [fromBase64String(base64)](./firestore_.bytes.md#bytesfrombase64string) | <code>static</code> | Creates a new <code>Bytes</code> object from the given Base64 string, converting it to bytes. |
26+
| [fromJSON(json)](./firestore_.bytes.md#bytesfromjson) | <code>static</code> | Builds a <code>Bytes</code> instance from a JSON serialized version of <code>Bytes</code>. |
2627
| [fromUint8Array(array)](./firestore_.bytes.md#bytesfromuint8array) | <code>static</code> | Creates a new <code>Bytes</code> object from the given Uint8Array. |
2728
| [isEqual(other)](./firestore_.bytes.md#bytesisequal) | | Returns true if this <code>Bytes</code> object is equal to the provided one. |
2829
| [toBase64()](./firestore_.bytes.md#bytestobase64) | | Returns the underlying bytes as a Base64-encoded string. |
30+
| [toJSON()](./firestore_.bytes.md#bytestojson) | | Returns a JSON-serializable representation of this <code>Bytes</code> instance. |
2931
| [toString()](./firestore_.bytes.md#bytestostring) | | Returns a string representation of the <code>Bytes</code> object. |
3032
| [toUint8Array()](./firestore_.bytes.md#bytestouint8array) | | Returns the underlying bytes in a new <code>Uint8Array</code>. |
3133

@@ -49,6 +51,26 @@ static fromBase64String(base64: string): Bytes;
4951

5052
[Bytes](./firestore_.bytes.md#bytes_class)
5153

54+
## Bytes.fromJSON()
55+
56+
Builds a `Bytes` instance from a JSON serialized version of `Bytes`<!-- -->.
57+
58+
<b>Signature:</b>
59+
60+
```typescript
61+
static fromJSON(json: object): Bytes;
62+
```
63+
64+
#### Parameters
65+
66+
| Parameter | Type | Description |
67+
| --- | --- | --- |
68+
| json | object | |
69+
70+
<b>Returns:</b>
71+
72+
[Bytes](./firestore_.bytes.md#bytes_class)
73+
5274
## Bytes.fromUint8Array()
5375

5476
Creates a new `Bytes` object from the given Uint8Array.
@@ -106,6 +128,19 @@ string
106128

107129
The Base64-encoded string created from the `Bytes` object.
108130

131+
## Bytes.toJSON()
132+
133+
Returns a JSON-serializable representation of this `Bytes` instance.
134+
135+
<b>Signature:</b>
136+
137+
```typescript
138+
toJSON(): object;
139+
```
140+
<b>Returns:</b>
141+
142+
object
143+
109144
## Bytes.toString()
110145

111146
Returns a string representation of the `Bytes` object.

docs-devsite/firestore_.vectorvalue.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,30 @@ export declare class VectorValue
2424

2525
| Method | Modifiers | Description |
2626
| --- | --- | --- |
27+
| [fromJSON(json)](./firestore_.vectorvalue.md#vectorvaluefromjson) | <code>static</code> | Builds a <code>Bytes</code> instance from a JSON serialized version of <code>Bytes</code>. |
2728
| [isEqual(other)](./firestore_.vectorvalue.md#vectorvalueisequal) | | Returns <code>true</code> if the two <code>VectorValue</code> values have the same raw number arrays, returns <code>false</code> otherwise. |
2829
| [toArray()](./firestore_.vectorvalue.md#vectorvaluetoarray) | | Returns a copy of the raw number array form of the vector. |
30+
| [toJSON()](./firestore_.vectorvalue.md#vectorvaluetojson) | | Returns a JSON-serializable representation of this <code>VectorValue</code> instance. |
31+
32+
## VectorValue.fromJSON()
33+
34+
Builds a `Bytes` instance from a JSON serialized version of `Bytes`<!-- -->.
35+
36+
<b>Signature:</b>
37+
38+
```typescript
39+
static fromJSON(json: object): VectorValue;
40+
```
41+
42+
#### Parameters
43+
44+
| Parameter | Type | Description |
45+
| --- | --- | --- |
46+
| json | object | |
47+
48+
<b>Returns:</b>
49+
50+
[VectorValue](./firestore_.vectorvalue.md#vectorvalue_class)
2951

3052
## VectorValue.isEqual()
3153

@@ -60,3 +82,16 @@ toArray(): number[];
6082

6183
number\[\]
6284

85+
## VectorValue.toJSON()
86+
87+
Returns a JSON-serializable representation of this `VectorValue` instance.
88+
89+
<b>Signature:</b>
90+
91+
```typescript
92+
toJSON(): object;
93+
```
94+
<b>Returns:</b>
95+
96+
object
97+

docs-devsite/firestore_lite.bytes.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ export declare class Bytes
2323
| Method | Modifiers | Description |
2424
| --- | --- | --- |
2525
| [fromBase64String(base64)](./firestore_lite.bytes.md#bytesfrombase64string) | <code>static</code> | Creates a new <code>Bytes</code> object from the given Base64 string, converting it to bytes. |
26+
| [fromJSON(json)](./firestore_lite.bytes.md#bytesfromjson) | <code>static</code> | Builds a <code>Bytes</code> instance from a JSON serialized version of <code>Bytes</code>. |
2627
| [fromUint8Array(array)](./firestore_lite.bytes.md#bytesfromuint8array) | <code>static</code> | Creates a new <code>Bytes</code> object from the given Uint8Array. |
2728
| [isEqual(other)](./firestore_lite.bytes.md#bytesisequal) | | Returns true if this <code>Bytes</code> object is equal to the provided one. |
2829
| [toBase64()](./firestore_lite.bytes.md#bytestobase64) | | Returns the underlying bytes as a Base64-encoded string. |
30+
| [toJSON()](./firestore_lite.bytes.md#bytestojson) | | Returns a JSON-serializable representation of this <code>Bytes</code> instance. |
2931
| [toString()](./firestore_lite.bytes.md#bytestostring) | | Returns a string representation of the <code>Bytes</code> object. |
3032
| [toUint8Array()](./firestore_lite.bytes.md#bytestouint8array) | | Returns the underlying bytes in a new <code>Uint8Array</code>. |
3133

@@ -49,6 +51,26 @@ static fromBase64String(base64: string): Bytes;
4951

5052
[Bytes](./firestore_lite.bytes.md#bytes_class)
5153

54+
## Bytes.fromJSON()
55+
56+
Builds a `Bytes` instance from a JSON serialized version of `Bytes`<!-- -->.
57+
58+
<b>Signature:</b>
59+
60+
```typescript
61+
static fromJSON(json: object): Bytes;
62+
```
63+
64+
#### Parameters
65+
66+
| Parameter | Type | Description |
67+
| --- | --- | --- |
68+
| json | object | |
69+
70+
<b>Returns:</b>
71+
72+
[Bytes](./firestore_lite.bytes.md#bytes_class)
73+
5274
## Bytes.fromUint8Array()
5375

5476
Creates a new `Bytes` object from the given Uint8Array.
@@ -106,6 +128,19 @@ string
106128

107129
The Base64-encoded string created from the `Bytes` object.
108130

131+
## Bytes.toJSON()
132+
133+
Returns a JSON-serializable representation of this `Bytes` instance.
134+
135+
<b>Signature:</b>
136+
137+
```typescript
138+
toJSON(): object;
139+
```
140+
<b>Returns:</b>
141+
142+
object
143+
109144
## Bytes.toString()
110145

111146
Returns a string representation of the `Bytes` object.

docs-devsite/firestore_lite.vectorvalue.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,30 @@ export declare class VectorValue
2424

2525
| Method | Modifiers | Description |
2626
| --- | --- | --- |
27+
| [fromJSON(json)](./firestore_lite.vectorvalue.md#vectorvaluefromjson) | <code>static</code> | Builds a <code>Bytes</code> instance from a JSON serialized version of <code>Bytes</code>. |
2728
| [isEqual(other)](./firestore_lite.vectorvalue.md#vectorvalueisequal) | | Returns <code>true</code> if the two <code>VectorValue</code> values have the same raw number arrays, returns <code>false</code> otherwise. |
2829
| [toArray()](./firestore_lite.vectorvalue.md#vectorvaluetoarray) | | Returns a copy of the raw number array form of the vector. |
30+
| [toJSON()](./firestore_lite.vectorvalue.md#vectorvaluetojson) | | Returns a JSON-serializable representation of this <code>VectorValue</code> instance. |
31+
32+
## VectorValue.fromJSON()
33+
34+
Builds a `Bytes` instance from a JSON serialized version of `Bytes`<!-- -->.
35+
36+
<b>Signature:</b>
37+
38+
```typescript
39+
static fromJSON(json: object): VectorValue;
40+
```
41+
42+
#### Parameters
43+
44+
| Parameter | Type | Description |
45+
| --- | --- | --- |
46+
| json | object | |
47+
48+
<b>Returns:</b>
49+
50+
[VectorValue](./firestore_lite.vectorvalue.md#vectorvalue_class)
2951

3052
## VectorValue.isEqual()
3153

@@ -60,3 +82,16 @@ toArray(): number[];
6082

6183
number\[\]
6284

85+
## VectorValue.toJSON()
86+
87+
Returns a JSON-serializable representation of this `VectorValue` instance.
88+
89+
<b>Signature:</b>
90+
91+
```typescript
92+
toJSON(): object;
93+
```
94+
<b>Returns:</b>
95+
96+
object
97+

packages/firestore/src/lite-api/vector_value.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717

1818
import { isPrimitiveArrayEqual } from '../util/array';
19+
import { Code, FirestoreError } from '../util/error';
1920

2021
/**
2122
* Represents a vector type in Firestore documents.
@@ -48,4 +49,49 @@ export class VectorValue {
4849
isEqual(other: VectorValue): boolean {
4950
return isPrimitiveArrayEqual(this._values, other._values);
5051
}
52+
53+
/** Returns a JSON-serializable representation of this `VectorValue` instance. */
54+
toJSON(): object {
55+
return {
56+
type: 'firestore/vectorValue/1.0',
57+
data: this._values
58+
};
59+
}
60+
/** Builds a `Bytes` instance from a JSON serialized version of `Bytes`. */
61+
static fromJSON(json: object): VectorValue {
62+
const requiredFields = ['type', 'data'];
63+
let error: string | undefined = undefined;
64+
let data: number[] = [];
65+
for (const key of requiredFields) {
66+
if (!(key in json)) {
67+
error = `json missing required field: ${key}`;
68+
}
69+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
70+
const value = (json as any)[key];
71+
if (key === 'type') {
72+
if (typeof value !== 'string') {
73+
error = `json field 'type' must be a string.`;
74+
break;
75+
} else if (value !== 'firestore/vectorValue/1.0') {
76+
error = "Expected 'type' field to equal 'firestore/vectorValue/1.0'";
77+
break;
78+
}
79+
} else {
80+
// First, confirm it's actually an array
81+
if (
82+
Array.isArray(value) &&
83+
value.every(element => typeof element === 'number')
84+
) {
85+
data = value;
86+
} else {
87+
error = "Expected 'data' field to be a number array";
88+
break;
89+
}
90+
}
91+
}
92+
if (error) {
93+
throw new FirestoreError(Code.INVALID_ARGUMENT, error);
94+
}
95+
return new VectorValue(data);
96+
}
5197
}

packages/firestore/test/unit/api/bytes.test.ts

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,26 @@ describe('Bytes', () => {
5555
expectEqual(blob(1, 2, 3), blob(1, 2, 3));
5656
expectNotEqual(blob(1, 2, 3), blob(4, 5, 6));
5757
});
58-
58+
5959
it('fromJSON reconstructs the value from toJSON', () => {
60-
const bytes = Bytes.fromUint8Array(new Uint8Array([0, 1, 2, 3, 4, 5]));
61-
expect(() => { Bytes.fromJSON(bytes.toJSON())} ).to.not.throw;
60+
const bytes = Bytes.fromUint8Array(new Uint8Array([0, 1, 2, 3, 4, 5]));
61+
expect(() => {
62+
Bytes.fromJSON(bytes.toJSON());
63+
}).to.not.throw;
6264
expect(Bytes.fromJSON(bytes.toJSON()).isEqual(bytes)).to.be.true;
6365
});
6466

67+
it('fromJSON parameter order does not matter', () => {
68+
const type = 'firestore/bytes/1.0';
69+
const data = 'AA==';
70+
expect(() => {
71+
Bytes.fromJSON({ data, type });
72+
}).to.not.throw;
73+
expect(() => {
74+
Bytes.fromJSON({ type, data });
75+
}).to.not.throw;
76+
});
77+
6578
it('toJSON -> fromJSON bytes comparison', () => {
6679
Object.keys(base64Mappings).forEach(base64Str => {
6780
const bytesToSerialize = Bytes.fromBase64String(base64Str);
@@ -74,12 +87,20 @@ describe('Bytes', () => {
7487
expect(actualUint8Array[i]).to.equal(expectedUint8Array[i]);
7588
}
7689
});
77-
})
90+
});
7891

7992
it('fromJSON misisng fields throws', () => {
80-
expect(() => {Bytes.fromJSON({type: 'firestore/bytes/1.0' /* missing data */})}).to.throw;
81-
expect(() => {Bytes.fromJSON({data: 'AA==' /* missing type */})}).to.throw;
82-
expect(() => {Bytes.fromJSON({type: 1, data: 'AA==' })}).to.throw;
83-
expect(() => {Bytes.fromJSON({type: 'firestore/bytes/1.0', data: 1 })}).to.throw;
93+
expect(() => {
94+
Bytes.fromJSON({ type: 'firestore/bytes/1.0' /* missing data */ });
95+
}).to.throw;
96+
expect(() => {
97+
Bytes.fromJSON({ data: 'AA==' /* missing type */ });
98+
}).to.throw;
99+
expect(() => {
100+
Bytes.fromJSON({ type: 1, data: 'AA==' });
101+
}).to.throw;
102+
expect(() => {
103+
Bytes.fromJSON({ type: 'firestore/bytes/1.0', data: 1 });
104+
}).to.throw;
84105
});
85106
});

0 commit comments

Comments
 (0)