mirror of https://github.com/grpc/grpc-node.git
Enable loadFileDescriptorSetFile to parse JSON files
This commit is contained in:
parent
bf98c167cd
commit
63af3bcd6a
|
@ -391,12 +391,12 @@ export function loadFileDescriptorSet(
|
|||
options = options || {};
|
||||
|
||||
let decodedDescriptorSet: DecodedDescriptorSet;
|
||||
if (typeof descriptorSet === 'object') {
|
||||
decodedDescriptorSet = descriptor.FileDescriptorSet.fromObject(
|
||||
if (Buffer.isBuffer(descriptorSet)) {
|
||||
decodedDescriptorSet = descriptor.FileDescriptorSet.decode(
|
||||
descriptorSet
|
||||
) as DecodedDescriptorSet;
|
||||
} else {
|
||||
decodedDescriptorSet = descriptor.FileDescriptorSet.decode(
|
||||
decodedDescriptorSet = descriptor.FileDescriptorSet.fromObject(
|
||||
descriptorSet
|
||||
) as DecodedDescriptorSet;
|
||||
}
|
||||
|
@ -418,7 +418,12 @@ export function loadFileDescriptorSetFile(
|
|||
return reject(err);
|
||||
}
|
||||
|
||||
return resolve(loadFileDescriptorSet(data, options));
|
||||
try {
|
||||
const jsonData = JSON.parse(data.toString());
|
||||
return resolve(loadFileDescriptorSet(jsonData, options));
|
||||
} catch (e) {
|
||||
return resolve(loadFileDescriptorSet(data, options));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
|
@ -103,7 +103,12 @@ describe('Descriptor types', () => {
|
|||
|
||||
it('Can load binary-encoded proto file descriptor sets', () => {
|
||||
// This will throw if the rpc descriptor cannot be decoded
|
||||
proto_loader.loadFileDescriptorSetFile(`${TEST_PROTO_DIR}/rpc.desc`);
|
||||
proto_loader.loadFileDescriptorSetFile(`${TEST_PROTO_DIR}/rpc.desc.bin`);
|
||||
});
|
||||
|
||||
it('Can load json file descriptor sets', () => {
|
||||
// This will throw if the rpc descriptor JSON cannot be decoded
|
||||
proto_loader.loadFileDescriptorSetFile(`${TEST_PROTO_DIR}/rpc.desc.json`);
|
||||
});
|
||||
|
||||
it('Can parse plain file descriptor set objects', () => {
|
||||
|
|
|
@ -0,0 +1,46 @@
|
|||
{
|
||||
"file": [
|
||||
{
|
||||
"name": "test_protos/rpc.proto",
|
||||
"messageType": [
|
||||
{
|
||||
"name": "MyRequest",
|
||||
"field": [
|
||||
{
|
||||
"name": "path",
|
||||
"number": 1,
|
||||
"label": "LABEL_OPTIONAL",
|
||||
"type": "TYPE_STRING",
|
||||
"jsonName": "path"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "MyResponse",
|
||||
"field": [
|
||||
{
|
||||
"name": "status",
|
||||
"number": 2,
|
||||
"label": "LABEL_OPTIONAL",
|
||||
"type": "TYPE_INT32",
|
||||
"jsonName": "status"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"service": [
|
||||
{
|
||||
"name": "MyService",
|
||||
"method": [
|
||||
{
|
||||
"name": "MyMethod",
|
||||
"inputType": ".MyRequest",
|
||||
"outputType": ".MyResponse"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"syntax": "proto3"
|
||||
}
|
||||
]
|
||||
}
|
Loading…
Reference in New Issue