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 all commits
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
4 changes: 3 additions & 1 deletion cli/pbjs.js
Expand Up @@ -41,7 +41,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", "service", "es6", "sparse", "keep-case", "force-long", "force-number", "force-enum-string", "force-message", "null-defaults" ],
boolean: [ "create", "encode", "decode", "verify", "convert", "delimited", "typeurl", "beautify", "comments", "service", "es6", "sparse", "keep-case", "force-long", "force-number", "force-enum-string", "force-message", "null-defaults" ],
default: {
target: "json",
create: true,
Expand All @@ -50,6 +50,7 @@ exports.main = function main(args, callback) {
verify: true,
convert: true,
delimited: true,
typeurl: true,
beautify: true,
comments: true,
service: true,
Expand Down Expand Up @@ -132,6 +133,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.",
" --no-service Does not output service classes.",
Expand Down
16 changes: 16 additions & 0 deletions cli/targets/static.js
Expand Up @@ -589,6 +589,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
3 changes: 2 additions & 1 deletion scripts/gentests.js
Expand Up @@ -13,7 +13,8 @@ var fs = require("fs"),
{ file: "tests/data/rpc.proto", flags: [] },
{ file: "tests/data/rpc-reserved.proto", flags: [] },
{ file: "tests/data/test.proto", flags: [] },
{ file: "bench/data/bench.proto", flags: ["no-create", "no-verify", "no-delimited", "no-convert", "no-comments"], out: "bench/data/static_pbjs.js" }
{ file: "tests/data/type_url.proto", flags: [] },
{ file: "bench/data/bench.proto", flags: ["no-create", "no-verify", "no-delimited", "no-convert", "no-verify", "no-typeurl", "no-comments"], out: "bench/data/static_pbjs.js" }
]
.forEach(function({ file, flags, out }) {
var basename = file.replace(/\.proto$/, "");
Expand Down
2 changes: 2 additions & 0 deletions tests/data/comments.d.ts
Expand Up @@ -19,6 +19,7 @@ export class Test1 implements ITest1 {
public static fromObject(object: { [k: string]: any }): Test1;
public static toObject(message: Test1, options?: $protobuf.IConversionOptions): { [k: string]: any };
public toJSON(): { [k: string]: any };
public static getTypeUrl(): string;
}

export interface ITest2 {
Expand All @@ -35,6 +36,7 @@ export class Test2 implements ITest2 {
public static fromObject(object: { [k: string]: any }): Test2;
public static toObject(message: Test2, options?: $protobuf.IConversionOptions): { [k: string]: any };
public toJSON(): { [k: string]: any };
public static getTypeUrl(): string;
}

export enum Test3 {
Expand Down
22 changes: 22 additions & 0 deletions tests/data/comments.js
Expand Up @@ -241,6 +241,17 @@ $root.Test1 = (function() {
return this.constructor.toObject(this, $protobuf.util.toJSONOptions);
};

/**
* Gets the default type url for Test1
* @function getTypeUrl
* @memberof Test1
* @static
* @returns {string} The default type url
*/
Test1.getTypeUrl = function getTypeUrl() {
return "type.googleapis.com/Test1";
};

return Test1;
})();

Expand Down Expand Up @@ -401,6 +412,17 @@ $root.Test2 = (function() {
return this.constructor.toObject(this, $protobuf.util.toJSONOptions);
};

/**
* Gets the default type url for Test2
* @function getTypeUrl
* @memberof Test2
* @static
* @returns {string} The default type url
*/
Test2.getTypeUrl = function getTypeUrl() {
return "type.googleapis.com/Test2";
};

return Test2;
})();

Expand Down
1 change: 1 addition & 0 deletions tests/data/convert.d.ts
Expand Up @@ -31,6 +31,7 @@ export class Message implements IMessage {
public static fromObject(object: { [k: string]: any }): Message;
public static toObject(message: Message, options?: $protobuf.IConversionOptions): { [k: string]: any };
public toJSON(): { [k: string]: any };
public static getTypeUrl(): string;
}

export namespace Message {
Expand Down
11 changes: 11 additions & 0 deletions tests/data/convert.js
Expand Up @@ -562,6 +562,17 @@ $root.Message = (function() {
return this.constructor.toObject(this, $protobuf.util.toJSONOptions);
};

/**
* Gets the default type url for Message
* @function getTypeUrl
* @memberof Message
* @static
* @returns {string} The default type url
*/
Message.getTypeUrl = function getTypeUrl() {
return "type.googleapis.com/Message";
};

/**
* SomeEnum enum.
* @name Message.SomeEnum
Expand Down
4 changes: 4 additions & 0 deletions tests/data/mapbox/vector_tile.d.ts
Expand Up @@ -17,6 +17,7 @@ export namespace vector_tile {
public static fromObject(object: { [k: string]: any }): vector_tile.Tile;
public static toObject(message: vector_tile.Tile, options?: $protobuf.IConversionOptions): { [k: string]: any };
public toJSON(): { [k: string]: any };
public static getTypeUrl(): string;
}

namespace Tile {
Expand Down Expand Up @@ -56,6 +57,7 @@ export namespace vector_tile {
public static fromObject(object: { [k: string]: any }): vector_tile.Tile.Value;
public static toObject(message: vector_tile.Tile.Value, options?: $protobuf.IConversionOptions): { [k: string]: any };
public toJSON(): { [k: string]: any };
public static getTypeUrl(): string;
}

interface IFeature {
Expand All @@ -80,6 +82,7 @@ export namespace vector_tile {
public static fromObject(object: { [k: string]: any }): vector_tile.Tile.Feature;
public static toObject(message: vector_tile.Tile.Feature, options?: $protobuf.IConversionOptions): { [k: string]: any };
public toJSON(): { [k: string]: any };
public static getTypeUrl(): string;
}

interface ILayer {
Expand Down Expand Up @@ -108,6 +111,7 @@ export namespace vector_tile {
public static fromObject(object: { [k: string]: any }): vector_tile.Tile.Layer;
public static toObject(message: vector_tile.Tile.Layer, options?: $protobuf.IConversionOptions): { [k: string]: any };
public toJSON(): { [k: string]: any };
public static getTypeUrl(): string;
}
}
}
44 changes: 44 additions & 0 deletions tests/data/mapbox/vector_tile.js
Expand Up @@ -223,6 +223,17 @@ $root.vector_tile = (function() {
return this.constructor.toObject(this, $protobuf.util.toJSONOptions);
};

/**
* Gets the default type url for Tile
* @function getTypeUrl
* @memberof vector_tile.Tile
* @static
* @returns {string} The default type url
*/
Tile.getTypeUrl = function getTypeUrl() {
return "type.googleapis.com/vector_tile.Tile";
};

/**
* GeomType enum.
* @name vector_tile.Tile.GeomType
Expand Down Expand Up @@ -600,6 +611,17 @@ $root.vector_tile = (function() {
return this.constructor.toObject(this, $protobuf.util.toJSONOptions);
};

/**
* Gets the default type url for Value
* @function getTypeUrl
* @memberof vector_tile.Tile.Value
* @static
* @returns {string} The default type url
*/
Value.getTypeUrl = function getTypeUrl() {
return "type.googleapis.com/vector_tile.Tile.Value";
};

return Value;
})();

Expand Down Expand Up @@ -941,6 +963,17 @@ $root.vector_tile = (function() {
return this.constructor.toObject(this, $protobuf.util.toJSONOptions);
};

/**
* Gets the default type url for Feature
* @function getTypeUrl
* @memberof vector_tile.Tile.Feature
* @static
* @returns {string} The default type url
*/
Feature.getTypeUrl = function getTypeUrl() {
return "type.googleapis.com/vector_tile.Tile.Feature";
};

return Feature;
})();

Expand Down Expand Up @@ -1299,6 +1332,17 @@ $root.vector_tile = (function() {
return this.constructor.toObject(this, $protobuf.util.toJSONOptions);
};

/**
* Gets the default type url for Layer
* @function getTypeUrl
* @memberof vector_tile.Tile.Layer
* @static
* @returns {string} The default type url
*/
Layer.getTypeUrl = function getTypeUrl() {
return "type.googleapis.com/vector_tile.Tile.Layer";
};

return Layer;
})();

Expand Down
2 changes: 2 additions & 0 deletions tests/data/package.d.ts
Expand Up @@ -47,6 +47,7 @@ export class Package implements IPackage {
public static fromObject(object: { [k: string]: any }): Package;
public static toObject(message: Package, options?: $protobuf.IConversionOptions): { [k: string]: any };
public toJSON(): { [k: string]: any };
public static getTypeUrl(): string;
}

export namespace Package {
Expand All @@ -69,5 +70,6 @@ export namespace Package {
public static fromObject(object: { [k: string]: any }): Package.Repository;
public static toObject(message: Package.Repository, options?: $protobuf.IConversionOptions): { [k: string]: any };
public toJSON(): { [k: string]: any };
public static getTypeUrl(): string;
}
}
22 changes: 22 additions & 0 deletions tests/data/package.js
Expand Up @@ -724,6 +724,17 @@ $root.Package = (function() {
return this.constructor.toObject(this, $protobuf.util.toJSONOptions);
};

/**
* Gets the default type url for Package
* @function getTypeUrl
* @memberof Package
* @static
* @returns {string} The default type url
*/
Package.getTypeUrl = function getTypeUrl() {
return "type.googleapis.com/Package";
};

Package.Repository = (function() {

/**
Expand Down Expand Up @@ -931,6 +942,17 @@ $root.Package = (function() {
return this.constructor.toObject(this, $protobuf.util.toJSONOptions);
};

/**
* Gets the default type url for Repository
* @function getTypeUrl
* @memberof Package.Repository
* @static
* @returns {string} The default type url
*/
Repository.getTypeUrl = function getTypeUrl() {
return "type.googleapis.com/Package.Repository";
};

return Repository;
})();

Expand Down
2 changes: 2 additions & 0 deletions tests/data/rpc-es6.d.ts
Expand Up @@ -27,6 +27,7 @@ export class MyRequest implements IMyRequest {
public static fromObject(object: { [k: string]: any }): MyRequest;
public static toObject(message: MyRequest, options?: $protobuf.IConversionOptions): { [k: string]: any };
public toJSON(): { [k: string]: any };
public static getTypeUrl(): string;
}

export interface IMyResponse {
Expand All @@ -45,4 +46,5 @@ export class MyResponse implements IMyResponse {
public static fromObject(object: { [k: string]: any }): MyResponse;
public static toObject(message: MyResponse, options?: $protobuf.IConversionOptions): { [k: string]: any };
public toJSON(): { [k: string]: any };
public static getTypeUrl(): string;
}
28 changes: 26 additions & 2 deletions tests/data/rpc-es6.js
@@ -1,5 +1,7 @@
/*eslint-disable block-scoped-var, id-length, no-control-regex, no-magic-numbers, no-prototype-builtins, no-redeclare, no-shadow, no-var, sort-vars*/
import * as $protobuf from "../../minimal";
"use strict";

var $protobuf = require("../../minimal");

// Common aliases
const $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;
Expand Down Expand Up @@ -259,6 +261,17 @@ export const MyRequest = $root.MyRequest = (() => {
return this.constructor.toObject(this, $protobuf.util.toJSONOptions);
};

/**
* Gets the default type url for MyRequest
* @function getTypeUrl
* @memberof MyRequest
* @static
* @returns {string} The default type url
*/
MyRequest.getTypeUrl = function getTypeUrl() {
return "type.googleapis.com/MyRequest";
};

return MyRequest;
})();

Expand Down Expand Up @@ -446,7 +459,18 @@ export const MyResponse = $root.MyResponse = (() => {
return this.constructor.toObject(this, $protobuf.util.toJSONOptions);
};

/**
* Gets the default type url for MyResponse
* @function getTypeUrl
* @memberof MyResponse
* @static
* @returns {string} The default type url
*/
MyResponse.getTypeUrl = function getTypeUrl() {
return "type.googleapis.com/MyResponse";
};

return MyResponse;
})();

export { $root as default };
module.exports = $root;
2 changes: 2 additions & 0 deletions tests/data/rpc-reserved.d.ts
Expand Up @@ -27,6 +27,7 @@ export class MyRequest implements IMyRequest {
public static fromObject(object: { [k: string]: any }): MyRequest;
public static toObject(message: MyRequest, options?: $protobuf.IConversionOptions): { [k: string]: any };
public toJSON(): { [k: string]: any };
public static getTypeUrl(): string;
}

export interface IMyResponse {
Expand All @@ -45,4 +46,5 @@ export class MyResponse implements IMyResponse {
public static fromObject(object: { [k: string]: any }): MyResponse;
public static toObject(message: MyResponse, options?: $protobuf.IConversionOptions): { [k: string]: any };
public toJSON(): { [k: string]: any };
public static getTypeUrl(): string;
}