Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions lib/src/firestore-to-proto.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,34 @@ describe('toMessage()', () => {
},
}),
},
{
desc: 'skips null nested message',
input: {
'4': null,
},
expected: new Job(),
},
{
desc: 'skips null map field',
input: {
'4': null,
},
expected: new Survey(),
},
{
desc: 'skips non-object map field',
input: {
'4': 'not an object',
},
expected: new Survey(),
},
{
desc: 'skips array map field',
input: {
'4': [1, 2, 3],
},
expected: new Survey(),
},
{
desc: 'converts enum value',
input: {
Expand Down
9 changes: 5 additions & 4 deletions lib/src/firestore-to-proto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ function toMessageValue(
if (!descriptor.fields) return null;
const field = descriptor.fields[fieldName];
const fieldType = field?.type;
if (field.keyType) {
if (field?.keyType) {
if (field.keyType !== 'string')
return Error(`${field.keyType} map keys not supported`);
// TODO: Check that firestoreValue is an object.
if (typeof firestoreValue !== 'object' || Array.isArray(firestoreValue))
return Error(`Expected object, but got ${firestoreValue}`);
return toMapValue(messageTypePath, fieldType, firestoreValue);
} else if (field?.rule === 'repeated') {
if (!Array.isArray(firestoreValue))
Expand All @@ -96,6 +97,7 @@ function toMapValue(
valueType: string,
nestedObject: [key: string]
): any | null {
if (!nestedObject) return null;
const messageMap: { [key: string]: any } = {};
for (const key in nestedObject) {
const firestoreValue = nestedObject[key];
Expand Down Expand Up @@ -149,8 +151,7 @@ function toMessageOrEnumValue(
? registry.getMessageDescriptor(constructor)
: null;
if (constructor && nestedDescriptor) {
if (!nestedDescriptor)
return Error(`Unknown message type ${nestedDescriptor}`);
if (firestoreValue === null || firestoreValue === undefined) return null;
// TODO: Check firestoreValue is a DocumentData.
return toMessageInternal(
firestoreValue as DocumentData,
Expand Down
Loading