diff --git a/integration/batching-with-context/batching.ts b/integration/batching-with-context/batching.ts index 30968e1ec..4da12298a 100644 --- a/integration/batching-with-context/batching.ts +++ b/integration/batching-with-context/batching.ts @@ -236,12 +236,10 @@ export const BatchMapQueryResponse = { fromJSON(object: any): BatchMapQueryResponse { const message = { ...baseBatchMapQueryResponse } as BatchMapQueryResponse; - message.entities = {}; - if (object.entities !== undefined && object.entities !== null) { - Object.entries(object.entities).forEach(([key, value]) => { - message.entities[key] = Entity.fromJSON(value); - }); - } + message.entities = Object.entries(object.entities ?? {}).reduce<{ [key: string]: Entity }>((acc, [key, value]) => { + acc[key] = Entity.fromJSON(value); + return acc; + }, {}); return message; }, @@ -258,14 +256,12 @@ export const BatchMapQueryResponse = { fromPartial(object: DeepPartial): BatchMapQueryResponse { const message = { ...baseBatchMapQueryResponse } as BatchMapQueryResponse; - message.entities = {}; - if (object.entities !== undefined && object.entities !== null) { - Object.entries(object.entities).forEach(([key, value]) => { - if (value !== undefined) { - message.entities[key] = Entity.fromPartial(value); - } - }); - } + message.entities = Object.entries(object.entities ?? {}).reduce<{ [key: string]: Entity }>((acc, [key, value]) => { + if (value !== undefined) { + acc[key] = Entity.fromPartial(value); + } + return acc; + }, {}); return message; }, }; diff --git a/integration/batching/batching.ts b/integration/batching/batching.ts index 9eef7d4c8..1aa547719 100644 --- a/integration/batching/batching.ts +++ b/integration/batching/batching.ts @@ -234,12 +234,10 @@ export const BatchMapQueryResponse = { fromJSON(object: any): BatchMapQueryResponse { const message = { ...baseBatchMapQueryResponse } as BatchMapQueryResponse; - message.entities = {}; - if (object.entities !== undefined && object.entities !== null) { - Object.entries(object.entities).forEach(([key, value]) => { - message.entities[key] = Entity.fromJSON(value); - }); - } + message.entities = Object.entries(object.entities ?? {}).reduce<{ [key: string]: Entity }>((acc, [key, value]) => { + acc[key] = Entity.fromJSON(value); + return acc; + }, {}); return message; }, @@ -256,14 +254,12 @@ export const BatchMapQueryResponse = { fromPartial(object: DeepPartial): BatchMapQueryResponse { const message = { ...baseBatchMapQueryResponse } as BatchMapQueryResponse; - message.entities = {}; - if (object.entities !== undefined && object.entities !== null) { - Object.entries(object.entities).forEach(([key, value]) => { - if (value !== undefined) { - message.entities[key] = Entity.fromPartial(value); - } - }); - } + message.entities = Object.entries(object.entities ?? {}).reduce<{ [key: string]: Entity }>((acc, [key, value]) => { + if (value !== undefined) { + acc[key] = Entity.fromPartial(value); + } + return acc; + }, {}); return message; }, }; diff --git a/integration/simple-long/simple.ts b/integration/simple-long/simple.ts index 55dfae0da..95c7eda7a 100644 --- a/integration/simple-long/simple.ts +++ b/integration/simple-long/simple.ts @@ -211,24 +211,27 @@ export const SimpleWithMap = { fromJSON(object: any): SimpleWithMap { const message = { ...baseSimpleWithMap } as SimpleWithMap; - message.nameLookup = {}; - if (object.nameLookup !== undefined && object.nameLookup !== null) { - Object.entries(object.nameLookup).forEach(([key, value]) => { - message.nameLookup[key] = String(value); - }); - } - message.intLookup = {}; - if (object.intLookup !== undefined && object.intLookup !== null) { - Object.entries(object.intLookup).forEach(([key, value]) => { - message.intLookup[Number(key)] = Number(value); - }); - } - message.longLookup = {}; - if (object.longLookup !== undefined && object.longLookup !== null) { - Object.entries(object.longLookup).forEach(([key, value]) => { - message.longLookup[key] = Long.fromString(value as string); - }); - } + message.nameLookup = Object.entries(object.nameLookup ?? {}).reduce<{ [key: string]: string }>( + (acc, [key, value]) => { + acc[key] = String(value); + return acc; + }, + {} + ); + message.intLookup = Object.entries(object.intLookup ?? {}).reduce<{ [key: number]: number }>( + (acc, [key, value]) => { + acc[Number(key)] = Number(value); + return acc; + }, + {} + ); + message.longLookup = Object.entries(object.longLookup ?? {}).reduce<{ [key: string]: Long }>( + (acc, [key, value]) => { + acc[key] = Long.fromString(value as string); + return acc; + }, + {} + ); return message; }, @@ -257,30 +260,33 @@ export const SimpleWithMap = { fromPartial(object: DeepPartial): SimpleWithMap { const message = { ...baseSimpleWithMap } as SimpleWithMap; - message.nameLookup = {}; - if (object.nameLookup !== undefined && object.nameLookup !== null) { - Object.entries(object.nameLookup).forEach(([key, value]) => { + message.nameLookup = Object.entries(object.nameLookup ?? {}).reduce<{ [key: string]: string }>( + (acc, [key, value]) => { if (value !== undefined) { - message.nameLookup[key] = String(value); + acc[key] = String(value); } - }); - } - message.intLookup = {}; - if (object.intLookup !== undefined && object.intLookup !== null) { - Object.entries(object.intLookup).forEach(([key, value]) => { + return acc; + }, + {} + ); + message.intLookup = Object.entries(object.intLookup ?? {}).reduce<{ [key: number]: number }>( + (acc, [key, value]) => { if (value !== undefined) { - message.intLookup[Number(key)] = Number(value); + acc[Number(key)] = Number(value); } - }); - } - message.longLookup = {}; - if (object.longLookup !== undefined && object.longLookup !== null) { - Object.entries(object.longLookup).forEach(([key, value]) => { + return acc; + }, + {} + ); + message.longLookup = Object.entries(object.longLookup ?? {}).reduce<{ [key: string]: Long }>( + (acc, [key, value]) => { if (value !== undefined) { - message.longLookup[key] = Long.fromValue(value); + acc[key] = Long.fromValue(value); } - }); - } + return acc; + }, + {} + ); return message; }, }; diff --git a/integration/simple-optionals/simple.ts b/integration/simple-optionals/simple.ts index f3b9af737..6108c6ad8 100644 --- a/integration/simple-optionals/simple.ts +++ b/integration/simple-optionals/simple.ts @@ -892,24 +892,27 @@ export const SimpleWithMap = { fromJSON(object: any): SimpleWithMap { const message = { ...baseSimpleWithMap } as SimpleWithMap; - message.entitiesById = {}; - if (object.entitiesById !== undefined && object.entitiesById !== null) { - Object.entries(object.entitiesById).forEach(([key, value]) => { - message.entitiesById[Number(key)] = Entity.fromJSON(value); - }); - } - message.nameLookup = {}; - if (object.nameLookup !== undefined && object.nameLookup !== null) { - Object.entries(object.nameLookup).forEach(([key, value]) => { - message.nameLookup[key] = String(value); - }); - } - message.intLookup = {}; - if (object.intLookup !== undefined && object.intLookup !== null) { - Object.entries(object.intLookup).forEach(([key, value]) => { - message.intLookup[Number(key)] = Number(value); - }); - } + message.entitiesById = Object.entries(object.entitiesById ?? {}).reduce<{ [key: number]: Entity }>( + (acc, [key, value]) => { + acc[Number(key)] = Entity.fromJSON(value); + return acc; + }, + {} + ); + message.nameLookup = Object.entries(object.nameLookup ?? {}).reduce<{ [key: string]: string }>( + (acc, [key, value]) => { + acc[key] = String(value); + return acc; + }, + {} + ); + message.intLookup = Object.entries(object.intLookup ?? {}).reduce<{ [key: number]: number }>( + (acc, [key, value]) => { + acc[Number(key)] = Number(value); + return acc; + }, + {} + ); return message; }, @@ -938,30 +941,33 @@ export const SimpleWithMap = { fromPartial(object: DeepPartial): SimpleWithMap { const message = { ...baseSimpleWithMap } as SimpleWithMap; - message.entitiesById = {}; - if (object.entitiesById !== undefined && object.entitiesById !== null) { - Object.entries(object.entitiesById).forEach(([key, value]) => { + message.entitiesById = Object.entries(object.entitiesById ?? {}).reduce<{ [key: number]: Entity }>( + (acc, [key, value]) => { if (value !== undefined) { - message.entitiesById[Number(key)] = Entity.fromPartial(value); + acc[Number(key)] = Entity.fromPartial(value); } - }); - } - message.nameLookup = {}; - if (object.nameLookup !== undefined && object.nameLookup !== null) { - Object.entries(object.nameLookup).forEach(([key, value]) => { + return acc; + }, + {} + ); + message.nameLookup = Object.entries(object.nameLookup ?? {}).reduce<{ [key: string]: string }>( + (acc, [key, value]) => { if (value !== undefined) { - message.nameLookup[key] = String(value); + acc[key] = String(value); } - }); - } - message.intLookup = {}; - if (object.intLookup !== undefined && object.intLookup !== null) { - Object.entries(object.intLookup).forEach(([key, value]) => { + return acc; + }, + {} + ); + message.intLookup = Object.entries(object.intLookup ?? {}).reduce<{ [key: number]: number }>( + (acc, [key, value]) => { if (value !== undefined) { - message.intLookup[Number(key)] = Number(value); + acc[Number(key)] = Number(value); } - }); - } + return acc; + }, + {} + ); return message; }, }; @@ -1168,12 +1174,13 @@ export const SimpleWithSnakeCaseMap = { fromJSON(object: any): SimpleWithSnakeCaseMap { const message = { ...baseSimpleWithSnakeCaseMap } as SimpleWithSnakeCaseMap; - message.entitiesById = {}; - if (object.entitiesById !== undefined && object.entitiesById !== null) { - Object.entries(object.entitiesById).forEach(([key, value]) => { - message.entitiesById[Number(key)] = Entity.fromJSON(value); - }); - } + message.entitiesById = Object.entries(object.entitiesById ?? {}).reduce<{ [key: number]: Entity }>( + (acc, [key, value]) => { + acc[Number(key)] = Entity.fromJSON(value); + return acc; + }, + {} + ); return message; }, @@ -1190,14 +1197,15 @@ export const SimpleWithSnakeCaseMap = { fromPartial(object: DeepPartial): SimpleWithSnakeCaseMap { const message = { ...baseSimpleWithSnakeCaseMap } as SimpleWithSnakeCaseMap; - message.entitiesById = {}; - if (object.entitiesById !== undefined && object.entitiesById !== null) { - Object.entries(object.entitiesById).forEach(([key, value]) => { + message.entitiesById = Object.entries(object.entitiesById ?? {}).reduce<{ [key: number]: Entity }>( + (acc, [key, value]) => { if (value !== undefined) { - message.entitiesById[Number(key)] = Entity.fromPartial(value); + acc[Number(key)] = Entity.fromPartial(value); } - }); - } + return acc; + }, + {} + ); return message; }, }; diff --git a/integration/simple-snake/simple.ts b/integration/simple-snake/simple.ts index 0dea90a41..4ce1a42aa 100644 --- a/integration/simple-snake/simple.ts +++ b/integration/simple-snake/simple.ts @@ -892,24 +892,27 @@ export const SimpleWithMap = { fromJSON(object: any): SimpleWithMap { const message = { ...baseSimpleWithMap } as SimpleWithMap; - message.entitiesById = {}; - if (object.entitiesById !== undefined && object.entitiesById !== null) { - Object.entries(object.entitiesById).forEach(([key, value]) => { - message.entitiesById[Number(key)] = Entity.fromJSON(value); - }); - } - message.nameLookup = {}; - if (object.nameLookup !== undefined && object.nameLookup !== null) { - Object.entries(object.nameLookup).forEach(([key, value]) => { - message.nameLookup[key] = String(value); - }); - } - message.intLookup = {}; - if (object.intLookup !== undefined && object.intLookup !== null) { - Object.entries(object.intLookup).forEach(([key, value]) => { - message.intLookup[Number(key)] = Number(value); - }); - } + message.entitiesById = Object.entries(object.entitiesById ?? {}).reduce<{ [key: number]: Entity }>( + (acc, [key, value]) => { + acc[Number(key)] = Entity.fromJSON(value); + return acc; + }, + {} + ); + message.nameLookup = Object.entries(object.nameLookup ?? {}).reduce<{ [key: string]: string }>( + (acc, [key, value]) => { + acc[key] = String(value); + return acc; + }, + {} + ); + message.intLookup = Object.entries(object.intLookup ?? {}).reduce<{ [key: number]: number }>( + (acc, [key, value]) => { + acc[Number(key)] = Number(value); + return acc; + }, + {} + ); return message; }, @@ -938,30 +941,33 @@ export const SimpleWithMap = { fromPartial(object: DeepPartial): SimpleWithMap { const message = { ...baseSimpleWithMap } as SimpleWithMap; - message.entitiesById = {}; - if (object.entitiesById !== undefined && object.entitiesById !== null) { - Object.entries(object.entitiesById).forEach(([key, value]) => { + message.entitiesById = Object.entries(object.entitiesById ?? {}).reduce<{ [key: number]: Entity }>( + (acc, [key, value]) => { if (value !== undefined) { - message.entitiesById[Number(key)] = Entity.fromPartial(value); + acc[Number(key)] = Entity.fromPartial(value); } - }); - } - message.nameLookup = {}; - if (object.nameLookup !== undefined && object.nameLookup !== null) { - Object.entries(object.nameLookup).forEach(([key, value]) => { + return acc; + }, + {} + ); + message.nameLookup = Object.entries(object.nameLookup ?? {}).reduce<{ [key: string]: string }>( + (acc, [key, value]) => { if (value !== undefined) { - message.nameLookup[key] = String(value); + acc[key] = String(value); } - }); - } - message.intLookup = {}; - if (object.intLookup !== undefined && object.intLookup !== null) { - Object.entries(object.intLookup).forEach(([key, value]) => { + return acc; + }, + {} + ); + message.intLookup = Object.entries(object.intLookup ?? {}).reduce<{ [key: number]: number }>( + (acc, [key, value]) => { if (value !== undefined) { - message.intLookup[Number(key)] = Number(value); + acc[Number(key)] = Number(value); } - }); - } + return acc; + }, + {} + ); return message; }, }; @@ -1168,12 +1174,13 @@ export const SimpleWithSnakeCaseMap = { fromJSON(object: any): SimpleWithSnakeCaseMap { const message = { ...baseSimpleWithSnakeCaseMap } as SimpleWithSnakeCaseMap; - message.entities_by_id = {}; - if (object.entities_by_id !== undefined && object.entities_by_id !== null) { - Object.entries(object.entities_by_id).forEach(([key, value]) => { - message.entities_by_id[Number(key)] = Entity.fromJSON(value); - }); - } + message.entities_by_id = Object.entries(object.entities_by_id ?? {}).reduce<{ [key: number]: Entity }>( + (acc, [key, value]) => { + acc[Number(key)] = Entity.fromJSON(value); + return acc; + }, + {} + ); return message; }, @@ -1190,14 +1197,15 @@ export const SimpleWithSnakeCaseMap = { fromPartial(object: DeepPartial): SimpleWithSnakeCaseMap { const message = { ...baseSimpleWithSnakeCaseMap } as SimpleWithSnakeCaseMap; - message.entities_by_id = {}; - if (object.entities_by_id !== undefined && object.entities_by_id !== null) { - Object.entries(object.entities_by_id).forEach(([key, value]) => { + message.entities_by_id = Object.entries(object.entities_by_id ?? {}).reduce<{ [key: number]: Entity }>( + (acc, [key, value]) => { if (value !== undefined) { - message.entities_by_id[Number(key)] = Entity.fromPartial(value); + acc[Number(key)] = Entity.fromPartial(value); } - }); - } + return acc; + }, + {} + ); return message; }, }; diff --git a/integration/simple-unrecognized-enum/simple.ts b/integration/simple-unrecognized-enum/simple.ts index 2de1a45ef..d5d6ac01b 100644 --- a/integration/simple-unrecognized-enum/simple.ts +++ b/integration/simple-unrecognized-enum/simple.ts @@ -883,24 +883,27 @@ export const SimpleWithMap = { fromJSON(object: any): SimpleWithMap { const message = { ...baseSimpleWithMap } as SimpleWithMap; - message.entitiesById = {}; - if (object.entitiesById !== undefined && object.entitiesById !== null) { - Object.entries(object.entitiesById).forEach(([key, value]) => { - message.entitiesById[Number(key)] = Entity.fromJSON(value); - }); - } - message.nameLookup = {}; - if (object.nameLookup !== undefined && object.nameLookup !== null) { - Object.entries(object.nameLookup).forEach(([key, value]) => { - message.nameLookup[key] = String(value); - }); - } - message.intLookup = {}; - if (object.intLookup !== undefined && object.intLookup !== null) { - Object.entries(object.intLookup).forEach(([key, value]) => { - message.intLookup[Number(key)] = Number(value); - }); - } + message.entitiesById = Object.entries(object.entitiesById ?? {}).reduce<{ [key: number]: Entity }>( + (acc, [key, value]) => { + acc[Number(key)] = Entity.fromJSON(value); + return acc; + }, + {} + ); + message.nameLookup = Object.entries(object.nameLookup ?? {}).reduce<{ [key: string]: string }>( + (acc, [key, value]) => { + acc[key] = String(value); + return acc; + }, + {} + ); + message.intLookup = Object.entries(object.intLookup ?? {}).reduce<{ [key: number]: number }>( + (acc, [key, value]) => { + acc[Number(key)] = Number(value); + return acc; + }, + {} + ); return message; }, @@ -929,30 +932,33 @@ export const SimpleWithMap = { fromPartial(object: DeepPartial): SimpleWithMap { const message = { ...baseSimpleWithMap } as SimpleWithMap; - message.entitiesById = {}; - if (object.entitiesById !== undefined && object.entitiesById !== null) { - Object.entries(object.entitiesById).forEach(([key, value]) => { + message.entitiesById = Object.entries(object.entitiesById ?? {}).reduce<{ [key: number]: Entity }>( + (acc, [key, value]) => { if (value !== undefined) { - message.entitiesById[Number(key)] = Entity.fromPartial(value); + acc[Number(key)] = Entity.fromPartial(value); } - }); - } - message.nameLookup = {}; - if (object.nameLookup !== undefined && object.nameLookup !== null) { - Object.entries(object.nameLookup).forEach(([key, value]) => { + return acc; + }, + {} + ); + message.nameLookup = Object.entries(object.nameLookup ?? {}).reduce<{ [key: string]: string }>( + (acc, [key, value]) => { if (value !== undefined) { - message.nameLookup[key] = String(value); + acc[key] = String(value); } - }); - } - message.intLookup = {}; - if (object.intLookup !== undefined && object.intLookup !== null) { - Object.entries(object.intLookup).forEach(([key, value]) => { + return acc; + }, + {} + ); + message.intLookup = Object.entries(object.intLookup ?? {}).reduce<{ [key: number]: number }>( + (acc, [key, value]) => { if (value !== undefined) { - message.intLookup[Number(key)] = Number(value); + acc[Number(key)] = Number(value); } - }); - } + return acc; + }, + {} + ); return message; }, }; @@ -1159,12 +1165,13 @@ export const SimpleWithSnakeCaseMap = { fromJSON(object: any): SimpleWithSnakeCaseMap { const message = { ...baseSimpleWithSnakeCaseMap } as SimpleWithSnakeCaseMap; - message.entitiesById = {}; - if (object.entitiesById !== undefined && object.entitiesById !== null) { - Object.entries(object.entitiesById).forEach(([key, value]) => { - message.entitiesById[Number(key)] = Entity.fromJSON(value); - }); - } + message.entitiesById = Object.entries(object.entitiesById ?? {}).reduce<{ [key: number]: Entity }>( + (acc, [key, value]) => { + acc[Number(key)] = Entity.fromJSON(value); + return acc; + }, + {} + ); return message; }, @@ -1181,14 +1188,15 @@ export const SimpleWithSnakeCaseMap = { fromPartial(object: DeepPartial): SimpleWithSnakeCaseMap { const message = { ...baseSimpleWithSnakeCaseMap } as SimpleWithSnakeCaseMap; - message.entitiesById = {}; - if (object.entitiesById !== undefined && object.entitiesById !== null) { - Object.entries(object.entitiesById).forEach(([key, value]) => { + message.entitiesById = Object.entries(object.entitiesById ?? {}).reduce<{ [key: number]: Entity }>( + (acc, [key, value]) => { if (value !== undefined) { - message.entitiesById[Number(key)] = Entity.fromPartial(value); + acc[Number(key)] = Entity.fromPartial(value); } - }); - } + return acc; + }, + {} + ); return message; }, }; diff --git a/integration/simple/simple.ts b/integration/simple/simple.ts index f2329834f..93d273821 100644 --- a/integration/simple/simple.ts +++ b/integration/simple/simple.ts @@ -1035,48 +1035,54 @@ export const SimpleWithMap = { fromJSON(object: any): SimpleWithMap { const message = { ...baseSimpleWithMap } as SimpleWithMap; - message.entitiesById = {}; - if (object.entitiesById !== undefined && object.entitiesById !== null) { - Object.entries(object.entitiesById).forEach(([key, value]) => { - message.entitiesById[Number(key)] = Entity.fromJSON(value); - }); - } - message.nameLookup = {}; - if (object.nameLookup !== undefined && object.nameLookup !== null) { - Object.entries(object.nameLookup).forEach(([key, value]) => { - message.nameLookup[key] = String(value); - }); - } - message.intLookup = {}; - if (object.intLookup !== undefined && object.intLookup !== null) { - Object.entries(object.intLookup).forEach(([key, value]) => { - message.intLookup[Number(key)] = Number(value); - }); - } - message.mapOfTimestamps = {}; - if (object.mapOfTimestamps !== undefined && object.mapOfTimestamps !== null) { - Object.entries(object.mapOfTimestamps).forEach(([key, value]) => { - message.mapOfTimestamps[key] = fromJsonTimestamp(value); - }); - } - message.mapOfBytes = {}; - if (object.mapOfBytes !== undefined && object.mapOfBytes !== null) { - Object.entries(object.mapOfBytes).forEach(([key, value]) => { - message.mapOfBytes[key] = bytesFromBase64(value as string); - }); - } - message.mapOfStringValues = {}; - if (object.mapOfStringValues !== undefined && object.mapOfStringValues !== null) { - Object.entries(object.mapOfStringValues).forEach(([key, value]) => { - message.mapOfStringValues[key] = value as string | undefined; - }); - } - message.longLookup = {}; - if (object.longLookup !== undefined && object.longLookup !== null) { - Object.entries(object.longLookup).forEach(([key, value]) => { - message.longLookup[Number(key)] = Number(value); - }); - } + message.entitiesById = Object.entries(object.entitiesById ?? {}).reduce<{ [key: number]: Entity }>( + (acc, [key, value]) => { + acc[Number(key)] = Entity.fromJSON(value); + return acc; + }, + {} + ); + message.nameLookup = Object.entries(object.nameLookup ?? {}).reduce<{ [key: string]: string }>( + (acc, [key, value]) => { + acc[key] = String(value); + return acc; + }, + {} + ); + message.intLookup = Object.entries(object.intLookup ?? {}).reduce<{ [key: number]: number }>( + (acc, [key, value]) => { + acc[Number(key)] = Number(value); + return acc; + }, + {} + ); + message.mapOfTimestamps = Object.entries(object.mapOfTimestamps ?? {}).reduce<{ [key: string]: Date }>( + (acc, [key, value]) => { + acc[key] = fromJsonTimestamp(value); + return acc; + }, + {} + ); + message.mapOfBytes = Object.entries(object.mapOfBytes ?? {}).reduce<{ [key: string]: Uint8Array }>( + (acc, [key, value]) => { + acc[key] = bytesFromBase64(value as string); + return acc; + }, + {} + ); + message.mapOfStringValues = Object.entries(object.mapOfStringValues ?? {}).reduce<{ + [key: string]: string | undefined; + }>((acc, [key, value]) => { + acc[key] = value as string | undefined; + return acc; + }, {}); + message.longLookup = Object.entries(object.longLookup ?? {}).reduce<{ [key: number]: number }>( + (acc, [key, value]) => { + acc[Number(key)] = Number(value); + return acc; + }, + {} + ); return message; }, @@ -1129,62 +1135,68 @@ export const SimpleWithMap = { fromPartial(object: DeepPartial): SimpleWithMap { const message = { ...baseSimpleWithMap } as SimpleWithMap; - message.entitiesById = {}; - if (object.entitiesById !== undefined && object.entitiesById !== null) { - Object.entries(object.entitiesById).forEach(([key, value]) => { + message.entitiesById = Object.entries(object.entitiesById ?? {}).reduce<{ [key: number]: Entity }>( + (acc, [key, value]) => { if (value !== undefined) { - message.entitiesById[Number(key)] = Entity.fromPartial(value); + acc[Number(key)] = Entity.fromPartial(value); } - }); - } - message.nameLookup = {}; - if (object.nameLookup !== undefined && object.nameLookup !== null) { - Object.entries(object.nameLookup).forEach(([key, value]) => { - if (value !== undefined) { - message.nameLookup[key] = String(value); - } - }); - } - message.intLookup = {}; - if (object.intLookup !== undefined && object.intLookup !== null) { - Object.entries(object.intLookup).forEach(([key, value]) => { + return acc; + }, + {} + ); + message.nameLookup = Object.entries(object.nameLookup ?? {}).reduce<{ [key: string]: string }>( + (acc, [key, value]) => { if (value !== undefined) { - message.intLookup[Number(key)] = Number(value); + acc[key] = String(value); } - }); - } - message.mapOfTimestamps = {}; - if (object.mapOfTimestamps !== undefined && object.mapOfTimestamps !== null) { - Object.entries(object.mapOfTimestamps).forEach(([key, value]) => { + return acc; + }, + {} + ); + message.intLookup = Object.entries(object.intLookup ?? {}).reduce<{ [key: number]: number }>( + (acc, [key, value]) => { if (value !== undefined) { - message.mapOfTimestamps[key] = value; + acc[Number(key)] = Number(value); } - }); - } - message.mapOfBytes = {}; - if (object.mapOfBytes !== undefined && object.mapOfBytes !== null) { - Object.entries(object.mapOfBytes).forEach(([key, value]) => { + return acc; + }, + {} + ); + message.mapOfTimestamps = Object.entries(object.mapOfTimestamps ?? {}).reduce<{ [key: string]: Date }>( + (acc, [key, value]) => { if (value !== undefined) { - message.mapOfBytes[key] = value; + acc[key] = value; } - }); - } - message.mapOfStringValues = {}; - if (object.mapOfStringValues !== undefined && object.mapOfStringValues !== null) { - Object.entries(object.mapOfStringValues).forEach(([key, value]) => { + return acc; + }, + {} + ); + message.mapOfBytes = Object.entries(object.mapOfBytes ?? {}).reduce<{ [key: string]: Uint8Array }>( + (acc, [key, value]) => { if (value !== undefined) { - message.mapOfStringValues[key] = value; + acc[key] = value; } - }); - } - message.longLookup = {}; - if (object.longLookup !== undefined && object.longLookup !== null) { - Object.entries(object.longLookup).forEach(([key, value]) => { + return acc; + }, + {} + ); + message.mapOfStringValues = Object.entries(object.mapOfStringValues ?? {}).reduce<{ + [key: string]: string | undefined; + }>((acc, [key, value]) => { + if (value !== undefined) { + acc[key] = value; + } + return acc; + }, {}); + message.longLookup = Object.entries(object.longLookup ?? {}).reduce<{ [key: number]: number }>( + (acc, [key, value]) => { if (value !== undefined) { - message.longLookup[Number(key)] = Number(value); + acc[Number(key)] = Number(value); } - }); - } + return acc; + }, + {} + ); return message; }, }; @@ -1618,12 +1630,13 @@ export const SimpleWithSnakeCaseMap = { fromJSON(object: any): SimpleWithSnakeCaseMap { const message = { ...baseSimpleWithSnakeCaseMap } as SimpleWithSnakeCaseMap; - message.entitiesById = {}; - if (object.entitiesById !== undefined && object.entitiesById !== null) { - Object.entries(object.entitiesById).forEach(([key, value]) => { - message.entitiesById[Number(key)] = Entity.fromJSON(value); - }); - } + message.entitiesById = Object.entries(object.entitiesById ?? {}).reduce<{ [key: number]: Entity }>( + (acc, [key, value]) => { + acc[Number(key)] = Entity.fromJSON(value); + return acc; + }, + {} + ); return message; }, @@ -1640,14 +1653,15 @@ export const SimpleWithSnakeCaseMap = { fromPartial(object: DeepPartial): SimpleWithSnakeCaseMap { const message = { ...baseSimpleWithSnakeCaseMap } as SimpleWithSnakeCaseMap; - message.entitiesById = {}; - if (object.entitiesById !== undefined && object.entitiesById !== null) { - Object.entries(object.entitiesById).forEach(([key, value]) => { + message.entitiesById = Object.entries(object.entitiesById ?? {}).reduce<{ [key: number]: Entity }>( + (acc, [key, value]) => { if (value !== undefined) { - message.entitiesById[Number(key)] = Entity.fromPartial(value); + acc[Number(key)] = Entity.fromPartial(value); } - }); - } + return acc; + }, + {} + ); return message; }, }; @@ -1742,12 +1756,13 @@ export const SimpleWithMapOfEnums = { fromJSON(object: any): SimpleWithMapOfEnums { const message = { ...baseSimpleWithMapOfEnums } as SimpleWithMapOfEnums; - message.enumsById = {}; - if (object.enumsById !== undefined && object.enumsById !== null) { - Object.entries(object.enumsById).forEach(([key, value]) => { - message.enumsById[Number(key)] = value as number; - }); - } + message.enumsById = Object.entries(object.enumsById ?? {}).reduce<{ [key: number]: StateEnum }>( + (acc, [key, value]) => { + acc[Number(key)] = value as number; + return acc; + }, + {} + ); return message; }, @@ -1764,14 +1779,15 @@ export const SimpleWithMapOfEnums = { fromPartial(object: DeepPartial): SimpleWithMapOfEnums { const message = { ...baseSimpleWithMapOfEnums } as SimpleWithMapOfEnums; - message.enumsById = {}; - if (object.enumsById !== undefined && object.enumsById !== null) { - Object.entries(object.enumsById).forEach(([key, value]) => { + message.enumsById = Object.entries(object.enumsById ?? {}).reduce<{ [key: number]: StateEnum }>( + (acc, [key, value]) => { if (value !== undefined) { - message.enumsById[Number(key)] = value as number; + acc[Number(key)] = value as number; } - }); - } + return acc; + }, + {} + ); return message; }, }; diff --git a/integration/struct/google/protobuf/struct.ts b/integration/struct/google/protobuf/struct.ts index d54b6ca9b..20465f053 100644 --- a/integration/struct/google/protobuf/struct.ts +++ b/integration/struct/google/protobuf/struct.ts @@ -126,12 +126,13 @@ export const Struct = { fromJSON(object: any): Struct { const message = { ...baseStruct } as Struct; - message.fields = {}; - if (object.fields !== undefined && object.fields !== null) { - Object.entries(object.fields).forEach(([key, value]) => { - message.fields[key] = value as any | undefined; - }); - } + message.fields = Object.entries(object.fields ?? {}).reduce<{ [key: string]: any | undefined }>( + (acc, [key, value]) => { + acc[key] = value as any | undefined; + return acc; + }, + {} + ); return message; }, @@ -148,14 +149,15 @@ export const Struct = { fromPartial(object: DeepPartial): Struct { const message = { ...baseStruct } as Struct; - message.fields = {}; - if (object.fields !== undefined && object.fields !== null) { - Object.entries(object.fields).forEach(([key, value]) => { + message.fields = Object.entries(object.fields ?? {}).reduce<{ [key: string]: any | undefined }>( + (acc, [key, value]) => { if (value !== undefined) { - message.fields[key] = value; + acc[key] = value; } - }); - } + return acc; + }, + {} + ); return message; }, }; diff --git a/integration/use-date-string/use-date-string.ts b/integration/use-date-string/use-date-string.ts index 57bf66733..d7e15656f 100644 --- a/integration/use-date-string/use-date-string.ts +++ b/integration/use-date-string/use-date-string.ts @@ -85,12 +85,13 @@ export const Todo = { object.optionalTimestamp !== undefined && object.optionalTimestamp !== null ? String(object.optionalTimestamp) : undefined; - message.mapOfTimestamps = {}; - if (object.mapOfTimestamps !== undefined && object.mapOfTimestamps !== null) { - Object.entries(object.mapOfTimestamps).forEach(([key, value]) => { - message.mapOfTimestamps[key] = String(value); - }); - } + message.mapOfTimestamps = Object.entries(object.mapOfTimestamps ?? {}).reduce<{ [key: string]: string }>( + (acc, [key, value]) => { + acc[key] = String(value); + return acc; + }, + {} + ); return message; }, @@ -119,14 +120,15 @@ export const Todo = { message.timestamp = object.timestamp ?? undefined; message.repeatedTimestamp = (object.repeatedTimestamp ?? []).map((e) => e); message.optionalTimestamp = object.optionalTimestamp ?? undefined; - message.mapOfTimestamps = {}; - if (object.mapOfTimestamps !== undefined && object.mapOfTimestamps !== null) { - Object.entries(object.mapOfTimestamps).forEach(([key, value]) => { + message.mapOfTimestamps = Object.entries(object.mapOfTimestamps ?? {}).reduce<{ [key: string]: string }>( + (acc, [key, value]) => { if (value !== undefined) { - message.mapOfTimestamps[key] = value; + acc[key] = value; } - }); - } + return acc; + }, + {} + ); return message; }, }; diff --git a/integration/use-date-true/use-date-true.ts b/integration/use-date-true/use-date-true.ts index d820bf767..172e251c7 100644 --- a/integration/use-date-true/use-date-true.ts +++ b/integration/use-date-true/use-date-true.ts @@ -85,12 +85,13 @@ export const Todo = { object.optionalTimestamp !== undefined && object.optionalTimestamp !== null ? fromJsonTimestamp(object.optionalTimestamp) : undefined; - message.mapOfTimestamps = {}; - if (object.mapOfTimestamps !== undefined && object.mapOfTimestamps !== null) { - Object.entries(object.mapOfTimestamps).forEach(([key, value]) => { - message.mapOfTimestamps[key] = fromJsonTimestamp(value); - }); - } + message.mapOfTimestamps = Object.entries(object.mapOfTimestamps ?? {}).reduce<{ [key: string]: Date }>( + (acc, [key, value]) => { + acc[key] = fromJsonTimestamp(value); + return acc; + }, + {} + ); return message; }, @@ -119,14 +120,15 @@ export const Todo = { message.timestamp = object.timestamp ?? undefined; message.repeatedTimestamp = (object.repeatedTimestamp ?? []).map((e) => e); message.optionalTimestamp = object.optionalTimestamp ?? undefined; - message.mapOfTimestamps = {}; - if (object.mapOfTimestamps !== undefined && object.mapOfTimestamps !== null) { - Object.entries(object.mapOfTimestamps).forEach(([key, value]) => { + message.mapOfTimestamps = Object.entries(object.mapOfTimestamps ?? {}).reduce<{ [key: string]: Date }>( + (acc, [key, value]) => { if (value !== undefined) { - message.mapOfTimestamps[key] = value; + acc[key] = value; } - }); - } + return acc; + }, + {} + ); return message; }, }; diff --git a/integration/value/google/protobuf/struct.ts b/integration/value/google/protobuf/struct.ts index d54b6ca9b..20465f053 100644 --- a/integration/value/google/protobuf/struct.ts +++ b/integration/value/google/protobuf/struct.ts @@ -126,12 +126,13 @@ export const Struct = { fromJSON(object: any): Struct { const message = { ...baseStruct } as Struct; - message.fields = {}; - if (object.fields !== undefined && object.fields !== null) { - Object.entries(object.fields).forEach(([key, value]) => { - message.fields[key] = value as any | undefined; - }); - } + message.fields = Object.entries(object.fields ?? {}).reduce<{ [key: string]: any | undefined }>( + (acc, [key, value]) => { + acc[key] = value as any | undefined; + return acc; + }, + {} + ); return message; }, @@ -148,14 +149,15 @@ export const Struct = { fromPartial(object: DeepPartial): Struct { const message = { ...baseStruct } as Struct; - message.fields = {}; - if (object.fields !== undefined && object.fields !== null) { - Object.entries(object.fields).forEach(([key, value]) => { + message.fields = Object.entries(object.fields ?? {}).reduce<{ [key: string]: any | undefined }>( + (acc, [key, value]) => { if (value !== undefined) { - message.fields[key] = value; + acc[key] = value; } - }); - } + return acc; + }, + {} + ); return message; }, }; diff --git a/src/main.ts b/src/main.ts index 3f2436aae..e9f196a9e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1082,15 +1082,14 @@ function generateFromJson(ctx: Context, fullName: string, messageDesc: Descripto // and then use the snippet to handle repeated fields if necessary if (isRepeated(field)) { if (isMapType(ctx, messageDesc, field)) { - chunks.push(code`message.${fieldName} = {};`); - chunks.push(code`if (object.${fieldName} !== undefined && object.${fieldName} !== null) {`); + const fieldType = toTypeName(ctx, messageDesc, field); const i = maybeCastToNumber(ctx, messageDesc, field, 'key'); chunks.push(code` - Object.entries(object.${fieldName}).forEach(([key, value]) => { - message.${fieldName}[${i}] = ${readSnippet('value')}; - }); + message.${fieldName} = Object.entries(object.${fieldName} ?? {}).reduce<${fieldType}>((acc, [key, value]) => { + acc[${i}] = ${readSnippet('value')}; + return acc; + }, {}); `); - chunks.push(code`}`); } else if (isAnyValueType(field)) { chunks.push(code` message.${fieldName} = Array.isArray(object?.${fieldName}) ? [...object.${fieldName}] : []; @@ -1303,17 +1302,16 @@ function generateFromPartial(ctx: Context, fullName: string, messageDesc: Descri // and then use the snippet to handle repeated fields if necessary if (isRepeated(field)) { if (isMapType(ctx, messageDesc, field)) { - chunks.push(code`message.${fieldName} = {};`); - chunks.push(code`if (object.${fieldName} !== undefined && object.${fieldName} !== null) {`); + const fieldType = toTypeName(ctx, messageDesc, field); const i = maybeCastToNumber(ctx, messageDesc, field, 'key'); chunks.push(code` - Object.entries(object.${fieldName}).forEach(([key, value]) => { + message.${fieldName} = Object.entries(object.${fieldName} ?? {}).reduce<${fieldType}>((acc, [key, value]) => { if (value !== undefined) { - message.${fieldName}[${i}] = ${readSnippet('value')}; + acc[${i}] = ${readSnippet('value')}; } - }); + return acc; + }, {}); `); - chunks.push(code`}`); } else { chunks.push(code` message.${fieldName} = (object.${fieldName} ?? []).map((e) => ${readSnippet('e')});