Skip to content

Commit 0425b58

Browse files
committed
Other: More decorator progress (MapField.d, optional Type.d)
1 parent f63d436 commit 0425b58

27 files changed

+693
-303
lines changed

README.md

+31-19
Original file line numberDiff line numberDiff line change
@@ -510,48 +510,44 @@ import { AwesomeMessage } from "./bundle.js";
510510
// example code
511511
let message = AwesomeMessage.create({ awesomeField: "hello" });
512512
let buffer = AwesomeMessage.encode(message).finish();
513-
let decoded = AweesomeMessage.decode(buffer);
513+
let decoded = AwesomeMessage.decode(buffer);
514514
```
515515

516516
#### Using decorators
517517

518518
The library also includes an early implementation of [decorators](https://www.typescriptlang.org/docs/handbook/decorators.html).
519519

520-
**Note** that this API is rather new in protobuf.js (and probably buggy) and that decorators are an experimental subject-to-change-without-notice feature in TypeScript. Also note that declaration order is important depending on the JS target. For example, `@Field.d(2, AwesomeArrayMessage)` requires that `AwesomeArrayMessage` has been defined earlier when targeting `es5`.
520+
**Note** that this API is rather new in protobuf.js (and probably buggy) and that decorators are an experimental feature in TypeScript. Also note that declaration order is important depending on the JS target. For example, `@Field.d(2, AwesomeArrayMessage)` requires that `AwesomeArrayMessage` has been defined earlier when targeting `ES5`.
521521

522522
```ts
523523
import { Message, Type, Field, OneOf } from "protobufjs/light"; // respectively "./node_modules/protobufjs/light.js"
524524

525-
@Type.d()
526-
export class AwesomeArrayMessage extends Message<AwesomeArrayMessage> {
527-
528-
@Field.d(1, "uint32", "repeated")
529-
public awesomeArray: number[];
530-
531-
}
532-
533-
@Type.d()
534-
export class AwesomeStringMessage extends Message<AwesomeStringMessage> {
525+
export class AwesomeSubMessage extends Message<AwesomeSubMessage> {
535526

536527
@Field.d(1, "string")
537528
public awesomeString: string;
538529

539530
}
540531

541-
@Type.d()
532+
export enum AwesomeEnum {
533+
ONE = 1,
534+
TWO = 2
535+
}
536+
537+
@Type.d("SuperAwesomeMessage")
542538
export class AwesomeMessage extends Message<AwesomeMessage> {
543539

544540
@Field.d(1, "string", "optional", "awesome default string")
545541
public awesomeField: string;
546542

547-
@Field.d(2, AwesomeArrayMessage)
548-
public awesomeArrayMessage: AwesomeArrayMessage;
543+
@Field.d(2, AwesomeSubMessage)
544+
public awesomeSubMessage: AwesomeSubMessage;
549545

550-
@Field.d(3, AwesomeStringMessage)
551-
public awesomeStringMessage: AwesomeStringMessage;
546+
@Field.d(3, AwesomeEnum)
547+
public awesomeEnum: AwesomeEnum;
552548

553-
@OneOf.d("awesomeArrayMessage", "awesomeStringMessage")
554-
public whichAwesomeMessage: string;
549+
@OneOf.d("awesomeSubMessage", "awesomeEnum")
550+
public which: string;
555551

556552
}
557553

@@ -560,6 +556,22 @@ let buffer = AwesomeMessage.encode(message).finish();
560556
let decoded = AwesomeMessage.decode(buffer);
561557
```
562558

559+
Supported decorators are:
560+
561+
* **Type.d(typeName?: `string`)**<br />
562+
optionally annotates a class as a protobuf message type. If `typeName` is not specified, the constructor's runtime function name is used.
563+
564+
* **Field.d&lt;T>(fieldId: `number`, fieldType: `string | TMessageConstructor<TField>`, fieldRule?: `"optional" | "required" | "repeated"`, defaultValue?: `T`)**<br />
565+
annotates a property as a protobuf field with the specified id and protobuf type.
566+
567+
* **MapField.d&lt;T extends { [key: string]: any }>(fieldId: `number`, fieldKeyType: `string`, fieldValueType. `string | TConstructor<{}>`)**<br />
568+
annotates a property as a protobuf map field with the specified id, protobuf key and value type.
569+
570+
* **OneOf.d&lt;T extends string>(...fieldNames: `string[]`)**<br />
571+
annotates a property as a protobuf oneof covering the specified fields.
572+
573+
Decorated types reside in `protobuf.roots.decorators` using a flat structure (no duplicate names).
574+
563575
Command line
564576
------------
565577

cli/lib/tsd-jsdoc/publish.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,9 @@ function getTypeOf(element) {
232232
name = name.replace(/\bfunction(?:\(\))?([^\w]|$)/g, "Function");
233233

234234
// Convert plain Object back to just object
235-
if (name === "Object")
236-
name = "object";
235+
name = name.replace(/\b(Object(?!\.))/g, function($0, $1) {
236+
return $1.toLowerCase();
237+
});
237238

238239
return name;
239240
}

0 commit comments

Comments
 (0)