Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions handwritten/firestore/.jsdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@

'use strict';

// BigInt JSON serialization.
// https://github.com/jsdoc/jsdoc/issues/1918
BigInt.prototype.toJSON = function() {
return this.toString() + 'n';
};

module.exports = {
opts: {
readme: './README.md',
Expand Down
78 changes: 64 additions & 14 deletions handwritten/firestore/dev/src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,18 @@ import {ApiMapValue, ProtobufJsValue} from './types';
import {validateObject} from './validate';

import api = google.firestore.v1;
import {RESERVED_MAP_KEY, RESERVED_MAP_KEY_VECTOR_VALUE} from './map-type';
import {
RESERVED_BSON_BINARY_KEY,
RESERVED_INT32_KEY,
RESERVED_MAP_KEY,
RESERVED_MAP_KEY_VECTOR_VALUE,
RESERVED_MAX_KEY,
RESERVED_MIN_KEY,
RESERVED_BSON_OBJECT_ID_KEY,
RESERVED_REGEX_KEY,
RESERVED_BSON_TIMESTAMP_KEY,
RESERVED_DECIMAL128_KEY,
} from './map-type';

/*!
* @module firestore/convert
Expand Down Expand Up @@ -104,6 +115,46 @@ function bytesFromJson(bytesValue: string | Uint8Array): Uint8Array {
}
}

/**
* Detects the 'valueType' for cases where a map value has been used to
* represent another type.
* @param mapValue The map value to probe.
*/
function detectMapRepresentation(
mapValue: api.IMapValue | null | undefined
): string {
const fields = mapValue?.fields;
if (fields) {
const props = Object.keys(fields);
if (
props.indexOf(RESERVED_MAP_KEY) !== -1 &&
detectValueType(fields[RESERVED_MAP_KEY]) === 'stringValue' &&
fields[RESERVED_MAP_KEY].stringValue === RESERVED_MAP_KEY_VECTOR_VALUE
) {
return 'vectorValue';
} else if (props.indexOf(RESERVED_MIN_KEY) !== -1) {
return 'minKeyValue';
} else if (props.indexOf(RESERVED_MAX_KEY) !== -1) {
return 'maxKeyValue';
} else if (props.indexOf(RESERVED_REGEX_KEY) !== -1) {
return 'regexValue';
} else if (props.indexOf(RESERVED_BSON_OBJECT_ID_KEY) !== -1) {
return 'bsonObjectIdValue';
} else if (props.indexOf(RESERVED_INT32_KEY) !== -1) {
return 'int32Value';
} else if (props.indexOf(RESERVED_DECIMAL128_KEY) !== -1) {
return 'decimal128Value';
} else if (props.indexOf(RESERVED_BSON_TIMESTAMP_KEY) !== -1) {
return 'bsonTimestampValue';
} else if (props.indexOf(RESERVED_BSON_BINARY_KEY) !== -1) {
return 'bsonBinaryValue';
}
Comment on lines +128 to +151
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using Object.keys(fields) and multiple indexOf calls to probe the fields is inefficient. Since fields is a plain object, we can perform direct property checks which are $O(1)$ and much cleaner.

    if (
      fields[RESERVED_MAP_KEY] !== undefined &&
      detectValueType(fields[RESERVED_MAP_KEY]) === 'stringValue' &&
      fields[RESERVED_MAP_KEY].stringValue === RESERVED_MAP_KEY_VECTOR_VALUE
    ) {
      return 'vectorValue';
    } else if (fields[RESERVED_MIN_KEY] !== undefined) {
      return 'minKeyValue';
    } else if (fields[RESERVED_MAX_KEY] !== undefined) {
      return 'maxKeyValue';
    } else if (fields[RESERVED_REGEX_KEY] !== undefined) {
      return 'regexValue';
    } else if (fields[RESERVED_BSON_OBJECT_ID_KEY] !== undefined) {
      return 'bsonObjectIdValue';
    } else if (fields[RESERVED_INT32_KEY] !== undefined) {
      return 'int32Value';
    } else if (fields[RESERVED_DECIMAL128_KEY] !== undefined) {
      return 'decimal128Value';
    } else if (fields[RESERVED_BSON_TIMESTAMP_KEY] !== undefined) {
      return 'bsonTimestampValue';
    } else if (fields[RESERVED_BSON_BINARY_KEY] !== undefined) {
      return 'bsonBinaryValue';
    }

}

// If none of the above cases apply, it's a regular map.
return 'mapValue';
}

/**
* Detects 'valueType' from a Proto3 JSON `firestore.v1.Value` proto.
*
Expand Down Expand Up @@ -163,19 +214,10 @@ export function detectValueType(proto: ProtobufJsValue): string {
valueType = detectedValues[0];
}

// Special handling of mapValues used to represent other data types
// Special handling of mapValues which may or may not have been
// used to represent other data types.
if (valueType === 'mapValue') {
const fields = proto.mapValue?.fields;
if (fields) {
const props = Object.keys(fields);
if (
props.indexOf(RESERVED_MAP_KEY) !== -1 &&
detectValueType(fields[RESERVED_MAP_KEY]) === 'stringValue' &&
fields[RESERVED_MAP_KEY].stringValue === RESERVED_MAP_KEY_VECTOR_VALUE
) {
valueType = 'vectorValue';
}
}
valueType = detectMapRepresentation(proto.mapValue);
}

return valueType;
Expand Down Expand Up @@ -261,7 +303,15 @@ export function valueFromJson(fieldValue: api.IValue): api.IValue {
};
}
case 'mapValue':
case 'vectorValue': {
case 'vectorValue':
case 'regexValue':
case 'bsonObjectIdValue':
case 'bsonBinaryValue':
case 'int32Value':
case 'decimal128Value':
case 'bsonTimestampValue':
case 'minKeyValue':
case 'maxKeyValue': {
const mapValue: ApiMapValue = {};
const fields = fieldValue.mapValue!.fields;
if (fields) {
Expand Down
Loading
Loading