Skip to content
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

refactor(deprecation)!: remove legacy support for array arguments #625

Merged
merged 6 commits into from May 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions dev/conformance/runner.ts
Expand Up @@ -162,7 +162,7 @@ const convertInput = {
if (path.field.length === 1 && path.field[0] === '__name__') {
return FieldPath.documentId();
}
return new FieldPath(path.field);
return new FieldPath(...path.field);
},
paths: (fields: ConformanceProto): FieldPath[] => {
const convertedPaths: FieldPath[] = [];
Expand Down Expand Up @@ -409,7 +409,7 @@ function runTest(spec: ConformanceProto) {
} else if (spec.option && spec.option.fields) {
setOption.mergeFields = [];
for (const fieldPath of spec.option.fields) {
setOption.mergeFields.push(new FieldPath(fieldPath.field));
setOption.mergeFields.push(new FieldPath(...fieldPath.field));
}
}
return docRef(setSpec.docRefPath).set(
Expand Down
5 changes: 2 additions & 3 deletions dev/src/index.ts
Expand Up @@ -819,9 +819,8 @@ export class Firestore {
* followed by any additional `DocumentReference` documents. If used, the
* optional `ReadOptions` must be the last argument.
*
* @param {Array.<DocumentReference|ReadOptions>} documentRefsOrReadOptions
* The `DocumentReferences` to receive, followed by an optional field
* mask.
* @param {...DocumentReference|ReadOptions} documentRefsOrReadOptions The
* `DocumentReferences` to receive, followed by an optional field mask.
* @returns {Promise<Array.<DocumentSnapshot>>} A Promise that
* contains an array with the resulting document snapshots.
*
Expand Down
22 changes: 12 additions & 10 deletions dev/src/path.ts
Expand Up @@ -508,8 +508,7 @@ export class FieldPath extends Path<FieldPath> {
/**
* Constructs a Firestore Field Path.
*
* @param {...string|string[]} segments Sequence of field names that form
* this path.
* @param {...string} segments Sequence of field names that form this path.
*
* @example
* let query = firestore.collection('col');
Expand All @@ -522,20 +521,23 @@ export class FieldPath extends Path<FieldPath> {
* });
*/
constructor(...segments: string[]) {
const elements: string[] = Array.isArray(segments[0])
? ((segments[0] as unknown) as string[])
: segments;
if (Array.isArray(segments[0])) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove string[] from the param type definition above?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Also removed "undocumented" from the PR description...

throw new Error(
'The FieldPath constructor no longer supports an array as its first argument. ' +
'Please unpack your array and call FieldPath() with individual arguments.'
);
}

validateMinNumberOfArguments('FieldPath', elements, 1);
validateMinNumberOfArguments('FieldPath', segments, 1);

for (let i = 0; i < elements.length; ++i) {
validateString(i, elements[i]);
if (elements[i].length === 0) {
for (let i = 0; i < segments.length; ++i) {
validateString(i, segments[i]);
if (segments[i].length === 0) {
throw new Error(`Element at index ${i} should not be an empty string.`);
}
}

super(elements);
super(segments);
}

/**
Expand Down
44 changes: 18 additions & 26 deletions dev/src/transaction.ts
Expand Up @@ -163,9 +163,8 @@ export class Transaction {
* followed by any additional `DocumentReference` documents. If used, the
* optional `ReadOptions` must be the last argument.
*
* @param {Array.<DocumentReference|ReadOptions>} documentRefsOrReadOptions
* The `DocumentReferences` to receive, followed by an optional field
* mask.
* @param {...DocumentReference|ReadOptions} documentRefsOrReadOptions The
* `DocumentReferences` to receive, followed by an optional field mask.
* @returns {Promise<Array.<DocumentSnapshot>>} A Promise that
* contains an array with the resulting document snapshots.
*
Expand Down Expand Up @@ -433,30 +432,23 @@ export function parseGetAllArguments(
let documents: DocumentReference[];
Copy link
Contributor

Choose a reason for hiding this comment

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

I can't comment on unchanged code, but does the getAll() parameter definition need to be updated somehow?

   * @param {Array.<DocumentReference|ReadOptions>} documentRefsOrReadOptions
   * The `DocumentReferences` to receive, followed by an optional field
   * mask.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. I fixed the JSDoc as well.

let readOptions: ReadOptions | undefined = undefined;

// In the original release of the SDK, getAll() was documented to accept
// either a varargs list of DocumentReferences or a single array of
// DocumentReferences. To support this usage in the TypeScript client, we have
// to manually verify the arguments to determine which input the user
// provided.
const usesDeprecatedArgumentStyle = Array.isArray(
documentRefsOrReadOptions[0]
);

if (usesDeprecatedArgumentStyle) {
documents = documentRefsOrReadOptions[0] as DocumentReference[];
readOptions = documentRefsOrReadOptions[1] as ReadOptions;
if (Array.isArray(documentRefsOrReadOptions[0])) {
throw new Error(
'getAll() no longer accepts an array as its first argument. ' +
'Please unpack your array and call getAll() with individual arguments.'
);
}

if (
documentRefsOrReadOptions.length > 0 &&
isPlainObject(
documentRefsOrReadOptions[documentRefsOrReadOptions.length - 1]
)
) {
readOptions = documentRefsOrReadOptions.pop() as ReadOptions;
documents = documentRefsOrReadOptions as DocumentReference[];
} else {
if (
documentRefsOrReadOptions.length > 0 &&
isPlainObject(
documentRefsOrReadOptions[documentRefsOrReadOptions.length - 1]
)
) {
readOptions = documentRefsOrReadOptions.pop() as ReadOptions;
documents = documentRefsOrReadOptions as DocumentReference[];
} else {
documents = documentRefsOrReadOptions as DocumentReference[];
}
documents = documentRefsOrReadOptions as DocumentReference[];
}

for (let i = 0; i < documents.length; ++i) {
Expand Down
14 changes: 0 additions & 14 deletions dev/test/index.ts
Expand Up @@ -938,20 +938,6 @@ describe('getAll() method', () => {
});
});

it('accepts array', () => {
const overrides: ApiOverride = {
batchGetDocuments: () => stream(found('documentId')),
};

return createInstance(overrides).then(firestore => {
return (firestore as InvalidApiUsage)
.getAll([firestore.doc('collectionId/documentId')])
.then((result: DocumentSnapshot[]) => {
resultEquals(result, found('documentId'));
});
});
});

it('returns not found for missing documents', () => {
const overrides: ApiOverride = {
batchGetDocuments: () => stream(found('exists'), missing('missing')),
Expand Down