Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Use 0n for BigInt(0). #932

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions integration/map-bigint-optional/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,15 @@ export const MapBigInt = {
};

function createBaseMapBigInt_MapEntry(): MapBigInt_MapEntry {
return { key: BigInt("0"), value: BigInt("0") };
return { key: 0n, value: 0n };
}

export const MapBigInt_MapEntry = {
encode(message: MapBigInt_MapEntry, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.key !== BigInt("0")) {
if (message.key !== 0n) {
writer.uint32(9).fixed64(message.key.toString());
}
if (message.value !== BigInt("0")) {
if (message.value !== 0n) {
writer.uint32(16).int64(message.value.toString());
}
if (message._unknownFields !== undefined) {
Expand Down Expand Up @@ -196,18 +196,15 @@ export const MapBigInt_MapEntry = {
},

fromJSON(object: any): MapBigInt_MapEntry {
return {
key: isSet(object.key) ? BigInt(object.key) : BigInt("0"),
value: isSet(object.value) ? BigInt(object.value) : BigInt("0"),
};
return { key: isSet(object.key) ? BigInt(object.key) : 0n, value: isSet(object.value) ? BigInt(object.value) : 0n };
},

toJSON(message: MapBigInt_MapEntry): unknown {
const obj: any = {};
if (message.key !== BigInt("0")) {
if (message.key !== 0n) {
obj.key = message.key.toString();
}
if (message.value !== BigInt("0")) {
if (message.value !== 0n) {
obj.value = message.value.toString();
}
return obj;
Expand All @@ -218,8 +215,8 @@ export const MapBigInt_MapEntry = {
},
fromPartial<I extends Exact<DeepPartial<MapBigInt_MapEntry>, I>>(object: I): MapBigInt_MapEntry {
const message = createBaseMapBigInt_MapEntry();
message.key = object.key ?? BigInt("0");
message.value = object.value ?? BigInt("0");
message.key = object.key ?? 0n;
message.value = object.value ?? 0n;
return message;
},
};
Expand Down
10 changes: 5 additions & 5 deletions integration/simple-long-bigint/google/protobuf/timestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ export interface Timestamp {
}

function createBaseTimestamp(): Timestamp {
return { seconds: BigInt("0"), nanos: 0 };
return { seconds: 0n, nanos: 0 };
}

export const Timestamp = {
encode(message: Timestamp, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.seconds !== BigInt("0")) {
if (message.seconds !== 0n) {
writer.uint32(8).int64(message.seconds.toString());
}
if (message.nanos !== 0) {
Expand Down Expand Up @@ -158,14 +158,14 @@ export const Timestamp = {

fromJSON(object: any): Timestamp {
return {
seconds: isSet(object.seconds) ? BigInt(object.seconds) : BigInt("0"),
seconds: isSet(object.seconds) ? BigInt(object.seconds) : 0n,
nanos: isSet(object.nanos) ? Number(object.nanos) : 0,
};
},

toJSON(message: Timestamp): unknown {
const obj: any = {};
if (message.seconds !== BigInt("0")) {
if (message.seconds !== 0n) {
obj.seconds = message.seconds.toString();
}
if (message.nanos !== 0) {
Expand All @@ -179,7 +179,7 @@ export const Timestamp = {
},
fromPartial<I extends Exact<DeepPartial<Timestamp>, I>>(object: I): Timestamp {
const message = createBaseTimestamp();
message.seconds = object.seconds ?? BigInt("0");
message.seconds = object.seconds ?? 0n;
message.nanos = object.nanos ?? 0;
return message;
},
Expand Down
20 changes: 10 additions & 10 deletions integration/simple-long-bigint/google/protobuf/wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,12 @@ export const FloatValue = {
};

function createBaseInt64Value(): Int64Value {
return { value: BigInt("0") };
return { value: 0n };
}

export const Int64Value = {
encode(message: Int64Value, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.value !== BigInt("0")) {
if (message.value !== 0n) {
writer.uint32(8).int64(message.value.toString());
}
return writer;
Expand Down Expand Up @@ -244,12 +244,12 @@ export const Int64Value = {
},

fromJSON(object: any): Int64Value {
return { value: isSet(object.value) ? BigInt(object.value) : BigInt("0") };
return { value: isSet(object.value) ? BigInt(object.value) : 0n };
},

toJSON(message: Int64Value): unknown {
const obj: any = {};
if (message.value !== BigInt("0")) {
if (message.value !== 0n) {
obj.value = message.value.toString();
}
return obj;
Expand All @@ -260,18 +260,18 @@ export const Int64Value = {
},
fromPartial<I extends Exact<DeepPartial<Int64Value>, I>>(object: I): Int64Value {
const message = createBaseInt64Value();
message.value = object.value ?? BigInt("0");
message.value = object.value ?? 0n;
return message;
},
};

function createBaseUInt64Value(): UInt64Value {
return { value: BigInt("0") };
return { value: 0n };
}

export const UInt64Value = {
encode(message: UInt64Value, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.value !== BigInt("0")) {
if (message.value !== 0n) {
writer.uint32(8).uint64(message.value.toString());
}
return writer;
Expand Down Expand Up @@ -301,12 +301,12 @@ export const UInt64Value = {
},

fromJSON(object: any): UInt64Value {
return { value: isSet(object.value) ? BigInt(object.value) : BigInt("0") };
return { value: isSet(object.value) ? BigInt(object.value) : 0n };
},

toJSON(message: UInt64Value): unknown {
const obj: any = {};
if (message.value !== BigInt("0")) {
if (message.value !== 0n) {
obj.value = message.value.toString();
}
return obj;
Expand All @@ -317,7 +317,7 @@ export const UInt64Value = {
},
fromPartial<I extends Exact<DeepPartial<UInt64Value>, I>>(object: I): UInt64Value {
const message = createBaseUInt64Value();
message.value = object.value ?? BigInt("0");
message.value = object.value ?? 0n;
return message;
},
};
Expand Down
50 changes: 25 additions & 25 deletions integration/simple-long-bigint/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ function createBaseNumbers(): Numbers {
double: 0,
float: 0,
int32: 0,
int64: BigInt("0"),
int64: 0n,
uint32: 0,
uint64: BigInt("0"),
uint64: 0n,
sint32: 0,
sint64: BigInt("0"),
sint64: 0n,
fixed32: 0,
fixed64: BigInt("0"),
fixed64: 0n,
sfixed32: 0,
sfixed64: BigInt("0"),
sfixed64: 0n,
guint64: undefined,
timestamp: undefined,
uint64s: [],
Expand All @@ -55,31 +55,31 @@ export const Numbers = {
if (message.int32 !== 0) {
writer.uint32(24).int32(message.int32);
}
if (message.int64 !== BigInt("0")) {
if (message.int64 !== 0n) {
writer.uint32(32).int64(message.int64.toString());
}
if (message.uint32 !== 0) {
writer.uint32(40).uint32(message.uint32);
}
if (message.uint64 !== BigInt("0")) {
if (message.uint64 !== 0n) {
writer.uint32(48).uint64(message.uint64.toString());
}
if (message.sint32 !== 0) {
writer.uint32(56).sint32(message.sint32);
}
if (message.sint64 !== BigInt("0")) {
if (message.sint64 !== 0n) {
writer.uint32(64).sint64(message.sint64.toString());
}
if (message.fixed32 !== 0) {
writer.uint32(77).fixed32(message.fixed32);
}
if (message.fixed64 !== BigInt("0")) {
if (message.fixed64 !== 0n) {
writer.uint32(81).fixed64(message.fixed64.toString());
}
if (message.sfixed32 !== 0) {
writer.uint32(93).sfixed32(message.sfixed32);
}
if (message.sfixed64 !== BigInt("0")) {
if (message.sfixed64 !== 0n) {
writer.uint32(97).sfixed64(message.sfixed64.toString());
}
if (message.guint64 !== undefined) {
Expand Down Expand Up @@ -232,15 +232,15 @@ export const Numbers = {
double: isSet(object.double) ? Number(object.double) : 0,
float: isSet(object.float) ? Number(object.float) : 0,
int32: isSet(object.int32) ? Number(object.int32) : 0,
int64: isSet(object.int64) ? BigInt(object.int64) : BigInt("0"),
int64: isSet(object.int64) ? BigInt(object.int64) : 0n,
uint32: isSet(object.uint32) ? Number(object.uint32) : 0,
uint64: isSet(object.uint64) ? BigInt(object.uint64) : BigInt("0"),
uint64: isSet(object.uint64) ? BigInt(object.uint64) : 0n,
sint32: isSet(object.sint32) ? Number(object.sint32) : 0,
sint64: isSet(object.sint64) ? BigInt(object.sint64) : BigInt("0"),
sint64: isSet(object.sint64) ? BigInt(object.sint64) : 0n,
fixed32: isSet(object.fixed32) ? Number(object.fixed32) : 0,
fixed64: isSet(object.fixed64) ? BigInt(object.fixed64) : BigInt("0"),
fixed64: isSet(object.fixed64) ? BigInt(object.fixed64) : 0n,
sfixed32: isSet(object.sfixed32) ? Number(object.sfixed32) : 0,
sfixed64: isSet(object.sfixed64) ? BigInt(object.sfixed64) : BigInt("0"),
sfixed64: isSet(object.sfixed64) ? BigInt(object.sfixed64) : 0n,
guint64: isSet(object.guint64) ? BigInt(object.guint64) : undefined,
timestamp: isSet(object.timestamp) ? fromJsonTimestamp(object.timestamp) : undefined,
uint64s: globalThis.Array.isArray(object?.uint64s) ? object.uint64s.map((e: any) => BigInt(e)) : [],
Expand All @@ -258,31 +258,31 @@ export const Numbers = {
if (message.int32 !== 0) {
obj.int32 = Math.round(message.int32);
}
if (message.int64 !== BigInt("0")) {
if (message.int64 !== 0n) {
obj.int64 = message.int64.toString();
}
if (message.uint32 !== 0) {
obj.uint32 = Math.round(message.uint32);
}
if (message.uint64 !== BigInt("0")) {
if (message.uint64 !== 0n) {
obj.uint64 = message.uint64.toString();
}
if (message.sint32 !== 0) {
obj.sint32 = Math.round(message.sint32);
}
if (message.sint64 !== BigInt("0")) {
if (message.sint64 !== 0n) {
obj.sint64 = message.sint64.toString();
}
if (message.fixed32 !== 0) {
obj.fixed32 = Math.round(message.fixed32);
}
if (message.fixed64 !== BigInt("0")) {
if (message.fixed64 !== 0n) {
obj.fixed64 = message.fixed64.toString();
}
if (message.sfixed32 !== 0) {
obj.sfixed32 = Math.round(message.sfixed32);
}
if (message.sfixed64 !== BigInt("0")) {
if (message.sfixed64 !== 0n) {
obj.sfixed64 = message.sfixed64.toString();
}
if (message.guint64 !== undefined) {
Expand All @@ -305,15 +305,15 @@ export const Numbers = {
message.double = object.double ?? 0;
message.float = object.float ?? 0;
message.int32 = object.int32 ?? 0;
message.int64 = object.int64 ?? BigInt("0");
message.int64 = object.int64 ?? 0n;
message.uint32 = object.uint32 ?? 0;
message.uint64 = object.uint64 ?? BigInt("0");
message.uint64 = object.uint64 ?? 0n;
message.sint32 = object.sint32 ?? 0;
message.sint64 = object.sint64 ?? BigInt("0");
message.sint64 = object.sint64 ?? 0n;
message.fixed32 = object.fixed32 ?? 0;
message.fixed64 = object.fixed64 ?? BigInt("0");
message.fixed64 = object.fixed64 ?? 0n;
message.sfixed32 = object.sfixed32 ?? 0;
message.sfixed64 = object.sfixed64 ?? BigInt("0");
message.sfixed64 = object.sfixed64 ?? 0n;
message.guint64 = object.guint64 ?? undefined;
message.timestamp = object.timestamp ?? undefined;
message.uint64s = object.uint64s?.map((e) => e) || [];
Expand Down
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ export function defaultValue(ctx: Context, field: FieldDescriptorProto): any {
} else if (options.forceLong === LongOption.STRING) {
return '"0"';
} else if (options.forceLong === LongOption.BIGINT) {
return 'BigInt("0")';
return '0n';
} else {
return 0;
}
Expand All @@ -221,7 +221,7 @@ export function defaultValue(ctx: Context, field: FieldDescriptorProto): any {
} else if (options.forceLong === LongOption.STRING) {
return '"0"';
} else if (options.forceLong === LongOption.BIGINT) {
return 'BigInt("0")';
return '0n';
} else {
return 0;
}
Expand Down Expand Up @@ -286,7 +286,7 @@ export function notDefaultCheck(
} else if (options.forceLong === LongOption.STRING) {
return code`${maybeNotUndefinedAnd} ${place} !== "0"`;
} else if (options.forceLong === LongOption.BIGINT) {
return code`${maybeNotUndefinedAnd} ${place} !== BigInt("0")`;
return code`${maybeNotUndefinedAnd} ${place} !== 0n`;
} else {
return code`${maybeNotUndefinedAnd} ${place} !== 0`;
}
Expand Down