Skip to content

Commit efdbf47

Browse files
authoredAug 15, 2023
fix: remove-enum-prefix for nested enums (#903)
1 parent 54661eb commit efdbf47

10 files changed

+488
-43
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,46 @@
1-
import { EnumFields, Foo, Bar } from "./remove-enum-prefix-string-enums";
1+
import {
2+
Foo,
3+
Bar,
4+
fooFromJSON,
5+
fooToJSON,
6+
barFromJSON,
7+
barToJSON,
8+
WithNestedEnum_Baz,
9+
WithNestedEnum_Qux,
10+
withNestedEnum_BazFromJSON,
11+
withNestedEnum_BazToJSON,
12+
withNestedEnum_QuxFromJSON,
13+
withNestedEnum_QuxToJSON,
14+
} from "./remove-enum-prefix-string-enums";
215

3-
describe("nestjs-metadata-test", () => {
4-
it("compiles", () => {
5-
const msg: EnumFields = {
6-
foo: Foo.BAR,
7-
bar: Bar.BAZ,
8-
};
9-
const out = EnumFields.toJSON(msg);
10-
expect(out).not.toBeUndefined();
16+
function testEnumFromJSONAndToJSON<ENUM>(
17+
fromJSON: (s: string) => ENUM,
18+
toJSON: (e: ENUM) => string,
19+
valueMap: Record<string, ENUM>,
20+
) {
21+
for (const [jsonValue, enumValue] of Object.entries(valueMap)) {
22+
expect(fromJSON(jsonValue)).toBe(enumValue);
23+
expect(toJSON(enumValue)).toBe(jsonValue);
24+
}
25+
}
26+
27+
describe("remove-enum-prefix-string-enums", () => {
28+
it("encode and decode correctly", () => {
29+
testEnumFromJSONAndToJSON(fooFromJSON, fooToJSON, {
30+
FOO_UNSPECIFIED: Foo.UNSPECIFIED,
31+
FOO_BAR: Foo.BAR,
32+
});
33+
testEnumFromJSONAndToJSON(barFromJSON, barToJSON, {
34+
BAR_UNSPECIFIED: Bar.UNSPECIFIED,
35+
BAZ: Bar.BAZ,
36+
});
37+
testEnumFromJSONAndToJSON(withNestedEnum_BazFromJSON, withNestedEnum_BazToJSON, {
38+
BAZ_UNSPECIFIED: WithNestedEnum_Baz.UNSPECIFIED,
39+
BAZ_ONE: WithNestedEnum_Baz.ONE,
40+
});
41+
testEnumFromJSONAndToJSON(withNestedEnum_QuxFromJSON, withNestedEnum_QuxToJSON, {
42+
QUX_UNSPECIFIED: WithNestedEnum_Qux.UNSPECIFIED,
43+
ONE: WithNestedEnum_Qux.ONE,
44+
});
1145
});
1246
});
Binary file not shown.

‎integration/remove-enum-prefix-string-enums/remove-enum-prefix-string-enums.proto

+16-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,21 @@ enum Bar {
1212
QUX = 2;
1313
}
1414

15-
message EnumFields {
15+
message WithNestedEnum {
16+
enum Baz {
17+
BAZ_UNSPECIFIED = 0;
18+
BAZ_ONE = 1;
19+
BAZ_TWO = 2;
20+
}
21+
22+
enum Qux {
23+
QUX_UNSPECIFIED = 0;
24+
ONE = 1;
25+
TWO = 2;
26+
}
27+
1628
Foo foo = 1;
17-
Bar bar = 2;
29+
Bar Bar = 2;
30+
Baz baz = 3;
31+
Qux qux = 4;
1832
}

‎integration/remove-enum-prefix-string-enums/remove-enum-prefix-string-enums.ts

+154-21
Original file line numberDiff line numberDiff line change
@@ -99,30 +99,139 @@ export function barToNumber(object: Bar): number {
9999
}
100100
}
101101

102-
export interface EnumFields {
102+
export interface WithNestedEnum {
103103
foo: Foo;
104-
bar: Bar;
104+
Bar: Bar;
105+
baz: WithNestedEnum_Baz;
106+
qux: WithNestedEnum_Qux;
105107
}
106108

107-
function createBaseEnumFields(): EnumFields {
108-
return { foo: Foo.UNSPECIFIED, bar: Bar.UNSPECIFIED };
109+
export enum WithNestedEnum_Baz {
110+
UNSPECIFIED = "BAZ_UNSPECIFIED",
111+
ONE = "BAZ_ONE",
112+
TWO = "BAZ_TWO",
109113
}
110114

111-
export const EnumFields = {
112-
encode(message: EnumFields, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
115+
export function withNestedEnum_BazFromJSON(object: any): WithNestedEnum_Baz {
116+
switch (object) {
117+
case 0:
118+
case "BAZ_UNSPECIFIED":
119+
return WithNestedEnum_Baz.UNSPECIFIED;
120+
case 1:
121+
case "BAZ_ONE":
122+
return WithNestedEnum_Baz.ONE;
123+
case 2:
124+
case "BAZ_TWO":
125+
return WithNestedEnum_Baz.TWO;
126+
default:
127+
throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum WithNestedEnum_Baz");
128+
}
129+
}
130+
131+
export function withNestedEnum_BazToJSON(object: WithNestedEnum_Baz): string {
132+
switch (object) {
133+
case WithNestedEnum_Baz.UNSPECIFIED:
134+
return "BAZ_UNSPECIFIED";
135+
case WithNestedEnum_Baz.ONE:
136+
return "BAZ_ONE";
137+
case WithNestedEnum_Baz.TWO:
138+
return "BAZ_TWO";
139+
default:
140+
throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum WithNestedEnum_Baz");
141+
}
142+
}
143+
144+
export function withNestedEnum_BazToNumber(object: WithNestedEnum_Baz): number {
145+
switch (object) {
146+
case WithNestedEnum_Baz.UNSPECIFIED:
147+
return 0;
148+
case WithNestedEnum_Baz.ONE:
149+
return 1;
150+
case WithNestedEnum_Baz.TWO:
151+
return 2;
152+
default:
153+
throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum WithNestedEnum_Baz");
154+
}
155+
}
156+
157+
export enum WithNestedEnum_Qux {
158+
UNSPECIFIED = "QUX_UNSPECIFIED",
159+
ONE = "ONE",
160+
TWO = "TWO",
161+
}
162+
163+
export function withNestedEnum_QuxFromJSON(object: any): WithNestedEnum_Qux {
164+
switch (object) {
165+
case 0:
166+
case "QUX_UNSPECIFIED":
167+
return WithNestedEnum_Qux.UNSPECIFIED;
168+
case 1:
169+
case "ONE":
170+
return WithNestedEnum_Qux.ONE;
171+
case 2:
172+
case "TWO":
173+
return WithNestedEnum_Qux.TWO;
174+
default:
175+
throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum WithNestedEnum_Qux");
176+
}
177+
}
178+
179+
export function withNestedEnum_QuxToJSON(object: WithNestedEnum_Qux): string {
180+
switch (object) {
181+
case WithNestedEnum_Qux.UNSPECIFIED:
182+
return "QUX_UNSPECIFIED";
183+
case WithNestedEnum_Qux.ONE:
184+
return "ONE";
185+
case WithNestedEnum_Qux.TWO:
186+
return "TWO";
187+
default:
188+
throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum WithNestedEnum_Qux");
189+
}
190+
}
191+
192+
export function withNestedEnum_QuxToNumber(object: WithNestedEnum_Qux): number {
193+
switch (object) {
194+
case WithNestedEnum_Qux.UNSPECIFIED:
195+
return 0;
196+
case WithNestedEnum_Qux.ONE:
197+
return 1;
198+
case WithNestedEnum_Qux.TWO:
199+
return 2;
200+
default:
201+
throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum WithNestedEnum_Qux");
202+
}
203+
}
204+
205+
function createBaseWithNestedEnum(): WithNestedEnum {
206+
return {
207+
foo: Foo.UNSPECIFIED,
208+
Bar: Bar.UNSPECIFIED,
209+
baz: WithNestedEnum_Baz.UNSPECIFIED,
210+
qux: WithNestedEnum_Qux.UNSPECIFIED,
211+
};
212+
}
213+
214+
export const WithNestedEnum = {
215+
encode(message: WithNestedEnum, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
113216
if (message.foo !== Foo.UNSPECIFIED) {
114217
writer.uint32(8).int32(fooToNumber(message.foo));
115218
}
116-
if (message.bar !== Bar.UNSPECIFIED) {
117-
writer.uint32(16).int32(barToNumber(message.bar));
219+
if (message.Bar !== Bar.UNSPECIFIED) {
220+
writer.uint32(16).int32(barToNumber(message.Bar));
221+
}
222+
if (message.baz !== WithNestedEnum_Baz.UNSPECIFIED) {
223+
writer.uint32(24).int32(withNestedEnum_BazToNumber(message.baz));
224+
}
225+
if (message.qux !== WithNestedEnum_Qux.UNSPECIFIED) {
226+
writer.uint32(32).int32(withNestedEnum_QuxToNumber(message.qux));
118227
}
119228
return writer;
120229
},
121230

122-
decode(input: _m0.Reader | Uint8Array, length?: number): EnumFields {
231+
decode(input: _m0.Reader | Uint8Array, length?: number): WithNestedEnum {
123232
const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
124233
let end = length === undefined ? reader.len : reader.pos + length;
125-
const message = createBaseEnumFields();
234+
const message = createBaseWithNestedEnum();
126235
while (reader.pos < end) {
127236
const tag = reader.uint32();
128237
switch (tag >>> 3) {
@@ -138,7 +247,21 @@ export const EnumFields = {
138247
break;
139248
}
140249

141-
message.bar = barFromJSON(reader.int32());
250+
message.Bar = barFromJSON(reader.int32());
251+
continue;
252+
case 3:
253+
if (tag !== 24) {
254+
break;
255+
}
256+
257+
message.baz = withNestedEnum_BazFromJSON(reader.int32());
258+
continue;
259+
case 4:
260+
if (tag !== 32) {
261+
break;
262+
}
263+
264+
message.qux = withNestedEnum_QuxFromJSON(reader.int32());
142265
continue;
143266
}
144267
if ((tag & 7) === 4 || tag === 0) {
@@ -149,31 +272,41 @@ export const EnumFields = {
149272
return message;
150273
},
151274

152-
fromJSON(object: any): EnumFields {
275+
fromJSON(object: any): WithNestedEnum {
153276
return {
154277
foo: isSet(object.foo) ? fooFromJSON(object.foo) : Foo.UNSPECIFIED,
155-
bar: isSet(object.bar) ? barFromJSON(object.bar) : Bar.UNSPECIFIED,
278+
Bar: isSet(object.Bar) ? barFromJSON(object.Bar) : Bar.UNSPECIFIED,
279+
baz: isSet(object.baz) ? withNestedEnum_BazFromJSON(object.baz) : WithNestedEnum_Baz.UNSPECIFIED,
280+
qux: isSet(object.qux) ? withNestedEnum_QuxFromJSON(object.qux) : WithNestedEnum_Qux.UNSPECIFIED,
156281
};
157282
},
158283

159-
toJSON(message: EnumFields): unknown {
284+
toJSON(message: WithNestedEnum): unknown {
160285
const obj: any = {};
161286
if (message.foo !== Foo.UNSPECIFIED) {
162287
obj.foo = fooToJSON(message.foo);
163288
}
164-
if (message.bar !== Bar.UNSPECIFIED) {
165-
obj.bar = barToJSON(message.bar);
289+
if (message.Bar !== Bar.UNSPECIFIED) {
290+
obj.Bar = barToJSON(message.Bar);
291+
}
292+
if (message.baz !== WithNestedEnum_Baz.UNSPECIFIED) {
293+
obj.baz = withNestedEnum_BazToJSON(message.baz);
294+
}
295+
if (message.qux !== WithNestedEnum_Qux.UNSPECIFIED) {
296+
obj.qux = withNestedEnum_QuxToJSON(message.qux);
166297
}
167298
return obj;
168299
},
169300

170-
create<I extends Exact<DeepPartial<EnumFields>, I>>(base?: I): EnumFields {
171-
return EnumFields.fromPartial(base ?? ({} as any));
301+
create<I extends Exact<DeepPartial<WithNestedEnum>, I>>(base?: I): WithNestedEnum {
302+
return WithNestedEnum.fromPartial(base ?? ({} as any));
172303
},
173-
fromPartial<I extends Exact<DeepPartial<EnumFields>, I>>(object: I): EnumFields {
174-
const message = createBaseEnumFields();
304+
fromPartial<I extends Exact<DeepPartial<WithNestedEnum>, I>>(object: I): WithNestedEnum {
305+
const message = createBaseWithNestedEnum();
175306
message.foo = object.foo ?? Foo.UNSPECIFIED;
176-
message.bar = object.bar ?? Bar.UNSPECIFIED;
307+
message.Bar = object.Bar ?? Bar.UNSPECIFIED;
308+
message.baz = object.baz ?? WithNestedEnum_Baz.UNSPECIFIED;
309+
message.qux = object.qux ?? WithNestedEnum_Qux.UNSPECIFIED;
177310
return message;
178311
},
179312
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import {
2+
Foo,
3+
Bar,
4+
fooFromJSON,
5+
fooToJSON,
6+
barFromJSON,
7+
barToJSON,
8+
withNestedEnum_BazFromJSON,
9+
WithNestedEnum_Baz,
10+
withNestedEnum_BazToJSON,
11+
withNestedEnum_QuxFromJSON,
12+
withNestedEnum_QuxToJSON,
13+
WithNestedEnum_Qux,
14+
} from "./remove-enum-prefix";
15+
16+
function testEnumFromJSONAndToJSON<ENUM>(
17+
fromJSON: (s: string) => ENUM,
18+
toJSON: (e: ENUM) => string,
19+
valueMap: Record<string, ENUM>,
20+
) {
21+
for (const [jsonValue, enumValue] of Object.entries(valueMap)) {
22+
expect(fromJSON(jsonValue)).toBe(enumValue);
23+
expect(toJSON(enumValue)).toBe(jsonValue);
24+
}
25+
}
26+
27+
describe("remove-enum-prefix", () => {
28+
it("encode and decode correctly", () => {
29+
testEnumFromJSONAndToJSON(fooFromJSON, fooToJSON, {
30+
FOO_UNSPECIFIED: Foo.UNSPECIFIED,
31+
FOO_BAR: Foo.BAR,
32+
});
33+
testEnumFromJSONAndToJSON(barFromJSON, barToJSON, {
34+
BAR_UNSPECIFIED: Bar.UNSPECIFIED,
35+
BAZ: Bar.BAZ,
36+
});
37+
testEnumFromJSONAndToJSON(withNestedEnum_BazFromJSON, withNestedEnum_BazToJSON, {
38+
BAZ_UNSPECIFIED: WithNestedEnum_Baz.UNSPECIFIED,
39+
BAZ_ONE: WithNestedEnum_Baz.ONE,
40+
});
41+
testEnumFromJSONAndToJSON(withNestedEnum_QuxFromJSON, withNestedEnum_QuxToJSON, {
42+
QUX_UNSPECIFIED: WithNestedEnum_Qux.UNSPECIFIED,
43+
ONE: WithNestedEnum_Qux.ONE,
44+
});
45+
});
46+
});
Binary file not shown.

‎integration/remove-enum-prefix/remove-enum-prefix.proto

+19
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,22 @@ enum Bar {
1111
BAZ = 1;
1212
QUX = 2;
1313
}
14+
15+
message WithNestedEnum {
16+
enum Baz {
17+
BAZ_UNSPECIFIED = 0;
18+
BAZ_ONE = 1;
19+
BAZ_TWO = 2;
20+
}
21+
22+
enum Qux {
23+
QUX_UNSPECIFIED = 0;
24+
ONE = 1;
25+
TWO = 2;
26+
}
27+
28+
Foo foo = 1;
29+
Bar Bar = 2;
30+
Baz baz = 3;
31+
Qux qux = 4;
32+
}

‎integration/remove-enum-prefix/remove-enum-prefix.ts

+197
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* eslint-disable */
2+
import * as _m0 from "protobufjs/minimal";
23

34
export const protobufPackage = "";
45

@@ -72,6 +73,187 @@ export function barToJSON(object: Bar): string {
7273
}
7374
}
7475

76+
export interface WithNestedEnum {
77+
foo: Foo;
78+
Bar: Bar;
79+
baz: WithNestedEnum_Baz;
80+
qux: WithNestedEnum_Qux;
81+
}
82+
83+
export enum WithNestedEnum_Baz {
84+
UNSPECIFIED = 0,
85+
ONE = 1,
86+
TWO = 2,
87+
}
88+
89+
export function withNestedEnum_BazFromJSON(object: any): WithNestedEnum_Baz {
90+
switch (object) {
91+
case 0:
92+
case "BAZ_UNSPECIFIED":
93+
return WithNestedEnum_Baz.UNSPECIFIED;
94+
case 1:
95+
case "BAZ_ONE":
96+
return WithNestedEnum_Baz.ONE;
97+
case 2:
98+
case "BAZ_TWO":
99+
return WithNestedEnum_Baz.TWO;
100+
default:
101+
throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum WithNestedEnum_Baz");
102+
}
103+
}
104+
105+
export function withNestedEnum_BazToJSON(object: WithNestedEnum_Baz): string {
106+
switch (object) {
107+
case WithNestedEnum_Baz.UNSPECIFIED:
108+
return "BAZ_UNSPECIFIED";
109+
case WithNestedEnum_Baz.ONE:
110+
return "BAZ_ONE";
111+
case WithNestedEnum_Baz.TWO:
112+
return "BAZ_TWO";
113+
default:
114+
throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum WithNestedEnum_Baz");
115+
}
116+
}
117+
118+
export enum WithNestedEnum_Qux {
119+
UNSPECIFIED = 0,
120+
ONE = 1,
121+
TWO = 2,
122+
}
123+
124+
export function withNestedEnum_QuxFromJSON(object: any): WithNestedEnum_Qux {
125+
switch (object) {
126+
case 0:
127+
case "QUX_UNSPECIFIED":
128+
return WithNestedEnum_Qux.UNSPECIFIED;
129+
case 1:
130+
case "ONE":
131+
return WithNestedEnum_Qux.ONE;
132+
case 2:
133+
case "TWO":
134+
return WithNestedEnum_Qux.TWO;
135+
default:
136+
throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum WithNestedEnum_Qux");
137+
}
138+
}
139+
140+
export function withNestedEnum_QuxToJSON(object: WithNestedEnum_Qux): string {
141+
switch (object) {
142+
case WithNestedEnum_Qux.UNSPECIFIED:
143+
return "QUX_UNSPECIFIED";
144+
case WithNestedEnum_Qux.ONE:
145+
return "ONE";
146+
case WithNestedEnum_Qux.TWO:
147+
return "TWO";
148+
default:
149+
throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum WithNestedEnum_Qux");
150+
}
151+
}
152+
153+
function createBaseWithNestedEnum(): WithNestedEnum {
154+
return { foo: 0, Bar: 0, baz: 0, qux: 0 };
155+
}
156+
157+
export const WithNestedEnum = {
158+
encode(message: WithNestedEnum, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
159+
if (message.foo !== 0) {
160+
writer.uint32(8).int32(message.foo);
161+
}
162+
if (message.Bar !== 0) {
163+
writer.uint32(16).int32(message.Bar);
164+
}
165+
if (message.baz !== 0) {
166+
writer.uint32(24).int32(message.baz);
167+
}
168+
if (message.qux !== 0) {
169+
writer.uint32(32).int32(message.qux);
170+
}
171+
return writer;
172+
},
173+
174+
decode(input: _m0.Reader | Uint8Array, length?: number): WithNestedEnum {
175+
const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
176+
let end = length === undefined ? reader.len : reader.pos + length;
177+
const message = createBaseWithNestedEnum();
178+
while (reader.pos < end) {
179+
const tag = reader.uint32();
180+
switch (tag >>> 3) {
181+
case 1:
182+
if (tag !== 8) {
183+
break;
184+
}
185+
186+
message.foo = reader.int32() as any;
187+
continue;
188+
case 2:
189+
if (tag !== 16) {
190+
break;
191+
}
192+
193+
message.Bar = reader.int32() as any;
194+
continue;
195+
case 3:
196+
if (tag !== 24) {
197+
break;
198+
}
199+
200+
message.baz = reader.int32() as any;
201+
continue;
202+
case 4:
203+
if (tag !== 32) {
204+
break;
205+
}
206+
207+
message.qux = reader.int32() as any;
208+
continue;
209+
}
210+
if ((tag & 7) === 4 || tag === 0) {
211+
break;
212+
}
213+
reader.skipType(tag & 7);
214+
}
215+
return message;
216+
},
217+
218+
fromJSON(object: any): WithNestedEnum {
219+
return {
220+
foo: isSet(object.foo) ? fooFromJSON(object.foo) : 0,
221+
Bar: isSet(object.Bar) ? barFromJSON(object.Bar) : 0,
222+
baz: isSet(object.baz) ? withNestedEnum_BazFromJSON(object.baz) : 0,
223+
qux: isSet(object.qux) ? withNestedEnum_QuxFromJSON(object.qux) : 0,
224+
};
225+
},
226+
227+
toJSON(message: WithNestedEnum): unknown {
228+
const obj: any = {};
229+
if (message.foo !== 0) {
230+
obj.foo = fooToJSON(message.foo);
231+
}
232+
if (message.Bar !== 0) {
233+
obj.Bar = barToJSON(message.Bar);
234+
}
235+
if (message.baz !== 0) {
236+
obj.baz = withNestedEnum_BazToJSON(message.baz);
237+
}
238+
if (message.qux !== 0) {
239+
obj.qux = withNestedEnum_QuxToJSON(message.qux);
240+
}
241+
return obj;
242+
},
243+
244+
create<I extends Exact<DeepPartial<WithNestedEnum>, I>>(base?: I): WithNestedEnum {
245+
return WithNestedEnum.fromPartial(base ?? ({} as any));
246+
},
247+
fromPartial<I extends Exact<DeepPartial<WithNestedEnum>, I>>(object: I): WithNestedEnum {
248+
const message = createBaseWithNestedEnum();
249+
message.foo = object.foo ?? 0;
250+
message.Bar = object.Bar ?? 0;
251+
message.baz = object.baz ?? 0;
252+
message.qux = object.qux ?? 0;
253+
return message;
254+
},
255+
};
256+
75257
declare const self: any | undefined;
76258
declare const window: any | undefined;
77259
declare const global: any | undefined;
@@ -90,3 +272,18 @@ const tsProtoGlobalThis: any = (() => {
90272
}
91273
throw "Unable to locate global object";
92274
})();
275+
276+
type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;
277+
278+
export type DeepPartial<T> = T extends Builtin ? T
279+
: T extends Array<infer U> ? Array<DeepPartial<U>> : T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>>
280+
: T extends {} ? { [K in keyof T]?: DeepPartial<T[K]> }
281+
: Partial<T>;
282+
283+
type KeysOfUnion<T> = T extends T ? keyof T : never;
284+
export type Exact<P, I extends P> = P extends Builtin ? P
285+
: P & { [K in keyof P]: Exact<P[K], I[K]> } & { [K in Exclude<keyof I, KeysOfUnion<P>>]: never };
286+
287+
function isSet(value: any): boolean {
288+
return value !== null && value !== undefined;
289+
}

‎src/enums.ts

+11-7
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export function generateEnum(
3131
enumDesc.value.forEach((valueDesc, index) => {
3232
const info = sourceInfo.lookup(Fields.enum.value, index);
3333
const valueName = getValueName(ctx, fullName, valueDesc);
34-
const memberName = getMemberName(ctx, fullName, valueDesc);
34+
const memberName = getMemberName(ctx, enumDesc, valueDesc);
3535
maybeAddComment(info, chunks, valueDesc.options?.deprecated, `${memberName} - `);
3636
chunks.push(
3737
code`${memberName} ${delimiter} ${options.stringEnums ? `"${valueName}"` : valueDesc.number.toString()},`,
@@ -78,7 +78,7 @@ export function generateEnumFromJson(ctx: Context, fullName: string, enumDesc: E
7878
chunks.push(code`switch (object) {`);
7979

8080
for (const valueDesc of enumDesc.value) {
81-
const memberName = getMemberName(ctx, fullName, valueDesc);
81+
const memberName = getMemberName(ctx, enumDesc, valueDesc);
8282
const valueName = getValueName(ctx, fullName, valueDesc);
8383
chunks.push(code`
8484
case ${valueDesc.number}:
@@ -123,10 +123,10 @@ export function generateEnumToJson(ctx: Context, fullName: string, enumDesc: Enu
123123

124124
for (const valueDesc of enumDesc.value) {
125125
if (ctx.options.useNumericEnumForJson) {
126-
const memberName = getMemberName(ctx, fullName, valueDesc);
126+
const memberName = getMemberName(ctx, enumDesc, valueDesc);
127127
chunks.push(code`case ${fullName}.${memberName}: return ${valueDesc.number};`);
128128
} else {
129-
const memberName = getMemberName(ctx, fullName, valueDesc);
129+
const memberName = getMemberName(ctx, enumDesc, valueDesc);
130130
const valueName = getValueName(ctx, fullName, valueDesc);
131131
chunks.push(code`case ${fullName}.${memberName}: return "${valueName}";`);
132132
}
@@ -170,7 +170,7 @@ export function generateEnumToNumber(ctx: Context, fullName: string, enumDesc: E
170170
chunks.push(code`export function ${def(functionName)}(object: ${fullName}): number {`);
171171
chunks.push(code`switch (object) {`);
172172
for (const valueDesc of enumDesc.value) {
173-
chunks.push(code`case ${fullName}.${getMemberName(ctx, fullName, valueDesc)}: return ${valueDesc.number};`);
173+
chunks.push(code`case ${fullName}.${getMemberName(ctx, enumDesc, valueDesc)}: return ${valueDesc.number};`);
174174
}
175175

176176
if (options.unrecognizedEnum) {
@@ -192,9 +192,13 @@ export function generateEnumToNumber(ctx: Context, fullName: string, enumDesc: E
192192
return joinCode(chunks, { on: "\n" });
193193
}
194194

195-
export function getMemberName(ctx: Context, fullName: string, valueDesc: EnumValueDescriptorProto): string {
195+
export function getMemberName(
196+
ctx: Context,
197+
enumDesc: EnumDescriptorProto,
198+
valueDesc: EnumValueDescriptorProto,
199+
): string {
196200
if (ctx.options.removeEnumPrefix) {
197-
return valueDesc.name.replace(`${camelToSnake(fullName)}_`, "");
201+
return valueDesc.name.replace(`${camelToSnake(enumDesc.name)}_`, "");
198202
}
199203
return valueDesc.name;
200204
}

‎src/types.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,10 @@ export function defaultValue(ctx: Context, field: FieldDescriptorProto): any {
195195
// and I believe the semantics of those in the proto2 world are generally undefined.
196196
const typeInfo = typeMap.get(field.typeName)!;
197197
const enumProto = typeInfo[2] as EnumDescriptorProto;
198-
const enumFullName = typeInfo[1];
199198
const zerothValue = enumProto.value.find((v) => v.number === 0) || enumProto.value[0];
200199
if (options.stringEnums) {
201200
const enumType = messageToTypeName(ctx, field.typeName);
202-
return code`${enumType}.${getEnumMemberName(ctx, enumFullName, zerothValue)}`;
201+
return code`${enumType}.${getEnumMemberName(ctx, enumProto, zerothValue)}`;
203202
} else {
204203
return zerothValue.number;
205204
}
@@ -269,11 +268,10 @@ export function notDefaultCheck(
269268
// and I believe the semantics of those in the proto2 world are generally undefined.
270269
const typeInfo = typeMap.get(field.typeName)!;
271270
const enumProto = typeInfo[2] as EnumDescriptorProto;
272-
const enumFullName = typeInfo[1];
273271
const zerothValue = enumProto.value.find((v) => v.number === 0) || enumProto.value[0];
274272
if (options.stringEnums) {
275273
const enumType = messageToTypeName(ctx, field.typeName);
276-
const enumValue = getEnumMemberName(ctx, enumFullName, zerothValue);
274+
const enumValue = getEnumMemberName(ctx, enumProto, zerothValue);
277275
return code`${maybeNotUndefinedAnd} ${place} !== ${enumType}.${enumValue}`;
278276
} else {
279277
return code`${maybeNotUndefinedAnd} ${place} !== ${zerothValue.number}`;

0 commit comments

Comments
 (0)
Please sign in to comment.