Merge pull request #1320 from murgatroid99/node_load_json

Added JSON option for gRPC file loading
pull/1330/head
Tim Emiola 10 years ago
commit d9a3425148
  1. 19
      src/node/index.js
  2. 22
      src/node/test/surface_test.js
  3. 55
      src/node/test/test_service.json

@ -67,10 +67,25 @@ function loadObject(value) {
/** /**
* 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} filename The file to load
* @param {string=} format The file format to expect. Must be either 'proto' or
* 'json'. Defaults to 'proto'
* @return {Object<string, *>} The resulting gRPC object * @return {Object<string, *>} The resulting gRPC object
*/ */
function load(filename) { function load(filename, format) {
var builder = ProtoBuf.loadProtoFile(filename); if (!format) {
format = 'proto';
}
var builder;
switch(format) {
case 'proto':
builder = ProtoBuf.loadProtoFile(filename);
break;
case 'json':
builder = ProtoBuf.loadJsonFile(filename);
break;
default:
throw new Error('Unrecognized format "' + format + '"');
}
return loadObject(builder.ns); return loadObject(builder.ns);
} }

@ -47,6 +47,28 @@ var mathService = math_proto.lookup('math.Math');
var capitalize = require('underscore.string/capitalize'); var capitalize = require('underscore.string/capitalize');
describe('File loader', function() {
it('Should load a proto file by default', function() {
assert.doesNotThrow(function() {
grpc.load(__dirname + '/test_service.proto');
});
});
it('Should load a proto file with the proto format', function() {
assert.doesNotThrow(function() {
grpc.load(__dirname + '/test_service.proto', 'proto');
});
});
it('Should load a json file with the json format', function() {
assert.doesNotThrow(function() {
grpc.load(__dirname + '/test_service.json', 'json');
});
});
it('Should fail to load a file with an unknown format', function() {
assert.throws(function() {
grpc.load(__dirname + '/test_service.proto', 'fake_format');
});
});
});
describe('Surface server constructor', function() { describe('Surface server constructor', function() {
it('Should fail with conflicting method names', function() { it('Should fail with conflicting method names', function() {
assert.throws(function() { assert.throws(function() {

@ -0,0 +1,55 @@
{
"package": null,
"messages": [
{
"name": "Request",
"fields": [
{
"rule": "optional",
"type": "bool",
"name": "error",
"id": 1
}
]
},
{
"name": "Response",
"fields": [
{
"rule": "optional",
"type": "int32",
"name": "count",
"id": 1
}
]
}
],
"services": [
{
"name": "TestService",
"options": {},
"rpc": {
"Unary": {
"request": "Request",
"response": "Response",
"options": {}
},
"ClientStream": {
"request": "Request",
"response": "Response",
"options": {}
},
"ServerStream": {
"request": "Request",
"response": "Response",
"options": {}
},
"BidiStream": {
"request": "Request",
"response": "Response",
"options": {}
}
}
}
]
}
Loading…
Cancel
Save