-
Notifications
You must be signed in to change notification settings - Fork 934
Add FieldValue methods to the Lite SDK #3135
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
Changes from 6 commits
a957d31
6a29c6e
7ce99ba
fe34ecb
c9589ed
5297799
6121239
d1727da
7862dc4
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 |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/** | ||
* @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 firestore from '../../'; | ||
|
||
import { validateAtLeastNumberOfArgs } from '../../../src/util/input_validation'; | ||
import { | ||
ArrayRemoveFieldValueImpl, | ||
ArrayUnionFieldValueImpl, | ||
DeleteFieldValueImpl, | ||
NumericIncrementFieldValueImpl, | ||
SerializableFieldValue, | ||
ServerTimestampFieldValueImpl | ||
} from '../../../src/api/field_value'; | ||
import { ParseContext } from '../../../src/api/user_data_reader'; | ||
import { FieldTransform } from '../../../src/model/mutation'; | ||
|
||
/** The public FieldValue class of the lite API. */ | ||
export abstract class FieldValue extends SerializableFieldValue | ||
implements firestore.FieldValue {} | ||
|
||
/** | ||
* A delegate class that allows the FieldValue implementations returned by | ||
* deleteField(), serverTimestamp(), arrayUnion(), arrayRemove() and | ||
* increment() to be an instance of the lite FieldValue class declared above. | ||
* | ||
* We don't directly subclass `FieldValue` in the various field value | ||
* implementations as the base FieldValue class differs between the lite, full | ||
* and legacy SDK. | ||
*/ | ||
class FieldValueDelegate extends FieldValue implements firestore.FieldValue { | ||
readonly _methodName: string; | ||
|
||
constructor(readonly _delegate: SerializableFieldValue) { | ||
super(); | ||
this._methodName = _delegate._methodName; | ||
} | ||
|
||
_toFieldTransform(context: ParseContext): FieldTransform | null { | ||
return this._delegate._toFieldTransform(context); | ||
} | ||
|
||
isEqual(other: firestore.FieldValue): boolean { | ||
if (!(other instanceof FieldValueDelegate)) { | ||
return false; | ||
} | ||
return this._delegate.isEqual(other._delegate); | ||
} | ||
} | ||
|
||
export function deleteField(): firestore.FieldValue { | ||
return new FieldValueDelegate(new DeleteFieldValueImpl('delete')); | ||
} | ||
|
||
export function serverTimestamp(): firestore.FieldValue { | ||
return new FieldValueDelegate( | ||
new ServerTimestampFieldValueImpl('serverTimestamp') | ||
); | ||
} | ||
|
||
export function arrayUnion(...elements: unknown[]): firestore.FieldValue { | ||
validateAtLeastNumberOfArgs('arrayUnion()', arguments, 1); | ||
// NOTE: We don't actually parse the data until it's used in set() or | ||
// update() since we need access to the Firestore instance. | ||
return new FieldValueDelegate( | ||
new ArrayUnionFieldValueImpl('arrayUnion', elements) | ||
); | ||
} | ||
|
||
export function arrayRemove(...elements: unknown[]): firestore.FieldValue { | ||
validateAtLeastNumberOfArgs('arrayRemove()', arguments, 1); | ||
// NOTE: We don't actually parse the data until it's used in set() or | ||
// update() since we need access to the Firestore instance. | ||
return new FieldValueDelegate( | ||
new ArrayRemoveFieldValueImpl('arrayRemove', elements) | ||
); | ||
} | ||
|
||
export function increment(n: number): firestore.FieldValue { | ||
return new FieldValueDelegate( | ||
new NumericIncrementFieldValueImpl('increment', n) | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,8 @@ import { | |
getFirestore, | ||
initializeFirestore | ||
} from '../src/api/database'; | ||
import { expectEqual, expectNotEqual } from '../../test/util/helpers'; | ||
import { FieldValue } from '../../src/api/field_value'; | ||
|
||
describe('Firestore', () => { | ||
it('can provide setting', () => { | ||
|
@@ -57,3 +59,19 @@ describe('Firestore', () => { | |
); | ||
}); | ||
}); | ||
|
||
describe('FieldValue', () => { | ||
it('support equality checking with isEqual()', () => { | ||
expectEqual(FieldValue.delete(), FieldValue.delete()); | ||
expectEqual(FieldValue.serverTimestamp(), FieldValue.serverTimestamp()); | ||
expectNotEqual(FieldValue.delete(), FieldValue.serverTimestamp()); | ||
}); | ||
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. Add a test case to exercise the 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. Added TODO to do this when documentId() is merged into master. |
||
|
||
it('support instanceof checks', () => { | ||
expect(FieldValue.delete()).to.be.an.instanceOf(FieldValue); | ||
expect(FieldValue.serverTimestamp()).to.be.an.instanceOf(FieldValue); | ||
expect(FieldValue.increment(1)).to.be.an.instanceOf(FieldValue); | ||
expect(FieldValue.arrayUnion('a')).to.be.an.instanceOf(FieldValue); | ||
expect(FieldValue.arrayRemove('a')).to.be.an.instanceOf(FieldValue); | ||
}); | ||
}); |
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.
Consider tweaking this part of the sentence to read: "since we'd need the Firestore instance to do this." (also on line 87).
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.
Done (4x times)