Skip to content

Commit

Permalink
feat: allow message.getTypeUrl provide custom typeUrlPrefix (#1762)
Browse files Browse the repository at this point in the history
* feat: allow message.getTypeUrl provide custom tyepUrlPrefix

* Update static.js

* test: added tests, regenerated test files

Co-authored-by: Alexander Fenster <fenster@google.com>
  • Loading branch information
kairlec and alexander-fenster committed Jul 7, 2022
1 parent e2f33a0 commit 8aad1dd
Show file tree
Hide file tree
Showing 20 changed files with 561 additions and 249 deletions.
10 changes: 8 additions & 2 deletions cli/targets/static.js
Expand Up @@ -597,11 +597,17 @@ function buildType(ref, type) {
"@function getTypeUrl",
"@memberof " + exportName(type),
"@static",
"@param {string} [typeUrlPrefix] your custom typeUrlPrefix(default \"type.googleapis.com\")",
"@returns {string} The default type url"
]);
push(escapeName(type.name) + ".getTypeUrl = function getTypeUrl() {");
push(escapeName(type.name) + ".getTypeUrl = function getTypeUrl(typeUrlPrefix) {");
++indent;
push("return \"type.googleapis.com/" + exportName(type) + "\";");
push("if (typeUrlPrefix === undefined) {");
++indent;
push("typeUrlPrefix = \"type.googleapis.com\";");
--indent;
push("}");
push("return typeUrlPrefix + \"/" + exportName(type) + "\";");
--indent;
push("};");
}
Expand Down
9 changes: 8 additions & 1 deletion tests/cli.js
Expand Up @@ -44,11 +44,12 @@ tape.test("pbjs generates static code", function(test) {
decode: true,
encode: true,
convert: true,
typeurl: true,
}, function(err, jsCode) {
test.error(err, 'static code generation worked');

// jsCode is the generated code; we'll eval it
// (since this is what we normally does with the code, right?)
// (since this is what we normally do with the code, right?)
// This is a test code. Do not use this in production.
var $protobuf = protobuf;
eval(jsCode);
Expand Down Expand Up @@ -78,6 +79,12 @@ tape.test("pbjs generates static code", function(test) {
var instance1 = OneofContainerDynamic.toObject(OneofContainerDynamic.fromObject(instance));
test.deepEqual(instance, instance1, "fromObject and toObject work for instance of the static type");

// Check that getTypeUrl works
var defaultTypeUrl = Message.getTypeUrl();
var customTypeUrl = Message.getTypeUrl("example.com");
test.equal(defaultTypeUrl, "type.googleapis.com/Message", "getTypeUrl returns expected url");
test.equal(customTypeUrl, "example.com/Message", "getTypeUrl returns custom url");

test.end();
});
});
Expand Down
7 changes: 4 additions & 3 deletions tests/data/comments.d.ts
Expand Up @@ -19,7 +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;
public static getTypeUrl(typeUrlPrefix?: string): string;
}

export interface ITest2 {
Expand All @@ -36,12 +36,13 @@ 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;
public static getTypeUrl(typeUrlPrefix?: string): string;
}

