The proto2 getField() accessors return undefined when called on an unset field.
This behavior is expected by the unit tests:
http://code.google.com/p/closure-library/source/browse/trunk/closure/goog/proto2
/proto_test.html
// Check non-set values.
assertUndefined(message.getOptionalInt32());
assertUndefined(message.getOptionalInt64());
assertUndefined(message.getOptionalFloat());
assertUndefined(message.getOptionalString());
assertUndefined(message.getOptionalBytes());
assertUndefined(message.getOptionalNestedMessage());
assertUndefined(message.getOptionalNestedEnum());
However, the jsdoc type generated in the sample *.pb.js files, implies that
null should be returned instead.
http://code.google.com/p/closure-library/source/browse/trunk/closure/goog/proto2
/test.pb.js
/**
* Gets the value of the optional_int32 field.
* @return {?number} The value.
*/
proto2.TestAllTypes.prototype.getOptionalInt32 = function() {
return /** @type {?number} */ (this.get$Value(1));
};
I believe the best solution is return null (instead of undefined) when a field
is unset.
Original issue reported on code.google.com by ahochhau...@gtempaccount.com on 16 May 2011 at 11:37
Original issue reported on code.google.com by
ahochhau...@gtempaccount.com
on 16 May 2011 at 11:37