Skip to content

Commit 8aad1dd

Browse files
feat: allow message.getTypeUrl provide custom typeUrlPrefix (#1762)
* feat: allow message.getTypeUrl provide custom tyepUrlPrefix * Update static.js * test: added tests, regenerated test files Co-authored-by: Alexander Fenster <[email protected]>
1 parent e2f33a0 commit 8aad1dd

20 files changed

+561
-249
lines changed

cli/targets/static.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -597,11 +597,17 @@ function buildType(ref, type) {
597597
"@function getTypeUrl",
598598
"@memberof " + exportName(type),
599599
"@static",
600+
"@param {string} [typeUrlPrefix] your custom typeUrlPrefix(default \"type.googleapis.com\")",
600601
"@returns {string} The default type url"
601602
]);
602-
push(escapeName(type.name) + ".getTypeUrl = function getTypeUrl() {");
603+
push(escapeName(type.name) + ".getTypeUrl = function getTypeUrl(typeUrlPrefix) {");
603604
++indent;
604-
push("return \"type.googleapis.com/" + exportName(type) + "\";");
605+
push("if (typeUrlPrefix === undefined) {");
606+
++indent;
607+
push("typeUrlPrefix = \"type.googleapis.com\";");
608+
--indent;
609+
push("}");
610+
push("return typeUrlPrefix + \"/" + exportName(type) + "\";");
605611
--indent;
606612
push("};");
607613
}

tests/cli.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@ tape.test("pbjs generates static code", function(test) {
4444
decode: true,
4545
encode: true,
4646
convert: true,
47+
typeurl: true,
4748
}, function(err, jsCode) {
4849
test.error(err, 'static code generation worked');
4950

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

82+
// Check that getTypeUrl works
83+
var defaultTypeUrl = Message.getTypeUrl();
84+
var customTypeUrl = Message.getTypeUrl("example.com");
85+
test.equal(defaultTypeUrl, "type.googleapis.com/Message", "getTypeUrl returns expected url");
86+
test.equal(customTypeUrl, "example.com/Message", "getTypeUrl returns custom url");
87+
8188
test.end();
8289
});
8390
});

tests/data/comments.d.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class Test1 implements ITest1 {
1919
public static fromObject(object: { [k: string]: any }): Test1;
2020
public static toObject(message: Test1, options?: $protobuf.IConversionOptions): { [k: string]: any };
2121
public toJSON(): { [k: string]: any };
22-
public static getTypeUrl(): string;
22+
public static getTypeUrl(typeUrlPrefix?: string): string;
2323
}
2424

