Skip to content

Commit

Permalink
Add getTypeUrl method to static-module generated code
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorcode committed Jul 29, 2020
1 parent 9f33784 commit ba7fa51
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,7 @@ Translates between file formats and generates static code.
--no-verify Does not generate verify functions.
--no-convert Does not generate convert functions like from/toObject
--no-delimited Does not generate delimited encode/decode functions.
--no-typeurl Does not generate getTypeUrl function.
--no-beautify Does not beautify generated code.
--no-comments Does not output any JSDoc comments.
Expand Down
4 changes: 3 additions & 1 deletion cli/pbjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ exports.main = function main(args, callback) {
"force-message": "strict-message"
},
string: [ "target", "out", "path", "wrap", "dependency", "root", "lint" ],
boolean: [ "create", "encode", "decode", "verify", "convert", "delimited", "beautify", "comments", "es6", "sparse", "keep-case", "force-long", "force-number", "force-enum-string", "force-message" ],
boolean: [ "create", "encode", "decode", "verify", "convert", "delimited", "typeurl", "beautify", "comments", "es6", "sparse", "keep-case", "force-long", "force-number", "force-enum-string", "force-message" ],
default: {
target: "json",
create: true,
Expand All @@ -53,6 +53,7 @@ exports.main = function main(args, callback) {
verify: true,
convert: true,
delimited: true,
typeurl: true,
beautify: true,
comments: true,
es6: null,
Expand Down Expand Up @@ -133,6 +134,7 @@ exports.main = function main(args, callback) {
" --no-verify Does not generate verify functions.",
" --no-convert Does not generate convert functions like from/toObject",
" --no-delimited Does not generate delimited encode/decode functions.",
" --no-typeurl Does not generate getTypeUrl function.",
" --no-beautify Does not beautify generated code.",
" --no-comments Does not output any JSDoc comments.",
"",
Expand Down
16 changes: 16 additions & 0 deletions cli/targets/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,22 @@ function buildType(ref, type) {
--indent;
push("};");
}

if (config.typeurl) {
push("");
pushComment([
"Gets the default type url for " + type.name,
"@function getTypeUrl",
"@memberof " + exportName(type),
"@static",
"@returns {string} The default type url"
]);
push(escapeName(type.name) + ".getTypeUrl = function getTypeUrl() {");
++indent;
push("return \"type.googleapis.com/" + exportName(type) + "\";");
--indent;
push("};");
}
}

function buildService(ref, service) {
Expand Down

0 comments on commit ba7fa51

Please sign in to comment.