Skip to content

Commit 05a49aa

Browse files
committed
Timestamp, GeoPoint, VectorValue
1 parent 1652e8b commit 05a49aa

14 files changed

+400
-43
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,14 @@ export type FirestoreErrorCode = 'cancelled' | 'unknown' | 'invalid-argument' |
207207
// @public
208208
export class GeoPoint {
209209
constructor(latitude: number, longitude: number);
210+
static fromJSON(json: object): GeoPoint;
210211
isEqual(other: GeoPoint): boolean;
211212
get latitude(): number;
212213
get longitude(): number;
213214
toJSON(): {
214215
latitude: number;
215216
longitude: number;
217+
type: string;
216218
};
217219
}
218220

@@ -418,6 +420,7 @@ export class Timestamp {
418420
seconds: number,
419421
nanoseconds: number);
420422
static fromDate(date: Date): Timestamp;
423+
static fromJSON(json: object): Timestamp;
421424
static fromMillis(milliseconds: number): Timestamp;
422425
isEqual(other: Timestamp): boolean;
423426
readonly nanoseconds: number;
@@ -427,6 +430,7 @@ export class Timestamp {
427430
toJSON(): {
428431
seconds: number;
429432
nanoseconds: number;
433+
type: string;
430434
};
431435
toMillis(): number;
432436
toString(): string;

common/api-review/firestore.api.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,12 +268,14 @@ export interface FirestoreSettings {
268268
// @public
269269
export class GeoPoint {
270270
constructor(latitude: number, longitude: number);
271+
static fromJSON(json: object): GeoPoint;
271272
isEqual(other: GeoPoint): boolean;
272273
get latitude(): number;
273274
get longitude(): number;
274275
toJSON(): {
275276
latitude: number;
276277
longitude: number;
278+
type: string;
277279
};
278280
}
279281

@@ -742,6 +744,7 @@ export class Timestamp {
742744
seconds: number,
743745
nanoseconds: number);
744746
static fromDate(date: Date): Timestamp;
747+
static fromJSON(json: object): Timestamp;
745748
static fromMillis(milliseconds: number): Timestamp;
746749
isEqual(other: Timestamp): boolean;
747750
readonly nanoseconds: number;
@@ -751,6 +754,7 @@ export class Timestamp {
751754
toJSON(): {
752755
seconds: number;
753756
nanoseconds: number;
757+
type: string;
754758
};
755759
toMillis(): number;
756760
toString(): string;

docs-devsite/firestore_.geopoint.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export declare class GeoPoint
3737

3838
| Method | Modifiers | Description |
3939
| --- | --- | --- |
40+
| [fromJSON(json)](./firestore_.geopoint.md#geopointfromjson) | <code>static</code> | Builds a <code>Timestamp</code> instance from a JSON serialized version of <code>Bytes</code>. |
4041
| [isEqual(other)](./firestore_.geopoint.md#geopointisequal) | | Returns true if this <code>GeoPoint</code> is equal to the provided one. |
4142
| [toJSON()](./firestore_.geopoint.md#geopointtojson) | | Returns a JSON-serializable representation of this GeoPoint. |
4243

@@ -77,6 +78,26 @@ The longitude of this `GeoPoint` instance.
7778
get longitude(): number;
7879
```
7980

81+
## GeoPoint.fromJSON()
82+
83+
Builds a `Timestamp` instance from a JSON serialized version of `Bytes`<!-- -->.
84+
85+
<b>Signature:</b>
86+
87+
```typescript
88+
static fromJSON(json: object): GeoPoint;
89+
```
90+
91+
#### Parameters
92+
93+
| Parameter | Type | Description |
94+
| --- | --- | --- |
95+
| json | object | |
96+
97+
<b>Returns:</b>
98+
99+
[GeoPoint](./firestore_.geopoint.md#geopoint_class)
100+
80101
## GeoPoint.isEqual()
81102

82103
Returns true if this `GeoPoint` is equal to the provided one.
@@ -109,9 +130,10 @@ Returns a JSON-serializable representation of this GeoPoint.
109130
toJSON(): {
110131
latitude: number;
111132
longitude: number;
133+
type: string;
112134
};
113135
```
114136
<b>Returns:</b>
115137

116-
{ latitude: number; longitude: number; }
138+
{ latitude: number; longitude: number; type: string; }
117139

docs-devsite/firestore_.timestamp.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export declare class Timestamp
4040
| Method | Modifiers | Description |
4141
| --- | --- | --- |
4242
| [fromDate(date)](./firestore_.timestamp.md#timestampfromdate) | <code>static</code> | Creates a new timestamp from the given date. |
43+
| [fromJSON(json)](./firestore_.timestamp.md#timestampfromjson) | <code>static</code> | Builds a <code>Timestamp</code> instance from a JSON serialized version of <code>Bytes</code>. |
4344
| [fromMillis(milliseconds)](./firestore_.timestamp.md#timestampfrommillis) | <code>static</code> | Creates a new timestamp from the given number of milliseconds. |
4445
| [isEqual(other)](./firestore_.timestamp.md#timestampisequal) | | Returns true if this <code>Timestamp</code> is equal to the provided one. |
4546
| [now()](./firestore_.timestamp.md#timestampnow) | <code>static</code> | Creates a new timestamp with the current date, with millisecond precision. |
@@ -110,6 +111,26 @@ static fromDate(date: Date): Timestamp;
110111

111112
A new `Timestamp` representing the same point in time as the given date.
112113

114+
## Timestamp.fromJSON()
115+
116+
Builds a `Timestamp` instance from a JSON serialized version of `Bytes`<!-- -->.
117+
118+
<b>Signature:</b>
119+
120+
```typescript
121+
static fromJSON(json: object): Timestamp;
122+
```
123+
124+
#### Parameters
125+
126+
| Parameter | Type | Description |
127+
| --- | --- | --- |
128+
| json | object | |
129+
130+
<b>Returns:</b>
131+
132+
[Timestamp](./firestore_.timestamp.md#timestamp_class)
133+
113134
## Timestamp.fromMillis()
114135

115136
Creates a new timestamp from the given number of milliseconds.
@@ -194,11 +215,12 @@ Returns a JSON-serializable representation of this `Timestamp`<!-- -->.
194215
toJSON(): {
195216
seconds: number;
196217
nanoseconds: number;
218+
type: string;
197219
};
198220
```
199221
<b>Returns:</b>
200222

201-
{ seconds: number; nanoseconds: number; }
223+
{ seconds: number; nanoseconds: number; type: string; }
202224

203225
## Timestamp.toMillis()
204226

docs-devsite/firestore_lite.geopoint.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export declare class GeoPoint
3737

3838
| Method | Modifiers | Description |
3939
| --- | --- | --- |
40+
| [fromJSON(json)](./firestore_lite.geopoint.md#geopointfromjson) | <code>static</code> | Builds a <code>Timestamp</code> instance from a JSON serialized version of <code>Bytes</code>. |
4041
| [isEqual(other)](./firestore_lite.geopoint.md#geopointisequal) | | Returns true if this <code>GeoPoint</code> is equal to the provided one. |
4142
| [toJSON()](./firestore_lite.geopoint.md#geopointtojson) | | Returns a JSON-serializable representation of this GeoPoint. |
4243

@@ -77,6 +78,26 @@ The longitude of this `GeoPoint` instance.
7778
get longitude(): number;
7879
```
7980

81+
## GeoPoint.fromJSON()
82+
83+
Builds a `Timestamp` instance from a JSON serialized version of `Bytes`<!-- -->.
84+
85+
<b>Signature:</b>
86+
87+
```typescript
88+
static fromJSON(json: object): GeoPoint;
89+
```
90+
91+
#### Parameters
92+
93+
| Parameter | Type | Description |
94+
| --- | --- | --- |
95+
| json | object | |
96+
97+
<b>Returns:</b>
98+
99+
[GeoPoint](./firestore_lite.geopoint.md#geopoint_class)
100+
80101
## GeoPoint.isEqual()
81102

82103
Returns true if this `GeoPoint` is equal to the provided one.
@@ -109,9 +130,10 @@ Returns a JSON-serializable representation of this GeoPoint.
109130
toJSON(): {
110131
latitude: number;
111132
longitude: number;
133+
type: string;
112134
};
113135
```
114136
<b>Returns:</b>
115137

116-
{ latitude: number; longitude: number; }
138+
{ latitude: number; longitude: number; type: string; }
117139

docs-devsite/firestore_lite.timestamp.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export declare class Timestamp
4040
| Method | Modifiers | Description |
4141
| --- | --- | --- |
4242
| [fromDate(date)](./firestore_lite.timestamp.md#timestampfromdate) | <code>static</code> | Creates a new timestamp from the given date. |
43+
| [fromJSON(json)](./firestore_lite.timestamp.md#timestampfromjson) | <code>static</code> | Builds a <code>Timestamp</code> instance from a JSON serialized version of <code>Bytes</code>. |
4344
| [fromMillis(milliseconds)](./firestore_lite.timestamp.md#timestampfrommillis) | <code>static</code> | Creates a new timestamp from the given number of milliseconds. |
4445
| [isEqual(other)](./firestore_lite.timestamp.md#timestampisequal) | | Returns true if this <code>Timestamp</code> is equal to the provided one. |
4546
| [now()](./firestore_lite.timestamp.md#timestampnow) | <code>static</code> | Creates a new timestamp with the current date, with millisecond precision. |
@@ -110,6 +111,26 @@ static fromDate(date: Date): Timestamp;
110111

111112
A new `Timestamp` representing the same point in time as the given date.
112113

114+
## Timestamp.fromJSON()
115+
116+
Builds a `Timestamp` instance from a JSON serialized version of `Bytes`<!-- -->.
117+
118+
<b>Signature:</b>
119+
120+
```typescript
121+
static fromJSON(json: object): Timestamp;
122+
```
123+
124+
#### Parameters
125+
126+
| Parameter | Type | Description |
127+
| --- | --- | --- |
128+
| json | object | |
129+
130+
<b>Returns:</b>
131+
132+
[Timestamp](./firestore_lite.timestamp.md#timestamp_class)
133+
113134
## Timestamp.fromMillis()
114135

115136
Creates a new timestamp from the given number of milliseconds.
@@ -194,11 +215,12 @@ Returns a JSON-serializable representation of this `Timestamp`<!-- -->.
194215
toJSON(): {
195216
seconds: number;
196217
nanoseconds: number;
218+
type: string;
197219
};
198220
```
199221
<b>Returns:</b>
200222

201-
{ seconds: number; nanoseconds: number; }
223+
{ seconds: number; nanoseconds: number; type: string; }
202224

203225
## Timestamp.toMillis()
204226

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,19 @@ export class Bytes {
9696
toJSON(): object {
9797
return {
9898
type: 'firestore/bytes/1.0',
99-
data: this.toBase64()
99+
bytes: this.toBase64()
100100
};
101101
}
102+
102103
/** Builds a `Bytes` instance from a JSON serialized version of `Bytes`. */
103104
static fromJSON(json: object): Bytes {
104-
const requiredFields = ['type', 'data'];
105+
const requiredFields = ['type', 'bytes'];
105106
let error: string | undefined = undefined;
106-
let data: string = '';
107+
let bytesData: string = '';
107108
for (const key of requiredFields) {
108109
if (!(key in json)) {
109110
error = `json missing required field: ${key}`;
111+
break;
110112
}
111113
// eslint-disable-next-line @typescript-eslint/no-explicit-any
112114
const value = (json as any)[key];
@@ -116,13 +118,13 @@ export class Bytes {
116118
} else if (key === 'type' && value !== 'firestore/bytes/1.0') {
117119
error = "Expected 'type' field to equal 'firestore/bytes/1.0'";
118120
break;
119-
} else if (key === 'data') {
120-
data = value;
121+
} else if (key === 'bytes') {
122+
bytesData = value;
121123
}
122124
}
123125
if (error) {
124126
throw new FirestoreError(Code.INVALID_ARGUMENT, error);
125127
}
126-
return Bytes.fromBase64String(data);
128+
return Bytes.fromBase64String(bytesData);
127129
}
128130
}

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

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,6 @@ export class GeoPoint {
7979
return this._lat === other._lat && this._long === other._long;
8080
}
8181

82-
/** Returns a JSON-serializable representation of this GeoPoint. */
83-
toJSON(): { latitude: number; longitude: number } {
84-
return { latitude: this._lat, longitude: this._long };
85-
}
86-
8782
/**
8883
* Actually private to JS consumers of our API, so this function is prefixed
8984
* with an underscore.
@@ -94,4 +89,53 @@ export class GeoPoint {
9489
primitiveComparator(this._long, other._long)
9590
);
9691
}
92+
93+
/** Returns a JSON-serializable representation of this GeoPoint. */
94+
toJSON(): { latitude: number; longitude: number; type: string } {
95+
return {
96+
latitude: this._lat,
97+
longitude: this._long,
98+
type: 'firestore/geopoint/1.0'
99+
};
100+
}
101+
102+
/** Builds a `Timestamp` instance from a JSON serialized version of `Bytes`. */
103+
static fromJSON(json: object): GeoPoint {
104+
const requiredFields = ['type', 'latitude', 'longitude'];
105+
let error: string | undefined = undefined;
106+
let lat: number = 0;
107+
let long: number = 0;
108+
for (const key of requiredFields) {
109+
if (!(key in json)) {
110+
error = `json missing required field: ${key}`;
111+
}
112+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
113+
const value = (json as any)[key];
114+
if (key === 'type') {
115+
if (typeof value !== 'string') {
116+
error = `json field 'type' must be a string.`;
117+
break;
118+
} else if (value !== 'firestore/geopoint/1.0') {
119+
error = "Expected 'type' field to equal 'firestore/geopoint/1.0'";
120+
break;
121+
}
122+
} else if (key === 'latitude') {
123+
if (typeof value !== 'number') {
124+
error = `json field 'latitude' must be a number.`;
125+
break;
126+
}
127+
lat = value;
128+
} else {
129+
if (typeof value !== 'number') {
130+
error = `json field 'longitude' must be a string.`;
131+
break;
132+
}
133+
long = value;
134+
}
135+
}
136+
if (error) {
137+
throw new FirestoreError(Code.INVALID_ARGUMENT, error);
138+
}
139+
return new GeoPoint(lat, long);
140+
}
97141
}

0 commit comments

Comments
 (0)