-
Notifications
You must be signed in to change notification settings - Fork 934
Add small Proto helpers #2753
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add small Proto helpers #2753
Changes from 10 commits
05bb427
d69fe94
69ffaf0
c5d598b
d55fc37
51503ca
d2d6622
47b1379
39ff5bc
cc76c51
a4aa9b6
6a33327
5f0779d
a4b4b53
c82c97f
e25cca2
31b567a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ import { TypeOrder } from './field_value'; | |
import { assert, fail } from '../util/assert'; | ||
import { forEach, keys, size } from '../util/obj'; | ||
import { ByteString } from '../util/byte_string'; | ||
import { DocumentKey } from './document_key'; | ||
import { | ||
numericComparator, | ||
numericEquals, | ||
|
@@ -359,8 +360,7 @@ function canonifyValue(value: api.Value): string { | |
} else if ('bytesValue' in value) { | ||
return canonifyByteString(value.bytesValue!); | ||
} else if ('referenceValue' in value) { | ||
// TODO(mrschmidt): Use document key only | ||
return value.referenceValue!; | ||
return canonifyReference(value.referenceValue!); | ||
} else if ('geoPointValue' in value) { | ||
return canonifyGeoPoint(value.geoPointValue!); | ||
} else if ('arrayValue' in value) { | ||
|
@@ -385,6 +385,10 @@ function canonifyGeoPoint(geoPoint: api.LatLng): string { | |
return `geo(${geoPoint.latitude},${geoPoint.longitude})`; | ||
} | ||
|
||
function canonifyReference(referenceValue: string): string { | ||
return DocumentKey.fromName(referenceValue).toString(); | ||
} | ||
|
||
function canonifyMap(mapValue: api.MapValue): string { | ||
// Iteration order in JavaScript is not guaranteed. To ensure that we generate | ||
// matching canonical IDs for identical maps, we need to sort the keys. | ||
|
@@ -535,3 +539,43 @@ export function normalizeByteString(blob: string | Uint8Array): ByteString { | |
return ByteString.fromUint8Array(blob); | ||
} | ||
} | ||
|
||
/** Returns true if `value` is an IntegerValue. */ | ||
export function isInteger(value?: api.Value | null): boolean { | ||
return !!value && 'integerValue' in value; | ||
} | ||
|
||
/** Returns true if `value` is a DoubleValue. */ | ||
export function isDouble(value?: api.Value | null): boolean { | ||
return !!value && 'doubleValue' in value; | ||
} | ||
|
||
/** Returns true if `value` is either an IntegerValue or a DoubleValue. */ | ||
export function isNumber(value?: api.Value | null): boolean { | ||
return isInteger(value) || isDouble(value); | ||
} | ||
|
||
/** Returns true if `value` is an ArrayValue. */ | ||
export function isArray(value?: api.Value | null): boolean { | ||
return !!value && 'arrayValue' in value; | ||
} | ||
|
||
/** Returns true if `value` is a ReferenceValue. */ | ||
export function isReferenceValue(value?: api.Value | null): boolean { | ||
return !!value && 'referenceValue' in value; | ||
} | ||
|
||
/** Returns true if `value` is a NullValue. */ | ||
export function isNullValue(value?: api.Value | null): boolean { | ||
return !!value && 'nullValue' in value; | ||
} | ||
|
||
/** Returns true if `value` is NaN. */ | ||
export function isNanValue(value?: api.Value | null): boolean { | ||
return !!value && isNaN(Number(value.doubleValue)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't actually test that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ugh JavaScript:
So it looks like we need to test here that |
||
} | ||
|
||
/** Returns true if `value` is a MapValue. */ | ||
export function isMapValue(value?: api.Value | null): boolean { | ||
return !!value && 'mapValue' in value; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we defined an interface like
Then we could make this a type guard:
This PR doesn't include any uses of these functions, so it's not clear that this would necessarily be a win, but it seems like this could be helpful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated the PR to allow us to make asserts on the input type, but I kept it in the method declarations for now and did not plumb through separate types. This should allow us to get rid of bangs for future usages.