Skip to content

Commit

Permalink
Other: Initial descriptor.proto extension for reflection interoperabi…
Browse files Browse the repository at this point in the history
…lity, see #757
  • Loading branch information
dcodeIO committed Apr 12, 2017
1 parent b646cf7 commit fbb9948
Showing 1 changed file with 108 additions and 0 deletions.
108 changes: 108 additions & 0 deletions ext/descriptor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Extension for reflection interoperability with descriptor.proto types
// var protobuf = require("protobufjs"),
// descriptor = require("protobufjs/ext/descriptor");
// ...
"use strict";

/**
* Descriptor extension.
* @namespace
*/
var descriptor = exports;

var protobuf = require("..");

var root = protobuf.Root.fromJSON(require("../google/protobuf/descriptor.json")).resolveAll(),
Root = protobuf.Root,
Type = protobuf.Type,
Field = protobuf.Field;

// Root

/**
* FileDescriptorSet describing a root.
* @type {Type}
*/
descriptor.FileDescriptorSet = root.lookupType(".google.protobuf.FileDescriptorSet");

/**
* Creates a root from a descriptor set.
* @param {FileDescriptorSet|Reader|Uint8Array} descriptor Descriptor
* @returns {Root} Root instance
*/
protobuf.Root.fromDescriptor = function fromDescriptorSet(descriptor) {
throw Error("not implemented");
};

/**
* Converts a root to a descriptor set.
* @returns {FileDescriptorSet} Descriptor
*/
protobuf.Root.prototype.toDescriptor = function toDescriptorSet() {
throw Error("not implemented");
};

// Type

/**
* DescriptorProto describing a type.
* @type {Type}
*/
descriptor.DescriptorProto = root.lookupType(".google.protobuf.DescriptorProto");

/**
* Creates a type from a descriptor.
* @param {DescriptorProto|Reader|Uint8Array} descriptor Descriptor
* @returns {Type} Type instance
*/
protobuf.Type.fromDescriptor = function fromDescriptor(descriptor) {

// Decode the descriptor message if specified as a buffer:
if (typeof descriptor.length === "number")
descriptor = DescriptorProto.decode(reader);

// Create the message type
var type = new protobuf.Type(descriptor.name);

// Add fields
descriptor.field.map(protobuf.Field.fromDescriptor).forEach(protobuf.Type.prototype.add.bind(type));

// etc.

return type;
};

/**
* Converts a type to a descriptor.
* @returns {DescriptorProto}
*/
protobuf.Type.prototype.toDescriptor = function toDescriptor() {
throw Error("not implemented");
};

// Field

/**
* FieldDescriptorProto describing a field.
* @type {Type}
*/
descriptor.FieldDescriptorProto = root.lookupType(".google.protobuf.FieldDescriptorProto");

/**
* Creates a field from a descriptor.
* @param {FieldDescriptorProto|Reader|Uint8Array} descriptor Descriptor
* @returns {Field} Field instance
*/
protobuf.Field.fromDescriptor = function fromDescriptor(descriptor) {
throw Error("not implemented");
};

/**
* Converts a field to a descriptor.
* @returns {FieldDescriptorProto} Descriptor
*/
protobuf.Field.prototype.toDescriptor = function toDescriptor() {
throw Error("not implemented");
};

// etc.

0 comments on commit fbb9948

Please sign in to comment.