Skip to content

Commit d13d5d5

Browse files
author
Taylor McIntyre
authored
feat: add getTypeUrl method to generated code (#1463)
* Add getTypeUrl method to static-module generated code * add tests for getTypeUrl method
1 parent f5b893c commit d13d5d5

22 files changed

+1319
-4
lines changed

cli/pbjs.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ exports.main = function main(args, callback) {
4141
"force-message": "strict-message"
4242
},
4343
string: [ "target", "out", "path", "wrap", "dependency", "root", "lint" ],
44-
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" ],
44+
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" ],
4545
default: {
4646
target: "json",
4747
create: true,
@@ -50,6 +50,7 @@ exports.main = function main(args, callback) {
5050
verify: true,
5151
convert: true,
5252
delimited: true,
53+
typeurl: true,
5354
beautify: true,
5455
comments: true,
5556
service: true,
@@ -132,6 +133,7 @@ exports.main = function main(args, callback) {
132133
" --no-verify Does not generate verify functions.",
133134
" --no-convert Does not generate convert functions like from/toObject",
134135
" --no-delimited Does not generate delimited encode/decode functions.",
136+
" --no-typeurl Does not generate getTypeUrl function.",
135137
" --no-beautify Does not beautify generated code.",
136138
" --no-comments Does not output any JSDoc comments.",
137139
" --no-service Does not output service classes.",

cli/targets/static.js

+16
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,22 @@ function buildType(ref, type) {
589589
--indent;
590590
push("};");
591591
}
592+
593+
if (config.typeurl) {
594+
push("");
595+
pushComment([
596+
"Gets the default type url for " + type.name,
597+
"@function getTypeUrl",
598+
"@memberof " + exportName(type),
599+
"@static",
600+
"@returns {string} The default type url"
601+
]);
602+
push(escapeName(type.name) + ".getTypeUrl = function getTypeUrl() {");
603+
++indent;
604+
push("return \"type.googleapis.com/" + exportName(type) + "\";");
605+
--indent;
606+
push("};");
607+
}
592608
}
593609

594610
function buildService(ref, service) {

scripts/gentests.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ var fs = require("fs"),
1313
{ file: "tests/data/rpc.proto", flags: [] },
1414
{ file: "tests/data/rpc-reserved.proto", flags: [] },
1515
{ file: "tests/data/test.proto", flags: [] },
16-
{ file: "bench/data/bench.proto", flags: ["no-create", "no-verify", "no-delimited", "no-convert", "no-comments"], out: "bench/data/static_pbjs.js" }
16+
{ file: "tests/data/type_url.proto", flags: [] },
17+
{ 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" }
1718
]
1819
.forEach(function({ file, flags, out }) {
1920
var basename = file.replace(/\.proto$/, "");

tests/data/comments.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +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;
2223
}
2324

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

4042
export enum Test3 {

tests/data/comments.js

+22
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,17 @@ $root.Test1 = (function() {
241241
return this.constructor.toObject(this, $protobuf.util.toJSONOptions);
242242
};
243243

244+
/**
245+
* Gets the default type url for Test1
246+
* @function getTypeUrl
247+
* @memberof Test1
248+
* @static
249+
* @returns {string} The default type url
250+
*/
251+
Test1.getTypeUrl = function getTypeUrl() {
252+
return "type.googleapis.com/Test1";
253+
};
254+
244255
return Test1;
245256
})();
246257

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

415+
/**
416+
* Gets the default type url for Test2
417+
* @function getTypeUrl
418+
* @memberof Test2
419+
* @static
420+
* @returns {string} The default type url
421+
*/
422+
Test2.getTypeUrl = function getTypeUrl() {
423+
return "type.googleapis.com/Test2";
424+
};
425+
404426
return Test2;
405427
})();
406428

tests/data/convert.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +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;
3435
}
3536

3637
export namespace Message {

tests/data/convert.js

+11
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,17 @@ $root.Message = (function() {
562562
return this.constructor.toObject(this, $protobuf.util.toJSONOptions);
563563
};
564564

565+
/**
566+
* Gets the default type url for Message
567+
* @function getTypeUrl
568+
* @memberof Message
569+
* @static
570+
* @returns {string} The default type url
571+
*/
572+
Message.getTypeUrl = function getTypeUrl() {
573+
return "type.googleapis.com/Message";
574+
};
575+
565576
/**
566577
* SomeEnum enum.
567578
* @name Message.SomeEnum

tests/data/mapbox/vector_tile.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +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;
2021
}
2122

2223
namespace Tile {
@@ -56,6 +57,7 @@ export namespace vector_tile {
5657
public static fromObject(object: { [k: string]: any }): vector_tile.Tile.Value;
5758
public static toObject(message: vector_tile.Tile.Value, options?: $protobuf.IConversionOptions): { [k: string]: any };
5859
public toJSON(): { [k: string]: any };
60+
public static getTypeUrl(): string;
5961
}
6062

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

8588
interface ILayer {
@@ -108,6 +111,7 @@ export namespace vector_tile {
108111
public static fromObject(object: { [k: string]: any }): vector_tile.Tile.Layer;
109112
public static toObject(message: vector_tile.Tile.Layer, options?: $protobuf.IConversionOptions): { [k: string]: any };
110113
public toJSON(): { [k: string]: any };
114+
public static getTypeUrl(): string;
111115
}
112116
}
113117
}

tests/data/mapbox/vector_tile.js

+44
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,17 @@ $root.vector_tile = (function() {
223223
return this.constructor.toObject(this, $protobuf.util.toJSONOptions);
224224
};
225225

226+
/**
227+
* Gets the default type url for Tile
228+
* @function getTypeUrl
229+
* @memberof vector_tile.Tile
230+
* @static
231+
* @returns {string} The default type url
232+
*/
233+
Tile.getTypeUrl = function getTypeUrl() {
234+
return "type.googleapis.com/vector_tile.Tile";
235+
};
236+
226237
/**
227238
* GeomType enum.
228239
* @name vector_tile.Tile.GeomType
@@ -600,6 +611,17 @@ $root.vector_tile = (function() {
600611
return this.constructor.toObject(this, $protobuf.util.toJSONOptions);
601612
};
602613

614+
/**
615+
* Gets the default type url for Value
616+
* @function getTypeUrl
617+
* @memberof vector_tile.Tile.Value
618+
* @static
619+
* @returns {string} The default type url
620+
*/
621+
Value.getTypeUrl = function getTypeUrl() {
622+
return "type.googleapis.com/vector_tile.Tile.Value";
623+
};
624+
603625
return Value;
604626
})();
605627

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

966+
/**
967+
* Gets the default type url for Feature
968+
* @function getTypeUrl
969+
* @memberof vector_tile.Tile.Feature
970+
* @static
971+
* @returns {string} The default type url
972+
*/
973+
Feature.getTypeUrl = function getTypeUrl() {
974+
return "type.googleapis.com/vector_tile.Tile.Feature";
975+
};
976+
944977
return Feature;
945978
})();
946979

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