export enum Test3 {
ONE = 1,
TWO = 2,
THREE = 3,
FOUR = 4
FOUR = 4,
FIVE = 5
}
18 changes: 14 additions & 4 deletions tests/data/comments.js
Expand Up @@ -246,10 +246,14 @@ $root.Test1 = (function() {
* @function getTypeUrl
* @memberof Test1
* @static
* @param {string} [typeUrlPrefix] your custom typeUrlPrefix(default "type.googleapis.com")
* @returns {string} The default type url
*/
Test1.getTypeUrl = function getTypeUrl() {
return "type.googleapis.com/Test1";
Test1.getTypeUrl = function getTypeUrl(typeUrlPrefix) {
if (typeUrlPrefix === undefined) {
typeUrlPrefix = "type.googleapis.com";
}
return typeUrlPrefix + "/Test1";
};

return Test1;
Expand Down Expand Up @@ -417,10 +421,14 @@ $root.Test2 = (function() {
* @function getTypeUrl
* @memberof Test2
* @static
* @param {string} [typeUrlPrefix] your custom typeUrlPrefix(default "type.googleapis.com")
* @returns {string} The default type url
*/
Test2.getTypeUrl = function getTypeUrl() {
return "type.googleapis.com/Test2";
Test2.getTypeUrl = function getTypeUrl(typeUrlPrefix) {
if (typeUrlPrefix === undefined) {
typeUrlPrefix = "type.googleapis.com";
}
return typeUrlPrefix + "/Test2";
};

return Test2;
Expand All @@ -434,13 +442,15 @@ $root.Test2 = (function() {
* @property {number} TWO=2 TWO value
* @property {number} THREE=3 Preferred value with a comment.
* @property {number} FOUR=4 Other value with a comment.
* @property {number} FIVE=5 Leading comment for value with both types of comments after field with trailing comment.
*/
$root.Test3 = (function() {
var valuesById = {}, values = Object.create(valuesById);
values[valuesById[1] = "ONE"] = 1;
values[valuesById[2] = "TWO"] = 2;
values[valuesById[3] = "THREE"] = 3;
values[valuesById[4] = "FOUR"] = 4;
values[valuesById[5] = "FIVE"] = 5;
return values;
})();

Expand Down
2 changes: 1 addition & 1 deletion tests/data/convert.d.ts
Expand Up @@ -31,7 +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;
public static getTypeUrl(typeUrlPrefix?: string): string;
}

export namespace Message {
Expand Down
8 changes: 6 additions & 2 deletions tests/data/convert.js
Expand Up @@ -567,10 +567,14 @@ $root.Message = (function() {
* @function getTypeUrl
* @memberof Message
* @static
* @param {string} [typeUrlPrefix] your custom typeUrlPrefix(default "type.googleapis.com")
* @returns {string} The default type url
*/
Message.getTypeUrl = function getTypeUrl() {
return "type.googleapis.com/Message";
Message.getTypeUrl = function getTypeUrl(typeUrlPrefix) {
if (typeUrlPrefix === undefined) {
typeUrlPrefix = "type.googleapis.com";
}
return typeUrlPrefix + "/Message";
};

/**
Expand Down
8 changes: 4 additions & 4 deletions tests/data/mapbox/vector_tile.d.ts
Expand Up @@ -17,7 +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;
public static getTypeUrl(typeUrlPrefix?: string): string;
}

namespace Tile {
Expand Down Expand Up @@ -57,7 +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;
public static getTypeUrl(typeUrlPrefix?: string): string;
}

interface IFeature {
Expand All @@ -82,7 +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;
public static getTypeUrl(typeUrlPrefix?: string): string;
}

interface ILayer {
Expand Down Expand Up @@ -111,7 +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;
public static getTypeUrl(typeUrlPrefix?: string): string;
}
}
}
32 changes: 24 additions & 8 deletions tests/data/mapbox/vector_tile.js
Expand Up @@ -228,10 +228,14 @@ $root.vector_tile = (function() {
* @function getTypeUrl
* @memberof vector_tile.Tile
* @static
* @param {string} [typeUrlPrefix] your custom typeUrlPrefix(default "type.googleapis.com")
* @returns {string} The default type url
*/
Tile.getTypeUrl = function getTypeUrl() {
return "type.googleapis.com/vector_tile.Tile";
Tile.getTypeUrl = function getTypeUrl(typeUrlPrefix) {
if (typeUrlPrefix === undefined) {
typeUrlPrefix = "type.googleapis.com";
}
return typeUrlPrefix + "/vector_tile.Tile";
};

/**
Expand Down Expand Up @@ -616,10 +620,14 @@ $root.vector_tile = (function() {
* @function getTypeUrl
* @memberof vector_tile.Tile.Value
* @static
* @param {string} [typeUrlPrefix] your custom typeUrlPrefix(default "type.googleapis.com")
* @returns {string} The default type url
*/
Value.getTypeUrl = function getTypeUrl() {
return "type.googleapis.com/vector_tile.Tile.Value";
Value.getTypeUrl = function getTypeUrl(typeUrlPrefix) {
if (typeUrlPrefix === undefined) {
typeUrlPrefix = "type.googleapis.com";
}
return typeUrlPrefix + "/vector_tile.Tile.Value";
};

return Value;
Expand Down Expand Up @@ -968,10 +976,14 @@ $root.vector_tile = (function() {
* @function getTypeUrl
* @memberof vector_tile.Tile.Feature
* @static
* @param {string} [typeUrlPrefix] your custom typeUrlPrefix(default "type.googleapis.com")
* @returns {string} The default type url
*/
Feature.getTypeUrl = function getTypeUrl() {
return "type.googleapis.com/vector_tile.Tile.Feature";
Feature.getTypeUrl = function getTypeUrl(typeUrlPrefix) {
if (typeUrlPrefix === undefined) {
typeUrlPrefix = "type.googleapis.com";
}
return typeUrlPrefix + "/vector_tile.Tile.Feature";
};

return Feature;
Expand Down Expand Up @@ -1337,10 +1349,14 @@ $root.vector_tile = (function() {
* @function getTypeUrl
* @memberof vector_tile.Tile.Layer
* @static
* @param {string} [typeUrlPrefix] your custom typeUrlPrefix(default "type.googleapis.com")
* @returns {string} The default type url
*/
Layer.getTypeUrl = function getTypeUrl() {
return "type.googleapis.com/vector_tile.Tile.Layer";
Layer.getTypeUrl = function getTypeUrl(typeUrlPrefix) {
if (typeUrlPrefix === undefined) {
typeUrlPrefix = "type.googleapis.com";
}
return typeUrlPrefix + "/vector_tile.Tile.Layer";
};

return Layer;
Expand Down
4 changes: 2 additions & 2 deletions tests/data/package.d.ts
Expand Up @@ -47,7 +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;
public static getTypeUrl(typeUrlPrefix?: string): string;
}

export namespace Package {
Expand All @@ -70,6 +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;
public static getTypeUrl(typeUrlPrefix?: string): string;
}
}
16 changes: 12 additions & 4 deletions tests/data/package.js
Expand Up @@ -729,10 +729,14 @@ $root.Package = (function() {
* @function getTypeUrl
* @memberof Package
* @static
* @param {string} [typeUrlPrefix] your custom typeUrlPrefix(default "type.googleapis.com")
* @returns {string} The default type url
*/
Package.getTypeUrl = function getTypeUrl() {
return "type.googleapis.com/Package";
Package.getTypeUrl = function getTypeUrl(typeUrlPrefix) {
if (typeUrlPrefix === undefined) {
typeUrlPrefix = "type.googleapis.com";
}
return typeUrlPrefix + "/Package";
};

Package.Repository = (function() {
Expand Down Expand Up @@ -947,10 +951,14 @@ $root.Package = (function() {
* @function getTypeUrl
* @memberof Package.Repository
* @static
* @param {string} [typeUrlPrefix] your custom typeUrlPrefix(default "type.googleapis.com")
* @returns {string} The default type url
*/
Repository.getTypeUrl = function getTypeUrl() {
return "type.googleapis.com/Package.Repository";
Repository.getTypeUrl = function getTypeUrl(typeUrlPrefix) {
if (typeUrlPrefix === undefined) {
typeUrlPrefix = "type.googleapis.com";
}
return typeUrlPrefix + "/Package.Repository";
};

return Repository;
Expand Down
4 changes: 2 additions & 2 deletions tests/data/rpc-es6.d.ts
Expand Up @@ -27,7 +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;
public static getTypeUrl(typeUrlPrefix?: string): string;
}

export interface IMyResponse {
Expand All @@ -46,5 +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;
public static getTypeUrl(typeUrlPrefix?: string): string;
}
22 changes: 14 additions & 8 deletions tests/data/rpc-es6.js
@@ -1,7 +1,5 @@
/*eslint-disable block-scoped-var, id-length, no-control-regex, no-magic-numbers, no-prototype-builtins, no-redeclare, no-shadow, no-var, sort-vars*/
"use strict";

var $protobuf = require("../../minimal");
import * as $protobuf from "../../minimal";

// Common aliases
const $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;
Expand Down Expand Up @@ -266,10 +264,14 @@ export const MyRequest = $root.MyRequest = (() => {
* @function getTypeUrl
* @memberof MyRequest
* @static
* @param {string} [typeUrlPrefix] your custom typeUrlPrefix(default "type.googleapis.com")
* @returns {string} The default type url
*/
MyRequest.getTypeUrl = function getTypeUrl() {
return "type.googleapis.com/MyRequest";
MyRequest.getTypeUrl = function getTypeUrl(typeUrlPrefix) {
if (typeUrlPrefix === undefined) {
typeUrlPrefix = "type.googleapis.com";
}
return typeUrlPrefix + "/MyRequest";
};

return MyRequest;
Expand Down Expand Up @@ -464,13 +466,17 @@ export const MyResponse = $root.MyResponse = (() => {
* @function getTypeUrl
* @memberof MyResponse
* @static
* @param {string} [typeUrlPrefix] your custom typeUrlPrefix(default "type.googleapis.com")
* @returns {string} The default type url
*/
MyResponse.getTypeUrl = function getTypeUrl() {
return "type.googleapis.com/MyResponse";
MyResponse.getTypeUrl = function getTypeUrl(typeUrlPrefix) {
if (typeUrlPrefix === undefined) {
typeUrlPrefix = "type.googleapis.com";
}
return typeUrlPrefix + "/MyResponse";
};

return MyResponse;
})();

module.exports = $root;
export { $root as default };
4 changes: 2 additions & 2 deletions tests/data/rpc-reserved.d.ts
Expand Up @@ -27,7 +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;
public static getTypeUrl(typeUrlPrefix?: string): string;
}

export interface IMyResponse {
Expand All @@ -46,5 +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;
public static getTypeUrl(typeUrlPrefix?: string): string;
}

0 comments on commit 8aad1dd

Please sign in to comment.