Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add getTypeUrl method to generated code #1463

Merged
merged 4 commits into from May 21, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
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
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
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