Skip to content

Commit

Permalink
Strip the prefixes required by proto for enums
Browse files Browse the repository at this point in the history
  • Loading branch information
mjgp2 committed Mar 10, 2021
1 parent d703f0f commit 653ed16
Show file tree
Hide file tree
Showing 15 changed files with 122 additions and 100 deletions.
10 changes: 9 additions & 1 deletion dist/light/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/light/protobuf.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/light/protobuf.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/light/protobuf.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/minimal/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/minimal/protobuf.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion dist/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/protobuf.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/protobuf.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/protobuf.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ function genValuePartial_toObject(gen, field, fieldIndex, prop) {
/* eslint-disable no-unexpected-multiline, block-scoped-var, no-redeclare */
if (field.resolvedType) {
if (field.resolvedType instanceof Enum) gen
("d%s=o.enums===String?types[%i].values[m%s].replace(new RegExp('^'+types[%i].name.replace(/([a-z])([A-Z])/g, '$1_$2')+'_', 'i'), ''):m%s", prop, fieldIndex, prop, fieldIndex, prop);
("d%s=o.enums===String?types[%i].values[m%s]:m%s", prop, fieldIndex, prop, prop);
else gen
("d%s=types[%i].toObject(m%s,o)", prop, fieldIndex, prop);
} else {
Expand Down
7 changes: 7 additions & 0 deletions src/enum.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ Enum.prototype.toJSON = function toJSON(toJSONOptions) {
Enum.prototype.add = function add(name, id, comment) {
// utilized by the parser but not by .fromJSON

// This will strip off the enum namespace prefixes
if (!this._stripPrefixRegexp) {
this._stripPrefixRegexp = new RegExp("^" + this.name.replace(/([a-z])([A-Z])/g, "$1_$2") + "_", "i");
}

name = name.replace(this._stripPrefixRegexp, "");

if (!util.isString(name))
throw TypeError("name must be a string");

Expand Down
1 change: 0 additions & 1 deletion tests/data/convert.proto
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ message Message {
repeated SomeEnum enum_repeated = 8;

enum SomeEnum {
SOME_ENUM_UNSPECIFIED = 0;
SOME_ENUM_ONE = 1;
SOME_ENUM_TWO = 2;
}
Expand Down
42 changes: 21 additions & 21 deletions tests/data/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -916,30 +916,30 @@ export namespace google {
namespace FieldDescriptorProto {

enum Type {
TYPE_DOUBLE = 1,
TYPE_FLOAT = 2,
TYPE_INT64 = 3,
TYPE_UINT64 = 4,
TYPE_INT32 = 5,
TYPE_FIXED64 = 6,
TYPE_FIXED32 = 7,
TYPE_BOOL = 8,
TYPE_STRING = 9,
TYPE_GROUP = 10,
TYPE_MESSAGE = 11,
TYPE_BYTES = 12,
TYPE_UINT32 = 13,
TYPE_ENUM = 14,
TYPE_SFIXED32 = 15,
TYPE_SFIXED64 = 16,
TYPE_SINT32 = 17,
TYPE_SINT64 = 18
DOUBLE = 1,
FLOAT = 2,
INT64 = 3,
UINT64 = 4,
INT32 = 5,
FIXED64 = 6,
FIXED32 = 7,
BOOL = 8,
STRING = 9,
GROUP = 10,
MESSAGE = 11,
BYTES = 12,
UINT32 = 13,
ENUM = 14,
SFIXED32 = 15,
SFIXED64 = 16,
SINT32 = 17,
SINT64 = 18
}

enum Label {
LABEL_OPTIONAL = 1,
LABEL_REQUIRED = 2,
LABEL_REPEATED = 3
OPTIONAL = 1,
REQUIRED = 2,
REPEATED = 3
}
}

Expand Down
130 changes: 65 additions & 65 deletions tests/data/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10411,89 +10411,89 @@ $root.google = (function() {
if (object.number != null)
message.number = object.number | 0;
switch (object.label) {
case "LABEL_OPTIONAL":
case "OPTIONAL":
case 1:
message.label = 1;
break;
case "LABEL_REQUIRED":
case "REQUIRED":
case 2:
message.label = 2;
break;
case "LABEL_REPEATED":
case "REPEATED":
case 3:
message.label = 3;
break;
}
switch (object.type) {
case "TYPE_DOUBLE":
case "DOUBLE":
case 1:
message.type = 1;
break;
case "TYPE_FLOAT":
case "FLOAT":
case 2:
message.type = 2;
break;
case "TYPE_INT64":
case "INT64":
case 3:
message.type = 3;
break;
case "TYPE_UINT64":
case "UINT64":
case 4:
message.type = 4;
break;
case "TYPE_INT32":
case "INT32":
case 5:
message.type = 5;
break;
case "TYPE_FIXED64":
case "FIXED64":
case 6:
message.type = 6;
break;
case "TYPE_FIXED32":
case "FIXED32":
case 7:
message.type = 7;
break;
case "TYPE_BOOL":
case "BOOL":
case 8:
message.type = 8;
break;
case "TYPE_STRING":
case "STRING":
case 9:
message.type = 9;
break;
case "TYPE_GROUP":
case "GROUP":
case 10:
message.type = 10;
break;
case "TYPE_MESSAGE":
case "MESSAGE":
case 11:
message.type = 11;
break;
case "TYPE_BYTES":
case "BYTES":
case 12:
message.type = 12;
break;
case "TYPE_UINT32":
case "UINT32":
case 13:
message.type = 13;
break;
case "TYPE_ENUM":
case "ENUM":
case 14:
message.type = 14;
break;
case "TYPE_SFIXED32":
case "SFIXED32":
case 15:
message.type = 15;
break;
case "TYPE_SFIXED64":
case "SFIXED64":
case 16:
message.type = 16;
break;
case "TYPE_SINT32":
case "SINT32":
case 17:
message.type = 17;
break;
case "TYPE_SINT64":
case "SINT64":
case 18:
message.type = 18;
break;
Expand Down Expand Up @@ -10533,8 +10533,8 @@ $root.google = (function() {
object.name = "";
object.extendee = "";
object.number = 0;
object.label = options.enums === String ? "LABEL_OPTIONAL" : 1;
object.type = options.enums === String ? "TYPE_DOUBLE" : 1;
object.label = options.enums === String ? "OPTIONAL" : 1;
object.type = options.enums === String ? "DOUBLE" : 1;
object.typeName = "";
object.defaultValue = "";
object.options = null;
Expand Down Expand Up @@ -10579,61 +10579,61 @@ $root.google = (function() {
* Type enum.
* @name google.protobuf.FieldDescriptorProto.Type
* @enum {number}
* @property {number} TYPE_DOUBLE=1 TYPE_DOUBLE value
* @property {number} TYPE_FLOAT=2 TYPE_FLOAT value
* @property {number} TYPE_INT64=3 TYPE_INT64 value
* @property {number} TYPE_UINT64=4 TYPE_UINT64 value
* @property {number} TYPE_INT32=5 TYPE_INT32 value
* @property {number} TYPE_FIXED64=6 TYPE_FIXED64 value
* @property {number} TYPE_FIXED32=7 TYPE_FIXED32 value
* @property {number} TYPE_BOOL=8 TYPE_BOOL value
* @property {number} TYPE_STRING=9 TYPE_STRING value
* @property {number} TYPE_GROUP=10 TYPE_GROUP value
* @property {number} TYPE_MESSAGE=11 TYPE_MESSAGE value
* @property {number} TYPE_BYTES=12 TYPE_BYTES value
* @property {number} TYPE_UINT32=13 TYPE_UINT32 value
* @property {number} TYPE_ENUM=14 TYPE_ENUM value
* @property {number} TYPE_SFIXED32=15 TYPE_SFIXED32 value
* @property {number} TYPE_SFIXED64=16 TYPE_SFIXED64 value
* @property {number} TYPE_SINT32=17 TYPE_SINT32 value
* @property {number} TYPE_SINT64=18 TYPE_SINT64 value
* @property {number} DOUBLE=1 DOUBLE value
* @property {number} FLOAT=2 FLOAT value
* @property {number} INT64=3 INT64 value
* @property {number} UINT64=4 UINT64 value
* @property {number} INT32=5 INT32 value
* @property {number} FIXED64=6 FIXED64 value
* @property {number} FIXED32=7 FIXED32 value
* @property {number} BOOL=8 BOOL value
* @property {number} STRING=9 STRING value
* @property {number} GROUP=10 GROUP value
* @property {number} MESSAGE=11 MESSAGE value
* @property {number} BYTES=12 BYTES value
* @property {number} UINT32=13 UINT32 value
* @property {number} ENUM=14 ENUM value
* @property {number} SFIXED32=15 SFIXED32 value
* @property {number} SFIXED64=16 SFIXED64 value
* @property {number} SINT32=17 SINT32 value
* @property {number} SINT64=18 SINT64 value
*/
FieldDescriptorProto.Type = (function() {
var valuesById = {}, values = Object.create(valuesById);
values[valuesById[1] = "TYPE_DOUBLE"] = 1;
values[valuesById[2] = "TYPE_FLOAT"] = 2;
values[valuesById[3] = "TYPE_INT64"] = 3;
values[valuesById[4] = "TYPE_UINT64"] = 4;
values[valuesById[5] = "TYPE_INT32"] = 5;
values[valuesById[6] = "TYPE_FIXED64"] = 6;
values[valuesById[7] = "TYPE_FIXED32"] = 7;
values[valuesById[8] = "TYPE_BOOL"] = 8;
values[valuesById[9] = "TYPE_STRING"] = 9;
values[valuesById[10] = "TYPE_GROUP"] = 10;
values[valuesById[11] = "TYPE_MESSAGE"] = 11;
values[valuesById[12] = "TYPE_BYTES"] = 12;
values[valuesById[13] = "TYPE_UINT32"] = 13;
values[valuesById[14] = "TYPE_ENUM"] = 14;
values[valuesById[15] = "TYPE_SFIXED32"] = 15;
values[valuesById[16] = "TYPE_SFIXED64"] = 16;
values[valuesById[17] = "TYPE_SINT32"] = 17;
values[valuesById[18] = "TYPE_SINT64"] = 18;
values[valuesById[1] = "DOUBLE"] = 1;
values[valuesById[2] = "FLOAT"] = 2;
values[valuesById[3] = "INT64"] = 3;
values[valuesById[4] = "UINT64"] = 4;
values[valuesById[5] = "INT32"] = 5;
values[valuesById[6] = "FIXED64"] = 6;
values[valuesById[7] = "FIXED32"] = 7;
values[valuesById[8] = "BOOL"] = 8;
values[valuesById[9] = "STRING"] = 9;
values[valuesById[10] = "GROUP"] = 10;
values[valuesById[11] = "MESSAGE"] = 11;
values[valuesById[12] = "BYTES"] = 12;
values[valuesById[13] = "UINT32"] = 13;
values[valuesById[14] = "ENUM"] = 14;
values[valuesById[15] = "SFIXED32"] = 15;
values[valuesById[16] = "SFIXED64"] = 16;
values[valuesById[17] = "SINT32"] = 17;
values[valuesById[18] = "SINT64"] = 18;
return values;
})();

/**
* Label enum.
* @name google.protobuf.FieldDescriptorProto.Label
* @enum {number}
* @property {number} LABEL_OPTIONAL=1 LABEL_OPTIONAL value
* @property {number} LABEL_REQUIRED=2 LABEL_REQUIRED value
* @property {number} LABEL_REPEATED=3 LABEL_REPEATED value
* @property {number} OPTIONAL=1 OPTIONAL value
* @property {number} REQUIRED=2 REQUIRED value
* @property {number} REPEATED=3 REPEATED value
*/
FieldDescriptorProto.Label = (function() {
var valuesById = {}, values = Object.create(valuesById);
values[valuesById[1] = "LABEL_OPTIONAL"] = 1;
values[valuesById[2] = "LABEL_REQUIRED"] = 2;
values[valuesById[3] = "LABEL_REPEATED"] = 3;
values[valuesById[1] = "OPTIONAL"] = 1;
values[valuesById[2] = "REQUIRED"] = 2;
values[valuesById[3] = "REPEATED"] = 3;
return values;
})();

Expand Down

0 comments on commit 653ed16

Please sign in to comment.