Skip to content

Commit

Permalink
Allow Protobuf arrays with unset values (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
schmidt-sebastian committed Jan 12, 2018
1 parent a986a6e commit 7a9c193
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/convert.js
Expand Up @@ -179,8 +179,10 @@ function convertValue(fieldValue) {
};
case 'arrayValue': {
let arrayValue = [];
for (let value of fieldValue.arrayValue.values) {
arrayValue.push(convertValue(value));
if (is.array(fieldValue.arrayValue.values)) {
for (let value of fieldValue.arrayValue.values) {
arrayValue.push(convertValue(value));
}
}
return {
valueType: 'arrayValue',
Expand Down
6 changes: 4 additions & 2 deletions src/document.js
Expand Up @@ -537,8 +537,10 @@ class DocumentSnapshot {
}
case 'arrayValue': {
let array = [];
for (let i = 0; i < proto.arrayValue.values.length; ++i) {
array.push(this._decodeValue(proto.arrayValue.values[i]));
if (is.array(proto.arrayValue.values)) {
for (let value of proto.arrayValue.values) {
array.push(this._decodeValue(value));
}
}
return array;
}
Expand Down
16 changes: 16 additions & 0 deletions test/index.js
Expand Up @@ -322,6 +322,10 @@ describe('snapshot_() method', function() {
],
},
},
emptyArray: {
valueType: 'arrayValue',
arrayValue: {},
},
dateValue: {
valueType: 'timestampValue',
timestampValue: {
Expand Down Expand Up @@ -364,6 +368,10 @@ describe('snapshot_() method', function() {
},
},
},
emptyObject: {
valueType: 'mapValue',
mapValue: {},
},
pathValue: {
valueType: 'referenceValue',
referenceValue: `${DATABASE_ROOT}/documents/collection/document`,
Expand Down Expand Up @@ -405,6 +413,9 @@ describe('snapshot_() method', function() {
],
},
},
emptyArray: {
arrayValue: {},
},
dateValue: {
timestampValue: '1985-03-18T07:20:00.123000000Z',
},
Expand Down Expand Up @@ -435,6 +446,9 @@ describe('snapshot_() method', function() {
},
},
},
emptyObject: {
mapValue: {},
},
pathValue: {
referenceValue: `${DATABASE_ROOT}/documents/collection/document`,
},
Expand Down Expand Up @@ -464,12 +478,14 @@ describe('snapshot_() method', function() {
infinityValue: Infinity,
negativeInfinityValue: -Infinity,
objectValue: {foo: 'bar'},
emptyObject: {},
dateValue: new Date('Mar 18, 1985 08:20:00.123 GMT+0100 (CET)'),
pathValue: new DocumentReference(
{formattedName: DATABASE_ROOT},
new ResourcePath('test-project', '(default)', 'collection', 'document')
),
arrayValue: ['foo', 42, 'bar'],
emptyArray: [],
nilValue: null,
geoPointValue: new Firestore.GeoPoint(50.1430847, -122.947778),
bytesValue: Buffer.from([0x1, 0x2]),
Expand Down

0 comments on commit 7a9c193

Please sign in to comment.