Skip to content

Adding test utilities to create Value types #2680

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

Merged
Merged
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
116 changes: 116 additions & 0 deletions packages/firestore/test/util/values.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as api from '../../src/protos/firestore_proto_api';
import * as typeUtils from '../../src/util/types';
import { Blob } from '../../src/api/blob';
import { Timestamp } from '../../src/api/timestamp';
import { GeoPoint } from '../../src/api/geo_point';
import { DocumentKeyReference } from '../../src/api/user_data_converter';
import { DatabaseId } from '../../src/core/database_info';
import { DocumentKey } from '../../src/model/document_key';
import { fail } from '../../src/util/assert';
import { Dict, forEach } from '../../src/util/obj';

/** Test helper to create Firestore Value protos from JavaScript types. */

// TODO(mrschmidt): Move into UserDataConverter
export function valueOf(
input: unknown,
useProto3Json: boolean = false
): api.Value {
if (input === null) {
return { nullValue: 'NULL_VALUE' };
} else if (typeof input === 'number') {
if (typeUtils.isSafeInteger(input)) {
return { integerValue: String(input) };
} else {
if (useProto3Json) {
// Proto 3 let's us encode NaN and Infinity as string values as
// expected by the backend. This is currently not checked by our unit
// tests because they rely on protobuf.js.
if (isNaN(input)) {
return { doubleValue: 'NaN' } as {};
} else if (input === Infinity) {
return { doubleValue: 'Infinity' } as {};
} else if (input === -Infinity) {
return { doubleValue: '-Infinity' } as {};
}
}
return { doubleValue: input };
}
} else if (typeof input === 'boolean') {
return { booleanValue: input };
} else if (typeof input === 'string') {
return { stringValue: input };
} else if (input instanceof Timestamp) {
return {
timestampValue: {
seconds: String(input.seconds),
nanos: input.nanoseconds
}
};
} else if (input instanceof GeoPoint) {
return {
geoPointValue: {
latitude: input.latitude,
longitude: input.longitude
}
};
} else if (input instanceof Blob) {
if (useProto3Json) {
return { bytesValue: input._byteString.toBase64() };
} else {
return { bytesValue: input._byteString.toUint8Array() };
}
} else if (input instanceof DocumentKeyReference) {
return {
referenceValue:
'projects/project/databases/(default)/documents/' + input.key.path
};
} else if (Array.isArray(input)) {
return {
arrayValue: { values: input.map(el => valueOf(el, useProto3Json)) }
};
} else if (typeof input === 'object') {
const result: api.Value = { mapValue: { fields: {} } };
forEach(input as Dict<unknown>, (key: string, val: unknown) => {
result.mapValue!.fields![key] = valueOf(val, useProto3Json);
});
return result;
} else {
fail(`Failed to serialize field: ${input}`);
}
}

/** Creates a MapValue from a list of key/value arguments. */
export function mapOf(...entries: unknown[]): api.Value {
const result: api.Value = { mapValue: { fields: {} } };
for (let i = 0; i < entries.length; i += 2) {
result.mapValue!.fields![entries[i] as string] = valueOf(
entries[i + 1],
/* useProto3Json= */ false
Copy link
Contributor

Choose a reason for hiding this comment

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

Curious: why is it OK to set it to always false here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For now, this is a test utility only. The best I can tell you that we will find out soon if it indeed is ok.

);
}
return result;
}

export function refValue(dbId: DatabaseId, key: DocumentKey): api.Value {
return {
referenceValue: `projects/${dbId.projectId}/databases/${dbId.database}/documents/${key.path}`
};
}