diff --git a/integration/remove-enum-prefix-string-enums/remove-enum-prefix-string-enums-test.ts b/integration/remove-enum-prefix-string-enums/remove-enum-prefix-string-enums-test.ts index 8c33aa917..8ee6271fb 100644 --- a/integration/remove-enum-prefix-string-enums/remove-enum-prefix-string-enums-test.ts +++ b/integration/remove-enum-prefix-string-enums/remove-enum-prefix-string-enums-test.ts @@ -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( + fromJSON: (s: string) => ENUM, + toJSON: (e: ENUM) => string, + valueMap: Record, +) { + 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, + }); }); }); diff --git a/integration/remove-enum-prefix-string-enums/remove-enum-prefix-string-enums.bin b/integration/remove-enum-prefix-string-enums/remove-enum-prefix-string-enums.bin index 60eb50eb2..1cf01c057 100644 Binary files a/integration/remove-enum-prefix-string-enums/remove-enum-prefix-string-enums.bin and b/integration/remove-enum-prefix-string-enums/remove-enum-prefix-string-enums.bin differ diff --git a/integration/remove-enum-prefix-string-enums/remove-enum-prefix-string-enums.proto b/integration/remove-enum-prefix-string-enums/remove-enum-prefix-string-enums.proto index 2d908f67d..a34d8684f 100644 --- a/integration/remove-enum-prefix-string-enums/remove-enum-prefix-string-enums.proto +++ b/integration/remove-enum-prefix-string-enums/remove-enum-prefix-string-enums.proto @@ -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; } \ No newline at end of file diff --git a/integration/remove-enum-prefix-string-enums/remove-enum-prefix-string-enums.ts b/integration/remove-enum-prefix-string-enums/remove-enum-prefix-string-enums.ts index 2661afe27..5351246ea 100644 --- a/integration/remove-enum-prefix-string-enums/remove-enum-prefix-string-enums.ts +++ b/integration/remove-enum-prefix-string-enums/remove-enum-prefix-string-enums.ts @@ -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) { @@ -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) { @@ -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>>(base?: I): EnumFields { - return EnumFields.fromPartial(base ?? ({} as any)); + create, I>>(base?: I): WithNestedEnum { + return WithNestedEnum.fromPartial(base ?? ({} as any)); }, - fromPartial, I>>(object: I): EnumFields { - const message = createBaseEnumFields(); + fromPartial, 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; }, }; diff --git a/integration/remove-enum-prefix/remove-enum-prefix-test.ts b/integration/remove-enum-prefix/remove-enum-prefix-test.ts new file mode 100644 index 000000000..2a83917f5 --- /dev/null +++ b/integration/remove-enum-prefix/remove-enum-prefix-test.ts @@ -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( + fromJSON: (s: string) => ENUM, + toJSON: (e: ENUM) => string, + valueMap: Record, +) { + 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, + }); + }); +}); diff --git a/integration/remove-enum-prefix/remove-enum-prefix.bin b/integration/remove-enum-prefix/remove-enum-prefix.bin index fad3310de..422757361 100644 Binary files a/integration/remove-enum-prefix/remove-enum-prefix.bin and b/integration/remove-enum-prefix/remove-enum-prefix.bin differ diff --git a/integration/remove-enum-prefix/remove-enum-prefix.proto b/integration/remove-enum-prefix/remove-enum-prefix.proto index 1a658da79..a34d8684f 100644 --- a/integration/remove-enum-prefix/remove-enum-prefix.proto +++ b/integration/remove-enum-prefix/remove-enum-prefix.proto @@ -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; +} \ No newline at end of file diff --git a/integration/remove-enum-prefix/remove-enum-prefix.ts b/integration/remove-enum-prefix/remove-enum-prefix.ts index c243421be..48b7b73c8 100644 --- a/integration/remove-enum-prefix/remove-enum-prefix.ts +++ b/integration/remove-enum-prefix/remove-enum-prefix.ts @@ -1,4 +1,5 @@ /* eslint-disable */ +import * as _m0 from "protobufjs/minimal"; export const protobufPackage = ""; @@ -72,6 +73,187 @@ export function barToJSON(object: Bar): string { } } +export interface WithNestedEnum { + foo: Foo; + Bar: Bar; + baz: WithNestedEnum_Baz; + qux: WithNestedEnum_Qux; +} + +export enum WithNestedEnum_Baz { + UNSPECIFIED = 0, + ONE = 1, + TWO = 2, +} + +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 enum WithNestedEnum_Qux { + UNSPECIFIED = 0, + ONE = 1, + TWO = 2, +} + +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"); + } +} + +function createBaseWithNestedEnum(): WithNestedEnum { + return { foo: 0, Bar: 0, baz: 0, qux: 0 }; +} + +export const WithNestedEnum = { + encode(message: WithNestedEnum, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer { + if (message.foo !== 0) { + writer.uint32(8).int32(message.foo); + } + if (message.Bar !== 0) { + writer.uint32(16).int32(message.Bar); + } + if (message.baz !== 0) { + writer.uint32(24).int32(message.baz); + } + if (message.qux !== 0) { + writer.uint32(32).int32(message.qux); + } + return writer; + }, + + 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 = createBaseWithNestedEnum(); + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + if (tag !== 8) { + break; + } + + message.foo = reader.int32() as any; + continue; + case 2: + if (tag !== 16) { + break; + } + + message.Bar = reader.int32() as any; + continue; + case 3: + if (tag !== 24) { + break; + } + + message.baz = reader.int32() as any; + continue; + case 4: + if (tag !== 32) { + break; + } + + message.qux = reader.int32() as any; + continue; + } + if ((tag & 7) === 4 || tag === 0) { + break; + } + reader.skipType(tag & 7); + } + return message; + }, + + fromJSON(object: any): WithNestedEnum { + return { + foo: isSet(object.foo) ? fooFromJSON(object.foo) : 0, + Bar: isSet(object.Bar) ? barFromJSON(object.Bar) : 0, + baz: isSet(object.baz) ? withNestedEnum_BazFromJSON(object.baz) : 0, + qux: isSet(object.qux) ? withNestedEnum_QuxFromJSON(object.qux) : 0, + }; + }, + + toJSON(message: WithNestedEnum): unknown { + const obj: any = {}; + if (message.foo !== 0) { + obj.foo = fooToJSON(message.foo); + } + if (message.Bar !== 0) { + obj.Bar = barToJSON(message.Bar); + } + if (message.baz !== 0) { + obj.baz = withNestedEnum_BazToJSON(message.baz); + } + if (message.qux !== 0) { + obj.qux = withNestedEnum_QuxToJSON(message.qux); + } + return obj; + }, + + create, I>>(base?: I): WithNestedEnum { + return WithNestedEnum.fromPartial(base ?? ({} as any)); + }, + fromPartial, I>>(object: I): WithNestedEnum { + const message = createBaseWithNestedEnum(); + message.foo = object.foo ?? 0; + message.Bar = object.Bar ?? 0; + message.baz = object.baz ?? 0; + message.qux = object.qux ?? 0; + return message; + }, +}; + declare const self: any | undefined; declare const window: any | undefined; declare const global: any | undefined; @@ -90,3 +272,18 @@ const tsProtoGlobalThis: any = (() => { } throw "Unable to locate global object"; })(); + +type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + +export type DeepPartial = T extends Builtin ? T + : T extends Array ? Array> : T extends ReadonlyArray ? ReadonlyArray> + : T extends {} ? { [K in keyof T]?: DeepPartial } + : Partial; + +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin ? P + : P & { [K in keyof P]: Exact } & { [K in Exclude>]: never }; + +function isSet(value: any): boolean { + return value !== null && value !== undefined; +} diff --git a/src/enums.ts b/src/enums.ts index 25da83511..f77a8cc75 100644 --- a/src/enums.ts +++ b/src/enums.ts @@ -31,7 +31,7 @@ export function generateEnum( enumDesc.value.forEach((valueDesc, index) => { const info = sourceInfo.lookup(Fields.enum.value, index); const valueName = getValueName(ctx, fullName, valueDesc); - const memberName = getMemberName(ctx, fullName, valueDesc); + const memberName = getMemberName(ctx, enumDesc, valueDesc); maybeAddComment(info, chunks, valueDesc.options?.deprecated, `${memberName} - `); chunks.push( code`${memberName} ${delimiter} ${options.stringEnums ? `"${valueName}"` : valueDesc.number.toString()},`, @@ -78,7 +78,7 @@ export function generateEnumFromJson(ctx: Context, fullName: string, enumDesc: E chunks.push(code`switch (object) {`); for (const valueDesc of enumDesc.value) { - const memberName = getMemberName(ctx, fullName, valueDesc); + const memberName = getMemberName(ctx, enumDesc, valueDesc); const valueName = getValueName(ctx, fullName, valueDesc); chunks.push(code` case ${valueDesc.number}: @@ -123,10 +123,10 @@ export function generateEnumToJson(ctx: Context, fullName: string, enumDesc: Enu for (const valueDesc of enumDesc.value) { if (ctx.options.useNumericEnumForJson) { - const memberName = getMemberName(ctx, fullName, valueDesc); + const memberName = getMemberName(ctx, enumDesc, valueDesc); chunks.push(code`case ${fullName}.${memberName}: return ${valueDesc.number};`); } else { - const memberName = getMemberName(ctx, fullName, valueDesc); + const memberName = getMemberName(ctx, enumDesc, valueDesc); const valueName = getValueName(ctx, fullName, valueDesc); chunks.push(code`case ${fullName}.${memberName}: return "${valueName}";`); } @@ -170,7 +170,7 @@ export function generateEnumToNumber(ctx: Context, fullName: string, enumDesc: E chunks.push(code`export function ${def(functionName)}(object: ${fullName}): number {`); chunks.push(code`switch (object) {`); for (const valueDesc of enumDesc.value) { - chunks.push(code`case ${fullName}.${getMemberName(ctx, fullName, valueDesc)}: return ${valueDesc.number};`); + chunks.push(code`case ${fullName}.${getMemberName(ctx, enumDesc, valueDesc)}: return ${valueDesc.number};`); } if (options.unrecognizedEnum) { @@ -192,9 +192,13 @@ export function generateEnumToNumber(ctx: Context, fullName: string, enumDesc: E return joinCode(chunks, { on: "\n" }); } -export function getMemberName(ctx: Context, fullName: string, valueDesc: EnumValueDescriptorProto): string { +export function getMemberName( + ctx: Context, + enumDesc: EnumDescriptorProto, + valueDesc: EnumValueDescriptorProto, +): string { if (ctx.options.removeEnumPrefix) { - return valueDesc.name.replace(`${camelToSnake(fullName)}_`, ""); + return valueDesc.name.replace(`${camelToSnake(enumDesc.name)}_`, ""); } return valueDesc.name; } diff --git a/src/types.ts b/src/types.ts index 38386360a..26c98a9cc 100644 --- a/src/types.ts +++ b/src/types.ts @@ -195,11 +195,10 @@ export function defaultValue(ctx: Context, field: FieldDescriptorProto): any { // and I believe the semantics of those in the proto2 world are generally undefined. const typeInfo = typeMap.get(field.typeName)!; const enumProto = typeInfo[2] as EnumDescriptorProto; - const enumFullName = typeInfo[1]; const zerothValue = enumProto.value.find((v) => v.number === 0) || enumProto.value[0]; if (options.stringEnums) { const enumType = messageToTypeName(ctx, field.typeName); - return code`${enumType}.${getEnumMemberName(ctx, enumFullName, zerothValue)}`; + return code`${enumType}.${getEnumMemberName(ctx, enumProto, zerothValue)}`; } else { return zerothValue.number; } @@ -269,11 +268,10 @@ export function notDefaultCheck( // and I believe the semantics of those in the proto2 world are generally undefined. const typeInfo = typeMap.get(field.typeName)!; const enumProto = typeInfo[2] as EnumDescriptorProto; - const enumFullName = typeInfo[1]; const zerothValue = enumProto.value.find((v) => v.number === 0) || enumProto.value[0]; if (options.stringEnums) { const enumType = messageToTypeName(ctx, field.typeName); - const enumValue = getEnumMemberName(ctx, enumFullName, zerothValue); + const enumValue = getEnumMemberName(ctx, enumProto, zerothValue); return code`${maybeNotUndefinedAnd} ${place} !== ${enumType}.${enumValue}`; } else { return code`${maybeNotUndefinedAnd} ${place} !== ${zerothValue.number}`;