Skip to content

Commit 708552b

Browse files
committed
Other: Added infrastructure for TypeScript support of extensions
1 parent 57d7d35 commit 708552b

10 files changed

Lines changed: 187 additions & 99 deletions

File tree

cli/lib/tsd-jsdoc/publish.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,6 @@ function notAModuleReference(ref) {
393393
return ref.indexOf("module:") === -1;
394394
}
395395

396-
397396
// handles a class or class-like
398397
function handleClass(element, parent) {
399398
var is_interface = isInterface(element);

cli/targets/static.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ function buildType(ref, type) {
367367
"@constructor",
368368
"@param {" + fullName + "$Properties=} [" + (config.beautify ? "properties" : "p") + "] Properties to set"
369369
]);
370-
buildFunction(type, type.name, Class.generate(type));
370+
buildFunction(type, type.name, Type.generateConstructor(type));
371371

372372
// default values
373373
var firstField = true;

ext/descriptor/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
protobufjs/ext/descriptor
2+
=========================
3+
4+
Experimental [protobuf.js](https://github.com/dcodeIO/protobuf.js) extension for interoperability with descriptor.proto types.
5+
6+
Usage
7+
-----
8+
9+
```js
10+
var protobuf = require("protobufjs"),
11+
descriptor = require("protobufjs/ext/descriptor");
12+
13+
var descriptor = ...; // either a FieldDescriptorSet buffer or JSON object
14+
var root = protobuf.Root.fromDescriptor(descriptor);
15+
var rootDescriptor = root.toDescriptor("proto3");
16+
```
17+
18+
API
19+
---
20+
21+
The extension adds `.fromDescriptor(descriptor[, syntax])` and `#toDescriptor([syntax])` methods to reflection objects and exports the internally used `Root` instance that contains the types present in descriptor.proto.

ext/descriptor/index.d.ts

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import * as $protobuf from "../..";
2+
3+
declare const descriptor: $protobuf.Root;
4+
5+
interface IFileDescriptorSet {
6+
file: IFileDescriptorProto[];
7+
}
8+
9+
interface IFileDescriptorProto {
10+
name?: string;
11+
package?: string;
12+
dependency?: any;
13+
publicDependency?: any;
14+
weakDependency?: any;
15+
messageType?: IDescriptorProto[];
16+
enumType?: IEnumDescriptorProto[];
17+
service?: IServiceDescriptorProto[];
18+
extension?: IFieldDescriptorProto[];
19+
options?: any;
20+
sourceCodeInfo?: any;
21+
syntax?: string;
22+
}
23+
24+
interface IDescriptorProto {
25+
name?: string;
26+
field?: IFieldDescriptorProto[];
27+
extension?: IFieldDescriptorProto[];
28+
nestedType?: IDescriptorProto[];
29+
enumType?: IEnumDescriptorProto[];
30+
extensionRange?: IExtensionRange[];
31+
oneofDecl?: IOneofDescriptorProto[];
32+
options?: IMessageOptions;
33+
reservedRange?: IReservedRange[];
34+
reservedName?: string[];
35+
}
36+
37+
interface IMessageOptions {
38+
mapEntry?: any;
39+
}
40+
41+
interface IExtensionRange {
42+
start?: number;
43+
end?: number;
44+
}
45+
46+
interface IReservedRange {
47+
start?: number;
48+
end?: number;
49+
}
50+
51+
interface IFieldDescriptorProto {
52+
name?: string;
53+
number?: number;
54+
label?: IFieldDescriptorProto_Label;
55+
type?: IFieldDescriptorProto_Type;
56+
typeName?: string;
57+
extendee?: string;
58+
defaultValue?: any;
59+
oneofIndex?: number;
60+
jsonName?: any;
61+
options?: IFieldOptions;
62+
}
63+
64+
type IFieldDescriptorProto_Label = number;
65+
66+
type IFieldDescriptorProto_Type = number;
67+
68+
interface IFieldOptions {
69+
packed?: boolean;
70+
}
71+
72+
interface IEnumDescriptorProto {
73+
name?: string;
74+
value?: IEnumValueDescriptorProto[];
75+
options?: IEnumOptions;
76+
}
77+
78+
interface IEnumValueDescriptorProto {
79+
name?: string;
80+
number?: number;
81+
options?: any;
82+
}
83+
84+
interface IEnumOptions {
85+
allowAlias?: boolean;
86+
}
87+
88+
interface IOneofDescriptorProto {
89+
name?: string;
90+
options?: any;
91+
}
92+
93+
interface IServiceDescriptorProto {
94+
name?: string;
95+
method?: IMethodDescriptorProto[];
96+
options?: any;
97+
}
98+
99+
interface IMethodDescriptorProto {
100+
name?: string;
101+
inputType?: string;
102+
outputType?: string;
103+
options?: any;
104+
clientStreaming?: boolean;
105+
serverStreaming?: boolean;
106+
}
107+
108+
export = descriptor;
Lines changed: 18 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,26 @@
1-
// [WIP] Extension for reflection interoperability with descriptor.proto types
2-
// var protobuf = require("protobufjs"),
3-
// descriptor = require("protobufjs/ext/descriptor");
4-
// ...
51
"use strict";
62

7-
var protobuf = require("..");
3+
var $protobuf = require("..");
84

95
/**
106
* Descriptor extension (ext/descriptor).
11-
* @namespace
7+
* @type {Root}
8+
* @tstype $protobuf.Root
9+
* @const
1210
*/
13-
14-
var descriptor = module.exports = protobuf.Root.fromJSON(require("../google/protobuf/descriptor.json")).lookup(".google.protobuf");
11+
var descriptor = module.exports = $protobuf.Root.fromJSON(require("../google/protobuf/descriptor.json")).lookup(".google.protobuf");
1512

1613
var google = descriptor,
17-
Root = protobuf.Root,
18-
Enum = protobuf.Enum,
19-
Type = protobuf.Type,
20-
Field = protobuf.Field,
21-
OneOf = protobuf.OneOf,
22-
Service = protobuf.Service,
23-
Method = protobuf.Method;
14+
Root = $protobuf.Root,
15+
Enum = $protobuf.Enum,
16+
Type = $protobuf.Type,
17+
Field = $protobuf.Field,
18+
OneOf = $protobuf.OneOf,
19+
Service = $protobuf.Service,
20+
Method = $protobuf.Method;
2421

2522
// --- Root ---
2623

27-
/**
28-
* Reflected type describing a root.
29-
* @name descriptor.FileDescriptorSet
30-
* @type {Type}
31-
*/
32-
3324
/**
3425
* @interface IFileDescriptorSet
3526
* @property {IFileDescriptorProto[]} file
@@ -118,12 +109,6 @@ Root.prototype.toDescriptor = function toDescriptor(syntax) {
118109

119110
// --- Type ---
120111

121-
/**
122-
* Reflected type describing a type.
123-
* @name descriptor.DescriptorProto
124-
* @type {Type}
125-
*/
126-
127112
/**
128113
* @interface IDescriptorProto
129114
* @property {string} [name]
@@ -175,15 +160,15 @@ Type.fromDescriptor = function fromDescriptor(descriptor, syntax) {
175160
i;
176161

177162
/* Fields */ for (i = 0; i < descriptor.field.length; ++i)
178-
type.add(protobuf.Field.fromDescriptor(descriptor.field[i], syntax));
163+
type.add(Field.fromDescriptor(descriptor.field[i], syntax));
179164
/* Extension fields */ for (i = 0; i < descriptor.extension.length; ++i)
180-
type.add(protobuf.Field.fromDescriptor(descriptor.extension[i], syntax));
165+
type.add(Field.fromDescriptor(descriptor.extension[i], syntax));
181166
/* Oneofs */ for (i = 0; i < descriptor.oneofDecl.length; ++i)
182-
type.add(protobuf.OneOf.fromDescriptor(descriptor.oneofDecl[i]));
167+
type.add(OneOf.fromDescriptor(descriptor.oneofDecl[i]));
183168
/* Nested types */ for (i = 0; i < descriptor.nestedType.length; ++i)
184-
type.add(protobuf.Type.fromDescriptor(descriptor.nestedType[i], syntax));
169+
type.add(Type.fromDescriptor(descriptor.nestedType[i], syntax));
185170
/* Nested enums */ for (i = 0; i < descriptor.enumType.length; ++i)
186-
type.add(protobuf.Enum.fromDescriptor(descriptor.enumType[i]));
171+
type.add(Enum.fromDescriptor(descriptor.enumType[i]));
187172
/* Extension ranges */ if (descriptor.extensionRange.length) {
188173
type.extensions = [];
189174
for (i = 0; i < descriptor.extensionRange.length; ++i)
@@ -236,18 +221,10 @@ Type.prototype.toDescriptor = function toDescriptor(syntax) {
236221

237222
// --- Field ---
238223

239-
/**
240-
* Reflected type describing a field.
241-
* @name descriptor.FieldDescriptorProto
242-
* @type {Type}
243-
* @property {Enum} Label Reflected descriptor describing a field label (rule)
244-
* @property {Enum} Type Reflected descriptor describing a field type
245-
*/
246-
247224
/**
248225
* @interface IFieldDescriptorProto
249226
* @property {string} [name]
250-
* @property {number} [number}
227+
* @property {number} [number]
251228
* @property {IFieldDescriptorProto_Label} [label]
252229
* @property {IFieldDescriptorProto_Type} [type]
253230
* @property {string} [typeName]
@@ -424,24 +401,6 @@ Field.prototype.toDescriptor = function toDescriptor(syntax) {
424401

425402
// --- Enum ---
426403

427-
/**
428-
* Reflected type describing an enum.
429-
* @name descriptor.EnumDescriptorProto
430-
* @type {Type}
431-
*/
432-
433-
/**
434-
* Reflected type describing an enum value.
435-
* @name descriptor.EnumValueDescriptorProto
436-
* @type {Type}
437-
*/
438-
439-
/**
440-
* Reflected type describing enum options.
441-
* @name descriptor.EnumOptions
442-
* @type {Type}
443-
*/
444-
445404
/**
446405
* @interface IEnumDescriptorProto
447406
* @property {string} [name]
@@ -511,12 +470,6 @@ Enum.prototype.toDescriptor = function toDescriptor() {
511470

512471
// --- OneOf ---
513472

514-
/**
515-
* Reflected type describing a oneof.
516-
* @name descriptor.OneofDescriptorProto
517-
* @type {Type}
518-
*/
519-
520473
/**
521474
* @interface IOneofDescriptorProto
522475
* @property {string} [name]
@@ -556,12 +509,6 @@ OneOf.prototype.toDescriptor = function toDescriptor() {
556509

557510
// --- Service ---
558511

559-
/**
560-
* Reflected type describing a service.
561-
* @name descriptor.ServiceDescriptorProto
562-
* @type {Type}
563-
*/
564-
565512
/**
566513
* @interface IServiceDescriptorProto
567514
* @property {string} [name]
@@ -610,12 +557,6 @@ Service.prototype.toDescriptor = function toDescriptor() {
610557

611558
// --- Method ---
612559

613-
/**
614-
* Reflected type describing a method.
615-
* @name descriptor.MethodDescriptorProto
616-
* @type {Type}
617-
*/
618-
619560
/**
620561
* @interface IMethodDescriptorProto
621562
* @property {string} [name]

index.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1719,6 +1719,13 @@ export class Type extends NamespaceBase {
17191719
*/
17201720
public ctor: Constructor<{}>;
17211721

1722+
/**
1723+
* Generates a constructor function for the specified type.
1724+
* @param {Type} type Type
1725+
* @returns {Codegen} Codegen instance
1726+
*/
1727+
public static generateConstructor(type: Type): Codegen;
1728+
17221729
/**
17231730
* Creates a message type from a message type descriptor.
17241731
* @param {string} name Message name

scripts/gentests.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ var fs = require("fs"),
4242
});
4343

4444
[
45-
"tests/data/rpc.js",
46-
"tests/data/test.js",
45+
{ file: "tests/data/rpc.js" },
46+
{ file: "tests/data/test.js" },
47+
{ file: "ext/descriptor/index.js", ext: true }
4748
]
48-
.forEach(function(file) {
49+
.forEach(function({ file, ext }) {
4950
var out = file.replace(/\.js$/, ".d.ts");
5051
pbts.main([
5152
"--no-comments",
@@ -54,7 +55,13 @@ var fs = require("fs"),
5455
if (err)
5556
throw err;
5657
var pathToProtobufjs = path.relative(path.dirname(out), "").replace(/\\/g, "/");
57-
fs.writeFileSync(out, output.replace(/"protobufjs"/g, JSON.stringify(pathToProtobufjs)));
58+
output = output.replace(/"protobufjs"/g, JSON.stringify(pathToProtobufjs));
59+
if (ext) {
60+
var extName;
61+
output = output.replace(/export (\w+) (\w+)/, function($0, $1, $2) { extName = $2; return "declare " + $1 + " " + extName; });
62+
output += "\nexport = " + extName + ";\n";
63+
}
64+
fs.writeFileSync(out, output);
5865
process.stdout.write("pbts: " + file + " -> " + out + "\n");
5966
});
60-
});
67+
});

src/type.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ Object.defineProperties(Type.prototype, {
152152
*/
153153
ctor: {
154154
get: function() {
155-
return this._ctor || (this.ctor = generateConstructor(this).eof(this.name));
155+
return this._ctor || (this.ctor = Type.generateConstructor(this).eof(this.name));
156156
},
157157
set: function(ctor) {
158158

@@ -189,7 +189,12 @@ Object.defineProperties(Type.prototype, {
189189
}
190190
});
191191

192-
function generateConstructor(type) {
192+
/**
193+
* Generates a constructor function for the specified type.
194+
* @param {Type} type Type
195+
* @returns {Codegen} Codegen instance
196+
*/
197+
Type.generateConstructor = function generateConstructor(type) {
193198
/* eslint-disable no-unexpected-multiline */
194199
var gen = util.codegen("p");
195200
// explicitly initialize mutable object/array fields so that these aren't just inherited from the prototype
@@ -202,7 +207,7 @@ function generateConstructor(type) {
202207
("if(p)for(var ks=Object.keys(p),i=0;i<ks.length;++i)if(p[ks[i]]!=null)") // omit undefined or null
203208
("this[ks[i]]=p[ks[i]]");
204209
/* eslint-enable no-unexpected-multiline */
205-
}
210+
};
206211

207212
function clearCache(type) {
208213
type._fieldsById = type._fieldsArray = type._oneofsArray = type._ctor = null;

0 commit comments

Comments
 (0)