1335+
/**
1336+
* Gets the default type url for Layer
1337+
* @function getTypeUrl
1338+
* @memberof vector_tile.Tile.Layer
1339+
* @static
1340+
* @returns {string} The default type url
1341+
*/
1342+
Layer.getTypeUrl = function getTypeUrl() {
1343+
return "type.googleapis.com/vector_tile.Tile.Layer";
1344+
};
1345+
13021346
return Layer;
13031347
})();
13041348

tests/data/package.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +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;
5051
}
5152

5253
export namespace Package {
@@ -69,5 +70,6 @@ export namespace Package {
6970
public static fromObject(object: { [k: string]: any }): Package.Repository;
7071
public static toObject(message: Package.Repository, options?: $protobuf.IConversionOptions): { [k: string]: any };
7172
public toJSON(): { [k: string]: any };
73+
public static getTypeUrl(): string;
7274
}
7375
}

tests/data/package.js

+22
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,17 @@ $root.Package = (function() {
724724
return this.constructor.toObject(this, $protobuf.util.toJSONOptions);
725725
};
726726

727+
/**
728+
* Gets the default type url for Package
729+
* @function getTypeUrl
730+
* @memberof Package
731+
* @static
732+
* @returns {string} The default type url
733+
*/
734+
Package.getTypeUrl = function getTypeUrl() {
735+
return "type.googleapis.com/Package";
736+
};
737+
727738
Package.Repository = (function() {
728739

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

945+
/**
946+
* Gets the default type url for Repository
947+
* @function getTypeUrl
948+
* @memberof Package.Repository
949+
* @static
950+
* @returns {string} The default type url
951+
*/
952+
Repository.getTypeUrl = function getTypeUrl() {
953+
return "type.googleapis.com/Package.Repository";
954+
};
955+
934956
return Repository;
935957
})();
936958

tests/data/rpc-es6.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +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;
3031
}
3132

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

tests/data/rpc-es6.js

+26-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
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-
import * as $protobuf from "../../minimal";
2+
"use strict";
3+
4+
var $protobuf = require("../../minimal");
35

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

264+
/**
265+
* Gets the default type url for MyRequest
266+
* @function getTypeUrl
267+
* @memberof MyRequest
268+
* @static
269+
* @returns {string} The default type url
270+
*/
271+
MyRequest.getTypeUrl = function getTypeUrl() {
272+
return "type.googleapis.com/MyRequest";
273+
};
274+
262275
return MyRequest;
263276
})();
264277

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

462+
/**
463+
* Gets the default type url for MyResponse
464+
* @function getTypeUrl
465+
* @memberof MyResponse
466+
* @static
467+
* @returns {string} The default type url
468+
*/
469+
MyResponse.getTypeUrl = function getTypeUrl() {
470+
return "type.googleapis.com/MyResponse";
471+
};
472+
449473
return MyResponse;
450474
})();
451475

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

tests/data/rpc-reserved.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +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;
3031
}
3132

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

0 commit comments

Comments
 (0)