2525
export interface ITest2 {
@@ -36,12 +36,13 @@ export class Test2 implements ITest2 {
3636
public static fromObject(object: { [k: string]: any }): Test2;
3737
public static toObject(message: Test2, options?: $protobuf.IConversionOptions): { [k: string]: any };
3838
public toJSON(): { [k: string]: any };
39-
public static getTypeUrl(): string;
39+
public static getTypeUrl(typeUrlPrefix?: string): string;
4040
}
4141

4242
export enum Test3 {
4343
ONE = 1,
4444
TWO = 2,
4545
THREE = 3,
46-
FOUR = 4
46+
FOUR = 4,
47+
FIVE = 5
4748
}

tests/data/comments.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,14 @@ $root.Test1 = (function() {
246246
* @function getTypeUrl
247247
* @memberof Test1
248248
* @static
249+
* @param {string} [typeUrlPrefix] your custom typeUrlPrefix(default "type.googleapis.com")
249250
* @returns {string} The default type url
250251
*/
251-
Test1.getTypeUrl = function getTypeUrl() {
252-
return "type.googleapis.com/Test1";
252+
Test1.getTypeUrl = function getTypeUrl(typeUrlPrefix) {
253+
if (typeUrlPrefix === undefined) {
254+
typeUrlPrefix = "type.googleapis.com";
255+
}
256+
return typeUrlPrefix + "/Test1";
253257
};
254258

255259
return Test1;
@@ -417,10 +421,14 @@ $root.Test2 = (function() {
417421
* @function getTypeUrl
418422
* @memberof Test2
419423
* @static
424+
* @param {string} [typeUrlPrefix] your custom typeUrlPrefix(default "type.googleapis.com")
420425
* @returns {string} The default type url
421426
*/
422-
Test2.getTypeUrl = function getTypeUrl() {
423-
return "type.googleapis.com/Test2";
427+
Test2.getTypeUrl = function getTypeUrl(typeUrlPrefix) {
428+
if (typeUrlPrefix === undefined) {
429+
typeUrlPrefix = "type.googleapis.com";
430+
}
431+
return typeUrlPrefix + "/Test2";
424432
};
425433

426434
return Test2;
@@ -434,13 +442,15 @@ $root.Test2 = (function() {
434442
* @property {number} TWO=2 TWO value
435443
* @property {number} THREE=3 Preferred value with a comment.
436444
* @property {number} FOUR=4 Other value with a comment.
445+
* @property {number} FIVE=5 Leading comment for value with both types of comments after field with trailing comment.
437446
*/
438447
$root.Test3 = (function() {
439448
var valuesById = {}, values = Object.create(valuesById);
440449
values[valuesById[1] = "ONE"] = 1;
441450
values[valuesById[2] = "TWO"] = 2;
442451
values[valuesById[3] = "THREE"] = 3;
443452
values[valuesById[4] = "FOUR"] = 4;
453+
values[valuesById[5] = "FIVE"] = 5;
444454
return values;
445455
})();
446456

tests/data/convert.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export class Message implements IMessage {
3131
public static fromObject(object: { [k: string]: any }): Message;
3232
public static toObject(message: Message, options?: $protobuf.IConversionOptions): { [k: string]: any };
3333
public toJSON(): { [k: string]: any };
34-
public static getTypeUrl(): string;
34+
public static getTypeUrl(typeUrlPrefix?: string): string;
3535
}
3636

3737
export namespace Message {

tests/data/convert.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -567,10 +567,14 @@ $root.Message = (function() {
567567
* @function getTypeUrl
568568
* @memberof Message
569569
* @static
570+
* @param {string} [typeUrlPrefix] your custom typeUrlPrefix(default "type.googleapis.com")
570571
* @returns {string} The default type url
571572
*/
572-
Message.getTypeUrl = function getTypeUrl() {
573-
return "type.googleapis.com/Message";
573+
Message.getTypeUrl = function getTypeUrl(typeUrlPrefix) {
574+
if (typeUrlPrefix === undefined) {
575+
typeUrlPrefix = "type.googleapis.com";
576+
}
577+
return typeUrlPrefix + "/Message";
574578
};
575579

576580
/**

tests/data/mapbox/vector_tile.d.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export namespace vector_tile {
1717
public static fromObject(object: { [k: string]: any }): vector_tile.Tile;
1818
public static toObject(message: vector_tile.Tile, options?: $protobuf.IConversionOptions): { [k: string]: any };
1919
public toJSON(): { [k: string]: any };
20-
public static getTypeUrl(): string;
20+
public static getTypeUrl(typeUrlPrefix?: string): string;
2121
}
2222

2323
namespace Tile {
@@ -57,7 +57,7 @@ export namespace vector_tile {
5757
public static fromObject(object: { [k: string]: any }): vector_tile.Tile.Value;
5858
public static toObject(message: vector_tile.Tile.Value, options?: $protobuf.IConversionOptions): { [k: string]: any };
5959
public toJSON(): { [k: string]: any };
60-
public static getTypeUrl(): string;
60+
public static getTypeUrl(typeUrlPrefix?: string): string;
6161
}
6262

6363
interface IFeature {
@@ -82,7 +82,7 @@ export namespace vector_tile {
8282
public static fromObject(object: { [k: string]: any }): vector_tile.Tile.Feature;
8383
public static toObject(message: vector_tile.Tile.Feature, options?: $protobuf.IConversionOptions): { [k: string]: any };
8484
public toJSON(): { [k: string]: any };
85-
public static getTypeUrl(): string;
85+
public static getTypeUrl(typeUrlPrefix?: string): string;
8686
}
8787

8888
interface ILayer {
@@ -111,7 +111,7 @@ export namespace vector_tile {
111111
public static fromObject(object: { [k: string]: any }): vector_tile.Tile.Layer;
112112
public static toObject(message: vector_tile.Tile.Layer, options?: $protobuf.IConversionOptions): { [k: string]: any };
113113
public toJSON(): { [k: string]: any };
114-
public static getTypeUrl(): string;
114+
public static getTypeUrl(typeUrlPrefix?: string): string;
115115
}
116116
}
117117
}

tests/data/mapbox/vector_tile.js

+24-8
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,14 @@ $root.vector_tile = (function() {
228228
* @function getTypeUrl
229229
* @memberof vector_tile.Tile
230230
* @static
231+
* @param {string} [typeUrlPrefix] your custom typeUrlPrefix(default "type.googleapis.com")
231232
* @returns {string} The default type url
232233
*/
233-
Tile.getTypeUrl = function getTypeUrl() {
234-
return "type.googleapis.com/vector_tile.Tile";
234+
Tile.getTypeUrl = function getTypeUrl(typeUrlPrefix) {
235+
if (typeUrlPrefix === undefined) {
236+
typeUrlPrefix = "type.googleapis.com";
237+
}
238+
return typeUrlPrefix + "/vector_tile.Tile";
235239
};
236240

237241
/**
@@ -616,10 +620,14 @@ $root.vector_tile = (function() {
616620
* @function getTypeUrl
617621
* @memberof vector_tile.Tile.Value
618622
* @static
623+
* @param {string} [typeUrlPrefix] your custom typeUrlPrefix(default "type.googleapis.com")
619624
* @returns {string} The default type url
620625
*/
621-
Value.getTypeUrl = function getTypeUrl() {
622-
return "type.googleapis.com/vector_tile.Tile.Value";
626+
Value.getTypeUrl = function getTypeUrl(typeUrlPrefix) {
627+
if (typeUrlPrefix === undefined) {
628+
typeUrlPrefix = "type.googleapis.com";
629+
}
630+
return typeUrlPrefix + "/vector_tile.Tile.Value";
623631
};
624632

625633
return Value;
@@ -968,10 +976,14 @@ $root.vector_tile = (function() {
968976
* @function getTypeUrl
969977
* @memberof vector_tile.Tile.Feature
970978
* @static
979+
* @param {string} [typeUrlPrefix] your custom typeUrlPrefix(default "type.googleapis.com")
971980
* @returns {string} The default type url
972981
*/
973-
Feature.getTypeUrl = function getTypeUrl() {
974-
return "type.googleapis.com/vector_tile.Tile.Feature";
982+
Feature.getTypeUrl = function getTypeUrl(typeUrlPrefix) {
983+
if (typeUrlPrefix === undefined) {
984+
typeUrlPrefix = "type.googleapis.com";
985+
}
986+
return typeUrlPrefix + "/vector_tile.Tile.Feature";
975987
};
976988

977989
return Feature;
@@ -1337,10 +1349,14 @@ $root.vector_tile = (function() {
13371349
* @function getTypeUrl
13381350
* @memberof vector_tile.Tile.Layer
13391351
* @static
1352+
* @param {string} [typeUrlPrefix] your custom typeUrlPrefix(default "type.googleapis.com")
13401353
* @returns {string} The default type url
13411354
*/
1342-
Layer.getTypeUrl = function getTypeUrl() {
1343-
return "type.googleapis.com/vector_tile.Tile.Layer";
1355+
Layer.getTypeUrl = function getTypeUrl(typeUrlPrefix) {
1356+
if (typeUrlPrefix === undefined) {
1357+
typeUrlPrefix = "type.googleapis.com";
1358+
}
1359+
return typeUrlPrefix + "/vector_tile.Tile.Layer";
13441360
};
13451361

13461362
return Layer;

tests/data/package.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class Package implements IPackage {
4747
public static fromObject(object: { [k: string]: any }): Package;
4848
public static toObject(message: Package, options?: $protobuf.IConversionOptions): { [k: string]: any };
4949
public toJSON(): { [k: string]: any };
50-
public static getTypeUrl(): string;
50+
public static getTypeUrl(typeUrlPrefix?: string): string;
5151
}
5252

5353
export namespace Package {
@@ -70,6 +70,6 @@ export namespace Package {
7070
public static fromObject(object: { [k: string]: any }): Package.Repository;
7171
public static toObject(message: Package.Repository, options?: $protobuf.IConversionOptions): { [k: string]: any };
7272
public toJSON(): { [k: string]: any };
73-
public static getTypeUrl(): string;
73+
public static getTypeUrl(typeUrlPrefix?: string): string;
7474
}
7575
}

tests/data/package.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -729,10 +729,14 @@ $root.Package = (function() {
729729
* @function getTypeUrl
730730
* @memberof Package
731731
* @static
732+
* @param {string} [typeUrlPrefix] your custom typeUrlPrefix(default "type.googleapis.com")
732733
* @returns {string} The default type url
733734
*/
734-
Package.getTypeUrl = function getTypeUrl() {
735-
return "type.googleapis.com/Package";
735+
Package.getTypeUrl = function getTypeUrl(typeUrlPrefix) {
736+
if (typeUrlPrefix === undefined) {
737+
typeUrlPrefix = "type.googleapis.com";
738+
}
739+
return typeUrlPrefix + "/Package";
736740
};
737741

738742
Package.Repository = (function() {
@@ -947,10 +951,14 @@ $root.Package = (function() {
947951
* @function getTypeUrl
948952
* @memberof Package.Repository
949953
* @static
954+
* @param {string} [typeUrlPrefix] your custom typeUrlPrefix(default "type.googleapis.com")
950955
* @returns {string} The default type url
951956
*/
952-
Repository.getTypeUrl = function getTypeUrl() {
953-
return "type.googleapis.com/Package.Repository";
957+
Repository.getTypeUrl = function getTypeUrl(typeUrlPrefix) {
958+
if (typeUrlPrefix === undefined) {
959+
typeUrlPrefix = "type.googleapis.com";
960+
}
961+
return typeUrlPrefix + "/Package.Repository";
954962
};
955963

956964
return Repository;

tests/data/rpc-es6.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class MyRequest implements IMyRequest {
2727
public static fromObject(object: { [k: string]: any }): MyRequest;
2828
public static toObject(message: MyRequest, options?: $protobuf.IConversionOptions): { [k: string]: any };
2929
public toJSON(): { [k: string]: any };
30-
public static getTypeUrl(): string;
30+
public static getTypeUrl(typeUrlPrefix?: string): string;
3131
}
3232

3333
export interface IMyResponse {
@@ -46,5 +46,5 @@ export class MyResponse implements IMyResponse {
4646
public static fromObject(object: { [k: string]: any }): MyResponse;
4747
public static toObject(message: MyResponse, options?: $protobuf.IConversionOptions): { [k: string]: any };
4848
public toJSON(): { [k: string]: any };
49-
public static getTypeUrl(): string;
49+
public static getTypeUrl(typeUrlPrefix?: string): string;
5050
}

tests/data/rpc-es6.js

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
/*eslint-disable block-scoped-var, id-length, no-control-regex, no-magic-numbers, no-prototype-builtins, no-redeclare, no-shadow, no-var, sort-vars*/
2-
"use strict";
3-
4-
var $protobuf = require("../../minimal");
2+
import * as $protobuf from "../../minimal";
53

64
// Common aliases
75
const $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;
@@ -266,10 +264,14 @@ export const MyRequest = $root.MyRequest = (() => {
266264
* @function getTypeUrl
267265
* @memberof MyRequest
268266
* @static
267+
* @param {string} [typeUrlPrefix] your custom typeUrlPrefix(default "type.googleapis.com")
269268
* @returns {string} The default type url
270269
*/
271-
MyRequest.getTypeUrl = function getTypeUrl() {
272-
return "type.googleapis.com/MyRequest";
270+
MyRequest.getTypeUrl = function getTypeUrl(typeUrlPrefix) {
271+
if (typeUrlPrefix === undefined) {
272+
typeUrlPrefix = "type.googleapis.com";
273+
}
274+
return typeUrlPrefix + "/MyRequest";
273275
};
274276

275277
return MyRequest;
@@ -464,13 +466,17 @@ export const MyResponse = $root.MyResponse = (() => {
464466
* @function getTypeUrl
465467
* @memberof MyResponse
466468
* @static
469+
* @param {string} [typeUrlPrefix] your custom typeUrlPrefix(default "type.googleapis.com")
467470
* @returns {string} The default type url
468471
*/
469-
MyResponse.getTypeUrl = function getTypeUrl() {
470-
return "type.googleapis.com/MyResponse";
472+
MyResponse.getTypeUrl = function getTypeUrl(typeUrlPrefix) {
473+
if (typeUrlPrefix === undefined) {
474+
typeUrlPrefix = "type.googleapis.com";
475+
}
476+
return typeUrlPrefix + "/MyResponse";
471477
};
472478

473479
return MyResponse;
474480
})();
475481

476-
module.exports = $root;
482+
export { $root as default };

tests/data/rpc-reserved.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class MyRequest implements IMyRequest {
2727
public static fromObject(object: { [k: string]: any }): MyRequest;
2828
public static toObject(message: MyRequest, options?: $protobuf.IConversionOptions): { [k: string]: any };
2929
public toJSON(): { [k: string]: any };
30-
public static getTypeUrl(): string;
30+
public static getTypeUrl(typeUrlPrefix?: string): string;
3131
}
3232

3333
export interface IMyResponse {
@@ -46,5 +46,5 @@ export class MyResponse implements IMyResponse {
4646
public static fromObject(object: { [k: string]: any }): MyResponse;
4747
public static toObject(message: MyResponse, options?: $protobuf.IConversionOptions): { [k: string]: any };
4848
public toJSON(): { [k: string]: any };
49-
public static getTypeUrl(): string;
49+
public static getTypeUrl(typeUrlPrefix?: string): string;
5050
}

0 commit comments

Comments
 (0)