Skip to content

Commit ed7e2e7

Browse files
committed
CLI: Accept null for optional fields in generated static code
1 parent c43243b commit ed7e2e7

29 files changed

+790
-777
lines changed

cli/targets/static.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,10 @@ function buildType(ref, type) {
365365
type.fieldsArray.forEach(function(field) {
366366
var prop = util.safeProp(field.name);
367367
prop = prop.substring(1, prop.charAt(0) === "[" ? prop.length - 1 : prop.length);
368-
typeDef.push("@property {" + toJsType(field) + "} " + (field.optional ? "[" + prop + "]" : field.name) + " " + (field.comment || type.name + " " + field.name));
368+
var jsType = toJsType(field);
369+
if (field.optional)
370+
jsType = jsType + "|null";
371+
typeDef.push("@property {" + jsType + "} " + (field.optional ? "[" + prop + "]" : prop) + " " + (field.comment || type.name + " " + field.name));
369372
});
370373
push("");
371374
pushComment(typeDef);
@@ -391,10 +394,10 @@ function buildType(ref, type) {
391394
push("");
392395
var jsType = toJsType(field);
393396
if (field.optional && !field.map && !field.repeated && field.resolvedType instanceof Type)
394-
jsType = "(" + jsType + "|null|undefined)";
397+
jsType = jsType + "|null|undefined";
395398
pushComment([
396399
field.comment || type.name + " " + field.name + ".",
397-
"@member {" + jsType + "}" + escapeName(field.name),
400+
"@member {" + jsType + "} " + escapeName(field.name),
398401
"@memberof " + exportName(type),
399402
"@instance"
400403
]);

dist/light/protobuf.js

+14-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/light/protobuf.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/light/protobuf.min.js

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/light/protobuf.min.js.gz

7 Bytes
Binary file not shown.

dist/light/protobuf.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/minimal/protobuf.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/minimal/protobuf.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/minimal/protobuf.min.js.gz

1 Byte
Binary file not shown.

dist/protobuf.js

+14-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.min.js

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.min.js.gz

-8 Bytes
Binary file not shown.

dist/protobuf.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "protobufjs",
3-
"version": "6.8.0",
3+
"version": "6.8.1",
44
"versionScheme": "~",
55
"description": "Protocol Buffers for JavaScript (& TypeScript).",
66
"author": "Daniel Wirtz <[email protected]>",

tests/data/comments.d.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import * as $protobuf from "../..";
22

33
export interface ITest1 {
4-
field1?: string;
5-
field2?: number;
6-
field3?: boolean;
4+
field1?: (string|null);
5+
field2?: (number|null);
6+
field3?: (boolean|null);
77
}
88

99
export class Test1 {

tests/data/comments.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ $root.Test1 = (function() {
1515
* Properties of a Test1.
1616
* @exports ITest1
1717
* @interface ITest1
18-
* @property {string} [field1] Field with a comment.
19-
* @property {number} [field2] Test1 field2
20-
* @property {boolean} [field3] Field with a comment and a <a href="http://example.com/foo/">link</a>
18+
* @property {string|null} [field1] Field with a comment.
19+
* @property {number|null} [field2] Test1 field2
20+
* @property {boolean|null} [field3] Field with a comment and a <a href="http://example.com/foo/">link</a>
2121
*/
2222

2323
/**
@@ -39,23 +39,23 @@ $root.Test1 = (function() {
3939

4040
/**
4141
* Field with a comment.
42-
* @member {string}field1
42+
* @member {string} field1
4343
* @memberof Test1
4444
* @instance
4545
*/
4646
Test1.prototype.field1 = "";
4747

4848
/**
4949
* Test1 field2.
50-
* @member {number}field2
50+
* @member {number} field2
5151
* @memberof Test1
5252
* @instance
5353
*/
5454
Test1.prototype.field2 = 0;
5555

5656
/**
5757
* Field with a comment and a <a href="http://example.com/foo/">link</a>
58-
* @member {boolean}field3
58+
* @member {boolean} field3
5959
* @memberof Test1
6060
* @instance
6161
*/

tests/data/convert.d.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import * as $protobuf from "../..";
22

33
export interface IMessage {
4-
stringVal?: string;
5-
stringRepeated?: string[];
6-
uint64Val?: (number|Long);
7-
uint64Repeated?: (number|Long)[];
8-
bytesVal?: Uint8Array;
9-
bytesRepeated?: Uint8Array[];
10-
enumVal?: Message.SomeEnum;
11-
enumRepeated?: Message.SomeEnum[];
12-
int64Map?: { [k: string]: (number|Long) };
4+
stringVal?: (string|null);
5+
stringRepeated?: (string[]|null);
6+
uint64Val?: (number|Long|null);
7+
uint64Repeated?: ((number|Long)[]|null);
8+
bytesVal?: (Uint8Array|null);
9+
bytesRepeated?: (Uint8Array[]|null);
10+
enumVal?: (Message.SomeEnum|null);
11+
enumRepeated?: (Message.SomeEnum[]|null);
12+
int64Map?: ({ [k: string]: (number|Long) }|null);
1313
}
1414

1515
export class Message {

tests/data/convert.js

+18-18
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ $root.Message = (function() {
1515
* Properties of a Message.
1616
* @exports IMessage
1717
* @interface IMessage
18-
* @property {string} [stringVal] Message stringVal
19-
* @property {Array.<string>} [stringRepeated] Message stringRepeated
20-
* @property {number|Long} [uint64Val] Message uint64Val
21-
* @property {Array.<number|Long>} [uint64Repeated] Message uint64Repeated
22-
* @property {Uint8Array} [bytesVal] Message bytesVal
23-
* @property {Array.<Uint8Array>} [bytesRepeated] Message bytesRepeated
24-
* @property {Message.SomeEnum} [enumVal] Message enumVal
25-
* @property {Array.<Message.SomeEnum>} [enumRepeated] Message enumRepeated
26-
* @property {Object.<string,number|Long>} [int64Map] Message int64Map
18+
* @property {string|null} [stringVal] Message stringVal
19+
* @property {Array.<string>|null} [stringRepeated] Message stringRepeated
20+
* @property {number|Long|null} [uint64Val] Message uint64Val
21+
* @property {Array.<number|Long>|null} [uint64Repeated] Message uint64Repeated
22+
* @property {Uint8Array|null} [bytesVal] Message bytesVal
23+
* @property {Array.<Uint8Array>|null} [bytesRepeated] Message bytesRepeated
24+
* @property {Message.SomeEnum|null} [enumVal] Message enumVal
25+
* @property {Array.<Message.SomeEnum>|null} [enumRepeated] Message enumRepeated
26+
* @property {Object.<string,number|Long>|null} [int64Map] Message int64Map
2727
*/
2828

2929
/**
@@ -47,71 +47,71 @@ $root.Message = (function() {
4747

4848
/**
4949
* Message stringVal.
50-
* @member {string}stringVal
50+
* @member {string} stringVal
5151
* @memberof Message
5252
* @instance
5353
*/
5454
Message.prototype.stringVal = "";
5555

5656
/**
5757
* Message stringRepeated.
58-
* @member {Array.<string>}stringRepeated
58+
* @member {Array.<string>} stringRepeated
5959
* @memberof Message
6060
* @instance
6161
*/
6262
Message.prototype.stringRepeated = $util.emptyArray;
6363

6464
/**
6565
* Message uint64Val.
66-
* @member {number|Long}uint64Val
66+
* @member {number|Long} uint64Val
6767
* @memberof Message
6868
* @instance
6969
*/
7070
Message.prototype.uint64Val = $util.Long ? $util.Long.fromBits(0,0,true) : 0;
7171

7272
/**
7373
* Message uint64Repeated.
74-
* @member {Array.<number|Long>}uint64Repeated
74+
* @member {Array.<number|Long>} uint64Repeated
7575
* @memberof Message
7676
* @instance
7777
*/
7878
Message.prototype.uint64Repeated = $util.emptyArray;
7979

8080
/**
8181
* Message bytesVal.
82-
* @member {Uint8Array}bytesVal
82+
* @member {Uint8Array} bytesVal
8383
* @memberof Message
8484
* @instance
8585
*/
8686
Message.prototype.bytesVal = $util.newBuffer([]);
8787

8888
/**
8989
* Message bytesRepeated.
90-
* @member {Array.<Uint8Array>}bytesRepeated
90+
* @member {Array.<Uint8Array>} bytesRepeated
9191
* @memberof Message
9292
* @instance
9393
*/
9494
Message.prototype.bytesRepeated = $util.emptyArray;
9595

9696
/**
9797
* Message enumVal.
98-
* @member {Message.SomeEnum}enumVal
98+
* @member {Message.SomeEnum} enumVal
9999
* @memberof Message
100100
* @instance
101101
*/
102102
Message.prototype.enumVal = 1;
103103

104104
/**
105105
* Message enumRepeated.
106-
* @member {Array.<Message.SomeEnum>}enumRepeated
106+
* @member {Array.<Message.SomeEnum>} enumRepeated
107107
* @memberof Message
108108
* @instance
109109
*/
110110
Message.prototype.enumRepeated = $util.emptyArray;
111111

112112
/**
113113
* Message int64Map.
114-
* @member {Object.<string,number|Long>}int64Map
114+
* @member {Object.<string,number|Long>} int64Map
115115
* @memberof Message
116116
* @instance
117117
*/

tests/data/mapbox/vector_tile.d.ts

+16-16
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as $protobuf from "../../..";
33
export namespace vector_tile {
44

55
interface ITile {
6-
layers?: vector_tile.Tile.ILayer[];
6+
layers?: (vector_tile.Tile.ILayer[]|null);
77
}
88

99
class Tile {
@@ -30,13 +30,13 @@ export namespace vector_tile {
3030
}
3131

3232
interface IValue {
33-
stringValue?: string;
34-
floatValue?: number;
35-
doubleValue?: number;
36-
intValue?: (number|Long);
37-
uintValue?: (number|Long);
38-
sintValue?: (number|Long);
39-
boolValue?: boolean;
33+
stringValue?: (string|null);
34+
floatValue?: (number|null);
35+
doubleValue?: (number|null);
36+
intValue?: (number|Long|null);
37+
uintValue?: (number|Long|null);
38+
sintValue?: (number|Long|null);
39+
boolValue?: (boolean|null);
4040
}
4141

4242
class Value {
@@ -60,10 +60,10 @@ export namespace vector_tile {
6060
}
6161

6262
interface IFeature {
63-
id?: (number|Long);
64-
tags?: number[];
65-
type?: vector_tile.Tile.GeomType;
66-
geometry?: number[];
63+
id?: (number|Long|null);
64+
tags?: (number[]|null);
65+
type?: (vector_tile.Tile.GeomType|null);
66+
geometry?: (number[]|null);
6767
}
6868

6969
class Feature {
@@ -86,10 +86,10 @@ export namespace vector_tile {
8686
interface ILayer {
8787
version: number;
8888
name: string;
89-
features?: vector_tile.Tile.IFeature[];
90-
keys?: string[];
91-
values?: vector_tile.Tile.IValue[];
92-
extent?: number;
89+
features?: (vector_tile.Tile.IFeature[]|null);
90+
keys?: (string[]|null);
91+
values?: (vector_tile.Tile.IValue[]|null);
92+
extent?: (number|null);
9393
}
9494

9595
class Layer {

0 commit comments

Comments
 (0)