Skip to content
Snippets Groups Projects
Commit ac7f90da authored by murgatroid99's avatar murgatroid99
Browse files

Add enumsAsStrings option, as the original upgrade PR did

parent c1b758f4
No related branches found
No related tags found
No related merge requests found
...@@ -64,6 +64,8 @@ grpc.setDefaultRootsPem(fs.readFileSync(SSL_ROOTS_PATH, 'ascii')); ...@@ -64,6 +64,8 @@ grpc.setDefaultRootsPem(fs.readFileSync(SSL_ROOTS_PATH, 'ascii'));
* Buffers. Defaults to false * Buffers. Defaults to false
* - longsAsStrings: deserialize long values as strings instead of objects. * - longsAsStrings: deserialize long values as strings instead of objects.
* Defaults to true * Defaults to true
* - enumsAsStrings: deserialize enum values as strings instead of numbers.
* Defaults to true
* - deprecatedArgumentOrder: Use the beta method argument order for client * - deprecatedArgumentOrder: Use the beta method argument order for client
* methods, with optional arguments after the callback. Defaults to false. * methods, with optional arguments after the callback. Defaults to false.
* This option is only a temporary stopgap measure to smooth an API breakage. * This option is only a temporary stopgap measure to smooth an API breakage.
...@@ -131,6 +133,8 @@ function applyProtoRoot(filename, root) { ...@@ -131,6 +133,8 @@ function applyProtoRoot(filename, root) {
* Buffers. Defaults to false * Buffers. Defaults to false
* - longsAsStrings: deserialize long values as strings instead of objects. * - longsAsStrings: deserialize long values as strings instead of objects.
* Defaults to true * Defaults to true
* - enumsAsStrings: deserialize enum values as strings instead of numbers.
* Defaults to true
* - deprecatedArgumentOrder: Use the beta method argument order for client * - deprecatedArgumentOrder: Use the beta method argument order for client
* methods, with optional arguments after the callback. Defaults to false. * methods, with optional arguments after the callback. Defaults to false.
* This option is only a temporary stopgap measure to smooth an API breakage. * This option is only a temporary stopgap measure to smooth an API breakage.
......
...@@ -87,5 +87,6 @@ exports.defaultGrpcOptions = { ...@@ -87,5 +87,6 @@ exports.defaultGrpcOptions = {
convertFieldsToCamelCase: false, convertFieldsToCamelCase: false,
binaryAsBase64: false, binaryAsBase64: false,
longsAsStrings: true, longsAsStrings: true,
enumsAsStrings: true,
deprecatedArgumentOrder: false deprecatedArgumentOrder: false
}; };
...@@ -50,7 +50,7 @@ exports.deserializeCls = function deserializeCls(cls, options) { ...@@ -50,7 +50,7 @@ exports.deserializeCls = function deserializeCls(cls, options) {
defaults: true, defaults: true,
bytes: options.binaryAsBase64 ? String : Buffer, bytes: options.binaryAsBase64 ? String : Buffer,
longs: options.longsAsStrings ? String : null, longs: options.longsAsStrings ? String : null,
enums: String, enums: options.enumsAsStrings ? String : null,
oneofs: true oneofs: true
}; };
/** /**
......
...@@ -179,3 +179,25 @@ describe('Proto message oneof serialize and deserialize', function() { ...@@ -179,3 +179,25 @@ describe('Proto message oneof serialize and deserialize', function() {
assert.equal(deserialized2.oneof_choice, 'int_choice'); 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 { ...@@ -47,4 +47,14 @@ message OneOfValues {
int32 int_choice = 1; int32 int_choice = 1;
string string_choice = 2; string string_choice = 2;
} }
}
enum TestEnum {
ZERO = 0;
ONE = 1;
TWO = 2;
}
message EnumValues {
TestEnum enum_value = 1;
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment