diff --git a/integration/map-bigint-optional/test.ts b/integration/map-bigint-optional/test.ts index 19f092464..8abf1023a 100644 --- a/integration/map-bigint-optional/test.ts +++ b/integration/map-bigint-optional/test.ts @@ -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) { @@ -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; @@ -218,8 +215,8 @@ export const MapBigInt_MapEntry = { }, fromPartial, 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; }, }; diff --git a/integration/simple-long-bigint/google/protobuf/timestamp.ts b/integration/simple-long-bigint/google/protobuf/timestamp.ts index 6fbfc8858..0795c0425 100644 --- a/integration/simple-long-bigint/google/protobuf/timestamp.ts +++ b/integration/simple-long-bigint/google/protobuf/timestamp.ts @@ -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) { @@ -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) { @@ -179,7 +179,7 @@ export const Timestamp = { }, fromPartial, 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; }, diff --git a/integration/simple-long-bigint/google/protobuf/wrappers.ts b/integration/simple-long-bigint/google/protobuf/wrappers.ts index ddfc5c942..fd1578038 100644 --- a/integration/simple-long-bigint/google/protobuf/wrappers.ts +++ b/integration/simple-long-bigint/google/protobuf/wrappers.ts @@ -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; @@ -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; @@ -260,18 +260,18 @@ export const Int64Value = { }, fromPartial, 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; @@ -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; @@ -317,7 +317,7 @@ export const UInt64Value = { }, fromPartial, I>>(object: I): UInt64Value { const message = createBaseUInt64Value(); - message.value = object.value ?? BigInt("0"); + message.value = object.value ?? 0n; return message; }, }; diff --git a/integration/simple-long-bigint/simple.ts b/integration/simple-long-bigint/simple.ts index 5a3b26970..f5c592f3e 100644 --- a/integration/simple-long-bigint/simple.ts +++ b/integration/simple-long-bigint/simple.ts @@ -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: [], @@ -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) { @@ -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)) : [], @@ -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) { @@ -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) || []; diff --git a/src/types.ts b/src/types.ts index d3722e75e..18e531779 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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; } @@ -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; } @@ -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`; }