Skip to content

Commit 358d336

Browse files
Hide the fix for PB3 optional type declarations behind the flag --pb3-optional
1 parent b38a668 commit 358d336

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

cli/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ Translates between file formats and generates static code.
6868
--force-long Enforces the use of 'Long' for s-/u-/int64 and s-/fixed64 fields.
6969
--force-number Enforces the use of 'number' for s-/u-/int64 and s-/fixed64 fields.
7070
--force-message Enforces the use of message instances instead of plain objects.
71+
--pb3-optional Make type declarations respect optional fields for PB3.
7172
7273
usage: pbjs [options] file1.proto file2.json ... (or pipe) other | pbjs [options] -
7374
```

cli/pbjs.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ exports.main = function main(args, callback) {
4141
"force-message": "strict-message"
4242
},
4343
string: [ "target", "out", "path", "wrap", "dependency", "root", "lint", "filter" ],
44-
boolean: [ "create", "encode", "decode", "verify", "convert", "delimited", "typeurl", "beautify", "comments", "service", "es6", "sparse", "keep-case", "alt-comment", "force-long", "force-number", "force-enum-string", "force-message", "null-defaults" ],
44+
boolean: [ "create", "encode", "decode", "verify", "convert", "delimited", "typeurl", "beautify", "comments", "service", "es6", "sparse", "keep-case", "alt-comment", "force-long", "force-number", "force-enum-string", "force-message", "null-defaults", "pb3-optional" ],
4545
default: {
4646
target: "json",
4747
create: true,
@@ -63,6 +63,7 @@ exports.main = function main(args, callback) {
6363
"force-enum-string": false,
6464
"force-message": false,
6565
"null-defaults": false,
66+
"pb3-optional": false
6667
}
6768
});
6869

@@ -148,6 +149,7 @@ exports.main = function main(args, callback) {
148149
" --force-message Enforces the use of message instances instead of plain objects.",
149150
"",
150151
" --null-defaults Default value for optional fields is null instead of zero value.",
152+
" --pb3-optional Make type declarations respect optional fields for PB3.",
151153
"",
152154
"usage: " + chalk.bold.green("pbjs") + " [options] file1.proto file2.json ..." + chalk.gray(" (or pipe) ") + "other | " + chalk.bold.green("pbjs") + " [options] -",
153155
""

cli/targets/static.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,16 @@ function buildType(ref, type) {
374374
var prop = util.safeProp(field.name); // either .name or ["name"]
375375
prop = prop.substring(1, prop.charAt(0) === "[" ? prop.length - 1 : prop.length);
376376
var jsType = toJsType(field);
377-
jsType = handleOptionalFields(jsType, field);
377+
378+
// Hide the fix for PB3 optional fields behind a config flag, it is an API change in generated output
379+
if (config.pb3Optional) {
380+
jsType = handleOptionalFields(jsType, field);
381+
}
382+
else {
383+
if (field.optional)
384+
jsType = jsType + "|null";
385+
}
386+
378387
typeDef.push("@property {" + jsType + "} " + (field.optional ? "[" + prop + "]" : prop) + " " + (field.comment || type.name + " " + field.name));
379388
});
380389
push("");
@@ -401,6 +410,7 @@ function buildType(ref, type) {
401410
if (config.comments) {
402411
push("");
403412
var jsType = toJsType(field);
413+
// No need to check config.tsOptional for member types, this logic has not changed
404414
jsType = handleOptionalFields(jsType, field);
405415
pushComment([
406416
field.comment || type.name + " " + field.name + ".",

0 commit comments

Comments
 (0)