Skip to content

Commit

Permalink
fix: remove-enum-prefix for nested enums (#903)
Browse files Browse the repository at this point in the history
  • Loading branch information
buzap authored Aug 15, 2023
1 parent 54661eb commit efdbf47
Show file tree
Hide file tree
Showing 10 changed files with 488 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,46 @@
import { EnumFields, Foo, Bar } from "./remove-enum-prefix-string-enums";
import {
Foo,
Bar,
fooFromJSON,
fooToJSON,
barFromJSON,
barToJSON,
WithNestedEnum_Baz,
WithNestedEnum_Qux,
withNestedEnum_BazFromJSON,
withNestedEnum_BazToJSON,
withNestedEnum_QuxFromJSON,
withNestedEnum_QuxToJSON,
} from "./remove-enum-prefix-string-enums";

describe("nestjs-metadata-test", () => {
it("compiles", () => {
const msg: EnumFields = {
foo: Foo.BAR,
bar: Bar.BAZ,
};
const out = EnumFields.toJSON(msg);
expect(out).not.toBeUndefined();
function testEnumFromJSONAndToJSON<ENUM>(
fromJSON: (s: string) => ENUM,
toJSON: (e: ENUM) => string,
valueMap: Record<string, ENUM>,
) {
for (const [jsonValue, enumValue] of Object.entries(valueMap)) {
expect(fromJSON(jsonValue)).toBe(enumValue);
expect(toJSON(enumValue)).toBe(jsonValue);
}
}

describe("remove-enum-prefix-string-enums", () => {
it("encode and decode correctly", () => {
testEnumFromJSONAndToJSON(fooFromJSON, fooToJSON, {
FOO_UNSPECIFIED: Foo.UNSPECIFIED,
FOO_BAR: Foo.BAR,
});
testEnumFromJSONAndToJSON(barFromJSON, barToJSON, {
BAR_UNSPECIFIED: Bar.UNSPECIFIED,
BAZ: Bar.BAZ,
});
testEnumFromJSONAndToJSON(withNestedEnum_BazFromJSON, withNestedEnum_BazToJSON, {
BAZ_UNSPECIFIED: WithNestedEnum_Baz.UNSPECIFIED,
BAZ_ONE: WithNestedEnum_Baz.ONE,
});
testEnumFromJSONAndToJSON(withNestedEnum_QuxFromJSON, withNestedEnum_QuxToJSON, {
QUX_UNSPECIFIED: WithNestedEnum_Qux.UNSPECIFIED,
ONE: WithNestedEnum_Qux.ONE,
});
});
});
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,21 @@ enum Bar {
QUX = 2;
}

message EnumFields {
message WithNestedEnum {
enum Baz {
BAZ_UNSPECIFIED = 0;
BAZ_ONE = 1;
BAZ_TWO = 2;
}

enum Qux {
QUX_UNSPECIFIED = 0;
ONE = 1;
TWO = 2;
}

Foo foo = 1;
Bar bar = 2;
Bar Bar = 2;
Baz baz = 3;
Qux qux = 4;
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,30 +99,139 @@ export function barToNumber(object: Bar): number {
}
}

export interface EnumFields {
export interface WithNestedEnum {
foo: Foo;
bar: Bar;
Bar: Bar;
baz: WithNestedEnum_Baz;
qux: WithNestedEnum_Qux;
}

function createBaseEnumFields(): EnumFields {
return { foo: Foo.UNSPECIFIED, bar: Bar.UNSPECIFIED };
export enum WithNestedEnum_Baz {
UNSPECIFIED = "BAZ_UNSPECIFIED",
ONE = "BAZ_ONE",
TWO = "BAZ_TWO",
}

export const EnumFields = {
encode(message: EnumFields, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
export function withNestedEnum_BazFromJSON(object: any): WithNestedEnum_Baz {
switch (object) {
case 0:
case "BAZ_UNSPECIFIED":
return WithNestedEnum_Baz.UNSPECIFIED;
case 1:
case "BAZ_ONE":
return WithNestedEnum_Baz.ONE;
case 2:
case "BAZ_TWO":
return WithNestedEnum_Baz.TWO;
default:
throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum WithNestedEnum_Baz");
}
}

export function withNestedEnum_BazToJSON(object: WithNestedEnum_Baz): string {
switch (object) {
case WithNestedEnum_Baz.UNSPECIFIED:
return "BAZ_UNSPECIFIED";
case WithNestedEnum_Baz.ONE:
return "BAZ_ONE";
case WithNestedEnum_Baz.TWO:
return "BAZ_TWO";
default:
throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum WithNestedEnum_Baz");
}
}

export function withNestedEnum_BazToNumber(object: WithNestedEnum_Baz): number {
switch (object) {
case WithNestedEnum_Baz.UNSPECIFIED:
return 0;
case WithNestedEnum_Baz.ONE:
return 1;
case WithNestedEnum_Baz.TWO:
return 2;
default:
throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum WithNestedEnum_Baz");
}
}

export enum WithNestedEnum_Qux {
UNSPECIFIED = "QUX_UNSPECIFIED",
ONE = "ONE",
TWO = "TWO",
}

export function withNestedEnum_QuxFromJSON(object: any): WithNestedEnum_Qux {
switch (object) {
case 0:
case "QUX_UNSPECIFIED":
return WithNestedEnum_Qux.UNSPECIFIED;
case 1:
case "ONE":
return WithNestedEnum_Qux.ONE;
case 2:
case "TWO":
return WithNestedEnum_Qux.TWO;
default:
throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum WithNestedEnum_Qux");
}
}

export function withNestedEnum_QuxToJSON(object: WithNestedEnum_Qux): string {
switch (object) {
case WithNestedEnum_Qux.UNSPECIFIED:
return "QUX_UNSPECIFIED";
case WithNestedEnum_Qux.ONE:
return "ONE";
case WithNestedEnum_Qux.TWO:
return "TWO";
default:
throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum WithNestedEnum_Qux");
}
}

export function withNestedEnum_QuxToNumber(object: WithNestedEnum_Qux): number {
switch (object) {
case WithNestedEnum_Qux.UNSPECIFIED:
return 0;
case WithNestedEnum_Qux.ONE:
return 1;
case WithNestedEnum_Qux.TWO:
return 2;
default:
throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum WithNestedEnum_Qux");
}
}

function createBaseWithNestedEnum(): WithNestedEnum {
return {
foo: Foo.UNSPECIFIED,
Bar: Bar.UNSPECIFIED,
baz: WithNestedEnum_Baz.UNSPECIFIED,
qux: WithNestedEnum_Qux.UNSPECIFIED,
};
}

export const WithNestedEnum = {
encode(message: WithNestedEnum, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.foo !== Foo.UNSPECIFIED) {
writer.uint32(8).int32(fooToNumber(message.foo));
}
if (message.bar !== Bar.UNSPECIFIED) {
writer.uint32(16).int32(barToNumber(message.bar));
if (message.Bar !== Bar.UNSPECIFIED) {
writer.uint32(16).int32(barToNumber(message.Bar));
}
if (message.baz !== WithNestedEnum_Baz.UNSPECIFIED) {
writer.uint32(24).int32(withNestedEnum_BazToNumber(message.baz));
}
if (message.qux !== WithNestedEnum_Qux.UNSPECIFIED) {
writer.uint32(32).int32(withNestedEnum_QuxToNumber(message.qux));
}
return writer;
},

decode(input: _m0.Reader | Uint8Array, length?: number): EnumFields {
decode(input: _m0.Reader | Uint8Array, length?: number): WithNestedEnum {
const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseEnumFields();
const message = createBaseWithNestedEnum();
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
Expand All @@ -138,7 +247,21 @@ export const EnumFields = {
break;
}

message.bar = barFromJSON(reader.int32());
message.Bar = barFromJSON(reader.int32());
continue;
case 3:
if (tag !== 24) {
break;
}

message.baz = withNestedEnum_BazFromJSON(reader.int32());
continue;
case 4:
if (tag !== 32) {
break;
}

message.qux = withNestedEnum_QuxFromJSON(reader.int32());
continue;
}
if ((tag & 7) === 4 || tag === 0) {
Expand All @@ -149,31 +272,41 @@ export const EnumFields = {
return message;
},

fromJSON(object: any): EnumFields {
fromJSON(object: any): WithNestedEnum {
return {
foo: isSet(object.foo) ? fooFromJSON(object.foo) : Foo.UNSPECIFIED,
bar: isSet(object.bar) ? barFromJSON(object.bar) : Bar.UNSPECIFIED,
Bar: isSet(object.Bar) ? barFromJSON(object.Bar) : Bar.UNSPECIFIED,
baz: isSet(object.baz) ? withNestedEnum_BazFromJSON(object.baz) : WithNestedEnum_Baz.UNSPECIFIED,
qux: isSet(object.qux) ? withNestedEnum_QuxFromJSON(object.qux) : WithNestedEnum_Qux.UNSPECIFIED,
};
},

toJSON(message: EnumFields): unknown {
toJSON(message: WithNestedEnum): unknown {
const obj: any = {};
if (message.foo !== Foo.UNSPECIFIED) {
obj.foo = fooToJSON(message.foo);
}
if (message.bar !== Bar.UNSPECIFIED) {
obj.bar = barToJSON(message.bar);
if (message.Bar !== Bar.UNSPECIFIED) {
obj.Bar = barToJSON(message.Bar);
}
if (message.baz !== WithNestedEnum_Baz.UNSPECIFIED) {
obj.baz = withNestedEnum_BazToJSON(message.baz);
}
if (message.qux !== WithNestedEnum_Qux.UNSPECIFIED) {
obj.qux = withNestedEnum_QuxToJSON(message.qux);
}
return obj;
},

create<I extends Exact<DeepPartial<EnumFields>, I>>(base?: I): EnumFields {
return EnumFields.fromPartial(base ?? ({} as any));
create<I extends Exact<DeepPartial<WithNestedEnum>, I>>(base?: I): WithNestedEnum {
return WithNestedEnum.fromPartial(base ?? ({} as any));
},
fromPartial<I extends Exact<DeepPartial<EnumFields>, I>>(object: I): EnumFields {
const message = createBaseEnumFields();
fromPartial<I extends Exact<DeepPartial<WithNestedEnum>, I>>(object: I): WithNestedEnum {
const message = createBaseWithNestedEnum();
message.foo = object.foo ?? Foo.UNSPECIFIED;
message.bar = object.bar ?? Bar.UNSPECIFIED;
message.Bar = object.Bar ?? Bar.UNSPECIFIED;
message.baz = object.baz ?? WithNestedEnum_Baz.UNSPECIFIED;
message.qux = object.qux ?? WithNestedEnum_Qux.UNSPECIFIED;
return message;
},
};
Expand Down
46 changes: 46 additions & 0 deletions integration/remove-enum-prefix/remove-enum-prefix-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {
Foo,
Bar,
fooFromJSON,
fooToJSON,
barFromJSON,
barToJSON,
withNestedEnum_BazFromJSON,
WithNestedEnum_Baz,
withNestedEnum_BazToJSON,
withNestedEnum_QuxFromJSON,
withNestedEnum_QuxToJSON,
WithNestedEnum_Qux,
} from "./remove-enum-prefix";

function testEnumFromJSONAndToJSON<ENUM>(
fromJSON: (s: string) => ENUM,
toJSON: (e: ENUM) => string,
valueMap: Record<string, ENUM>,
) {
for (const [jsonValue, enumValue] of Object.entries(valueMap)) {
expect(fromJSON(jsonValue)).toBe(enumValue);
expect(toJSON(enumValue)).toBe(jsonValue);
}
}

describe("remove-enum-prefix", () => {
it("encode and decode correctly", () => {
testEnumFromJSONAndToJSON(fooFromJSON, fooToJSON, {
FOO_UNSPECIFIED: Foo.UNSPECIFIED,
FOO_BAR: Foo.BAR,
});
testEnumFromJSONAndToJSON(barFromJSON, barToJSON, {
BAR_UNSPECIFIED: Bar.UNSPECIFIED,
BAZ: Bar.BAZ,
});
testEnumFromJSONAndToJSON(withNestedEnum_BazFromJSON, withNestedEnum_BazToJSON, {
BAZ_UNSPECIFIED: WithNestedEnum_Baz.UNSPECIFIED,
BAZ_ONE: WithNestedEnum_Baz.ONE,
});
testEnumFromJSONAndToJSON(withNestedEnum_QuxFromJSON, withNestedEnum_QuxToJSON, {
QUX_UNSPECIFIED: WithNestedEnum_Qux.UNSPECIFIED,
ONE: WithNestedEnum_Qux.ONE,
});
});
});
Binary file modified integration/remove-enum-prefix/remove-enum-prefix.bin
Binary file not shown.
19 changes: 19 additions & 0 deletions integration/remove-enum-prefix/remove-enum-prefix.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,22 @@ enum Bar {
BAZ = 1;
QUX = 2;
}

message WithNestedEnum {
enum Baz {
BAZ_UNSPECIFIED = 0;
BAZ_ONE = 1;
BAZ_TWO = 2;
}

enum Qux {
QUX_UNSPECIFIED = 0;
ONE = 1;
TWO = 2;
}

Foo foo = 1;
Bar Bar = 2;
Baz baz = 3;
Qux qux = 4;
}
Loading

0 comments on commit efdbf47

Please sign in to comment.