From 71dbb8636d75d7de0cb575c2156c3d16a34470b7 Mon Sep 17 00:00:00 2001
From: murgatroid99 <mlumish@google.com>
Date: Mon, 20 Apr 2015 11:22:51 -0700
Subject: [PATCH] Added JSON option for gRPC file loading

---
 src/node/index.js               | 19 ++++++++++--
 src/node/test/surface_test.js   | 22 +++++++++++++
 src/node/test/test_service.json | 55 +++++++++++++++++++++++++++++++++
 3 files changed, 94 insertions(+), 2 deletions(-)
 create mode 100644 src/node/test/test_service.json

diff --git a/src/node/index.js b/src/node/index.js
index 0b768edc6b..875756328d 100644
--- a/src/node/index.js
+++ b/src/node/index.js
@@ -67,10 +67,25 @@ function loadObject(value) {
 /**
  * Load a gRPC object from a .proto file.
  * @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
  */
-function load(filename) {
-  var builder = ProtoBuf.loadProtoFile(filename);
+function load(filename, format) {
+  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);
 }
diff --git a/src/node/test/surface_test.js b/src/node/test/surface_test.js
index 590c644c71..6f63f1044f 100644
--- a/src/node/test/surface_test.js
+++ b/src/node/test/surface_test.js
@@ -47,6 +47,28 @@ var mathService = math_proto.lookup('math.Math');
 
 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() {
   it('Should fail with conflicting method names', function() {
     assert.throws(function() {
diff --git a/src/node/test/test_service.json b/src/node/test/test_service.json
new file mode 100644
index 0000000000..6f952c6ad2
--- /dev/null
+++ b/src/node/test/test_service.json
@@ -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": {}
+                }
+            }
+        }
+    ]
+}
\ No newline at end of file
-- 
GitLab