Skip to content

Commit 7a6f98b

Browse files
committed
Breaking: Initial implementation of TypeScript decorators; Breaking: Refactored protobuf.Class away; Breaking: TypeScript definitions now have (a lot of) generics; Breaking: Removed deprecated features; Other: tsd-jsdoc now has limited generics support
1 parent 57f1da6 commit 7a6f98b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+1681
-2198
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# [6.7.3](https://github.com/dcodeIO/protobuf.js/releases/tag/6.7.3)
2+
3+
## Other
4+
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/57f1da64945f2dc5537c6eaa53e08e8fdd477b67) long, @types/long and @types/node are just dependencies, see [#753](https://github.com/dcodeIO/protobuf.js/issues/753)<br />
5+
16
# [6.7.2](https://github.com/dcodeIO/protobuf.js/releases/tag/6.7.2)
27

38
## New

README.md

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ protobuf.load("awesome.proto", function(err, root) {
249249
throw Error(errMsg);
250250

251251
// Create a new message
252-
var message = AwesomeMessage.creeate(payload); // or use .fromObject if conversion is necessary
252+
var message = AwesomeMessage.create(payload); // or use .fromObject if conversion is necessary
253253

254254
// Encode a message to an Uint8Array (browser) or Buffer (node)
255255
var buffer = AwesomeMessage.encode(message).finish();
@@ -518,6 +518,53 @@ If you are not building for node and/or not using long.js and want to exclude th
518518
/// <reference path="./node_modules/protobufjs/stub-node.d.ts" />
519519
```
520520

521+
#### Experimental decorators
522+
523+
**WARNING:** Just introduced, not well tested, probably buggy.
524+
525+
protobuf.js ships with an initial implementation of decorators, but note that decorators in TypeScript are an experimental feature and are subject to change without notice - plus - you have to enable the feature explicitly with the `experimentalDecorators` option:
526+
527+
```ts
528+
import { Message, Type, Field, OneOf } from "protobufjs/light";
529+
530+
@Type.d()
531+
export class AwesomeArrayMessage extends Message<AwesomeArrayMessage> {
532+
533+
@Field.d(1, "uint32", "repeated")
534+
public awesomeArray: number[];
535+
536+
}
537+
538+
@Type.d()
539+
export class AwesomeStringMessage extends Message<AwesomeStringMessage> {
540+
541+
@Field.d(1, "string")
542+
public awesomeString: string;
543+
544+
}
545+
546+
@Type.d()
547+
export class AwesomeMessage extends Message<AwesomeMessage> {
548+
549+
@Field.d(1, "string", "optional", "awesome default string")
550+
public awesomeField: string;
551+
552+
@Field.d(2, AwesomeArrayMessage)
553+
public awesomeArrayMessage: AwesomeArrayMessage;
554+
555+
@Field.d(3, AwesomeStringMessage)
556+
public awesomeStringMessage: AwesomeStringMessage;
557+
558+
@OneOf.d("awesomeArrayMessage", "awesomeStringMessage")
559+
public whichAwesomeMessage: string;
560+
561+
}
562+
563+
let awesomeMessage = new AwesomeMessage({ awesomeField: "hi" });
564+
let awesomeBuffer = AwesomeMessage.encode(awesomeMessage).finish();
565+
let awesomeDecoded = AwesomeMessage.decode(awesomeBuffer);
566+
```
567+
521568
Command line
522569
------------
523570

cli/lib/tsd-jsdoc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
"includePattern": ".+\\.js(doc)?$",
88
"excludePattern": "(^|\\/|\\\\)_"
99
},
10+
"plugins": [
11+
"./tsd-jsdoc/plugin"
12+
],
1013
"opts": {
1114
"encoding" : "utf8",
1215
"recurse" : true,

cli/lib/tsd-jsdoc/plugin.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"use strict";
2+
exports.defineTags = function(dictionary) {
3+
4+
dictionary.defineTag("template", {
5+
mustHaveValue: true,
6+
canHaveType: false,
7+
canHaveName: false,
8+
onTagged: function(doclet, tag) {
9+
(doclet.templates || (doclet.templates = [])).push(tag.text);
10+
}
11+
});
12+
13+
dictionary.defineTag("tstype", {
14+
mustHaveValue: true,
15+
canHaveType: false,
16+
canHaveName: false,
17+
onTagged: function(doclet, tag) {
18+
doclet.tsType = tag.text;
19+
}
20+
});
21+
};

cli/lib/tsd-jsdoc/publish.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ function getChildrenOf(parent) {
195195

196196
// gets the literal type of an element
197197
function getTypeOf(element) {
198+
if (element.tsType)
199+
return element.tsType;
198200
var name = "any";
199201
var type = element.type;
200202
if (type && type.names && type.names.length) {
@@ -211,9 +213,9 @@ function getTypeOf(element) {
211213
// Ensure upper case Object for map expressions below
212214
name = name.replace(/\bobject\b/g, "Object");
213215

214-
// Correct Promise.<Something> to Promise<Something>
215-
name = replaceRecursive(name, /\bPromise\.<([^>]*)>/gi, function($0, $1) {
216-
return "Promise<" + $1 + ">";
216+
// Correct Something.<Something> to Something<Something>
217+
name = replaceRecursive(name, /\b(?!Object|Array)([\w$]+)\.<([^>]*)>/gi, function($0, $1, $2) {
218+
return $1 + "<" + $2 + ">";
217219
});
218220

219221
// Replace Array.<string> with string[]
@@ -226,8 +228,8 @@ function getTypeOf(element) {
226228
return "{ [k: " + $1 + "]: " + $2 + " }";
227229
});
228230

229-
// Replace functions (there are no signatures) with () => any
230-
name = name.replace(/\bfunction(?:\(\))?([^\w]|$)/gi, "() => any");
231+
// Replace functions (there are no signatures) with Function
232+
name = name.replace(/\bfunction(?:\(\))?([^\w]|$)/g, "Function");
231233

232234
// Convert plain Object back to just object
233235
if (name === "Object")
@@ -402,7 +404,10 @@ function handleClass(element, parent) {
402404
write("abstract ");
403405
write("class ");
404406
}
405-
write(element.name, " ");
407+
write(element.name);
408+
if (element.templates && element.templates.length)
409+
write("<", element.templates.join(", "), ">");
410+
write(" ");
406411

407412
// extended classes
408413
if (element.augments) {
@@ -520,6 +525,8 @@ function handleFunction(element, parent, isConstructor) {
520525
} else
521526
write("function ");
522527
write(element.name);
528+
if (element.templates && element.templates.length)
529+
write("<", element.templates.join(", "), ">");
523530
}
524531
writeFunctionSignature(element, isConstructor, false);
525532
writeln(";");
@@ -538,7 +545,10 @@ function handleTypeDef(element, parent) {
538545
// see: https://github.com/dcodeIO/protobuf.js/issues/737
539546
// begin(element, true);
540547
writeln();
541-
write("type ", element.name, " = ");
548+
write("type ", element.name);
549+
if (element.templates && element.templates.length)
550+
write("<", element.templates.join(", "), ">");
551+
write(" = ");
542552
var type = getTypeOf(element);
543553
if (element.type && element.type.names.length === 1 && element.type.names[0] === "function")
544554
writeFunctionSignature(element, false, true);

cli/targets/static.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -520,16 +520,6 @@ function buildType(ref, type) {
520520
]);
521521
buildFunction(type, "fromObject", protobuf.converter.fromObject(type));
522522

523-
push("");
524-
pushComment([
525-
"Creates " + aOrAn(type.name) + " message from a plain object. Also converts values to their respective internal types.",
526-
"This is an alias of {@link " + fullName + ".fromObject}.",
527-
"@function",
528-
"@param {Object.<string,*>} object Plain object",
529-
"@returns {" + fullName + "} " + type.name
530-
]);
531-
push(name(type.name) + ".from = " + name(type.name) + ".fromObject;");
532-
533523
push("");
534524
pushComment([
535525
"Creates a plain object from " + aOrAn(type.name) + " message. Also converts values to other types if specified.",

config/jsdoc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"tags": {
3-
"allowUnknownTags": false
3+
"allowUnknownTags": true
44
},
55
"source": {
66
"include": [

0 commit comments

Comments
 (0)