|
| 1 | +// Extension for reflection interoperability with descriptor.proto types |
| 2 | +// var protobuf = require("protobufjs"), |
| 3 | +// descriptor = require("protobufjs/ext/descriptor"); |
| 4 | +// ... |
| 5 | +"use strict"; |
| 6 | + |
| 7 | +/** |
| 8 | + * Descriptor extension. |
| 9 | + * @namespace |
| 10 | + */ |
| 11 | +var descriptor = exports; |
| 12 | + |
| 13 | +var protobuf = require(".."); |
| 14 | + |
| 15 | +var root = protobuf.Root.fromJSON(require("../google/protobuf/descriptor.json")).resolveAll(), |
| 16 | + Root = protobuf.Root, |
| 17 | + Type = protobuf.Type, |
| 18 | + Field = protobuf.Field; |
| 19 | + |
| 20 | +// Root |
| 21 | + |
| 22 | +/** |
| 23 | + * FileDescriptorSet describing a root. |
| 24 | + * @type {Type} |
| 25 | + */ |
| 26 | +descriptor.FileDescriptorSet = root.lookupType(".google.protobuf.FileDescriptorSet"); |
| 27 | + |
| 28 | +/** |
| 29 | + * Creates a root from a descriptor set. |
| 30 | + * @param {FileDescriptorSet|Reader|Uint8Array} descriptor Descriptor |
| 31 | + * @returns {Root} Root instance |
| 32 | + */ |
| 33 | +protobuf.Root.fromDescriptor = function fromDescriptorSet(descriptor) { |
| 34 | + throw Error("not implemented"); |
| 35 | +}; |
| 36 | + |
| 37 | +/** |
| 38 | + * Converts a root to a descriptor set. |
| 39 | + * @returns {FileDescriptorSet} Descriptor |
| 40 | + */ |
| 41 | +protobuf.Root.prototype.toDescriptor = function toDescriptorSet() { |
| 42 | + throw Error("not implemented"); |
| 43 | +}; |
| 44 | + |
| 45 | +// Type |
| 46 | + |
| 47 | +/** |
| 48 | + * DescriptorProto describing a type. |
| 49 | + * @type {Type} |
| 50 | + */ |
| 51 | +descriptor.DescriptorProto = root.lookupType(".google.protobuf.DescriptorProto"); |
| 52 | + |
| 53 | +/** |
| 54 | + * Creates a type from a descriptor. |
| 55 | + * @param {DescriptorProto|Reader|Uint8Array} descriptor Descriptor |
| 56 | + * @returns {Type} Type instance |
| 57 | + */ |
| 58 | +protobuf.Type.fromDescriptor = function fromDescriptor(descriptor) { |
| 59 | + |
| 60 | + // Decode the descriptor message if specified as a buffer: |
| 61 | + if (typeof descriptor.length === "number") |
| 62 | + descriptor = DescriptorProto.decode(reader); |
| 63 | + |
| 64 | + // Create the message type |
| 65 | + var type = new protobuf.Type(descriptor.name); |
| 66 | + |
| 67 | + // Add fields |
| 68 | + descriptor.field.map(protobuf.Field.fromDescriptor).forEach(protobuf.Type.prototype.add.bind(type)); |
| 69 | + |
| 70 | + // etc. |
| 71 | + |
| 72 | + return type; |
| 73 | +}; |
| 74 | + |
| 75 | +/** |
| 76 | + * Converts a type to a descriptor. |
| 77 | + * @returns {DescriptorProto} |
| 78 | + */ |
| 79 | +protobuf.Type.prototype.toDescriptor = function toDescriptor() { |
| 80 | + throw Error("not implemented"); |
| 81 | +}; |
| 82 | + |
| 83 | +// Field |
| 84 | + |
| 85 | +/** |
| 86 | + * FieldDescriptorProto describing a field. |
| 87 | + * @type {Type} |
| 88 | + */ |
| 89 | +descriptor.FieldDescriptorProto = root.lookupType(".google.protobuf.FieldDescriptorProto"); |
| 90 | + |
| 91 | +/** |
| 92 | + * Creates a field from a descriptor. |
| 93 | + * @param {FieldDescriptorProto|Reader|Uint8Array} descriptor Descriptor |
| 94 | + * @returns {Field} Field instance |
| 95 | + */ |
| 96 | +protobuf.Field.fromDescriptor = function fromDescriptor(descriptor) { |
| 97 | + throw Error("not implemented"); |
| 98 | +}; |
| 99 | + |
| 100 | +/** |
| 101 | + * Converts a field to a descriptor. |
| 102 | + * @returns {FieldDescriptorProto} Descriptor |
| 103 | + */ |
| 104 | +protobuf.Field.prototype.toDescriptor = function toDescriptor() { |
| 105 | + throw Error("not implemented"); |
| 106 | +}; |
| 107 | + |
| 108 | +// etc. |
0 commit comments