mirror of https://github.com/grpc/grpc-node.git
Node: add options to modify ProtoBuf behavior
This commit is contained in:
parent
30669be4c9
commit
0fbb313c3b
22
index.js
22
index.js
|
@ -56,17 +56,18 @@ var grpc = require('./src/grpc_extension');
|
||||||
/**
|
/**
|
||||||
* Load a gRPC object from an existing ProtoBuf.Reflect object.
|
* Load a gRPC object from an existing ProtoBuf.Reflect object.
|
||||||
* @param {ProtoBuf.Reflect.Namespace} value The ProtoBuf object to load.
|
* @param {ProtoBuf.Reflect.Namespace} value The ProtoBuf object to load.
|
||||||
|
* @param {Object=} options Options to apply to the loaded object
|
||||||
* @return {Object<string, *>} The resulting gRPC object
|
* @return {Object<string, *>} The resulting gRPC object
|
||||||
*/
|
*/
|
||||||
exports.loadObject = function loadObject(value) {
|
exports.loadObject = function loadObject(value, options) {
|
||||||
var result = {};
|
var result = {};
|
||||||
if (value.className === 'Namespace') {
|
if (value.className === 'Namespace') {
|
||||||
_.each(value.children, function(child) {
|
_.each(value.children, function(child) {
|
||||||
result[child.name] = loadObject(child);
|
result[child.name] = loadObject(child, options);
|
||||||
});
|
});
|
||||||
return result;
|
return result;
|
||||||
} else if (value.className === 'Service') {
|
} else if (value.className === 'Service') {
|
||||||
return client.makeProtobufClientConstructor(value);
|
return client.makeProtobufClientConstructor(value, options);
|
||||||
} else if (value.className === 'Message' || value.className === 'Enum') {
|
} else if (value.className === 'Message' || value.className === 'Enum') {
|
||||||
return value.build();
|
return value.build();
|
||||||
} else {
|
} else {
|
||||||
|
@ -78,16 +79,22 @@ var loadObject = exports.loadObject;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load a gRPC object from a .proto file.
|
* Load a gRPC object from a .proto file.
|
||||||
* @param {string} filename The file to load
|
* @param {string|{root: string, file: string}} filename The file to load
|
||||||
* @param {string=} format The file format to expect. Must be either 'proto' or
|
* @param {string=} format The file format to expect. Must be either 'proto' or
|
||||||
* 'json'. Defaults to 'proto'
|
* 'json'. Defaults to 'proto'
|
||||||
|
* @param {Object=} options Options to apply to the loaded file
|
||||||
* @return {Object<string, *>} The resulting gRPC object
|
* @return {Object<string, *>} The resulting gRPC object
|
||||||
*/
|
*/
|
||||||
exports.load = function load(filename, format) {
|
exports.load = function load(filename, format, options) {
|
||||||
if (!format) {
|
if (!format) {
|
||||||
format = 'proto';
|
format = 'proto';
|
||||||
}
|
}
|
||||||
|
var convertFieldsToCamelCaseOriginal = ProtoBuf.convertFieldsToCamelCase;
|
||||||
|
if(options && options.hasOwnProperty('convertFieldsToCamelCase')) {
|
||||||
|
ProtoBuf.convertFieldsToCamelCase = options.convertFieldsToCamelCase;
|
||||||
|
}
|
||||||
var builder;
|
var builder;
|
||||||
|
try {
|
||||||
switch(format) {
|
switch(format) {
|
||||||
case 'proto':
|
case 'proto':
|
||||||
builder = ProtoBuf.loadProtoFile(filename);
|
builder = ProtoBuf.loadProtoFile(filename);
|
||||||
|
@ -98,7 +105,10 @@ exports.load = function load(filename, format) {
|
||||||
default:
|
default:
|
||||||
throw new Error('Unrecognized format "' + format + '"');
|
throw new Error('Unrecognized format "' + format + '"');
|
||||||
}
|
}
|
||||||
return loadObject(builder.ns);
|
} finally {
|
||||||
|
ProtoBuf.convertFieldsToCamelCase = convertFieldsToCamelCaseOriginal;
|
||||||
|
}
|
||||||
|
return loadObject(builder.ns, options);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -698,13 +698,15 @@ exports.waitForClientReady = function(client, deadline, callback) {
|
||||||
* Creates a constructor for clients for the given service
|
* Creates a constructor for clients for the given service
|
||||||
* @param {ProtoBuf.Reflect.Service} service The service to generate a client
|
* @param {ProtoBuf.Reflect.Service} service The service to generate a client
|
||||||
* for
|
* for
|
||||||
|
* @param {Object=} options Options to apply to the client
|
||||||
* @return {function(string, Object)} New client constructor
|
* @return {function(string, Object)} New client constructor
|
||||||
*/
|
*/
|
||||||
exports.makeProtobufClientConstructor = function(service) {
|
exports.makeProtobufClientConstructor = function(service, options) {
|
||||||
var method_attrs = common.getProtobufServiceAttrs(service, service.name);
|
var method_attrs = common.getProtobufServiceAttrs(service, service.name, options);
|
||||||
var Client = exports.makeClientConstructor(
|
var Client = exports.makeClientConstructor(
|
||||||
method_attrs, common.fullyQualifiedName(service));
|
method_attrs, common.fullyQualifiedName(service));
|
||||||
Client.service = service;
|
Client.service = service;
|
||||||
|
Client.service.grpc_options = options;
|
||||||
return Client;
|
return Client;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -44,9 +44,20 @@ var _ = require('lodash');
|
||||||
/**
|
/**
|
||||||
* Get a function that deserializes a specific type of protobuf.
|
* Get a function that deserializes a specific type of protobuf.
|
||||||
* @param {function()} cls The constructor of the message type to deserialize
|
* @param {function()} cls The constructor of the message type to deserialize
|
||||||
|
* @param {bool=} binaryAsBase64 Deserialize bytes fields as base64 instead of binary.
|
||||||
|
* Defaults to false
|
||||||
|
* @param {bool=} longsAsStrings Deserialize long values as strings instead of doubles.
|
||||||
|
* Defaults to true
|
||||||
* @return {function(Buffer):cls} The deserialization function
|
* @return {function(Buffer):cls} The deserialization function
|
||||||
*/
|
*/
|
||||||
exports.deserializeCls = function deserializeCls(cls) {
|
exports.deserializeCls = function deserializeCls(cls, binaryAsBase64,
|
||||||
|
longsAsStrings) {
|
||||||
|
if (binaryAsBase64 === undefined || binaryAsBase64 === null) {
|
||||||
|
binaryAsBase64 = false;
|
||||||
|
}
|
||||||
|
if (longsAsStrings === undefined || longsAsStrings === null) {
|
||||||
|
longsAsStrings = true;
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* Deserialize a buffer to a message object
|
* Deserialize a buffer to a message object
|
||||||
* @param {Buffer} arg_buf The buffer to deserialize
|
* @param {Buffer} arg_buf The buffer to deserialize
|
||||||
|
@ -55,7 +66,7 @@ exports.deserializeCls = function deserializeCls(cls) {
|
||||||
return function deserialize(arg_buf) {
|
return function deserialize(arg_buf) {
|
||||||
// Convert to a native object with binary fields as Buffers (first argument)
|
// Convert to a native object with binary fields as Buffers (first argument)
|
||||||
// and longs as strings (second argument)
|
// and longs as strings (second argument)
|
||||||
return cls.decode(arg_buf).toRaw(false, true);
|
return cls.decode(arg_buf).toRaw(binaryAsBase64, longsAsStrings);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -119,19 +130,27 @@ exports.wrapIgnoreNull = function wrapIgnoreNull(func) {
|
||||||
/**
|
/**
|
||||||
* Return a map from method names to method attributes for the service.
|
* Return a map from method names to method attributes for the service.
|
||||||
* @param {ProtoBuf.Reflect.Service} service The service to get attributes for
|
* @param {ProtoBuf.Reflect.Service} service The service to get attributes for
|
||||||
|
* @param {Object=} options Options to apply to these attributes
|
||||||
* @return {Object} The attributes map
|
* @return {Object} The attributes map
|
||||||
*/
|
*/
|
||||||
exports.getProtobufServiceAttrs = function getProtobufServiceAttrs(service) {
|
exports.getProtobufServiceAttrs = function getProtobufServiceAttrs(service, options) {
|
||||||
var prefix = '/' + fullyQualifiedName(service) + '/';
|
var prefix = '/' + fullyQualifiedName(service) + '/';
|
||||||
|
var binaryAsBase64, longsAsStrings;
|
||||||
|
if (options) {
|
||||||
|
binaryAsBase64 = options.binaryAsBase64;
|
||||||
|
longsAsStrings = options.longsAsStrings;
|
||||||
|
}
|
||||||
return _.object(_.map(service.children, function(method) {
|
return _.object(_.map(service.children, function(method) {
|
||||||
return [_.camelCase(method.name), {
|
return [_.camelCase(method.name), {
|
||||||
path: prefix + method.name,
|
path: prefix + method.name,
|
||||||
requestStream: method.requestStream,
|
requestStream: method.requestStream,
|
||||||
responseStream: method.responseStream,
|
responseStream: method.responseStream,
|
||||||
requestSerialize: serializeCls(method.resolvedRequestType.build()),
|
requestSerialize: serializeCls(method.resolvedRequestType.build()),
|
||||||
requestDeserialize: deserializeCls(method.resolvedRequestType.build()),
|
requestDeserialize: deserializeCls(method.resolvedRequestType.build(),
|
||||||
|
binaryAsBase64, longsAsStrings),
|
||||||
responseSerialize: serializeCls(method.resolvedResponseType.build()),
|
responseSerialize: serializeCls(method.resolvedResponseType.build()),
|
||||||
responseDeserialize: deserializeCls(method.resolvedResponseType.build())
|
responseDeserialize: deserializeCls(method.resolvedResponseType.build(),
|
||||||
|
binaryAsBase64, longsAsStrings)
|
||||||
}];
|
}];
|
||||||
}));
|
}));
|
||||||
};
|
};
|
||||||
|
|
|
@ -737,7 +737,12 @@ Server.prototype.addService = function(service, implementation) {
|
||||||
* method implementation for the provided service.
|
* method implementation for the provided service.
|
||||||
*/
|
*/
|
||||||
Server.prototype.addProtoService = function(service, implementation) {
|
Server.prototype.addProtoService = function(service, implementation) {
|
||||||
this.addService(common.getProtobufServiceAttrs(service), implementation);
|
var options;
|
||||||
|
if (service.grpc_options) {
|
||||||
|
options = service.grpc_options;
|
||||||
|
}
|
||||||
|
this.addService(common.getProtobufServiceAttrs(service, options),
|
||||||
|
implementation);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue