Skip to content

Commit f318dcd

Browse files
Address feeback
1 parent 8a276f2 commit f318dcd

File tree

2 files changed

+30
-29
lines changed

2 files changed

+30
-29
lines changed

packages/firestore/src/model/document.ts

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ export class Document extends MaybeDocument {
7575
private readonly converter?: (value: api.Value) => FieldValue
7676
) {
7777
super(key, version);
78+
assert(
79+
this.objectValue !== undefined ||
80+
(this.proto !== undefined && this.converter !== undefined),
81+
'If objectValue is not defined, proto and converter need to be set.'
82+
);
83+
7884
this.hasLocalMutations = !!options.hasLocalMutations;
7985
this.hasCommittedMutations = !!options.hasCommittedMutations;
8086
}
@@ -83,11 +89,6 @@ export class Document extends MaybeDocument {
8389
if (this.objectValue) {
8490
return this.objectValue.field(path);
8591
} else {
86-
assert(
87-
this.proto !== undefined && this.converter !== undefined,
88-
'Expected proto and converter to be defined.'
89-
);
90-
9192
if (!this.fieldValueCache) {
9293
// TODO(b/136090445): Remove the cache when `getField` is no longer
9394
// called during Query ordering.
@@ -103,42 +104,21 @@ export class Document extends MaybeDocument {
103104
// deserialize the value at the requested field path. This speeds up
104105
// Query execution as query filters can discard documents based on a
105106
// single field.
106-
let protoValue: api.Value | undefined = this.proto!.fields[
107-
path.firstSegment()
108-
];
109-
for (let i = 1; protoValue !== undefined && i < path.length; ++i) {
110-
if (protoValue.mapValue) {
111-
protoValue = protoValue.mapValue.fields[path.get(i)];
112-
} else {
113-
protoValue = undefined;
114-
}
115-
}
116-
107+
const protoValue = this.getProtoField(path);
117108
if (protoValue === undefined) {
118109
fieldValue = null;
119110
} else {
120111
fieldValue = this.converter!(protoValue);
121112
}
122-
123113
this.fieldValueCache.set(canonicalPath, fieldValue);
124114
}
125115

126116
return fieldValue!;
127117
}
128118
}
129119

130-
fieldValue(path: FieldPath): unknown {
131-
const field = this.field(path);
132-
return field ? field.value() : undefined;
133-
}
134-
135120
data(): ObjectValue {
136121
if (!this.objectValue) {
137-
assert(
138-
this.proto !== undefined && this.converter !== undefined,
139-
'Expected proto and converter to be defined.'
140-
);
141-
142122
let result = ObjectValue.EMPTY;
143123
obj.forEach(this.proto!.fields, (key: string, value: api.Value) => {
144124
result = result.set(new FieldPath([key]), this.converter!(value));
@@ -180,6 +160,27 @@ export class Document extends MaybeDocument {
180160
return this.hasLocalMutations || this.hasCommittedMutations;
181161
}
182162

163+
/**
164+
* Returns a the nested Protobuf value for 'path`. Can only be called if
165+
* `proto` was provided at construction time.
166+
*/
167+
private getProtoField(path: FieldPath): api.Value | undefined {
168+
assert(
169+
this.proto !== undefined,
170+
"Can only call getProtoField() when proto is defined"
171+
);
172+
let protoValue: api.Value | undefined = this.proto!.fields[
173+
path.firstSegment()
174+
];
175+
for (let i = 1; i < path.length; ++i) {
176+
if (!protoValue || !protoValue.mapValue) {
177+
return undefined;
178+
}
179+
protoValue = protoValue.mapValue.fields[path.get(i)];
180+
}
181+
return protoValue;
182+
}
183+
183184
static compareByField(field: FieldPath, d1: Document, d2: Document): number {
184185
const v1 = d1.field(field);
185186
const v2 = d2.field(field);

packages/firestore/test/unit/model/document.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ describe('Document', () => {
4242
};
4343
const document = doc('rooms/Eros', 1, data, { hasLocalMutations: true });
4444

45-
expect(document.fieldValue(field('desc'))).to.deep.equal(
45+
expect(document.field(field('desc'))!.value()).to.deep.equal(
4646
'Discuss all the project related stuff'
4747
);
48-
expect(document.fieldValue(field('owner.title'))).to.deep.equal(
48+
expect(document.field(field('owner.title'))!.value()).to.deep.equal(
4949
'scallywag'
5050
);
5151
expect(document.hasLocalMutations).to.equal(true);

0 commit comments

Comments
 (0)