Skip to content

Commit 77a4e77

Browse files
committed
converter.fromObject: Do not skip null-checking all elements to unbreak optional fields, skip the check if it's a .google.protobuf.Value object so it can properly generate a NullValue
1 parent af52df0 commit 77a4e77

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

src/converter.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,14 @@ function genValuePartial_fromObject(gen, field, fieldIndex, prop) {
3232
("break");
3333
} gen
3434
("}");
35+
} else if (field.resolvedType.fullName === '.google.protobuf.Value') {
36+
// .google.protobuf.Value can render multiple types
37+
gen
38+
("m%s=types[%i].fromObject(d%s)", prop, fieldIndex, prop);
3539
} else {
3640
gen
37-
// ("if(typeof d%s!==\"object\")", prop)
38-
// ("throw TypeError(%j)", field.fullName + ": object expected")
41+
("if(typeof d%s!==\"object\")", prop)
42+
("throw TypeError(%j)", field.fullName + ": object expected")
3943
("m%s=types[%i].fromObject(d%s)", prop, fieldIndex, prop);
4044
}
4145
} else {
@@ -134,11 +138,12 @@ converter.fromObject = function fromObject(mtype) {
134138

135139
// Non-repeated fields
136140
} else {
137-
if (!(field.resolvedType instanceof Enum)) gen // no need to test for null/undefined if an enum (uses switch)
138-
// ("if(d%s!=null){", prop); // !== undefined && !== null
141+
var needsGuard = !(field.resolvedType instanceof Enum || (field.resolvedType && field.resolvedType.fullName === '.google.protobuf.Value'));
142+
if (needsGuard) gen // no need to test for null/undefined if an enum (uses switch)
143+
("if(d%s!=null){", prop); // !== undefined && !== null
139144
genValuePartial_fromObject(gen, field, /* not sorted */ i, prop);
140-
// if (!(field.resolvedType instanceof Enum)) gen
141-
// ("}");
145+
if (needsGuard) gen
146+
("}");
142147
}
143148
} return gen
144149
("return m");

src/wrappers.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ wrappers[".google.protobuf.Timestamp"] = {
186186

187187
// given a protobuf Timestamp object, return a plain JS object
188188
toObject: function(message, options) {
189-
var Timestamp = this.lookup("google.protobuf.Timestamp");
189+
190190
return new Date(message.seconds*1000 + message.nanos/1000);
191191
}
192192
}

0 commit comments

Comments
 (0)