diff --git a/integration/angular/simple-message.ts b/integration/angular/simple-message.ts index d0d91053c..7d17c402f 100644 --- a/integration/angular/simple-message.ts +++ b/integration/angular/simple-message.ts @@ -54,11 +54,7 @@ export const SimpleMessage = { fromPartial(object: DeepPartial): SimpleMessage { const message = { ...baseSimpleMessage } as SimpleMessage; - if (object.numberField !== undefined && object.numberField !== null) { - message.numberField = object.numberField; - } else { - message.numberField = 0; - } + message.numberField = object.numberField ?? 0; return message; }, }; diff --git a/integration/avoid-import-conflicts/simple.ts b/integration/avoid-import-conflicts/simple.ts index d9679acbc..b06a4bda3 100644 --- a/integration/avoid-import-conflicts/simple.ts +++ b/integration/avoid-import-conflicts/simple.ts @@ -117,11 +117,7 @@ export const Simple = { fromPartial(object: DeepPartial): Simple { const message = { ...baseSimple } as Simple; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = ''; - } + message.name = object.name ?? ''; if (object.otherSimple !== undefined && object.otherSimple !== null) { message.otherSimple = Simple2.fromPartial(object.otherSimple); } else { @@ -189,16 +185,8 @@ export const SimpleEnums = { fromPartial(object: DeepPartial): SimpleEnums { const message = { ...baseSimpleEnums } as SimpleEnums; - if (object.localEnum !== undefined && object.localEnum !== null) { - message.localEnum = object.localEnum; - } else { - message.localEnum = 0; - } - if (object.importEnum !== undefined && object.importEnum !== null) { - message.importEnum = object.importEnum; - } else { - message.importEnum = 0; - } + message.localEnum = object.localEnum ?? 0; + message.importEnum = object.importEnum ?? 0; return message; }, }; diff --git a/integration/avoid-import-conflicts/simple2.ts b/integration/avoid-import-conflicts/simple2.ts index 31433569f..8284e539f 100644 --- a/integration/avoid-import-conflicts/simple2.ts +++ b/integration/avoid-import-conflicts/simple2.ts @@ -105,16 +105,8 @@ export const Simple = { fromPartial(object: DeepPartial): Simple { const message = { ...baseSimple } as Simple; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = ''; - } - if (object.age !== undefined && object.age !== null) { - message.age = object.age; - } else { - message.age = 0; - } + message.name = object.name ?? ''; + message.age = object.age ?? 0; return message; }, }; diff --git a/integration/barrel-imports/bar.ts b/integration/barrel-imports/bar.ts index a8303f685..3cbfb3b15 100644 --- a/integration/barrel-imports/bar.ts +++ b/integration/barrel-imports/bar.ts @@ -65,16 +65,8 @@ export const Bar = { fromPartial(object: DeepPartial): Bar { const message = { ...baseBar } as Bar; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = ''; - } - if (object.age !== undefined && object.age !== null) { - message.age = object.age; - } else { - message.age = 0; - } + message.name = object.name ?? ''; + message.age = object.age ?? 0; return message; }, }; diff --git a/integration/barrel-imports/foo.ts b/integration/barrel-imports/foo.ts index 42307b7e7..78c08c18a 100644 --- a/integration/barrel-imports/foo.ts +++ b/integration/barrel-imports/foo.ts @@ -66,11 +66,7 @@ export const Foo = { fromPartial(object: DeepPartial): Foo { const message = { ...baseFoo } as Foo; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = ''; - } + message.name = object.name ?? ''; if (object.bar !== undefined && object.bar !== null) { message.bar = Bar.fromPartial(object.bar); } else { diff --git a/integration/batching-with-context/batching.ts b/integration/batching-with-context/batching.ts index 00edf4034..1afac519c 100644 --- a/integration/batching-with-context/batching.ts +++ b/integration/batching-with-context/batching.ts @@ -358,11 +358,7 @@ export const BatchMapQueryResponse_EntitiesEntry = { fromPartial(object: DeepPartial): BatchMapQueryResponse_EntitiesEntry { const message = { ...baseBatchMapQueryResponse_EntitiesEntry } as BatchMapQueryResponse_EntitiesEntry; - if (object.key !== undefined && object.key !== null) { - message.key = object.key; - } else { - message.key = ''; - } + message.key = object.key ?? ''; if (object.value !== undefined && object.value !== null) { message.value = Entity.fromPartial(object.value); } else { @@ -418,11 +414,7 @@ export const GetOnlyMethodRequest = { fromPartial(object: DeepPartial): GetOnlyMethodRequest { const message = { ...baseGetOnlyMethodRequest } as GetOnlyMethodRequest; - if (object.id !== undefined && object.id !== null) { - message.id = object.id; - } else { - message.id = ''; - } + message.id = object.id ?? ''; return message; }, }; @@ -528,11 +520,7 @@ export const WriteMethodRequest = { fromPartial(object: DeepPartial): WriteMethodRequest { const message = { ...baseWriteMethodRequest } as WriteMethodRequest; - if (object.id !== undefined && object.id !== null) { - message.id = object.id; - } else { - message.id = ''; - } + message.id = object.id ?? ''; return message; }, }; @@ -633,16 +621,8 @@ export const Entity = { fromPartial(object: DeepPartial): Entity { const message = { ...baseEntity } as Entity; - if (object.id !== undefined && object.id !== null) { - message.id = object.id; - } else { - message.id = ''; - } - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = ''; - } + message.id = object.id ?? ''; + message.name = object.name ?? ''; return message; }, }; diff --git a/integration/batching/batching.ts b/integration/batching/batching.ts index 25a025910..abf7f953a 100644 --- a/integration/batching/batching.ts +++ b/integration/batching/batching.ts @@ -356,11 +356,7 @@ export const BatchMapQueryResponse_EntitiesEntry = { fromPartial(object: DeepPartial): BatchMapQueryResponse_EntitiesEntry { const message = { ...baseBatchMapQueryResponse_EntitiesEntry } as BatchMapQueryResponse_EntitiesEntry; - if (object.key !== undefined && object.key !== null) { - message.key = object.key; - } else { - message.key = ''; - } + message.key = object.key ?? ''; if (object.value !== undefined && object.value !== null) { message.value = Entity.fromPartial(object.value); } else { @@ -416,11 +412,7 @@ export const GetOnlyMethodRequest = { fromPartial(object: DeepPartial): GetOnlyMethodRequest { const message = { ...baseGetOnlyMethodRequest } as GetOnlyMethodRequest; - if (object.id !== undefined && object.id !== null) { - message.id = object.id; - } else { - message.id = ''; - } + message.id = object.id ?? ''; return message; }, }; @@ -526,11 +518,7 @@ export const WriteMethodRequest = { fromPartial(object: DeepPartial): WriteMethodRequest { const message = { ...baseWriteMethodRequest } as WriteMethodRequest; - if (object.id !== undefined && object.id !== null) { - message.id = object.id; - } else { - message.id = ''; - } + message.id = object.id ?? ''; return message; }, }; @@ -631,16 +619,8 @@ export const Entity = { fromPartial(object: DeepPartial): Entity { const message = { ...baseEntity } as Entity; - if (object.id !== undefined && object.id !== null) { - message.id = object.id; - } else { - message.id = ''; - } - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = ''; - } + message.id = object.id ?? ''; + message.name = object.name ?? ''; return message; }, }; diff --git a/integration/bytes-as-base64/message.ts b/integration/bytes-as-base64/message.ts index 35b95de65..6b5ebc662 100644 --- a/integration/bytes-as-base64/message.ts +++ b/integration/bytes-as-base64/message.ts @@ -29,11 +29,7 @@ export const Message = { fromPartial(object: DeepPartial): Message { const message = { ...baseMessage } as Message; - if (object.data !== undefined && object.data !== null) { - message.data = object.data; - } else { - message.data = new Uint8Array(); - } + message.data = object.data ?? new Uint8Array(); return message; }, }; diff --git a/integration/bytes-node/point.ts b/integration/bytes-node/point.ts index 51371174c..1a37383ba 100644 --- a/integration/bytes-node/point.ts +++ b/integration/bytes-node/point.ts @@ -55,11 +55,7 @@ export const Point = { fromPartial(object: DeepPartial): Point { const message = { ...basePoint } as Point; - if (object.data !== undefined && object.data !== null) { - message.data = object.data; - } else { - message.data = Buffer.alloc(0); - } + message.data = object.data ?? Buffer.alloc(0); return message; }, }; diff --git a/integration/const-enum/const-enum.ts b/integration/const-enum/const-enum.ts index 1d6f70569..2b59e47d5 100644 --- a/integration/const-enum/const-enum.ts +++ b/integration/const-enum/const-enum.ts @@ -113,11 +113,7 @@ export const DividerData = { fromPartial(object: DeepPartial): DividerData { const message = { ...baseDividerData } as DividerData; - if (object.type !== undefined && object.type !== null) { - message.type = object.type; - } else { - message.type = DividerData_DividerType.DOUBLE; - } + message.type = object.type ?? DividerData_DividerType.DOUBLE; return message; }, }; diff --git a/integration/generic-service-definitions/simple.ts b/integration/generic-service-definitions/simple.ts index 020613289..5c8c2a568 100644 --- a/integration/generic-service-definitions/simple.ts +++ b/integration/generic-service-definitions/simple.ts @@ -54,11 +54,7 @@ export const TestMessage = { fromPartial(object: DeepPartial): TestMessage { const message = { ...baseTestMessage } as TestMessage; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = ''; - } + message.value = object.value ?? ''; return message; }, }; diff --git a/integration/global-this/global-this.ts b/integration/global-this/global-this.ts index 5fdc10435..8ce99ea98 100644 --- a/integration/global-this/global-this.ts +++ b/integration/global-this/global-this.ts @@ -58,11 +58,7 @@ export const Object = { fromPartial(object: DeepPartial): Object { const message = { ...baseObject } as Object; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = ''; - } + message.name = object.name ?? ''; return message; }, }; @@ -113,11 +109,7 @@ export const Error = { fromPartial(object: DeepPartial): Error { const message = { ...baseError } as Error; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = ''; - } + message.name = object.name ?? ''; return message; }, }; diff --git a/integration/grpc-js/google/protobuf/timestamp.ts b/integration/grpc-js/google/protobuf/timestamp.ts index 52ea73adb..89f9a4fdb 100644 --- a/integration/grpc-js/google/protobuf/timestamp.ts +++ b/integration/grpc-js/google/protobuf/timestamp.ts @@ -162,16 +162,8 @@ export const Timestamp = { fromPartial(object: DeepPartial): Timestamp { const message = { ...baseTimestamp } as Timestamp; - if (object.seconds !== undefined && object.seconds !== null) { - message.seconds = object.seconds; - } else { - message.seconds = 0; - } - if (object.nanos !== undefined && object.nanos !== null) { - message.nanos = object.nanos; - } else { - message.nanos = 0; - } + message.seconds = object.seconds ?? 0; + message.nanos = object.nanos ?? 0; return message; }, }; diff --git a/integration/grpc-js/google/protobuf/wrappers.ts b/integration/grpc-js/google/protobuf/wrappers.ts index 92da3e0a8..5f52d3fe7 100644 --- a/integration/grpc-js/google/protobuf/wrappers.ts +++ b/integration/grpc-js/google/protobuf/wrappers.ts @@ -140,11 +140,7 @@ export const DoubleValue = { fromPartial(object: DeepPartial): DoubleValue { const message = { ...baseDoubleValue } as DoubleValue; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.value = object.value ?? 0; return message; }, }; @@ -195,11 +191,7 @@ export const FloatValue = { fromPartial(object: DeepPartial): FloatValue { const message = { ...baseFloatValue } as FloatValue; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.value = object.value ?? 0; return message; }, }; @@ -250,11 +242,7 @@ export const Int64Value = { fromPartial(object: DeepPartial): Int64Value { const message = { ...baseInt64Value } as Int64Value; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.value = object.value ?? 0; return message; }, }; @@ -305,11 +293,7 @@ export const UInt64Value = { fromPartial(object: DeepPartial): UInt64Value { const message = { ...baseUInt64Value } as UInt64Value; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.value = object.value ?? 0; return message; }, }; @@ -360,11 +344,7 @@ export const Int32Value = { fromPartial(object: DeepPartial): Int32Value { const message = { ...baseInt32Value } as Int32Value; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.value = object.value ?? 0; return message; }, }; @@ -415,11 +395,7 @@ export const UInt32Value = { fromPartial(object: DeepPartial): UInt32Value { const message = { ...baseUInt32Value } as UInt32Value; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.value = object.value ?? 0; return message; }, }; @@ -470,11 +446,7 @@ export const BoolValue = { fromPartial(object: DeepPartial): BoolValue { const message = { ...baseBoolValue } as BoolValue; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = false; - } + message.value = object.value ?? false; return message; }, }; @@ -525,11 +497,7 @@ export const StringValue = { fromPartial(object: DeepPartial): StringValue { const message = { ...baseStringValue } as StringValue; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = ''; - } + message.value = object.value ?? ''; return message; }, }; @@ -581,11 +549,7 @@ export const BytesValue = { fromPartial(object: DeepPartial): BytesValue { const message = { ...baseBytesValue } as BytesValue; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = new Uint8Array(); - } + message.value = object.value ?? new Uint8Array(); return message; }, }; diff --git a/integration/grpc-js/simple.ts b/integration/grpc-js/simple.ts index b8928314b..50138d1b7 100644 --- a/integration/grpc-js/simple.ts +++ b/integration/grpc-js/simple.ts @@ -85,11 +85,7 @@ export const TestMessage = { fromPartial(object: DeepPartial): TestMessage { const message = { ...baseTestMessage } as TestMessage; - if (object.timestamp !== undefined && object.timestamp !== null) { - message.timestamp = object.timestamp; - } else { - message.timestamp = undefined; - } + message.timestamp = object.timestamp ?? undefined; return message; }, }; diff --git a/integration/grpc-web-go-server/example.ts b/integration/grpc-web-go-server/example.ts index 1135f99ba..cab052ced 100644 --- a/integration/grpc-web-go-server/example.ts +++ b/integration/grpc-web-go-server/example.ts @@ -149,16 +149,8 @@ export const DashFlash = { fromPartial(object: DeepPartial): DashFlash { const message = { ...baseDashFlash } as DashFlash; - if (object.msg !== undefined && object.msg !== null) { - message.msg = object.msg; - } else { - message.msg = ''; - } - if (object.type !== undefined && object.type !== null) { - message.type = object.type; - } else { - message.type = 0; - } + message.msg = object.msg ?? ''; + message.type = object.type ?? 0; return message; }, }; @@ -240,17 +232,13 @@ export const DashUserSettingsState = { fromPartial(object: DeepPartial): DashUserSettingsState { const message = { ...baseDashUserSettingsState } as DashUserSettingsState; - message.flashes = []; - if (object.email !== undefined && object.email !== null) { - message.email = object.email; - } else { - message.email = ''; - } + message.email = object.email ?? ''; if (object.urls !== undefined && object.urls !== null) { message.urls = DashUserSettingsState_URLs.fromPartial(object.urls); } else { message.urls = undefined; } + message.flashes = []; if (object.flashes !== undefined && object.flashes !== null) { for (const e of object.flashes) { message.flashes.push(DashFlash.fromPartial(e)); @@ -318,16 +306,8 @@ export const DashUserSettingsState_URLs = { fromPartial(object: DeepPartial): DashUserSettingsState_URLs { const message = { ...baseDashUserSettingsState_URLs } as DashUserSettingsState_URLs; - if (object.connectGoogle !== undefined && object.connectGoogle !== null) { - message.connectGoogle = object.connectGoogle; - } else { - message.connectGoogle = ''; - } - if (object.connectGithub !== undefined && object.connectGithub !== null) { - message.connectGithub = object.connectGithub; - } else { - message.connectGithub = ''; - } + message.connectGoogle = object.connectGoogle ?? ''; + message.connectGithub = object.connectGithub ?? ''; return message; }, }; @@ -414,26 +394,10 @@ export const DashCred = { fromPartial(object: DeepPartial): DashCred { const message = { ...baseDashCred } as DashCred; - if (object.description !== undefined && object.description !== null) { - message.description = object.description; - } else { - message.description = ''; - } - if (object.metadata !== undefined && object.metadata !== null) { - message.metadata = object.metadata; - } else { - message.metadata = ''; - } - if (object.token !== undefined && object.token !== null) { - message.token = object.token; - } else { - message.token = ''; - } - if (object.id !== undefined && object.id !== null) { - message.id = object.id; - } else { - message.id = ''; - } + message.description = object.description ?? ''; + message.metadata = object.metadata ?? ''; + message.token = object.token ?? ''; + message.id = object.id ?? ''; return message; }, }; @@ -496,16 +460,8 @@ export const DashAPICredsCreateReq = { fromPartial(object: DeepPartial): DashAPICredsCreateReq { const message = { ...baseDashAPICredsCreateReq } as DashAPICredsCreateReq; - if (object.description !== undefined && object.description !== null) { - message.description = object.description; - } else { - message.description = ''; - } - if (object.metadata !== undefined && object.metadata !== null) { - message.metadata = object.metadata; - } else { - message.metadata = ''; - } + message.description = object.description ?? ''; + message.metadata = object.metadata ?? ''; return message; }, }; @@ -592,26 +548,10 @@ export const DashAPICredsUpdateReq = { fromPartial(object: DeepPartial): DashAPICredsUpdateReq { const message = { ...baseDashAPICredsUpdateReq } as DashAPICredsUpdateReq; - if (object.credSid !== undefined && object.credSid !== null) { - message.credSid = object.credSid; - } else { - message.credSid = ''; - } - if (object.description !== undefined && object.description !== null) { - message.description = object.description; - } else { - message.description = ''; - } - if (object.metadata !== undefined && object.metadata !== null) { - message.metadata = object.metadata; - } else { - message.metadata = ''; - } - if (object.id !== undefined && object.id !== null) { - message.id = object.id; - } else { - message.id = ''; - } + message.credSid = object.credSid ?? ''; + message.description = object.description ?? ''; + message.metadata = object.metadata ?? ''; + message.id = object.id ?? ''; return message; }, }; @@ -674,16 +614,8 @@ export const DashAPICredsDeleteReq = { fromPartial(object: DeepPartial): DashAPICredsDeleteReq { const message = { ...baseDashAPICredsDeleteReq } as DashAPICredsDeleteReq; - if (object.credSid !== undefined && object.credSid !== null) { - message.credSid = object.credSid; - } else { - message.credSid = ''; - } - if (object.id !== undefined && object.id !== null) { - message.id = object.id; - } else { - message.id = ''; - } + message.credSid = object.credSid ?? ''; + message.id = object.id ?? ''; return message; }, }; diff --git a/integration/grpc-web-no-streaming-observable/example.ts b/integration/grpc-web-no-streaming-observable/example.ts index d74f8dc15..a64377c43 100644 --- a/integration/grpc-web-no-streaming-observable/example.ts +++ b/integration/grpc-web-no-streaming-observable/example.ts @@ -128,16 +128,8 @@ export const DashFlash = { fromPartial(object: DeepPartial): DashFlash { const message = { ...baseDashFlash } as DashFlash; - if (object.msg !== undefined && object.msg !== null) { - message.msg = object.msg; - } else { - message.msg = ''; - } - if (object.type !== undefined && object.type !== null) { - message.type = object.type; - } else { - message.type = 0; - } + message.msg = object.msg ?? ''; + message.type = object.type ?? 0; return message; }, }; @@ -219,17 +211,13 @@ export const DashUserSettingsState = { fromPartial(object: DeepPartial): DashUserSettingsState { const message = { ...baseDashUserSettingsState } as DashUserSettingsState; - message.flashes = []; - if (object.email !== undefined && object.email !== null) { - message.email = object.email; - } else { - message.email = ''; - } + message.email = object.email ?? ''; if (object.urls !== undefined && object.urls !== null) { message.urls = DashUserSettingsState_URLs.fromPartial(object.urls); } else { message.urls = undefined; } + message.flashes = []; if (object.flashes !== undefined && object.flashes !== null) { for (const e of object.flashes) { message.flashes.push(DashFlash.fromPartial(e)); @@ -297,16 +285,8 @@ export const DashUserSettingsState_URLs = { fromPartial(object: DeepPartial): DashUserSettingsState_URLs { const message = { ...baseDashUserSettingsState_URLs } as DashUserSettingsState_URLs; - if (object.connectGoogle !== undefined && object.connectGoogle !== null) { - message.connectGoogle = object.connectGoogle; - } else { - message.connectGoogle = ''; - } - if (object.connectGithub !== undefined && object.connectGithub !== null) { - message.connectGithub = object.connectGithub; - } else { - message.connectGithub = ''; - } + message.connectGoogle = object.connectGoogle ?? ''; + message.connectGithub = object.connectGithub ?? ''; return message; }, }; diff --git a/integration/grpc-web-no-streaming/example.ts b/integration/grpc-web-no-streaming/example.ts index 3c307e85c..24a1d88a9 100644 --- a/integration/grpc-web-no-streaming/example.ts +++ b/integration/grpc-web-no-streaming/example.ts @@ -126,16 +126,8 @@ export const DashFlash = { fromPartial(object: DeepPartial): DashFlash { const message = { ...baseDashFlash } as DashFlash; - if (object.msg !== undefined && object.msg !== null) { - message.msg = object.msg; - } else { - message.msg = ''; - } - if (object.type !== undefined && object.type !== null) { - message.type = object.type; - } else { - message.type = 0; - } + message.msg = object.msg ?? ''; + message.type = object.type ?? 0; return message; }, }; @@ -217,17 +209,13 @@ export const DashUserSettingsState = { fromPartial(object: DeepPartial): DashUserSettingsState { const message = { ...baseDashUserSettingsState } as DashUserSettingsState; - message.flashes = []; - if (object.email !== undefined && object.email !== null) { - message.email = object.email; - } else { - message.email = ''; - } + message.email = object.email ?? ''; if (object.urls !== undefined && object.urls !== null) { message.urls = DashUserSettingsState_URLs.fromPartial(object.urls); } else { message.urls = undefined; } + message.flashes = []; if (object.flashes !== undefined && object.flashes !== null) { for (const e of object.flashes) { message.flashes.push(DashFlash.fromPartial(e)); @@ -295,16 +283,8 @@ export const DashUserSettingsState_URLs = { fromPartial(object: DeepPartial): DashUserSettingsState_URLs { const message = { ...baseDashUserSettingsState_URLs } as DashUserSettingsState_URLs; - if (object.connectGoogle !== undefined && object.connectGoogle !== null) { - message.connectGoogle = object.connectGoogle; - } else { - message.connectGoogle = ''; - } - if (object.connectGithub !== undefined && object.connectGithub !== null) { - message.connectGithub = object.connectGithub; - } else { - message.connectGithub = ''; - } + message.connectGoogle = object.connectGoogle ?? ''; + message.connectGithub = object.connectGithub ?? ''; return message; }, }; diff --git a/integration/grpc-web/example.ts b/integration/grpc-web/example.ts index 2c33dc068..90607b0ff 100644 --- a/integration/grpc-web/example.ts +++ b/integration/grpc-web/example.ts @@ -152,16 +152,8 @@ export const DashFlash = { fromPartial(object: DeepPartial): DashFlash { const message = { ...baseDashFlash } as DashFlash; - if (object.msg !== undefined && object.msg !== null) { - message.msg = object.msg; - } else { - message.msg = ''; - } - if (object.type !== undefined && object.type !== null) { - message.type = object.type; - } else { - message.type = 0; - } + message.msg = object.msg ?? ''; + message.type = object.type ?? 0; return message; }, }; @@ -243,17 +235,13 @@ export const DashUserSettingsState = { fromPartial(object: DeepPartial): DashUserSettingsState { const message = { ...baseDashUserSettingsState } as DashUserSettingsState; - message.flashes = []; - if (object.email !== undefined && object.email !== null) { - message.email = object.email; - } else { - message.email = ''; - } + message.email = object.email ?? ''; if (object.urls !== undefined && object.urls !== null) { message.urls = DashUserSettingsState_URLs.fromPartial(object.urls); } else { message.urls = undefined; } + message.flashes = []; if (object.flashes !== undefined && object.flashes !== null) { for (const e of object.flashes) { message.flashes.push(DashFlash.fromPartial(e)); @@ -321,16 +309,8 @@ export const DashUserSettingsState_URLs = { fromPartial(object: DeepPartial): DashUserSettingsState_URLs { const message = { ...baseDashUserSettingsState_URLs } as DashUserSettingsState_URLs; - if (object.connectGoogle !== undefined && object.connectGoogle !== null) { - message.connectGoogle = object.connectGoogle; - } else { - message.connectGoogle = ''; - } - if (object.connectGithub !== undefined && object.connectGithub !== null) { - message.connectGithub = object.connectGithub; - } else { - message.connectGithub = ''; - } + message.connectGoogle = object.connectGoogle ?? ''; + message.connectGithub = object.connectGithub ?? ''; return message; }, }; @@ -417,26 +397,10 @@ export const DashCred = { fromPartial(object: DeepPartial): DashCred { const message = { ...baseDashCred } as DashCred; - if (object.description !== undefined && object.description !== null) { - message.description = object.description; - } else { - message.description = ''; - } - if (object.metadata !== undefined && object.metadata !== null) { - message.metadata = object.metadata; - } else { - message.metadata = ''; - } - if (object.token !== undefined && object.token !== null) { - message.token = object.token; - } else { - message.token = ''; - } - if (object.id !== undefined && object.id !== null) { - message.id = object.id; - } else { - message.id = ''; - } + message.description = object.description ?? ''; + message.metadata = object.metadata ?? ''; + message.token = object.token ?? ''; + message.id = object.id ?? ''; return message; }, }; @@ -499,16 +463,8 @@ export const DashAPICredsCreateReq = { fromPartial(object: DeepPartial): DashAPICredsCreateReq { const message = { ...baseDashAPICredsCreateReq } as DashAPICredsCreateReq; - if (object.description !== undefined && object.description !== null) { - message.description = object.description; - } else { - message.description = ''; - } - if (object.metadata !== undefined && object.metadata !== null) { - message.metadata = object.metadata; - } else { - message.metadata = ''; - } + message.description = object.description ?? ''; + message.metadata = object.metadata ?? ''; return message; }, }; @@ -595,26 +551,10 @@ export const DashAPICredsUpdateReq = { fromPartial(object: DeepPartial): DashAPICredsUpdateReq { const message = { ...baseDashAPICredsUpdateReq } as DashAPICredsUpdateReq; - if (object.credSid !== undefined && object.credSid !== null) { - message.credSid = object.credSid; - } else { - message.credSid = ''; - } - if (object.description !== undefined && object.description !== null) { - message.description = object.description; - } else { - message.description = ''; - } - if (object.metadata !== undefined && object.metadata !== null) { - message.metadata = object.metadata; - } else { - message.metadata = ''; - } - if (object.id !== undefined && object.id !== null) { - message.id = object.id; - } else { - message.id = ''; - } + message.credSid = object.credSid ?? ''; + message.description = object.description ?? ''; + message.metadata = object.metadata ?? ''; + message.id = object.id ?? ''; return message; }, }; @@ -677,16 +617,8 @@ export const DashAPICredsDeleteReq = { fromPartial(object: DeepPartial): DashAPICredsDeleteReq { const message = { ...baseDashAPICredsDeleteReq } as DashAPICredsDeleteReq; - if (object.credSid !== undefined && object.credSid !== null) { - message.credSid = object.credSid; - } else { - message.credSid = ''; - } - if (object.id !== undefined && object.id !== null) { - message.id = object.id; - } else { - message.id = ''; - } + message.credSid = object.credSid ?? ''; + message.id = object.id ?? ''; return message; }, }; diff --git a/integration/lower-case-svc-methods/math.ts b/integration/lower-case-svc-methods/math.ts index a82e96343..cf2627864 100644 --- a/integration/lower-case-svc-methods/math.ts +++ b/integration/lower-case-svc-methods/math.ts @@ -77,16 +77,8 @@ export const NumPair = { fromPartial(object: DeepPartial): NumPair { const message = { ...baseNumPair } as NumPair; - if (object.num1 !== undefined && object.num1 !== null) { - message.num1 = object.num1; - } else { - message.num1 = 0; - } - if (object.num2 !== undefined && object.num2 !== null) { - message.num2 = object.num2; - } else { - message.num2 = 0; - } + message.num1 = object.num1 ?? 0; + message.num2 = object.num2 ?? 0; return message; }, }; @@ -137,11 +129,7 @@ export const NumSingle = { fromPartial(object: DeepPartial): NumSingle { const message = { ...baseNumSingle } as NumSingle; - if (object.num !== undefined && object.num !== null) { - message.num = object.num; - } else { - message.num = 0; - } + message.num = object.num ?? 0; return message; }, }; diff --git a/integration/no-proto-package/no-proto-package.ts b/integration/no-proto-package/no-proto-package.ts index 4b5c6a19a..dd4dfd44e 100644 --- a/integration/no-proto-package/no-proto-package.ts +++ b/integration/no-proto-package/no-proto-package.ts @@ -57,11 +57,7 @@ export const User = { fromPartial(object: DeepPartial): User { const message = { ...baseUser } as User; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = ''; - } + message.name = object.name ?? ''; return message; }, }; diff --git a/integration/oneof-properties/oneof.ts b/integration/oneof-properties/oneof.ts index 135dffe81..8a1a2e31a 100644 --- a/integration/oneof-properties/oneof.ts +++ b/integration/oneof-properties/oneof.ts @@ -239,61 +239,21 @@ export const PleaseChoose = { fromPartial(object: DeepPartial): PleaseChoose { const message = { ...basePleaseChoose } as PleaseChoose; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = ''; - } - if (object.aNumber !== undefined && object.aNumber !== null) { - message.aNumber = object.aNumber; - } else { - message.aNumber = undefined; - } - if (object.aString !== undefined && object.aString !== null) { - message.aString = object.aString; - } else { - message.aString = undefined; - } + message.name = object.name ?? ''; + message.aNumber = object.aNumber ?? undefined; + message.aString = object.aString ?? undefined; if (object.aMessage !== undefined && object.aMessage !== null) { message.aMessage = PleaseChoose_Submessage.fromPartial(object.aMessage); } else { message.aMessage = undefined; } - if (object.aBool !== undefined && object.aBool !== null) { - message.aBool = object.aBool; - } else { - message.aBool = undefined; - } - if (object.bunchaBytes !== undefined && object.bunchaBytes !== null) { - message.bunchaBytes = object.bunchaBytes; - } else { - message.bunchaBytes = undefined; - } - if (object.anEnum !== undefined && object.anEnum !== null) { - message.anEnum = object.anEnum; - } else { - message.anEnum = undefined; - } - if (object.age !== undefined && object.age !== null) { - message.age = object.age; - } else { - message.age = 0; - } - if (object.either !== undefined && object.either !== null) { - message.either = object.either; - } else { - message.either = undefined; - } - if (object.or !== undefined && object.or !== null) { - message.or = object.or; - } else { - message.or = undefined; - } - if (object.thirdOption !== undefined && object.thirdOption !== null) { - message.thirdOption = object.thirdOption; - } else { - message.thirdOption = undefined; - } + message.aBool = object.aBool ?? undefined; + message.bunchaBytes = object.bunchaBytes ?? undefined; + message.anEnum = object.anEnum ?? undefined; + message.age = object.age ?? 0; + message.either = object.either ?? undefined; + message.or = object.or ?? undefined; + message.thirdOption = object.thirdOption ?? undefined; return message; }, }; @@ -344,11 +304,7 @@ export const PleaseChoose_Submessage = { fromPartial(object: DeepPartial): PleaseChoose_Submessage { const message = { ...basePleaseChoose_Submessage } as PleaseChoose_Submessage; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = ''; - } + message.name = object.name ?? ''; return message; }, }; diff --git a/integration/oneof-unions/oneof.ts b/integration/oneof-unions/oneof.ts index 4f419f7fd..d0267744b 100644 --- a/integration/oneof-unions/oneof.ts +++ b/integration/oneof-unions/oneof.ts @@ -231,11 +231,7 @@ export const PleaseChoose = { fromPartial(object: DeepPartial): PleaseChoose { const message = { ...basePleaseChoose } as PleaseChoose; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = ''; - } + message.name = object.name ?? ''; if (object.choice?.$case === 'aNumber' && object.choice?.aNumber !== undefined && object.choice?.aNumber !== null) { message.choice = { $case: 'aNumber', aNumber: object.choice.aNumber }; } @@ -262,11 +258,7 @@ export const PleaseChoose = { if (object.choice?.$case === 'anEnum' && object.choice?.anEnum !== undefined && object.choice?.anEnum !== null) { message.choice = { $case: 'anEnum', anEnum: object.choice.anEnum }; } - if (object.age !== undefined && object.age !== null) { - message.age = object.age; - } else { - message.age = 0; - } + message.age = object.age ?? 0; if ( object.eitherOr?.$case === 'either' && object.eitherOr?.either !== undefined && @@ -284,11 +276,7 @@ export const PleaseChoose = { ) { message.eitherOr = { $case: 'thirdOption', thirdOption: object.eitherOr.thirdOption }; } - if (object.signature !== undefined && object.signature !== null) { - message.signature = object.signature; - } else { - message.signature = new Uint8Array(); - } + message.signature = object.signature ?? new Uint8Array(); return message; }, }; @@ -337,11 +325,7 @@ export const PleaseChoose_Submessage = { fromPartial(object: DeepPartial): PleaseChoose_Submessage { const message = { ...basePleaseChoose_Submessage } as PleaseChoose_Submessage; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = ''; - } + message.name = object.name ?? ''; return message; }, }; @@ -400,16 +384,8 @@ export const SimpleButOptional = { fromPartial(object: DeepPartial): SimpleButOptional { const message = { ...baseSimpleButOptional } as SimpleButOptional; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = undefined; - } - if (object.age !== undefined && object.age !== null) { - message.age = object.age; - } else { - message.age = undefined; - } + message.name = object.name ?? undefined; + message.age = object.age ?? undefined; return message; }, }; diff --git a/integration/point/point.ts b/integration/point/point.ts index 007ae47f2..af3c42bd5 100644 --- a/integration/point/point.ts +++ b/integration/point/point.ts @@ -72,16 +72,8 @@ export const Point = { fromPartial(object: DeepPartial): Point { const message = { ...basePoint } as Point; - if (object.lat !== undefined && object.lat !== null) { - message.lat = object.lat; - } else { - message.lat = 0; - } - if (object.lng !== undefined && object.lng !== null) { - message.lng = object.lng; - } else { - message.lng = 0; - } + message.lat = object.lat ?? 0; + message.lng = object.lng ?? 0; return message; }, }; diff --git a/integration/return-observable/observable.ts b/integration/return-observable/observable.ts index aa8e89d3b..1421c0efd 100644 --- a/integration/return-observable/observable.ts +++ b/integration/return-observable/observable.ts @@ -59,11 +59,7 @@ export const ProduceRequest = { fromPartial(object: DeepPartial): ProduceRequest { const message = { ...baseProduceRequest } as ProduceRequest; - if (object.ingredients !== undefined && object.ingredients !== null) { - message.ingredients = object.ingredients; - } else { - message.ingredients = ''; - } + message.ingredients = object.ingredients ?? ''; return message; }, }; @@ -114,11 +110,7 @@ export const ProduceReply = { fromPartial(object: DeepPartial): ProduceReply { const message = { ...baseProduceReply } as ProduceReply; - if (object.result !== undefined && object.result !== null) { - message.result = object.result; - } else { - message.result = ''; - } + message.result = object.result ?? ''; return message; }, }; diff --git a/integration/simple-deprecated-fields/simple.ts b/integration/simple-deprecated-fields/simple.ts index ec7ab7318..02cf008fc 100644 --- a/integration/simple-deprecated-fields/simple.ts +++ b/integration/simple-deprecated-fields/simple.ts @@ -126,31 +126,15 @@ export const Simple = { fromPartial(object: DeepPartial): Simple { const message = { ...baseSimple } as Simple; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = ''; - } - if (object.age !== undefined && object.age !== null) { - message.age = object.age; - } else { - message.age = 0; - } + message.name = object.name ?? ''; + message.age = object.age ?? 0; if (object.child !== undefined && object.child !== null) { message.child = Child.fromPartial(object.child); } else { message.child = undefined; } - if (object.testField !== undefined && object.testField !== null) { - message.testField = object.testField; - } else { - message.testField = ''; - } - if (object.testNotDeprecated !== undefined && object.testNotDeprecated !== null) { - message.testNotDeprecated = object.testNotDeprecated; - } else { - message.testNotDeprecated = ''; - } + message.testField = object.testField ?? ''; + message.testNotDeprecated = object.testNotDeprecated ?? ''; return message; }, }; @@ -201,11 +185,7 @@ export const Child = { fromPartial(object: DeepPartial): Child { const message = { ...baseChild } as Child; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = ''; - } + message.name = object.name ?? ''; return message; }, }; diff --git a/integration/simple-esmodule-interop/simple.ts b/integration/simple-esmodule-interop/simple.ts index d8848a67f..893eb0d71 100644 --- a/integration/simple-esmodule-interop/simple.ts +++ b/integration/simple-esmodule-interop/simple.ts @@ -82,16 +82,8 @@ export const Simple = { fromPartial(object: DeepPartial): Simple { const message = { ...baseSimple } as Simple; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = ''; - } - if (object.age !== undefined && object.age !== null) { - message.age = object.age; - } else { - message.age = 0; - } + message.name = object.name ?? ''; + message.age = object.age ?? 0; return message; }, }; @@ -287,66 +279,18 @@ export const Numbers = { fromPartial(object: DeepPartial): Numbers { const message = { ...baseNumbers } as Numbers; - if (object.double !== undefined && object.double !== null) { - message.double = object.double; - } else { - message.double = 0; - } - if (object.float !== undefined && object.float !== null) { - message.float = object.float; - } else { - message.float = 0; - } - if (object.int32 !== undefined && object.int32 !== null) { - message.int32 = object.int32; - } else { - message.int32 = 0; - } - if (object.int64 !== undefined && object.int64 !== null) { - message.int64 = object.int64; - } else { - message.int64 = 0; - } - if (object.uint32 !== undefined && object.uint32 !== null) { - message.uint32 = object.uint32; - } else { - message.uint32 = 0; - } - if (object.uint64 !== undefined && object.uint64 !== null) { - message.uint64 = object.uint64; - } else { - message.uint64 = 0; - } - if (object.sint32 !== undefined && object.sint32 !== null) { - message.sint32 = object.sint32; - } else { - message.sint32 = 0; - } - if (object.sint64 !== undefined && object.sint64 !== null) { - message.sint64 = object.sint64; - } else { - message.sint64 = 0; - } - if (object.fixed32 !== undefined && object.fixed32 !== null) { - message.fixed32 = object.fixed32; - } else { - message.fixed32 = 0; - } - if (object.fixed64 !== undefined && object.fixed64 !== null) { - message.fixed64 = object.fixed64; - } else { - message.fixed64 = 0; - } - if (object.sfixed32 !== undefined && object.sfixed32 !== null) { - message.sfixed32 = object.sfixed32; - } else { - message.sfixed32 = 0; - } - if (object.sfixed64 !== undefined && object.sfixed64 !== null) { - message.sfixed64 = object.sfixed64; - } else { - message.sfixed64 = 0; - } + message.double = object.double ?? 0; + message.float = object.float ?? 0; + message.int32 = object.int32 ?? 0; + message.int64 = object.int64 ?? 0; + message.uint32 = object.uint32 ?? 0; + message.uint64 = object.uint64 ?? 0; + message.sint32 = object.sint32 ?? 0; + message.sint64 = object.sint64 ?? 0; + message.fixed32 = object.fixed32 ?? 0; + message.fixed64 = object.fixed64 ?? 0; + message.sfixed32 = object.sfixed32 ?? 0; + message.sfixed64 = object.sfixed64 ?? 0; return message; }, }; diff --git a/integration/simple-long-string/google/protobuf/timestamp.ts b/integration/simple-long-string/google/protobuf/timestamp.ts index 8795fb381..f9bcc2476 100644 --- a/integration/simple-long-string/google/protobuf/timestamp.ts +++ b/integration/simple-long-string/google/protobuf/timestamp.ts @@ -162,16 +162,8 @@ export const Timestamp = { fromPartial(object: DeepPartial): Timestamp { const message = { ...baseTimestamp } as Timestamp; - if (object.seconds !== undefined && object.seconds !== null) { - message.seconds = object.seconds; - } else { - message.seconds = '0'; - } - if (object.nanos !== undefined && object.nanos !== null) { - message.nanos = object.nanos; - } else { - message.nanos = 0; - } + message.seconds = object.seconds ?? '0'; + message.nanos = object.nanos ?? 0; return message; }, }; diff --git a/integration/simple-long-string/google/protobuf/wrappers.ts b/integration/simple-long-string/google/protobuf/wrappers.ts index 95bb5ea22..9ca672531 100644 --- a/integration/simple-long-string/google/protobuf/wrappers.ts +++ b/integration/simple-long-string/google/protobuf/wrappers.ts @@ -140,11 +140,7 @@ export const DoubleValue = { fromPartial(object: DeepPartial): DoubleValue { const message = { ...baseDoubleValue } as DoubleValue; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.value = object.value ?? 0; return message; }, }; @@ -195,11 +191,7 @@ export const FloatValue = { fromPartial(object: DeepPartial): FloatValue { const message = { ...baseFloatValue } as FloatValue; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.value = object.value ?? 0; return message; }, }; @@ -250,11 +242,7 @@ export const Int64Value = { fromPartial(object: DeepPartial): Int64Value { const message = { ...baseInt64Value } as Int64Value; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = '0'; - } + message.value = object.value ?? '0'; return message; }, }; @@ -305,11 +293,7 @@ export const UInt64Value = { fromPartial(object: DeepPartial): UInt64Value { const message = { ...baseUInt64Value } as UInt64Value; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = '0'; - } + message.value = object.value ?? '0'; return message; }, }; @@ -360,11 +344,7 @@ export const Int32Value = { fromPartial(object: DeepPartial): Int32Value { const message = { ...baseInt32Value } as Int32Value; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.value = object.value ?? 0; return message; }, }; @@ -415,11 +395,7 @@ export const UInt32Value = { fromPartial(object: DeepPartial): UInt32Value { const message = { ...baseUInt32Value } as UInt32Value; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.value = object.value ?? 0; return message; }, }; @@ -470,11 +446,7 @@ export const BoolValue = { fromPartial(object: DeepPartial): BoolValue { const message = { ...baseBoolValue } as BoolValue; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = false; - } + message.value = object.value ?? false; return message; }, }; @@ -525,11 +497,7 @@ export const StringValue = { fromPartial(object: DeepPartial): StringValue { const message = { ...baseStringValue } as StringValue; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = ''; - } + message.value = object.value ?? ''; return message; }, }; @@ -581,11 +549,7 @@ export const BytesValue = { fromPartial(object: DeepPartial): BytesValue { const message = { ...baseBytesValue } as BytesValue; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = new Uint8Array(); - } + message.value = object.value ?? new Uint8Array(); return message; }, }; diff --git a/integration/simple-long-string/simple.ts b/integration/simple-long-string/simple.ts index 4ced72ccd..e36bf4842 100644 --- a/integration/simple-long-string/simple.ts +++ b/integration/simple-long-string/simple.ts @@ -238,76 +238,20 @@ export const Numbers = { fromPartial(object: DeepPartial): Numbers { const message = { ...baseNumbers } as Numbers; - if (object.double !== undefined && object.double !== null) { - message.double = object.double; - } else { - message.double = 0; - } - if (object.float !== undefined && object.float !== null) { - message.float = object.float; - } else { - message.float = 0; - } - if (object.int32 !== undefined && object.int32 !== null) { - message.int32 = object.int32; - } else { - message.int32 = 0; - } - if (object.int64 !== undefined && object.int64 !== null) { - message.int64 = object.int64; - } else { - message.int64 = '0'; - } - if (object.uint32 !== undefined && object.uint32 !== null) { - message.uint32 = object.uint32; - } else { - message.uint32 = 0; - } - if (object.uint64 !== undefined && object.uint64 !== null) { - message.uint64 = object.uint64; - } else { - message.uint64 = '0'; - } - if (object.sint32 !== undefined && object.sint32 !== null) { - message.sint32 = object.sint32; - } else { - message.sint32 = 0; - } - if (object.sint64 !== undefined && object.sint64 !== null) { - message.sint64 = object.sint64; - } else { - message.sint64 = '0'; - } - if (object.fixed32 !== undefined && object.fixed32 !== null) { - message.fixed32 = object.fixed32; - } else { - message.fixed32 = 0; - } - if (object.fixed64 !== undefined && object.fixed64 !== null) { - message.fixed64 = object.fixed64; - } else { - message.fixed64 = '0'; - } - if (object.sfixed32 !== undefined && object.sfixed32 !== null) { - message.sfixed32 = object.sfixed32; - } else { - message.sfixed32 = 0; - } - if (object.sfixed64 !== undefined && object.sfixed64 !== null) { - message.sfixed64 = object.sfixed64; - } else { - message.sfixed64 = '0'; - } - if (object.guint64 !== undefined && object.guint64 !== null) { - message.guint64 = object.guint64; - } else { - message.guint64 = undefined; - } - if (object.timestamp !== undefined && object.timestamp !== null) { - message.timestamp = object.timestamp; - } else { - message.timestamp = undefined; - } + message.double = object.double ?? 0; + message.float = object.float ?? 0; + message.int32 = object.int32 ?? 0; + message.int64 = object.int64 ?? '0'; + message.uint32 = object.uint32 ?? 0; + message.uint64 = object.uint64 ?? '0'; + message.sint32 = object.sint32 ?? 0; + message.sint64 = object.sint64 ?? '0'; + message.fixed32 = object.fixed32 ?? 0; + message.fixed64 = object.fixed64 ?? '0'; + message.sfixed32 = object.sfixed32 ?? 0; + message.sfixed64 = object.sfixed64 ?? '0'; + message.guint64 = object.guint64 ?? undefined; + message.timestamp = object.timestamp ?? undefined; return message; }, }; diff --git a/integration/simple-long/google/protobuf/wrappers.ts b/integration/simple-long/google/protobuf/wrappers.ts index b6ec1513e..f63d5b340 100644 --- a/integration/simple-long/google/protobuf/wrappers.ts +++ b/integration/simple-long/google/protobuf/wrappers.ts @@ -140,11 +140,7 @@ export const DoubleValue = { fromPartial(object: DeepPartial): DoubleValue { const message = { ...baseDoubleValue } as DoubleValue; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.value = object.value ?? 0; return message; }, }; @@ -195,11 +191,7 @@ export const FloatValue = { fromPartial(object: DeepPartial): FloatValue { const message = { ...baseFloatValue } as FloatValue; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.value = object.value ?? 0; return message; }, }; @@ -360,11 +352,7 @@ export const Int32Value = { fromPartial(object: DeepPartial): Int32Value { const message = { ...baseInt32Value } as Int32Value; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.value = object.value ?? 0; return message; }, }; @@ -415,11 +403,7 @@ export const UInt32Value = { fromPartial(object: DeepPartial): UInt32Value { const message = { ...baseUInt32Value } as UInt32Value; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.value = object.value ?? 0; return message; }, }; @@ -470,11 +454,7 @@ export const BoolValue = { fromPartial(object: DeepPartial): BoolValue { const message = { ...baseBoolValue } as BoolValue; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = false; - } + message.value = object.value ?? false; return message; }, }; @@ -525,11 +505,7 @@ export const StringValue = { fromPartial(object: DeepPartial): StringValue { const message = { ...baseStringValue } as StringValue; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = ''; - } + message.value = object.value ?? ''; return message; }, }; @@ -581,11 +557,7 @@ export const BytesValue = { fromPartial(object: DeepPartial): BytesValue { const message = { ...baseBytesValue } as BytesValue; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = new Uint8Array(); - } + message.value = object.value ?? new Uint8Array(); return message; }, }; diff --git a/integration/simple-long/simple.ts b/integration/simple-long/simple.ts index 6e0a8392b..56d902e5e 100644 --- a/integration/simple-long/simple.ts +++ b/integration/simple-long/simple.ts @@ -164,33 +164,21 @@ export const SimpleWithWrappers = { fromPartial(object: DeepPartial): SimpleWithWrappers { const message = { ...baseSimpleWithWrappers } as SimpleWithWrappers; - message.coins = []; - message.snacks = []; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = undefined; - } - if (object.age !== undefined && object.age !== null) { - message.age = object.age; - } else { - message.age = undefined; - } - if (object.enabled !== undefined && object.enabled !== null) { - message.enabled = object.enabled; - } else { - message.enabled = undefined; - } + message.name = object.name ?? undefined; + message.age = object.age ?? undefined; + message.enabled = object.enabled ?? undefined; if (object.bananas !== undefined && object.bananas !== null) { message.bananas = object.bananas as Long | undefined; } else { message.bananas = undefined; } + message.coins = []; if (object.coins !== undefined && object.coins !== null) { for (const e of object.coins) { message.coins.push(e); } } + message.snacks = []; if (object.snacks !== undefined && object.snacks !== null) { for (const e of object.snacks) { message.snacks.push(e); @@ -279,7 +267,6 @@ export const SimpleWithMap = { fromPartial(object: DeepPartial): SimpleWithMap { const message = { ...baseSimpleWithMap } as SimpleWithMap; message.nameLookup = {}; - message.intLookup = {}; if (object.nameLookup !== undefined && object.nameLookup !== null) { Object.entries(object.nameLookup).forEach(([key, value]) => { if (value !== undefined) { @@ -287,6 +274,7 @@ export const SimpleWithMap = { } }); } + message.intLookup = {}; if (object.intLookup !== undefined && object.intLookup !== null) { Object.entries(object.intLookup).forEach(([key, value]) => { if (value !== undefined) { @@ -356,16 +344,8 @@ export const SimpleWithMap_NameLookupEntry = { fromPartial(object: DeepPartial): SimpleWithMap_NameLookupEntry { const message = { ...baseSimpleWithMap_NameLookupEntry } as SimpleWithMap_NameLookupEntry; - if (object.key !== undefined && object.key !== null) { - message.key = object.key; - } else { - message.key = ''; - } - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = ''; - } + message.key = object.key ?? ''; + message.value = object.value ?? ''; return message; }, }; @@ -428,16 +408,8 @@ export const SimpleWithMap_IntLookupEntry = { fromPartial(object: DeepPartial): SimpleWithMap_IntLookupEntry { const message = { ...baseSimpleWithMap_IntLookupEntry } as SimpleWithMap_IntLookupEntry; - if (object.key !== undefined && object.key !== null) { - message.key = object.key; - } else { - message.key = 0; - } - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.key = object.key ?? 0; + message.value = object.value ?? 0; return message; }, }; @@ -661,67 +633,39 @@ export const Numbers = { fromPartial(object: DeepPartial): Numbers { const message = { ...baseNumbers } as Numbers; - message.manyUint64 = []; - if (object.double !== undefined && object.double !== null) { - message.double = object.double; - } else { - message.double = 0; - } - if (object.float !== undefined && object.float !== null) { - message.float = object.float; - } else { - message.float = 0; - } - if (object.int32 !== undefined && object.int32 !== null) { - message.int32 = object.int32; - } else { - message.int32 = 0; - } + message.double = object.double ?? 0; + message.float = object.float ?? 0; + message.int32 = object.int32 ?? 0; if (object.int64 !== undefined && object.int64 !== null) { message.int64 = object.int64 as Long; } else { message.int64 = Long.ZERO; } - if (object.uint32 !== undefined && object.uint32 !== null) { - message.uint32 = object.uint32; - } else { - message.uint32 = 0; - } + message.uint32 = object.uint32 ?? 0; if (object.uint64 !== undefined && object.uint64 !== null) { message.uint64 = object.uint64 as Long; } else { message.uint64 = Long.UZERO; } - if (object.sint32 !== undefined && object.sint32 !== null) { - message.sint32 = object.sint32; - } else { - message.sint32 = 0; - } + message.sint32 = object.sint32 ?? 0; if (object.sint64 !== undefined && object.sint64 !== null) { message.sint64 = object.sint64 as Long; } else { message.sint64 = Long.ZERO; } - if (object.fixed32 !== undefined && object.fixed32 !== null) { - message.fixed32 = object.fixed32; - } else { - message.fixed32 = 0; - } + message.fixed32 = object.fixed32 ?? 0; if (object.fixed64 !== undefined && object.fixed64 !== null) { message.fixed64 = object.fixed64 as Long; } else { message.fixed64 = Long.UZERO; } - if (object.sfixed32 !== undefined && object.sfixed32 !== null) { - message.sfixed32 = object.sfixed32; - } else { - message.sfixed32 = 0; - } + message.sfixed32 = object.sfixed32 ?? 0; if (object.sfixed64 !== undefined && object.sfixed64 !== null) { message.sfixed64 = object.sfixed64 as Long; } else { message.sfixed64 = Long.ZERO; } + message.manyUint64 = []; if (object.manyUint64 !== undefined && object.manyUint64 !== null) { for (const e of object.manyUint64) { message.manyUint64.push(e); diff --git a/integration/simple-optionals/google/protobuf/timestamp.ts b/integration/simple-optionals/google/protobuf/timestamp.ts index 52ea73adb..89f9a4fdb 100644 --- a/integration/simple-optionals/google/protobuf/timestamp.ts +++ b/integration/simple-optionals/google/protobuf/timestamp.ts @@ -162,16 +162,8 @@ export const Timestamp = { fromPartial(object: DeepPartial): Timestamp { const message = { ...baseTimestamp } as Timestamp; - if (object.seconds !== undefined && object.seconds !== null) { - message.seconds = object.seconds; - } else { - message.seconds = 0; - } - if (object.nanos !== undefined && object.nanos !== null) { - message.nanos = object.nanos; - } else { - message.nanos = 0; - } + message.seconds = object.seconds ?? 0; + message.nanos = object.nanos ?? 0; return message; }, }; diff --git a/integration/simple-optionals/google/protobuf/wrappers.ts b/integration/simple-optionals/google/protobuf/wrappers.ts index 92da3e0a8..5f52d3fe7 100644 --- a/integration/simple-optionals/google/protobuf/wrappers.ts +++ b/integration/simple-optionals/google/protobuf/wrappers.ts @@ -140,11 +140,7 @@ export const DoubleValue = { fromPartial(object: DeepPartial): DoubleValue { const message = { ...baseDoubleValue } as DoubleValue; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.value = object.value ?? 0; return message; }, }; @@ -195,11 +191,7 @@ export const FloatValue = { fromPartial(object: DeepPartial): FloatValue { const message = { ...baseFloatValue } as FloatValue; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.value = object.value ?? 0; return message; }, }; @@ -250,11 +242,7 @@ export const Int64Value = { fromPartial(object: DeepPartial): Int64Value { const message = { ...baseInt64Value } as Int64Value; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.value = object.value ?? 0; return message; }, }; @@ -305,11 +293,7 @@ export const UInt64Value = { fromPartial(object: DeepPartial): UInt64Value { const message = { ...baseUInt64Value } as UInt64Value; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.value = object.value ?? 0; return message; }, }; @@ -360,11 +344,7 @@ export const Int32Value = { fromPartial(object: DeepPartial): Int32Value { const message = { ...baseInt32Value } as Int32Value; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.value = object.value ?? 0; return message; }, }; @@ -415,11 +395,7 @@ export const UInt32Value = { fromPartial(object: DeepPartial): UInt32Value { const message = { ...baseUInt32Value } as UInt32Value; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.value = object.value ?? 0; return message; }, }; @@ -470,11 +446,7 @@ export const BoolValue = { fromPartial(object: DeepPartial): BoolValue { const message = { ...baseBoolValue } as BoolValue; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = false; - } + message.value = object.value ?? false; return message; }, }; @@ -525,11 +497,7 @@ export const StringValue = { fromPartial(object: DeepPartial): StringValue { const message = { ...baseStringValue } as StringValue; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = ''; - } + message.value = object.value ?? ''; return message; }, }; @@ -581,11 +549,7 @@ export const BytesValue = { fromPartial(object: DeepPartial): BytesValue { const message = { ...baseBytesValue } as BytesValue; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = new Uint8Array(); - } + message.value = object.value ?? new Uint8Array(); return message; }, }; diff --git a/integration/simple-optionals/import_dir/thing.ts b/integration/simple-optionals/import_dir/thing.ts index 8acd814c2..bc254a2a7 100644 --- a/integration/simple-optionals/import_dir/thing.ts +++ b/integration/simple-optionals/import_dir/thing.ts @@ -55,11 +55,7 @@ export const ImportedThing = { fromPartial(object: DeepPartial): ImportedThing { const message = { ...baseImportedThing } as ImportedThing; - if (object.createdAt !== undefined && object.createdAt !== null) { - message.createdAt = object.createdAt; - } else { - message.createdAt = undefined; - } + message.createdAt = object.createdAt ?? undefined; return message; }, }; diff --git a/integration/simple-optionals/simple.ts b/integration/simple-optionals/simple.ts index edb7bea62..e1f10ef75 100644 --- a/integration/simple-optionals/simple.ts +++ b/integration/simple-optionals/simple.ts @@ -431,50 +431,34 @@ export const Simple = { fromPartial(object: DeepPartial): Simple { const message = { ...baseSimple } as Simple; - message.grandChildren = []; - message.coins = []; - message.snacks = []; - message.oldStates = []; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = ''; - } - if (object.age !== undefined && object.age !== null) { - message.age = object.age; - } else { - message.age = 0; - } - if (object.createdAt !== undefined && object.createdAt !== null) { - message.createdAt = object.createdAt; - } else { - message.createdAt = undefined; - } + message.name = object.name ?? ''; + message.age = object.age ?? 0; + message.createdAt = object.createdAt ?? undefined; if (object.child !== undefined && object.child !== null) { message.child = Child.fromPartial(object.child); } else { message.child = undefined; } - if (object.state !== undefined && object.state !== null) { - message.state = object.state; - } else { - message.state = 0; - } + message.state = object.state ?? 0; + message.grandChildren = []; if (object.grandChildren !== undefined && object.grandChildren !== null) { for (const e of object.grandChildren) { message.grandChildren.push(Child.fromPartial(e)); } } + message.coins = []; if (object.coins !== undefined && object.coins !== null) { for (const e of object.coins) { message.coins.push(e); } } + message.snacks = []; if (object.snacks !== undefined && object.snacks !== null) { for (const e of object.snacks) { message.snacks.push(e); } } + message.oldStates = []; if (object.oldStates !== undefined && object.oldStates !== null) { for (const e of object.oldStates) { message.oldStates.push(e); @@ -547,16 +531,8 @@ export const Child = { fromPartial(object: DeepPartial): Child { const message = { ...baseChild } as Child; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = ''; - } - if (object.type !== undefined && object.type !== null) { - message.type = object.type; - } else { - message.type = 0; - } + message.name = object.name ?? ''; + message.type = object.type ?? 0; return message; }, }; @@ -632,21 +608,13 @@ export const Nested = { fromPartial(object: DeepPartial): Nested { const message = { ...baseNested } as Nested; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = ''; - } + message.name = object.name ?? ''; if (object.message !== undefined && object.message !== null) { message.message = Nested_InnerMessage.fromPartial(object.message); } else { message.message = undefined; } - if (object.state !== undefined && object.state !== null) { - message.state = object.state; - } else { - message.state = 0; - } + message.state = object.state ?? 0; return message; }, }; @@ -710,11 +678,7 @@ export const Nested_InnerMessage = { fromPartial(object: DeepPartial): Nested_InnerMessage { const message = { ...baseNested_InnerMessage } as Nested_InnerMessage; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = ''; - } + message.name = object.name ?? ''; if (object.deep !== undefined && object.deep !== null) { message.deep = Nested_InnerMessage_DeepMessage.fromPartial(object.deep); } else { @@ -770,11 +734,7 @@ export const Nested_InnerMessage_DeepMessage = { fromPartial(object: DeepPartial): Nested_InnerMessage_DeepMessage { const message = { ...baseNested_InnerMessage_DeepMessage } as Nested_InnerMessage_DeepMessage; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = ''; - } + message.name = object.name ?? ''; return message; }, }; @@ -837,16 +797,8 @@ export const OneOfMessage = { fromPartial(object: DeepPartial): OneOfMessage { const message = { ...baseOneOfMessage } as OneOfMessage; - if (object.first !== undefined && object.first !== null) { - message.first = object.first; - } else { - message.first = undefined; - } - if (object.last !== undefined && object.last !== null) { - message.last = object.last; - } else { - message.last = undefined; - } + message.first = object.first ?? undefined; + message.last = object.last ?? undefined; return message; }, }; @@ -957,28 +909,16 @@ export const SimpleWithWrappers = { fromPartial(object: DeepPartial): SimpleWithWrappers { const message = { ...baseSimpleWithWrappers } as SimpleWithWrappers; + message.name = object.name ?? undefined; + message.age = object.age ?? undefined; + message.enabled = object.enabled ?? undefined; message.coins = []; - message.snacks = []; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = undefined; - } - if (object.age !== undefined && object.age !== null) { - message.age = object.age; - } else { - message.age = undefined; - } - if (object.enabled !== undefined && object.enabled !== null) { - message.enabled = object.enabled; - } else { - message.enabled = undefined; - } if (object.coins !== undefined && object.coins !== null) { for (const e of object.coins) { message.coins.push(e); } } + message.snacks = []; if (object.snacks !== undefined && object.snacks !== null) { for (const e of object.snacks) { message.snacks.push(e); @@ -1034,11 +974,7 @@ export const Entity = { fromPartial(object: DeepPartial): Entity { const message = { ...baseEntity } as Entity; - if (object.id !== undefined && object.id !== null) { - message.id = object.id; - } else { - message.id = 0; - } + message.id = object.id ?? 0; return message; }, }; @@ -1144,8 +1080,6 @@ export const SimpleWithMap = { fromPartial(object: DeepPartial): SimpleWithMap { const message = { ...baseSimpleWithMap } as SimpleWithMap; message.entitiesById = {}; - message.nameLookup = {}; - message.intLookup = {}; if (object.entitiesById !== undefined && object.entitiesById !== null) { Object.entries(object.entitiesById).forEach(([key, value]) => { if (value !== undefined) { @@ -1153,6 +1087,7 @@ export const SimpleWithMap = { } }); } + message.nameLookup = {}; if (object.nameLookup !== undefined && object.nameLookup !== null) { Object.entries(object.nameLookup).forEach(([key, value]) => { if (value !== undefined) { @@ -1160,6 +1095,7 @@ export const SimpleWithMap = { } }); } + message.intLookup = {}; if (object.intLookup !== undefined && object.intLookup !== null) { Object.entries(object.intLookup).forEach(([key, value]) => { if (value !== undefined) { @@ -1229,11 +1165,7 @@ export const SimpleWithMap_EntitiesByIdEntry = { fromPartial(object: DeepPartial): SimpleWithMap_EntitiesByIdEntry { const message = { ...baseSimpleWithMap_EntitiesByIdEntry } as SimpleWithMap_EntitiesByIdEntry; - if (object.key !== undefined && object.key !== null) { - message.key = object.key; - } else { - message.key = 0; - } + message.key = object.key ?? 0; if (object.value !== undefined && object.value !== null) { message.value = Entity.fromPartial(object.value); } else { @@ -1301,16 +1233,8 @@ export const SimpleWithMap_NameLookupEntry = { fromPartial(object: DeepPartial): SimpleWithMap_NameLookupEntry { const message = { ...baseSimpleWithMap_NameLookupEntry } as SimpleWithMap_NameLookupEntry; - if (object.key !== undefined && object.key !== null) { - message.key = object.key; - } else { - message.key = ''; - } - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = ''; - } + message.key = object.key ?? ''; + message.value = object.value ?? ''; return message; }, }; @@ -1373,16 +1297,8 @@ export const SimpleWithMap_IntLookupEntry = { fromPartial(object: DeepPartial): SimpleWithMap_IntLookupEntry { const message = { ...baseSimpleWithMap_IntLookupEntry } as SimpleWithMap_IntLookupEntry; - if (object.key !== undefined && object.key !== null) { - message.key = object.key; - } else { - message.key = 0; - } - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.key = object.key ?? 0; + message.value = object.value ?? 0; return message; }, }; @@ -1513,11 +1429,7 @@ export const SimpleWithSnakeCaseMap_EntitiesByIdEntry = { fromPartial(object: DeepPartial): SimpleWithSnakeCaseMap_EntitiesByIdEntry { const message = { ...baseSimpleWithSnakeCaseMap_EntitiesByIdEntry } as SimpleWithSnakeCaseMap_EntitiesByIdEntry; - if (object.key !== undefined && object.key !== null) { - message.key = object.key; - } else { - message.key = 0; - } + message.key = object.key ?? 0; if (object.value !== undefined && object.value !== null) { message.value = Entity.fromPartial(object.value); } else { @@ -1573,11 +1485,7 @@ export const PingRequest = { fromPartial(object: DeepPartial): PingRequest { const message = { ...basePingRequest } as PingRequest; - if (object.input !== undefined && object.input !== null) { - message.input = object.input; - } else { - message.input = ''; - } + message.input = object.input ?? ''; return message; }, }; @@ -1628,11 +1536,7 @@ export const PingResponse = { fromPartial(object: DeepPartial): PingResponse { const message = { ...basePingResponse } as PingResponse; - if (object.output !== undefined && object.output !== null) { - message.output = object.output; - } else { - message.output = ''; - } + message.output = object.output ?? ''; return message; }, }; @@ -1828,66 +1732,18 @@ export const Numbers = { fromPartial(object: DeepPartial): Numbers { const message = { ...baseNumbers } as Numbers; - if (object.double !== undefined && object.double !== null) { - message.double = object.double; - } else { - message.double = 0; - } - if (object.float !== undefined && object.float !== null) { - message.float = object.float; - } else { - message.float = 0; - } - if (object.int32 !== undefined && object.int32 !== null) { - message.int32 = object.int32; - } else { - message.int32 = 0; - } - if (object.int64 !== undefined && object.int64 !== null) { - message.int64 = object.int64; - } else { - message.int64 = 0; - } - if (object.uint32 !== undefined && object.uint32 !== null) { - message.uint32 = object.uint32; - } else { - message.uint32 = 0; - } - if (object.uint64 !== undefined && object.uint64 !== null) { - message.uint64 = object.uint64; - } else { - message.uint64 = 0; - } - if (object.sint32 !== undefined && object.sint32 !== null) { - message.sint32 = object.sint32; - } else { - message.sint32 = 0; - } - if (object.sint64 !== undefined && object.sint64 !== null) { - message.sint64 = object.sint64; - } else { - message.sint64 = 0; - } - if (object.fixed32 !== undefined && object.fixed32 !== null) { - message.fixed32 = object.fixed32; - } else { - message.fixed32 = 0; - } - if (object.fixed64 !== undefined && object.fixed64 !== null) { - message.fixed64 = object.fixed64; - } else { - message.fixed64 = 0; - } - if (object.sfixed32 !== undefined && object.sfixed32 !== null) { - message.sfixed32 = object.sfixed32; - } else { - message.sfixed32 = 0; - } - if (object.sfixed64 !== undefined && object.sfixed64 !== null) { - message.sfixed64 = object.sfixed64; - } else { - message.sfixed64 = 0; - } + message.double = object.double ?? 0; + message.float = object.float ?? 0; + message.int32 = object.int32 ?? 0; + message.int64 = object.int64 ?? 0; + message.uint32 = object.uint32 ?? 0; + message.uint64 = object.uint64 ?? 0; + message.sint32 = object.sint32 ?? 0; + message.sint64 = object.sint64 ?? 0; + message.fixed32 = object.fixed32 ?? 0; + message.fixed64 = object.fixed64 ?? 0; + message.sfixed32 = object.sfixed32 ?? 0; + message.sfixed64 = object.sfixed64 ?? 0; return message; }, }; diff --git a/integration/simple-optionals/thing.ts b/integration/simple-optionals/thing.ts index 609e9bc3e..4ae43b0b7 100644 --- a/integration/simple-optionals/thing.ts +++ b/integration/simple-optionals/thing.ts @@ -55,11 +55,7 @@ export const ImportedThing = { fromPartial(object: DeepPartial): ImportedThing { const message = { ...baseImportedThing } as ImportedThing; - if (object.createdAt !== undefined && object.createdAt !== null) { - message.createdAt = object.createdAt; - } else { - message.createdAt = undefined; - } + message.createdAt = object.createdAt ?? undefined; return message; }, }; diff --git a/integration/simple-proto2/simple.ts b/integration/simple-proto2/simple.ts index 3e56567d4..6a675359c 100644 --- a/integration/simple-proto2/simple.ts +++ b/integration/simple-proto2/simple.ts @@ -86,11 +86,7 @@ export const Issue56 = { fromPartial(object: DeepPartial): Issue56 { const message = { ...baseIssue56 } as Issue56; - if (object.test !== undefined && object.test !== null) { - message.test = object.test; - } else { - message.test = 1; - } + message.test = object.test ?? 1; return message; }, }; diff --git a/integration/simple-snake/google/protobuf/timestamp.ts b/integration/simple-snake/google/protobuf/timestamp.ts index 52ea73adb..89f9a4fdb 100644 --- a/integration/simple-snake/google/protobuf/timestamp.ts +++ b/integration/simple-snake/google/protobuf/timestamp.ts @@ -162,16 +162,8 @@ export const Timestamp = { fromPartial(object: DeepPartial): Timestamp { const message = { ...baseTimestamp } as Timestamp; - if (object.seconds !== undefined && object.seconds !== null) { - message.seconds = object.seconds; - } else { - message.seconds = 0; - } - if (object.nanos !== undefined && object.nanos !== null) { - message.nanos = object.nanos; - } else { - message.nanos = 0; - } + message.seconds = object.seconds ?? 0; + message.nanos = object.nanos ?? 0; return message; }, }; diff --git a/integration/simple-snake/google/protobuf/wrappers.ts b/integration/simple-snake/google/protobuf/wrappers.ts index 92da3e0a8..5f52d3fe7 100644 --- a/integration/simple-snake/google/protobuf/wrappers.ts +++ b/integration/simple-snake/google/protobuf/wrappers.ts @@ -140,11 +140,7 @@ export const DoubleValue = { fromPartial(object: DeepPartial): DoubleValue { const message = { ...baseDoubleValue } as DoubleValue; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.value = object.value ?? 0; return message; }, }; @@ -195,11 +191,7 @@ export const FloatValue = { fromPartial(object: DeepPartial): FloatValue { const message = { ...baseFloatValue } as FloatValue; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.value = object.value ?? 0; return message; }, }; @@ -250,11 +242,7 @@ export const Int64Value = { fromPartial(object: DeepPartial): Int64Value { const message = { ...baseInt64Value } as Int64Value; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.value = object.value ?? 0; return message; }, }; @@ -305,11 +293,7 @@ export const UInt64Value = { fromPartial(object: DeepPartial): UInt64Value { const message = { ...baseUInt64Value } as UInt64Value; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.value = object.value ?? 0; return message; }, }; @@ -360,11 +344,7 @@ export const Int32Value = { fromPartial(object: DeepPartial): Int32Value { const message = { ...baseInt32Value } as Int32Value; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.value = object.value ?? 0; return message; }, }; @@ -415,11 +395,7 @@ export const UInt32Value = { fromPartial(object: DeepPartial): UInt32Value { const message = { ...baseUInt32Value } as UInt32Value; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.value = object.value ?? 0; return message; }, }; @@ -470,11 +446,7 @@ export const BoolValue = { fromPartial(object: DeepPartial): BoolValue { const message = { ...baseBoolValue } as BoolValue; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = false; - } + message.value = object.value ?? false; return message; }, }; @@ -525,11 +497,7 @@ export const StringValue = { fromPartial(object: DeepPartial): StringValue { const message = { ...baseStringValue } as StringValue; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = ''; - } + message.value = object.value ?? ''; return message; }, }; @@ -581,11 +549,7 @@ export const BytesValue = { fromPartial(object: DeepPartial): BytesValue { const message = { ...baseBytesValue } as BytesValue; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = new Uint8Array(); - } + message.value = object.value ?? new Uint8Array(); return message; }, }; diff --git a/integration/simple-snake/import_dir/thing.ts b/integration/simple-snake/import_dir/thing.ts index 050b04f5a..e482a30b8 100644 --- a/integration/simple-snake/import_dir/thing.ts +++ b/integration/simple-snake/import_dir/thing.ts @@ -55,11 +55,7 @@ export const ImportedThing = { fromPartial(object: DeepPartial): ImportedThing { const message = { ...baseImportedThing } as ImportedThing; - if (object.created_at !== undefined && object.created_at !== null) { - message.created_at = object.created_at; - } else { - message.created_at = undefined; - } + message.created_at = object.created_at ?? undefined; return message; }, }; diff --git a/integration/simple-snake/simple.ts b/integration/simple-snake/simple.ts index a1c7a5cb4..8db58bdea 100644 --- a/integration/simple-snake/simple.ts +++ b/integration/simple-snake/simple.ts @@ -431,50 +431,34 @@ export const Simple = { fromPartial(object: DeepPartial): Simple { const message = { ...baseSimple } as Simple; - message.grand_children = []; - message.coins = []; - message.snacks = []; - message.old_states = []; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = ''; - } - if (object.age !== undefined && object.age !== null) { - message.age = object.age; - } else { - message.age = 0; - } - if (object.created_at !== undefined && object.created_at !== null) { - message.created_at = object.created_at; - } else { - message.created_at = undefined; - } + message.name = object.name ?? ''; + message.age = object.age ?? 0; + message.created_at = object.created_at ?? undefined; if (object.child !== undefined && object.child !== null) { message.child = Child.fromPartial(object.child); } else { message.child = undefined; } - if (object.state !== undefined && object.state !== null) { - message.state = object.state; - } else { - message.state = 0; - } + message.state = object.state ?? 0; + message.grand_children = []; if (object.grand_children !== undefined && object.grand_children !== null) { for (const e of object.grand_children) { message.grand_children.push(Child.fromPartial(e)); } } + message.coins = []; if (object.coins !== undefined && object.coins !== null) { for (const e of object.coins) { message.coins.push(e); } } + message.snacks = []; if (object.snacks !== undefined && object.snacks !== null) { for (const e of object.snacks) { message.snacks.push(e); } } + message.old_states = []; if (object.old_states !== undefined && object.old_states !== null) { for (const e of object.old_states) { message.old_states.push(e); @@ -547,16 +531,8 @@ export const Child = { fromPartial(object: DeepPartial): Child { const message = { ...baseChild } as Child; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = ''; - } - if (object.type !== undefined && object.type !== null) { - message.type = object.type; - } else { - message.type = 0; - } + message.name = object.name ?? ''; + message.type = object.type ?? 0; return message; }, }; @@ -632,21 +608,13 @@ export const Nested = { fromPartial(object: DeepPartial): Nested { const message = { ...baseNested } as Nested; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = ''; - } + message.name = object.name ?? ''; if (object.message !== undefined && object.message !== null) { message.message = Nested_InnerMessage.fromPartial(object.message); } else { message.message = undefined; } - if (object.state !== undefined && object.state !== null) { - message.state = object.state; - } else { - message.state = 0; - } + message.state = object.state ?? 0; return message; }, }; @@ -710,11 +678,7 @@ export const Nested_InnerMessage = { fromPartial(object: DeepPartial): Nested_InnerMessage { const message = { ...baseNested_InnerMessage } as Nested_InnerMessage; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = ''; - } + message.name = object.name ?? ''; if (object.deep !== undefined && object.deep !== null) { message.deep = Nested_InnerMessage_DeepMessage.fromPartial(object.deep); } else { @@ -770,11 +734,7 @@ export const Nested_InnerMessage_DeepMessage = { fromPartial(object: DeepPartial): Nested_InnerMessage_DeepMessage { const message = { ...baseNested_InnerMessage_DeepMessage } as Nested_InnerMessage_DeepMessage; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = ''; - } + message.name = object.name ?? ''; return message; }, }; @@ -837,16 +797,8 @@ export const OneOfMessage = { fromPartial(object: DeepPartial): OneOfMessage { const message = { ...baseOneOfMessage } as OneOfMessage; - if (object.first !== undefined && object.first !== null) { - message.first = object.first; - } else { - message.first = undefined; - } - if (object.last !== undefined && object.last !== null) { - message.last = object.last; - } else { - message.last = undefined; - } + message.first = object.first ?? undefined; + message.last = object.last ?? undefined; return message; }, }; @@ -957,28 +909,16 @@ export const SimpleWithWrappers = { fromPartial(object: DeepPartial): SimpleWithWrappers { const message = { ...baseSimpleWithWrappers } as SimpleWithWrappers; + message.name = object.name ?? undefined; + message.age = object.age ?? undefined; + message.enabled = object.enabled ?? undefined; message.coins = []; - message.snacks = []; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = undefined; - } - if (object.age !== undefined && object.age !== null) { - message.age = object.age; - } else { - message.age = undefined; - } - if (object.enabled !== undefined && object.enabled !== null) { - message.enabled = object.enabled; - } else { - message.enabled = undefined; - } if (object.coins !== undefined && object.coins !== null) { for (const e of object.coins) { message.coins.push(e); } } + message.snacks = []; if (object.snacks !== undefined && object.snacks !== null) { for (const e of object.snacks) { message.snacks.push(e); @@ -1034,11 +974,7 @@ export const Entity = { fromPartial(object: DeepPartial): Entity { const message = { ...baseEntity } as Entity; - if (object.id !== undefined && object.id !== null) { - message.id = object.id; - } else { - message.id = 0; - } + message.id = object.id ?? 0; return message; }, }; @@ -1144,8 +1080,6 @@ export const SimpleWithMap = { fromPartial(object: DeepPartial): SimpleWithMap { const message = { ...baseSimpleWithMap } as SimpleWithMap; message.entitiesById = {}; - message.nameLookup = {}; - message.intLookup = {}; if (object.entitiesById !== undefined && object.entitiesById !== null) { Object.entries(object.entitiesById).forEach(([key, value]) => { if (value !== undefined) { @@ -1153,6 +1087,7 @@ export const SimpleWithMap = { } }); } + message.nameLookup = {}; if (object.nameLookup !== undefined && object.nameLookup !== null) { Object.entries(object.nameLookup).forEach(([key, value]) => { if (value !== undefined) { @@ -1160,6 +1095,7 @@ export const SimpleWithMap = { } }); } + message.intLookup = {}; if (object.intLookup !== undefined && object.intLookup !== null) { Object.entries(object.intLookup).forEach(([key, value]) => { if (value !== undefined) { @@ -1229,11 +1165,7 @@ export const SimpleWithMap_EntitiesByIdEntry = { fromPartial(object: DeepPartial): SimpleWithMap_EntitiesByIdEntry { const message = { ...baseSimpleWithMap_EntitiesByIdEntry } as SimpleWithMap_EntitiesByIdEntry; - if (object.key !== undefined && object.key !== null) { - message.key = object.key; - } else { - message.key = 0; - } + message.key = object.key ?? 0; if (object.value !== undefined && object.value !== null) { message.value = Entity.fromPartial(object.value); } else { @@ -1301,16 +1233,8 @@ export const SimpleWithMap_NameLookupEntry = { fromPartial(object: DeepPartial): SimpleWithMap_NameLookupEntry { const message = { ...baseSimpleWithMap_NameLookupEntry } as SimpleWithMap_NameLookupEntry; - if (object.key !== undefined && object.key !== null) { - message.key = object.key; - } else { - message.key = ''; - } - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = ''; - } + message.key = object.key ?? ''; + message.value = object.value ?? ''; return message; }, }; @@ -1373,16 +1297,8 @@ export const SimpleWithMap_IntLookupEntry = { fromPartial(object: DeepPartial): SimpleWithMap_IntLookupEntry { const message = { ...baseSimpleWithMap_IntLookupEntry } as SimpleWithMap_IntLookupEntry; - if (object.key !== undefined && object.key !== null) { - message.key = object.key; - } else { - message.key = 0; - } - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.key = object.key ?? 0; + message.value = object.value ?? 0; return message; }, }; @@ -1513,11 +1429,7 @@ export const SimpleWithSnakeCaseMap_EntitiesByIdEntry = { fromPartial(object: DeepPartial): SimpleWithSnakeCaseMap_EntitiesByIdEntry { const message = { ...baseSimpleWithSnakeCaseMap_EntitiesByIdEntry } as SimpleWithSnakeCaseMap_EntitiesByIdEntry; - if (object.key !== undefined && object.key !== null) { - message.key = object.key; - } else { - message.key = 0; - } + message.key = object.key ?? 0; if (object.value !== undefined && object.value !== null) { message.value = Entity.fromPartial(object.value); } else { @@ -1573,11 +1485,7 @@ export const PingRequest = { fromPartial(object: DeepPartial): PingRequest { const message = { ...basePingRequest } as PingRequest; - if (object.input !== undefined && object.input !== null) { - message.input = object.input; - } else { - message.input = ''; - } + message.input = object.input ?? ''; return message; }, }; @@ -1628,11 +1536,7 @@ export const PingResponse = { fromPartial(object: DeepPartial): PingResponse { const message = { ...basePingResponse } as PingResponse; - if (object.output !== undefined && object.output !== null) { - message.output = object.output; - } else { - message.output = ''; - } + message.output = object.output ?? ''; return message; }, }; @@ -1828,66 +1732,18 @@ export const Numbers = { fromPartial(object: DeepPartial): Numbers { const message = { ...baseNumbers } as Numbers; - if (object.double !== undefined && object.double !== null) { - message.double = object.double; - } else { - message.double = 0; - } - if (object.float !== undefined && object.float !== null) { - message.float = object.float; - } else { - message.float = 0; - } - if (object.int32 !== undefined && object.int32 !== null) { - message.int32 = object.int32; - } else { - message.int32 = 0; - } - if (object.int64 !== undefined && object.int64 !== null) { - message.int64 = object.int64; - } else { - message.int64 = 0; - } - if (object.uint32 !== undefined && object.uint32 !== null) { - message.uint32 = object.uint32; - } else { - message.uint32 = 0; - } - if (object.uint64 !== undefined && object.uint64 !== null) { - message.uint64 = object.uint64; - } else { - message.uint64 = 0; - } - if (object.sint32 !== undefined && object.sint32 !== null) { - message.sint32 = object.sint32; - } else { - message.sint32 = 0; - } - if (object.sint64 !== undefined && object.sint64 !== null) { - message.sint64 = object.sint64; - } else { - message.sint64 = 0; - } - if (object.fixed32 !== undefined && object.fixed32 !== null) { - message.fixed32 = object.fixed32; - } else { - message.fixed32 = 0; - } - if (object.fixed64 !== undefined && object.fixed64 !== null) { - message.fixed64 = object.fixed64; - } else { - message.fixed64 = 0; - } - if (object.sfixed32 !== undefined && object.sfixed32 !== null) { - message.sfixed32 = object.sfixed32; - } else { - message.sfixed32 = 0; - } - if (object.sfixed64 !== undefined && object.sfixed64 !== null) { - message.sfixed64 = object.sfixed64; - } else { - message.sfixed64 = 0; - } + message.double = object.double ?? 0; + message.float = object.float ?? 0; + message.int32 = object.int32 ?? 0; + message.int64 = object.int64 ?? 0; + message.uint32 = object.uint32 ?? 0; + message.uint64 = object.uint64 ?? 0; + message.sint32 = object.sint32 ?? 0; + message.sint64 = object.sint64 ?? 0; + message.fixed32 = object.fixed32 ?? 0; + message.fixed64 = object.fixed64 ?? 0; + message.sfixed32 = object.sfixed32 ?? 0; + message.sfixed64 = object.sfixed64 ?? 0; return message; }, }; diff --git a/integration/simple-string-enums/simple.ts b/integration/simple-string-enums/simple.ts index 8a1d9dcf5..beed80d91 100644 --- a/integration/simple-string-enums/simple.ts +++ b/integration/simple-string-enums/simple.ts @@ -146,17 +146,9 @@ export const Simple = { fromPartial(object: DeepPartial): Simple { const message = { ...baseSimple } as Simple; + message.name = object.name ?? ''; + message.state = object.state ?? StateEnum.UNKNOWN; message.states = []; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = ''; - } - if (object.state !== undefined && object.state !== null) { - message.state = object.state; - } else { - message.state = StateEnum.UNKNOWN; - } if (object.states !== undefined && object.states !== null) { for (const e of object.states) { message.states.push(e); diff --git a/integration/simple-unrecognized-enum/google/protobuf/timestamp.ts b/integration/simple-unrecognized-enum/google/protobuf/timestamp.ts index 52ea73adb..89f9a4fdb 100644 --- a/integration/simple-unrecognized-enum/google/protobuf/timestamp.ts +++ b/integration/simple-unrecognized-enum/google/protobuf/timestamp.ts @@ -162,16 +162,8 @@ export const Timestamp = { fromPartial(object: DeepPartial): Timestamp { const message = { ...baseTimestamp } as Timestamp; - if (object.seconds !== undefined && object.seconds !== null) { - message.seconds = object.seconds; - } else { - message.seconds = 0; - } - if (object.nanos !== undefined && object.nanos !== null) { - message.nanos = object.nanos; - } else { - message.nanos = 0; - } + message.seconds = object.seconds ?? 0; + message.nanos = object.nanos ?? 0; return message; }, }; diff --git a/integration/simple-unrecognized-enum/google/protobuf/wrappers.ts b/integration/simple-unrecognized-enum/google/protobuf/wrappers.ts index 92da3e0a8..5f52d3fe7 100644 --- a/integration/simple-unrecognized-enum/google/protobuf/wrappers.ts +++ b/integration/simple-unrecognized-enum/google/protobuf/wrappers.ts @@ -140,11 +140,7 @@ export const DoubleValue = { fromPartial(object: DeepPartial): DoubleValue { const message = { ...baseDoubleValue } as DoubleValue; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.value = object.value ?? 0; return message; }, }; @@ -195,11 +191,7 @@ export const FloatValue = { fromPartial(object: DeepPartial): FloatValue { const message = { ...baseFloatValue } as FloatValue; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.value = object.value ?? 0; return message; }, }; @@ -250,11 +242,7 @@ export const Int64Value = { fromPartial(object: DeepPartial): Int64Value { const message = { ...baseInt64Value } as Int64Value; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.value = object.value ?? 0; return message; }, }; @@ -305,11 +293,7 @@ export const UInt64Value = { fromPartial(object: DeepPartial): UInt64Value { const message = { ...baseUInt64Value } as UInt64Value; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.value = object.value ?? 0; return message; }, }; @@ -360,11 +344,7 @@ export const Int32Value = { fromPartial(object: DeepPartial): Int32Value { const message = { ...baseInt32Value } as Int32Value; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.value = object.value ?? 0; return message; }, }; @@ -415,11 +395,7 @@ export const UInt32Value = { fromPartial(object: DeepPartial): UInt32Value { const message = { ...baseUInt32Value } as UInt32Value; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.value = object.value ?? 0; return message; }, }; @@ -470,11 +446,7 @@ export const BoolValue = { fromPartial(object: DeepPartial): BoolValue { const message = { ...baseBoolValue } as BoolValue; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = false; - } + message.value = object.value ?? false; return message; }, }; @@ -525,11 +497,7 @@ export const StringValue = { fromPartial(object: DeepPartial): StringValue { const message = { ...baseStringValue } as StringValue; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = ''; - } + message.value = object.value ?? ''; return message; }, }; @@ -581,11 +549,7 @@ export const BytesValue = { fromPartial(object: DeepPartial): BytesValue { const message = { ...baseBytesValue } as BytesValue; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = new Uint8Array(); - } + message.value = object.value ?? new Uint8Array(); return message; }, }; diff --git a/integration/simple-unrecognized-enum/import_dir/thing.ts b/integration/simple-unrecognized-enum/import_dir/thing.ts index 9c777434d..5ba6da998 100644 --- a/integration/simple-unrecognized-enum/import_dir/thing.ts +++ b/integration/simple-unrecognized-enum/import_dir/thing.ts @@ -55,11 +55,7 @@ export const ImportedThing = { fromPartial(object: DeepPartial): ImportedThing { const message = { ...baseImportedThing } as ImportedThing; - if (object.createdAt !== undefined && object.createdAt !== null) { - message.createdAt = object.createdAt; - } else { - message.createdAt = undefined; - } + message.createdAt = object.createdAt ?? undefined; return message; }, }; diff --git a/integration/simple-unrecognized-enum/simple.ts b/integration/simple-unrecognized-enum/simple.ts index 58cf9a554..bd45e247a 100644 --- a/integration/simple-unrecognized-enum/simple.ts +++ b/integration/simple-unrecognized-enum/simple.ts @@ -422,50 +422,34 @@ export const Simple = { fromPartial(object: DeepPartial): Simple { const message = { ...baseSimple } as Simple; - message.grandChildren = []; - message.coins = []; - message.snacks = []; - message.oldStates = []; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = ''; - } - if (object.age !== undefined && object.age !== null) { - message.age = object.age; - } else { - message.age = 0; - } - if (object.createdAt !== undefined && object.createdAt !== null) { - message.createdAt = object.createdAt; - } else { - message.createdAt = undefined; - } + message.name = object.name ?? ''; + message.age = object.age ?? 0; + message.createdAt = object.createdAt ?? undefined; if (object.child !== undefined && object.child !== null) { message.child = Child.fromPartial(object.child); } else { message.child = undefined; } - if (object.state !== undefined && object.state !== null) { - message.state = object.state; - } else { - message.state = 0; - } + message.state = object.state ?? 0; + message.grandChildren = []; if (object.grandChildren !== undefined && object.grandChildren !== null) { for (const e of object.grandChildren) { message.grandChildren.push(Child.fromPartial(e)); } } + message.coins = []; if (object.coins !== undefined && object.coins !== null) { for (const e of object.coins) { message.coins.push(e); } } + message.snacks = []; if (object.snacks !== undefined && object.snacks !== null) { for (const e of object.snacks) { message.snacks.push(e); } } + message.oldStates = []; if (object.oldStates !== undefined && object.oldStates !== null) { for (const e of object.oldStates) { message.oldStates.push(e); @@ -538,16 +522,8 @@ export const Child = { fromPartial(object: DeepPartial): Child { const message = { ...baseChild } as Child; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = ''; - } - if (object.type !== undefined && object.type !== null) { - message.type = object.type; - } else { - message.type = 0; - } + message.name = object.name ?? ''; + message.type = object.type ?? 0; return message; }, }; @@ -623,21 +599,13 @@ export const Nested = { fromPartial(object: DeepPartial): Nested { const message = { ...baseNested } as Nested; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = ''; - } + message.name = object.name ?? ''; if (object.message !== undefined && object.message !== null) { message.message = Nested_InnerMessage.fromPartial(object.message); } else { message.message = undefined; } - if (object.state !== undefined && object.state !== null) { - message.state = object.state; - } else { - message.state = 0; - } + message.state = object.state ?? 0; return message; }, }; @@ -701,11 +669,7 @@ export const Nested_InnerMessage = { fromPartial(object: DeepPartial): Nested_InnerMessage { const message = { ...baseNested_InnerMessage } as Nested_InnerMessage; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = ''; - } + message.name = object.name ?? ''; if (object.deep !== undefined && object.deep !== null) { message.deep = Nested_InnerMessage_DeepMessage.fromPartial(object.deep); } else { @@ -761,11 +725,7 @@ export const Nested_InnerMessage_DeepMessage = { fromPartial(object: DeepPartial): Nested_InnerMessage_DeepMessage { const message = { ...baseNested_InnerMessage_DeepMessage } as Nested_InnerMessage_DeepMessage; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = ''; - } + message.name = object.name ?? ''; return message; }, }; @@ -828,16 +788,8 @@ export const OneOfMessage = { fromPartial(object: DeepPartial): OneOfMessage { const message = { ...baseOneOfMessage } as OneOfMessage; - if (object.first !== undefined && object.first !== null) { - message.first = object.first; - } else { - message.first = undefined; - } - if (object.last !== undefined && object.last !== null) { - message.last = object.last; - } else { - message.last = undefined; - } + message.first = object.first ?? undefined; + message.last = object.last ?? undefined; return message; }, }; @@ -948,28 +900,16 @@ export const SimpleWithWrappers = { fromPartial(object: DeepPartial): SimpleWithWrappers { const message = { ...baseSimpleWithWrappers } as SimpleWithWrappers; + message.name = object.name ?? undefined; + message.age = object.age ?? undefined; + message.enabled = object.enabled ?? undefined; message.coins = []; - message.snacks = []; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = undefined; - } - if (object.age !== undefined && object.age !== null) { - message.age = object.age; - } else { - message.age = undefined; - } - if (object.enabled !== undefined && object.enabled !== null) { - message.enabled = object.enabled; - } else { - message.enabled = undefined; - } if (object.coins !== undefined && object.coins !== null) { for (const e of object.coins) { message.coins.push(e); } } + message.snacks = []; if (object.snacks !== undefined && object.snacks !== null) { for (const e of object.snacks) { message.snacks.push(e); @@ -1025,11 +965,7 @@ export const Entity = { fromPartial(object: DeepPartial): Entity { const message = { ...baseEntity } as Entity; - if (object.id !== undefined && object.id !== null) { - message.id = object.id; - } else { - message.id = 0; - } + message.id = object.id ?? 0; return message; }, }; @@ -1135,8 +1071,6 @@ export const SimpleWithMap = { fromPartial(object: DeepPartial): SimpleWithMap { const message = { ...baseSimpleWithMap } as SimpleWithMap; message.entitiesById = {}; - message.nameLookup = {}; - message.intLookup = {}; if (object.entitiesById !== undefined && object.entitiesById !== null) { Object.entries(object.entitiesById).forEach(([key, value]) => { if (value !== undefined) { @@ -1144,6 +1078,7 @@ export const SimpleWithMap = { } }); } + message.nameLookup = {}; if (object.nameLookup !== undefined && object.nameLookup !== null) { Object.entries(object.nameLookup).forEach(([key, value]) => { if (value !== undefined) { @@ -1151,6 +1086,7 @@ export const SimpleWithMap = { } }); } + message.intLookup = {}; if (object.intLookup !== undefined && object.intLookup !== null) { Object.entries(object.intLookup).forEach(([key, value]) => { if (value !== undefined) { @@ -1220,11 +1156,7 @@ export const SimpleWithMap_EntitiesByIdEntry = { fromPartial(object: DeepPartial): SimpleWithMap_EntitiesByIdEntry { const message = { ...baseSimpleWithMap_EntitiesByIdEntry } as SimpleWithMap_EntitiesByIdEntry; - if (object.key !== undefined && object.key !== null) { - message.key = object.key; - } else { - message.key = 0; - } + message.key = object.key ?? 0; if (object.value !== undefined && object.value !== null) { message.value = Entity.fromPartial(object.value); } else { @@ -1292,16 +1224,8 @@ export const SimpleWithMap_NameLookupEntry = { fromPartial(object: DeepPartial): SimpleWithMap_NameLookupEntry { const message = { ...baseSimpleWithMap_NameLookupEntry } as SimpleWithMap_NameLookupEntry; - if (object.key !== undefined && object.key !== null) { - message.key = object.key; - } else { - message.key = ''; - } - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = ''; - } + message.key = object.key ?? ''; + message.value = object.value ?? ''; return message; }, }; @@ -1364,16 +1288,8 @@ export const SimpleWithMap_IntLookupEntry = { fromPartial(object: DeepPartial): SimpleWithMap_IntLookupEntry { const message = { ...baseSimpleWithMap_IntLookupEntry } as SimpleWithMap_IntLookupEntry; - if (object.key !== undefined && object.key !== null) { - message.key = object.key; - } else { - message.key = 0; - } - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.key = object.key ?? 0; + message.value = object.value ?? 0; return message; }, }; @@ -1504,11 +1420,7 @@ export const SimpleWithSnakeCaseMap_EntitiesByIdEntry = { fromPartial(object: DeepPartial): SimpleWithSnakeCaseMap_EntitiesByIdEntry { const message = { ...baseSimpleWithSnakeCaseMap_EntitiesByIdEntry } as SimpleWithSnakeCaseMap_EntitiesByIdEntry; - if (object.key !== undefined && object.key !== null) { - message.key = object.key; - } else { - message.key = 0; - } + message.key = object.key ?? 0; if (object.value !== undefined && object.value !== null) { message.value = Entity.fromPartial(object.value); } else { @@ -1564,11 +1476,7 @@ export const PingRequest = { fromPartial(object: DeepPartial): PingRequest { const message = { ...basePingRequest } as PingRequest; - if (object.input !== undefined && object.input !== null) { - message.input = object.input; - } else { - message.input = ''; - } + message.input = object.input ?? ''; return message; }, }; @@ -1619,11 +1527,7 @@ export const PingResponse = { fromPartial(object: DeepPartial): PingResponse { const message = { ...basePingResponse } as PingResponse; - if (object.output !== undefined && object.output !== null) { - message.output = object.output; - } else { - message.output = ''; - } + message.output = object.output ?? ''; return message; }, }; @@ -1819,66 +1723,18 @@ export const Numbers = { fromPartial(object: DeepPartial): Numbers { const message = { ...baseNumbers } as Numbers; - if (object.double !== undefined && object.double !== null) { - message.double = object.double; - } else { - message.double = 0; - } - if (object.float !== undefined && object.float !== null) { - message.float = object.float; - } else { - message.float = 0; - } - if (object.int32 !== undefined && object.int32 !== null) { - message.int32 = object.int32; - } else { - message.int32 = 0; - } - if (object.int64 !== undefined && object.int64 !== null) { - message.int64 = object.int64; - } else { - message.int64 = 0; - } - if (object.uint32 !== undefined && object.uint32 !== null) { - message.uint32 = object.uint32; - } else { - message.uint32 = 0; - } - if (object.uint64 !== undefined && object.uint64 !== null) { - message.uint64 = object.uint64; - } else { - message.uint64 = 0; - } - if (object.sint32 !== undefined && object.sint32 !== null) { - message.sint32 = object.sint32; - } else { - message.sint32 = 0; - } - if (object.sint64 !== undefined && object.sint64 !== null) { - message.sint64 = object.sint64; - } else { - message.sint64 = 0; - } - if (object.fixed32 !== undefined && object.fixed32 !== null) { - message.fixed32 = object.fixed32; - } else { - message.fixed32 = 0; - } - if (object.fixed64 !== undefined && object.fixed64 !== null) { - message.fixed64 = object.fixed64; - } else { - message.fixed64 = 0; - } - if (object.sfixed32 !== undefined && object.sfixed32 !== null) { - message.sfixed32 = object.sfixed32; - } else { - message.sfixed32 = 0; - } - if (object.sfixed64 !== undefined && object.sfixed64 !== null) { - message.sfixed64 = object.sfixed64; - } else { - message.sfixed64 = 0; - } + message.double = object.double ?? 0; + message.float = object.float ?? 0; + message.int32 = object.int32 ?? 0; + message.int64 = object.int64 ?? 0; + message.uint32 = object.uint32 ?? 0; + message.uint64 = object.uint64 ?? 0; + message.sint32 = object.sint32 ?? 0; + message.sint64 = object.sint64 ?? 0; + message.fixed32 = object.fixed32 ?? 0; + message.fixed64 = object.fixed64 ?? 0; + message.sfixed32 = object.sfixed32 ?? 0; + message.sfixed64 = object.sfixed64 ?? 0; return message; }, }; diff --git a/integration/simple/google/protobuf/timestamp.ts b/integration/simple/google/protobuf/timestamp.ts index b62358285..a39e45400 100644 --- a/integration/simple/google/protobuf/timestamp.ts +++ b/integration/simple/google/protobuf/timestamp.ts @@ -171,16 +171,8 @@ export const Timestamp = { fromPartial(object: DeepPartial): Timestamp { const message = { ...baseTimestamp } as Timestamp; - if (object.seconds !== undefined && object.seconds !== null) { - message.seconds = object.seconds; - } else { - message.seconds = 0; - } - if (object.nanos !== undefined && object.nanos !== null) { - message.nanos = object.nanos; - } else { - message.nanos = 0; - } + message.seconds = object.seconds ?? 0; + message.nanos = object.nanos ?? 0; return message; }, }; diff --git a/integration/simple/google/protobuf/wrappers.ts b/integration/simple/google/protobuf/wrappers.ts index 92da3e0a8..5f52d3fe7 100644 --- a/integration/simple/google/protobuf/wrappers.ts +++ b/integration/simple/google/protobuf/wrappers.ts @@ -140,11 +140,7 @@ export const DoubleValue = { fromPartial(object: DeepPartial): DoubleValue { const message = { ...baseDoubleValue } as DoubleValue; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.value = object.value ?? 0; return message; }, }; @@ -195,11 +191,7 @@ export const FloatValue = { fromPartial(object: DeepPartial): FloatValue { const message = { ...baseFloatValue } as FloatValue; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.value = object.value ?? 0; return message; }, }; @@ -250,11 +242,7 @@ export const Int64Value = { fromPartial(object: DeepPartial): Int64Value { const message = { ...baseInt64Value } as Int64Value; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.value = object.value ?? 0; return message; }, }; @@ -305,11 +293,7 @@ export const UInt64Value = { fromPartial(object: DeepPartial): UInt64Value { const message = { ...baseUInt64Value } as UInt64Value; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.value = object.value ?? 0; return message; }, }; @@ -360,11 +344,7 @@ export const Int32Value = { fromPartial(object: DeepPartial): Int32Value { const message = { ...baseInt32Value } as Int32Value; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.value = object.value ?? 0; return message; }, }; @@ -415,11 +395,7 @@ export const UInt32Value = { fromPartial(object: DeepPartial): UInt32Value { const message = { ...baseUInt32Value } as UInt32Value; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.value = object.value ?? 0; return message; }, }; @@ -470,11 +446,7 @@ export const BoolValue = { fromPartial(object: DeepPartial): BoolValue { const message = { ...baseBoolValue } as BoolValue; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = false; - } + message.value = object.value ?? false; return message; }, }; @@ -525,11 +497,7 @@ export const StringValue = { fromPartial(object: DeepPartial): StringValue { const message = { ...baseStringValue } as StringValue; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = ''; - } + message.value = object.value ?? ''; return message; }, }; @@ -581,11 +549,7 @@ export const BytesValue = { fromPartial(object: DeepPartial): BytesValue { const message = { ...baseBytesValue } as BytesValue; - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = new Uint8Array(); - } + message.value = object.value ?? new Uint8Array(); return message; }, }; diff --git a/integration/simple/google/type/date.ts b/integration/simple/google/type/date.ts index 722f40458..33b2865a3 100644 --- a/integration/simple/google/type/date.ts +++ b/integration/simple/google/type/date.ts @@ -105,21 +105,9 @@ export const DateMessage = { fromPartial(object: DeepPartial): DateMessage { const message = { ...baseDateMessage } as DateMessage; - if (object.year !== undefined && object.year !== null) { - message.year = object.year; - } else { - message.year = 0; - } - if (object.month !== undefined && object.month !== null) { - message.month = object.month; - } else { - message.month = 0; - } - if (object.day !== undefined && object.day !== null) { - message.day = object.day; - } else { - message.day = 0; - } + message.year = object.year ?? 0; + message.month = object.month ?? 0; + message.day = object.day ?? 0; return message; }, }; diff --git a/integration/simple/import_dir/thing.ts b/integration/simple/import_dir/thing.ts index 9c777434d..5ba6da998 100644 --- a/integration/simple/import_dir/thing.ts +++ b/integration/simple/import_dir/thing.ts @@ -55,11 +55,7 @@ export const ImportedThing = { fromPartial(object: DeepPartial): ImportedThing { const message = { ...baseImportedThing } as ImportedThing; - if (object.createdAt !== undefined && object.createdAt !== null) { - message.createdAt = object.createdAt; - } else { - message.createdAt = undefined; - } + message.createdAt = object.createdAt ?? undefined; return message; }, }; diff --git a/integration/simple/simple.ts b/integration/simple/simple.ts index 6478da445..5d5cf1861 100644 --- a/integration/simple/simple.ts +++ b/integration/simple/simple.ts @@ -524,51 +524,34 @@ export const Simple = { fromPartial(object: DeepPartial): Simple { const message = { ...baseSimple } as Simple; - message.grandChildren = []; - message.coins = []; - message.snacks = []; - message.oldStates = []; - message.blobs = []; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = ''; - } - if (object.age !== undefined && object.age !== null) { - message.age = object.age; - } else { - message.age = 0; - } - if (object.createdAt !== undefined && object.createdAt !== null) { - message.createdAt = object.createdAt; - } else { - message.createdAt = undefined; - } + message.name = object.name ?? ''; + message.age = object.age ?? 0; + message.createdAt = object.createdAt ?? undefined; if (object.child !== undefined && object.child !== null) { message.child = Child.fromPartial(object.child); } else { message.child = undefined; } - if (object.state !== undefined && object.state !== null) { - message.state = object.state; - } else { - message.state = 0; - } + message.state = object.state ?? 0; + message.grandChildren = []; if (object.grandChildren !== undefined && object.grandChildren !== null) { for (const e of object.grandChildren) { message.grandChildren.push(Child.fromPartial(e)); } } + message.coins = []; if (object.coins !== undefined && object.coins !== null) { for (const e of object.coins) { message.coins.push(e); } } + message.snacks = []; if (object.snacks !== undefined && object.snacks !== null) { for (const e of object.snacks) { message.snacks.push(e); } } + message.oldStates = []; if (object.oldStates !== undefined && object.oldStates !== null) { for (const e of object.oldStates) { message.oldStates.push(e); @@ -579,6 +562,7 @@ export const Simple = { } else { message.thing = undefined; } + message.blobs = []; if (object.blobs !== undefined && object.blobs !== null) { for (const e of object.blobs) { message.blobs.push(e); @@ -589,11 +573,7 @@ export const Simple = { } else { message.birthday = undefined; } - if (object.blob !== undefined && object.blob !== null) { - message.blob = object.blob; - } else { - message.blob = new Uint8Array(); - } + message.blob = object.blob ?? new Uint8Array(); return message; }, }; @@ -656,16 +636,8 @@ export const Child = { fromPartial(object: DeepPartial): Child { const message = { ...baseChild } as Child; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = ''; - } - if (object.type !== undefined && object.type !== null) { - message.type = object.type; - } else { - message.type = 0; - } + message.name = object.name ?? ''; + message.type = object.type ?? 0; return message; }, }; @@ -741,21 +713,13 @@ export const Nested = { fromPartial(object: DeepPartial): Nested { const message = { ...baseNested } as Nested; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = ''; - } + message.name = object.name ?? ''; if (object.message !== undefined && object.message !== null) { message.message = Nested_InnerMessage.fromPartial(object.message); } else { message.message = undefined; } - if (object.state !== undefined && object.state !== null) { - message.state = object.state; - } else { - message.state = 0; - } + message.state = object.state ?? 0; return message; }, }; @@ -819,11 +783,7 @@ export const Nested_InnerMessage = { fromPartial(object: DeepPartial): Nested_InnerMessage { const message = { ...baseNested_InnerMessage } as Nested_InnerMessage; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = ''; - } + message.name = object.name ?? ''; if (object.deep !== undefined && object.deep !== null) { message.deep = Nested_InnerMessage_DeepMessage.fromPartial(object.deep); } else { @@ -879,11 +839,7 @@ export const Nested_InnerMessage_DeepMessage = { fromPartial(object: DeepPartial): Nested_InnerMessage_DeepMessage { const message = { ...baseNested_InnerMessage_DeepMessage } as Nested_InnerMessage_DeepMessage; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = ''; - } + message.name = object.name ?? ''; return message; }, }; @@ -946,16 +902,8 @@ export const OneOfMessage = { fromPartial(object: DeepPartial): OneOfMessage { const message = { ...baseOneOfMessage } as OneOfMessage; - if (object.first !== undefined && object.first !== null) { - message.first = object.first; - } else { - message.first = undefined; - } - if (object.last !== undefined && object.last !== null) { - message.last = object.last; - } else { - message.last = undefined; - } + message.first = object.first ?? undefined; + message.last = object.last ?? undefined; return message; }, }; @@ -1078,38 +1026,22 @@ export const SimpleWithWrappers = { fromPartial(object: DeepPartial): SimpleWithWrappers { const message = { ...baseSimpleWithWrappers } as SimpleWithWrappers; + message.name = object.name ?? undefined; + message.age = object.age ?? undefined; + message.enabled = object.enabled ?? undefined; message.coins = []; - message.snacks = []; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = undefined; - } - if (object.age !== undefined && object.age !== null) { - message.age = object.age; - } else { - message.age = undefined; - } - if (object.enabled !== undefined && object.enabled !== null) { - message.enabled = object.enabled; - } else { - message.enabled = undefined; - } if (object.coins !== undefined && object.coins !== null) { for (const e of object.coins) { message.coins.push(e); } } + message.snacks = []; if (object.snacks !== undefined && object.snacks !== null) { for (const e of object.snacks) { message.snacks.push(e); } } - if (object.id !== undefined && object.id !== null) { - message.id = object.id; - } else { - message.id = undefined; - } + message.id = object.id ?? undefined; return message; }, }; @@ -1160,11 +1092,7 @@ export const Entity = { fromPartial(object: DeepPartial): Entity { const message = { ...baseEntity } as Entity; - if (object.id !== undefined && object.id !== null) { - message.id = object.id; - } else { - message.id = 0; - } + message.id = object.id ?? 0; return message; }, }; @@ -1338,11 +1266,6 @@ export const SimpleWithMap = { fromPartial(object: DeepPartial): SimpleWithMap { const message = { ...baseSimpleWithMap } as SimpleWithMap; message.entitiesById = {}; - message.nameLookup = {}; - message.intLookup = {}; - message.mapOfTimestamps = {}; - message.mapOfBytes = {}; - message.mapOfStringValues = {}; if (object.entitiesById !== undefined && object.entitiesById !== null) { Object.entries(object.entitiesById).forEach(([key, value]) => { if (value !== undefined) { @@ -1350,6 +1273,7 @@ export const SimpleWithMap = { } }); } + message.nameLookup = {}; if (object.nameLookup !== undefined && object.nameLookup !== null) { Object.entries(object.nameLookup).forEach(([key, value]) => { if (value !== undefined) { @@ -1357,6 +1281,7 @@ export const SimpleWithMap = { } }); } + message.intLookup = {}; if (object.intLookup !== undefined && object.intLookup !== null) { Object.entries(object.intLookup).forEach(([key, value]) => { if (value !== undefined) { @@ -1364,6 +1289,7 @@ export const SimpleWithMap = { } }); } + message.mapOfTimestamps = {}; if (object.mapOfTimestamps !== undefined && object.mapOfTimestamps !== null) { Object.entries(object.mapOfTimestamps).forEach(([key, value]) => { if (value !== undefined) { @@ -1371,6 +1297,7 @@ export const SimpleWithMap = { } }); } + message.mapOfBytes = {}; if (object.mapOfBytes !== undefined && object.mapOfBytes !== null) { Object.entries(object.mapOfBytes).forEach(([key, value]) => { if (value !== undefined) { @@ -1378,6 +1305,7 @@ export const SimpleWithMap = { } }); } + message.mapOfStringValues = {}; if (object.mapOfStringValues !== undefined && object.mapOfStringValues !== null) { Object.entries(object.mapOfStringValues).forEach(([key, value]) => { if (value !== undefined) { @@ -1447,11 +1375,7 @@ export const SimpleWithMap_EntitiesByIdEntry = { fromPartial(object: DeepPartial): SimpleWithMap_EntitiesByIdEntry { const message = { ...baseSimpleWithMap_EntitiesByIdEntry } as SimpleWithMap_EntitiesByIdEntry; - if (object.key !== undefined && object.key !== null) { - message.key = object.key; - } else { - message.key = 0; - } + message.key = object.key ?? 0; if (object.value !== undefined && object.value !== null) { message.value = Entity.fromPartial(object.value); } else { @@ -1519,16 +1443,8 @@ export const SimpleWithMap_NameLookupEntry = { fromPartial(object: DeepPartial): SimpleWithMap_NameLookupEntry { const message = { ...baseSimpleWithMap_NameLookupEntry } as SimpleWithMap_NameLookupEntry; - if (object.key !== undefined && object.key !== null) { - message.key = object.key; - } else { - message.key = ''; - } - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = ''; - } + message.key = object.key ?? ''; + message.value = object.value ?? ''; return message; }, }; @@ -1591,16 +1507,8 @@ export const SimpleWithMap_IntLookupEntry = { fromPartial(object: DeepPartial): SimpleWithMap_IntLookupEntry { const message = { ...baseSimpleWithMap_IntLookupEntry } as SimpleWithMap_IntLookupEntry; - if (object.key !== undefined && object.key !== null) { - message.key = object.key; - } else { - message.key = 0; - } - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.key = object.key ?? 0; + message.value = object.value ?? 0; return message; }, }; @@ -1663,16 +1571,8 @@ export const SimpleWithMap_MapOfTimestampsEntry = { fromPartial(object: DeepPartial): SimpleWithMap_MapOfTimestampsEntry { const message = { ...baseSimpleWithMap_MapOfTimestampsEntry } as SimpleWithMap_MapOfTimestampsEntry; - if (object.key !== undefined && object.key !== null) { - message.key = object.key; - } else { - message.key = ''; - } - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = undefined; - } + message.key = object.key ?? ''; + message.value = object.value ?? undefined; return message; }, }; @@ -1736,16 +1636,8 @@ export const SimpleWithMap_MapOfBytesEntry = { fromPartial(object: DeepPartial): SimpleWithMap_MapOfBytesEntry { const message = { ...baseSimpleWithMap_MapOfBytesEntry } as SimpleWithMap_MapOfBytesEntry; - if (object.key !== undefined && object.key !== null) { - message.key = object.key; - } else { - message.key = ''; - } - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = new Uint8Array(); - } + message.key = object.key ?? ''; + message.value = object.value ?? new Uint8Array(); return message; }, }; @@ -1808,16 +1700,8 @@ export const SimpleWithMap_MapOfStringValuesEntry = { fromPartial(object: DeepPartial): SimpleWithMap_MapOfStringValuesEntry { const message = { ...baseSimpleWithMap_MapOfStringValuesEntry } as SimpleWithMap_MapOfStringValuesEntry; - if (object.key !== undefined && object.key !== null) { - message.key = object.key; - } else { - message.key = ''; - } - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = undefined; - } + message.key = object.key ?? ''; + message.value = object.value ?? undefined; return message; }, }; @@ -1948,11 +1832,7 @@ export const SimpleWithSnakeCaseMap_EntitiesByIdEntry = { fromPartial(object: DeepPartial): SimpleWithSnakeCaseMap_EntitiesByIdEntry { const message = { ...baseSimpleWithSnakeCaseMap_EntitiesByIdEntry } as SimpleWithSnakeCaseMap_EntitiesByIdEntry; - if (object.key !== undefined && object.key !== null) { - message.key = object.key; - } else { - message.key = 0; - } + message.key = object.key ?? 0; if (object.value !== undefined && object.value !== null) { message.value = Entity.fromPartial(object.value); } else { @@ -2088,16 +1968,8 @@ export const SimpleWithMapOfEnums_EnumsByIdEntry = { fromPartial(object: DeepPartial): SimpleWithMapOfEnums_EnumsByIdEntry { const message = { ...baseSimpleWithMapOfEnums_EnumsByIdEntry } as SimpleWithMapOfEnums_EnumsByIdEntry; - if (object.key !== undefined && object.key !== null) { - message.key = object.key; - } else { - message.key = 0; - } - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = 0; - } + message.key = object.key ?? 0; + message.value = object.value ?? 0; return message; }, }; @@ -2148,11 +2020,7 @@ export const PingRequest = { fromPartial(object: DeepPartial): PingRequest { const message = { ...basePingRequest } as PingRequest; - if (object.input !== undefined && object.input !== null) { - message.input = object.input; - } else { - message.input = ''; - } + message.input = object.input ?? ''; return message; }, }; @@ -2203,11 +2071,7 @@ export const PingResponse = { fromPartial(object: DeepPartial): PingResponse { const message = { ...basePingResponse } as PingResponse; - if (object.output !== undefined && object.output !== null) { - message.output = object.output; - } else { - message.output = ''; - } + message.output = object.output ?? ''; return message; }, }; @@ -2403,66 +2267,18 @@ export const Numbers = { fromPartial(object: DeepPartial): Numbers { const message = { ...baseNumbers } as Numbers; - if (object.double !== undefined && object.double !== null) { - message.double = object.double; - } else { - message.double = 0; - } - if (object.float !== undefined && object.float !== null) { - message.float = object.float; - } else { - message.float = 0; - } - if (object.int32 !== undefined && object.int32 !== null) { - message.int32 = object.int32; - } else { - message.int32 = 0; - } - if (object.int64 !== undefined && object.int64 !== null) { - message.int64 = object.int64; - } else { - message.int64 = 0; - } - if (object.uint32 !== undefined && object.uint32 !== null) { - message.uint32 = object.uint32; - } else { - message.uint32 = 0; - } - if (object.uint64 !== undefined && object.uint64 !== null) { - message.uint64 = object.uint64; - } else { - message.uint64 = 0; - } - if (object.sint32 !== undefined && object.sint32 !== null) { - message.sint32 = object.sint32; - } else { - message.sint32 = 0; - } - if (object.sint64 !== undefined && object.sint64 !== null) { - message.sint64 = object.sint64; - } else { - message.sint64 = 0; - } - if (object.fixed32 !== undefined && object.fixed32 !== null) { - message.fixed32 = object.fixed32; - } else { - message.fixed32 = 0; - } - if (object.fixed64 !== undefined && object.fixed64 !== null) { - message.fixed64 = object.fixed64; - } else { - message.fixed64 = 0; - } - if (object.sfixed32 !== undefined && object.sfixed32 !== null) { - message.sfixed32 = object.sfixed32; - } else { - message.sfixed32 = 0; - } - if (object.sfixed64 !== undefined && object.sfixed64 !== null) { - message.sfixed64 = object.sfixed64; - } else { - message.sfixed64 = 0; - } + message.double = object.double ?? 0; + message.float = object.float ?? 0; + message.int32 = object.int32 ?? 0; + message.int64 = object.int64 ?? 0; + message.uint32 = object.uint32 ?? 0; + message.uint64 = object.uint64 ?? 0; + message.sint32 = object.sint32 ?? 0; + message.sint64 = object.sint64 ?? 0; + message.fixed32 = object.fixed32 ?? 0; + message.fixed64 = object.fixed64 ?? 0; + message.sfixed32 = object.sfixed32 ?? 0; + message.sfixed64 = object.sfixed64 ?? 0; return message; }, }; @@ -2587,31 +2403,15 @@ export const SimpleButOptional = { fromPartial(object: DeepPartial): SimpleButOptional { const message = { ...baseSimpleButOptional } as SimpleButOptional; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = undefined; - } - if (object.age !== undefined && object.age !== null) { - message.age = object.age; - } else { - message.age = undefined; - } - if (object.createdAt !== undefined && object.createdAt !== null) { - message.createdAt = object.createdAt; - } else { - message.createdAt = undefined; - } + message.name = object.name ?? undefined; + message.age = object.age ?? undefined; + message.createdAt = object.createdAt ?? undefined; if (object.child !== undefined && object.child !== null) { message.child = Child.fromPartial(object.child); } else { message.child = undefined; } - if (object.state !== undefined && object.state !== null) { - message.state = object.state; - } else { - message.state = undefined; - } + message.state = object.state ?? undefined; if (object.thing !== undefined && object.thing !== null) { message.thing = ImportedThing.fromPartial(object.thing); } else { diff --git a/integration/type-registry/foo.ts b/integration/type-registry/foo.ts index f54103137..a046a04e2 100644 --- a/integration/type-registry/foo.ts +++ b/integration/type-registry/foo.ts @@ -64,11 +64,7 @@ export const Foo = { fromPartial(object: DeepPartial): Foo { const message = { ...baseFoo } as Foo; - if (object.timestamp !== undefined && object.timestamp !== null) { - message.timestamp = object.timestamp; - } else { - message.timestamp = undefined; - } + message.timestamp = object.timestamp ?? undefined; return message; }, }; @@ -123,11 +119,7 @@ export const Foo2 = { fromPartial(object: DeepPartial): Foo2 { const message = { ...baseFoo2 } as Foo2; - if (object.timestamp !== undefined && object.timestamp !== null) { - message.timestamp = object.timestamp; - } else { - message.timestamp = undefined; - } + message.timestamp = object.timestamp ?? undefined; return message; }, }; diff --git a/integration/type-registry/google/protobuf/timestamp.ts b/integration/type-registry/google/protobuf/timestamp.ts index b5a864401..f464d4aae 100644 --- a/integration/type-registry/google/protobuf/timestamp.ts +++ b/integration/type-registry/google/protobuf/timestamp.ts @@ -166,16 +166,8 @@ export const Timestamp = { fromPartial(object: DeepPartial): Timestamp { const message = { ...baseTimestamp } as Timestamp; - if (object.seconds !== undefined && object.seconds !== null) { - message.seconds = object.seconds; - } else { - message.seconds = 0; - } - if (object.nanos !== undefined && object.nanos !== null) { - message.nanos = object.nanos; - } else { - message.nanos = 0; - } + message.seconds = object.seconds ?? 0; + message.nanos = object.nanos ?? 0; return message; }, }; diff --git a/integration/use-date-false/google/protobuf/timestamp.ts b/integration/use-date-false/google/protobuf/timestamp.ts index 52ea73adb..89f9a4fdb 100644 --- a/integration/use-date-false/google/protobuf/timestamp.ts +++ b/integration/use-date-false/google/protobuf/timestamp.ts @@ -162,16 +162,8 @@ export const Timestamp = { fromPartial(object: DeepPartial): Timestamp { const message = { ...baseTimestamp } as Timestamp; - if (object.seconds !== undefined && object.seconds !== null) { - message.seconds = object.seconds; - } else { - message.seconds = 0; - } - if (object.nanos !== undefined && object.nanos !== null) { - message.nanos = object.nanos; - } else { - message.nanos = 0; - } + message.seconds = object.seconds ?? 0; + message.nanos = object.nanos ?? 0; return message; }, }; diff --git a/integration/use-date-string/google/protobuf/timestamp.ts b/integration/use-date-string/google/protobuf/timestamp.ts index 52ea73adb..89f9a4fdb 100644 --- a/integration/use-date-string/google/protobuf/timestamp.ts +++ b/integration/use-date-string/google/protobuf/timestamp.ts @@ -162,16 +162,8 @@ export const Timestamp = { fromPartial(object: DeepPartial): Timestamp { const message = { ...baseTimestamp } as Timestamp; - if (object.seconds !== undefined && object.seconds !== null) { - message.seconds = object.seconds; - } else { - message.seconds = 0; - } - if (object.nanos !== undefined && object.nanos !== null) { - message.nanos = object.nanos; - } else { - message.nanos = 0; - } + message.seconds = object.seconds ?? 0; + message.nanos = object.nanos ?? 0; return message; }, }; diff --git a/integration/use-date-string/use-date-string.ts b/integration/use-date-string/use-date-string.ts index e31c61837..23661339b 100644 --- a/integration/use-date-string/use-date-string.ts +++ b/integration/use-date-string/use-date-string.ts @@ -128,28 +128,16 @@ export const Todo = { fromPartial(object: DeepPartial): Todo { const message = { ...baseTodo } as Todo; + message.id = object.id ?? ''; + message.timestamp = object.timestamp ?? undefined; message.repeatedTimestamp = []; - message.mapOfTimestamps = {}; - if (object.id !== undefined && object.id !== null) { - message.id = object.id; - } else { - message.id = ''; - } - if (object.timestamp !== undefined && object.timestamp !== null) { - message.timestamp = object.timestamp; - } else { - message.timestamp = undefined; - } if (object.repeatedTimestamp !== undefined && object.repeatedTimestamp !== null) { for (const e of object.repeatedTimestamp) { message.repeatedTimestamp.push(e); } } - if (object.optionalTimestamp !== undefined && object.optionalTimestamp !== null) { - message.optionalTimestamp = object.optionalTimestamp; - } else { - message.optionalTimestamp = undefined; - } + message.optionalTimestamp = object.optionalTimestamp ?? undefined; + message.mapOfTimestamps = {}; if (object.mapOfTimestamps !== undefined && object.mapOfTimestamps !== null) { Object.entries(object.mapOfTimestamps).forEach(([key, value]) => { if (value !== undefined) { @@ -219,16 +207,8 @@ export const Todo_MapOfTimestampsEntry = { fromPartial(object: DeepPartial): Todo_MapOfTimestampsEntry { const message = { ...baseTodo_MapOfTimestampsEntry } as Todo_MapOfTimestampsEntry; - if (object.key !== undefined && object.key !== null) { - message.key = object.key; - } else { - message.key = ''; - } - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = undefined; - } + message.key = object.key ?? ''; + message.value = object.value ?? undefined; return message; }, }; diff --git a/integration/use-date-true/google/protobuf/timestamp.ts b/integration/use-date-true/google/protobuf/timestamp.ts index 52ea73adb..89f9a4fdb 100644 --- a/integration/use-date-true/google/protobuf/timestamp.ts +++ b/integration/use-date-true/google/protobuf/timestamp.ts @@ -162,16 +162,8 @@ export const Timestamp = { fromPartial(object: DeepPartial): Timestamp { const message = { ...baseTimestamp } as Timestamp; - if (object.seconds !== undefined && object.seconds !== null) { - message.seconds = object.seconds; - } else { - message.seconds = 0; - } - if (object.nanos !== undefined && object.nanos !== null) { - message.nanos = object.nanos; - } else { - message.nanos = 0; - } + message.seconds = object.seconds ?? 0; + message.nanos = object.nanos ?? 0; return message; }, }; diff --git a/integration/use-date-true/use-date-true.ts b/integration/use-date-true/use-date-true.ts index b8e69f6fc..f535467cf 100644 --- a/integration/use-date-true/use-date-true.ts +++ b/integration/use-date-true/use-date-true.ts @@ -128,28 +128,16 @@ export const Todo = { fromPartial(object: DeepPartial): Todo { const message = { ...baseTodo } as Todo; + message.id = object.id ?? ''; + message.timestamp = object.timestamp ?? undefined; message.repeatedTimestamp = []; - message.mapOfTimestamps = {}; - if (object.id !== undefined && object.id !== null) { - message.id = object.id; - } else { - message.id = ''; - } - if (object.timestamp !== undefined && object.timestamp !== null) { - message.timestamp = object.timestamp; - } else { - message.timestamp = undefined; - } if (object.repeatedTimestamp !== undefined && object.repeatedTimestamp !== null) { for (const e of object.repeatedTimestamp) { message.repeatedTimestamp.push(e); } } - if (object.optionalTimestamp !== undefined && object.optionalTimestamp !== null) { - message.optionalTimestamp = object.optionalTimestamp; - } else { - message.optionalTimestamp = undefined; - } + message.optionalTimestamp = object.optionalTimestamp ?? undefined; + message.mapOfTimestamps = {}; if (object.mapOfTimestamps !== undefined && object.mapOfTimestamps !== null) { Object.entries(object.mapOfTimestamps).forEach(([key, value]) => { if (value !== undefined) { @@ -219,16 +207,8 @@ export const Todo_MapOfTimestampsEntry = { fromPartial(object: DeepPartial): Todo_MapOfTimestampsEntry { const message = { ...baseTodo_MapOfTimestampsEntry } as Todo_MapOfTimestampsEntry; - if (object.key !== undefined && object.key !== null) { - message.key = object.key; - } else { - message.key = ''; - } - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = undefined; - } + message.key = object.key ?? ''; + message.value = object.value ?? undefined; return message; }, }; diff --git a/integration/vector-tile/vector_tile.ts b/integration/vector-tile/vector_tile.ts index ff6a2f396..d67538767 100644 --- a/integration/vector-tile/vector_tile.ts +++ b/integration/vector-tile/vector_tile.ts @@ -266,41 +266,13 @@ export const Tile_Value = { fromPartial(object: DeepPartial): Tile_Value { const message = { ...baseTile_Value } as Tile_Value; - if (object.stringValue !== undefined && object.stringValue !== null) { - message.stringValue = object.stringValue; - } else { - message.stringValue = ''; - } - if (object.floatValue !== undefined && object.floatValue !== null) { - message.floatValue = object.floatValue; - } else { - message.floatValue = 0; - } - if (object.doubleValue !== undefined && object.doubleValue !== null) { - message.doubleValue = object.doubleValue; - } else { - message.doubleValue = 0; - } - if (object.intValue !== undefined && object.intValue !== null) { - message.intValue = object.intValue; - } else { - message.intValue = 0; - } - if (object.uintValue !== undefined && object.uintValue !== null) { - message.uintValue = object.uintValue; - } else { - message.uintValue = 0; - } - if (object.sintValue !== undefined && object.sintValue !== null) { - message.sintValue = object.sintValue; - } else { - message.sintValue = 0; - } - if (object.boolValue !== undefined && object.boolValue !== null) { - message.boolValue = object.boolValue; - } else { - message.boolValue = false; - } + message.stringValue = object.stringValue ?? ''; + message.floatValue = object.floatValue ?? 0; + message.doubleValue = object.doubleValue ?? 0; + message.intValue = object.intValue ?? 0; + message.uintValue = object.uintValue ?? 0; + message.sintValue = object.sintValue ?? 0; + message.boolValue = object.boolValue ?? false; return message; }, }; @@ -417,23 +389,15 @@ export const Tile_Feature = { fromPartial(object: DeepPartial): Tile_Feature { const message = { ...baseTile_Feature } as Tile_Feature; + message.id = object.id ?? 0; message.tags = []; - message.geometry = []; - if (object.id !== undefined && object.id !== null) { - message.id = object.id; - } else { - message.id = 0; - } if (object.tags !== undefined && object.tags !== null) { for (const e of object.tags) { message.tags.push(e); } } - if (object.type !== undefined && object.type !== null) { - message.type = object.type; - } else { - message.type = 0; - } + message.type = object.type ?? 0; + message.geometry = []; if (object.geometry !== undefined && object.geometry !== null) { for (const e of object.geometry) { message.geometry.push(e); @@ -567,39 +531,27 @@ export const Tile_Layer = { fromPartial(object: DeepPartial): Tile_Layer { const message = { ...baseTile_Layer } as Tile_Layer; + message.version = object.version ?? 0; + message.name = object.name ?? ''; message.features = []; - message.keys = []; - message.values = []; - if (object.version !== undefined && object.version !== null) { - message.version = object.version; - } else { - message.version = 0; - } - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = ''; - } if (object.features !== undefined && object.features !== null) { for (const e of object.features) { message.features.push(Tile_Feature.fromPartial(e)); } } + message.keys = []; if (object.keys !== undefined && object.keys !== null) { for (const e of object.keys) { message.keys.push(e); } } + message.values = []; if (object.values !== undefined && object.values !== null) { for (const e of object.values) { message.values.push(Tile_Value.fromPartial(e)); } } - if (object.extent !== undefined && object.extent !== null) { - message.extent = object.extent; - } else { - message.extent = 0; - } + message.extent = object.extent ?? 0; return message; }, }; diff --git a/src/main.ts b/src/main.ts index 25c2827d2..f24269c51 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1127,7 +1127,6 @@ function generateToJson(ctx: Context, fullName: string, messageDesc: DescriptorP chunks.push(code`message.${fieldName} !== undefined && (obj.${fieldName} = ${v});`); } }); - chunks.push(code`return obj;`); chunks.push(code`}`); return joinCode(chunks, { on: '\n' }); @@ -1144,13 +1143,6 @@ function generateFromPartial(ctx: Context, fullName: string, messageDesc: Descri const message = { ...base${fullName} } as ${fullName}; `); - // initialize all lists - messageDesc.field.filter(isRepeated).forEach((field) => { - const value = isMapType(ctx, messageDesc, field) ? '{}' : '[]'; - const name = maybeSnakeToCamel(field.name, options); - chunks.push(code`message.${name} = ${value};`); - }); - // add a check for each incoming field messageDesc.field.forEach((field) => { const fieldName = maybeSnakeToCamel(field.name, options); @@ -1196,6 +1188,9 @@ function generateFromPartial(ctx: Context, fullName: string, messageDesc: Descri // and then use the snippet to handle repeated fields if necessary if (isRepeated(field)) { + const value = isMapType(ctx, messageDesc, field) ? '{}' : '[]'; + const name = maybeSnakeToCamel(field.name, options); + chunks.push(code`message.${name} = ${value};`); chunks.push(code`if (object.${fieldName} !== undefined && object.${fieldName} !== null) {`); if (isMapType(ctx, messageDesc, field)) { const i = maybeCastToNumber(ctx, messageDesc, field, 'key'); @@ -1213,6 +1208,7 @@ function generateFromPartial(ctx: Context, fullName: string, messageDesc: Descri } `); } + chunks.push(code`}`); } else if (isWithinOneOfThatShouldBeUnion(options, field)) { let oneofName = maybeSnakeToCamel(messageDesc.oneofDecl[field.oneofIndex].name, options); const v = readSnippet(`object.${oneofName}.${fieldName}`); @@ -1223,25 +1219,33 @@ function generateFromPartial(ctx: Context, fullName: string, messageDesc: Descri && object.${oneofName}?.${fieldName} !== null ) { message.${oneofName} = { $case: '${fieldName}', ${fieldName}: ${v} }; + } `); + } else if ((isLong(field) || isLongValueType(field)) && options.forceLong === LongOption.LONG) { + const v = readSnippet(`object.${fieldName}`); + const type = basicTypeName(ctx, field); + chunks.push(code`if (object.${fieldName} !== undefined && object.${fieldName} !== null) {`); + chunks.push(code`message.${fieldName} = ${v} as ${type};`); + chunks.push(code`} else {`); + const fallback = isWithinOneOf(field) ? 'undefined' : defaultValue(ctx, field); + chunks.push(code`message.${fieldName} = ${fallback}`); + chunks.push(code`}`); + } else if ( + isPrimitive(field) || + (isTimestamp(field) && (options.useDate === DateOption.DATE || options.useDate === DateOption.STRING)) || + isValueType(ctx, field) + ) { + // An optimized case of the else below that works when `readSnippet` returns the plain input + const fallback = isWithinOneOf(field) ? 'undefined' : defaultValue(ctx, field); + chunks.push(code`message.${fieldName} = object.${fieldName} ?? ${fallback};`); } else { chunks.push(code`if (object.${fieldName} !== undefined && object.${fieldName} !== null) {`); - if ((isLong(field) || isLongValueType(field)) && options.forceLong === LongOption.LONG) { - const v = readSnippet(`object.${fieldName}`); - const type = basicTypeName(ctx, field); - chunks.push(code`message.${fieldName} = ${v} as ${type};`); - } else { - chunks.push(code`message.${fieldName} = ${readSnippet(`object.${fieldName}`)};`); - } - } - - if (!isRepeated(field) && !isWithinOneOfThatShouldBeUnion(options, field)) { + chunks.push(code`message.${fieldName} = ${readSnippet(`object.${fieldName}`)};`); chunks.push(code`} else {`); - const v = isWithinOneOf(field) ? 'undefined' : defaultValue(ctx, field); - chunks.push(code`message.${fieldName} = ${v}`); + const fallback = isWithinOneOf(field) ? 'undefined' : defaultValue(ctx, field); + chunks.push(code`message.${fieldName} = ${fallback}`); + chunks.push(code`}`); } - - chunks.push(code`}`); }); // and then wrap up the switch/while/return