Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add package definition loader function to native package #196

Merged
merged 2 commits into from
Feb 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions packages/grpc-native-core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,29 @@ exports.load = function load(filename, format, options) {
return loadObject(builder.ns, options);
};

/**
* Load a gRPC package definition as a gRPC object hierarchy
* @param packageDef grpc~PackageDefinition The package definition object
* @return {Object<string, *>} The resulting gRPC object
*/
exports.loadPackageDefinition = function loadPackageDefintion(packageDef) {
const result = {};
for (const serviceFqn in packageDef) {
const service = packageDef[serviceFqn];
const nameComponents = serviceFqn.split('.');
const serviceName = nameComponents[nameComponents.length-1];
let current = result;
for (const packageName in nameComponents.slice(0, -1)) {
if (!current[packageName]) {
current[packageName] = {};
}
current = current[packageName];
}
current[serviceName] = client.makeClientConstructor(service, serviceName, {});
}
return result;
};

var log_template = _.template(
'{severity} {timestamp}\t{file}:{line}]\t{message}',
{interpolate: /{([\s\S]+?)}/g});
Expand Down
5 changes: 5 additions & 0 deletions packages/grpc-native-core/src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,8 @@ exports.defaultGrpcOptions = {
* An object that completely defines a service.
* @typedef {Object.<string, grpc~MethodDefinition>} grpc~ServiceDefinition
*/

/**
* An object that defines a package hierarchy with multiple services
* @typedef {Object.<string, grpc~ServiceDefinition>} grpc~PackageDefinition
*/