const ProtoBuf = require('protobufjs');
const grpc = require('grpc');
const path = require('path');

const listServices = function(root, def) {

	return Object.entries(def).reduce((results, [ propName, value ]) => {

		let objPath = propName;
		if (root) {
			objPath = root + '.' + propName;
		}

		if (value.hasOwnProperty('service')) {
			results[objPath] = value;
		} else {
			return Object.assign(results, listServices(objPath, value));
		}

		return results;
	}, {});
};

const loadProto = async function(filename) {

	const root = new ProtoBuf.Root();

	// Support google extension
	let _resolve = root.resolvePath;
	root.resolvePath = (origin, target) => {

		if (target.search('google') === 0) {
			return path.join(__dirname, '..', 'node_modules/google-proto-files', target);
		}

		return _resolve(origin, target);
	};

	// Loading file
	let parsedObj = await ProtoBuf.load(filename, root);
	let def = grpc.loadObject(parsedObj);

	return {
		services: listServices('', def)
	};
};


module.exports = {
	loadProto: loadProto,
	listServices: listServices
};

@@ -306,65 +306,188 @@
 alue
-.hasOwnProperty('service')) %7B%0A%09%09%09results%5BobjPath%5D = value
+ !== undefined && value.hasOwnProperty('super_') && value.hasOwnProperty('service')) %7B%0A%09%09%09results%5BobjPath%5D = value;%0A%09%09%7D else if (value instanceof ProtoBuf.Enum) %7B%0A%09%09%09return results
 ;%0A%09%09
@@ -601,25 +601,27 @@
 %0A%0Aconst 
-loadPr
+prepareRo
 ot
-o
  = async
@@ -630,24 +630,16 @@
 unction(
-filename
 ) %7B%0A%0A%09co
@@ -942,16 +942,344 @@
 );%0A%09%7D;%0A%0A
+%09return root;%0A%7D%0A%0Aconst parseProto = async function(proto) %7B%0A%0A%09const root = await prepareRoot();%0A%0A%09let parserResult = ProtoBuf.parse(proto, root);%0A%09let def = grpc.loadObject(parserResult.root);%0A%0A%09return %7B%0A%09%09services: listServices('', def)%0A%09%7D;%0A%7D;%0A%0Aconst loadProto = async function(filename) %7B%0A%0A%09const root = await prepareRoot();%0A%0A
 %09// Load
@@ -1453,16 +1453,41 @@
 rts = %7B%0A
+%09parseProto: parseProto,%0A
 %09loadPro

support Enum object