Add enumsAsStrings option, as the original upgrade PR did

pull/10204/head
murgatroid99 8 years ago
parent c1b758f488
commit ac7f90da26
  1. 4
      src/node/index.js
  2. 1
      src/node/src/common.js
  3. 2
      src/node/src/protobuf_js_6_common.js
  4. 22
      src/node/test/common_test.js
  5. 10
      src/node/test/test_messages.proto

@ -64,6 +64,8 @@ grpc.setDefaultRootsPem(fs.readFileSync(SSL_ROOTS_PATH, 'ascii'));
* Buffers. Defaults to false
* - longsAsStrings: deserialize long values as strings instead of objects.
* Defaults to true
* - enumsAsStrings: deserialize enum values as strings instead of numbers.
* Defaults to true
* - deprecatedArgumentOrder: Use the beta method argument order for client
* methods, with optional arguments after the callback. Defaults to false.
* This option is only a temporary stopgap measure to smooth an API breakage.
@ -131,6 +133,8 @@ function applyProtoRoot(filename, root) {
* Buffers. Defaults to false
* - longsAsStrings: deserialize long values as strings instead of objects.
* Defaults to true
* - enumsAsStrings: deserialize enum values as strings instead of numbers.
* Defaults to true
* - deprecatedArgumentOrder: Use the beta method argument order for client
* methods, with optional arguments after the callback. Defaults to false.
* This option is only a temporary stopgap measure to smooth an API breakage.

@ -87,5 +87,6 @@ exports.defaultGrpcOptions = {
convertFieldsToCamelCase: false,
binaryAsBase64: false,
longsAsStrings: true,
enumsAsStrings: true,
deprecatedArgumentOrder: false
};

@ -50,7 +50,7 @@ exports.deserializeCls = function deserializeCls(cls, options) {
defaults: true,
bytes: options.binaryAsBase64 ? String : Buffer,
longs: options.longsAsStrings ? String : null,
enums: String,
enums: options.enumsAsStrings ? String : null,
oneofs: true
};
/**

@ -179,3 +179,25 @@ describe('Proto message oneof serialize and deserialize', function() {
assert.equal(deserialized2.oneof_choice, 'int_choice');
});
});
describe('Proto message enum serialize and deserialize', function() {
var enumSerialize = serializeCls(messages_proto.EnumValues);
var enumDeserialize = deserializeCls(
messages_proto.EnumValues, default_options);
var enumIntOptions = _.defaults({enumsAsStrings: false}, default_options);
var enumIntDeserialize = deserializeCls(
messages_proto.EnumValues, enumIntOptions);
it('Should accept both names and numbers', function() {
var nameSerialized = enumSerialize({enum_value: 'ONE'});
var numberSerialized = enumSerialize({enum_value: 1});
assert.strictEqual(messages_proto.TestEnum.ONE, 1);
assert.deepEqual(enumDeserialize(nameSerialized),
enumDeserialize(numberSerialized));
});
it('Should deserialize as a string the enumsAsStrings option', function() {
var serialized = enumSerialize({enum_value: 'TWO'});
var nameDeserialized = enumDeserialize(serialized);
var numberDeserialized = enumIntDeserialize(serialized);
assert.deepEqual(nameDeserialized, {enum_value: 'TWO'});
assert.deepEqual(numberDeserialized, {enum_value: 2});
});
});

@ -47,4 +47,14 @@ message OneOfValues {
int32 int_choice = 1;
string string_choice = 2;
}
}
enum TestEnum {
ZERO = 0;
ONE = 1;
TWO = 2;
}
message EnumValues {
TestEnum enum_value = 1;
}
Loading…
Cancel
Save