From 2b28dc574f4e2d8451772e0e97ed4f550045065a Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Tue, 26 Oct 2021 10:34:36 +0200 Subject: [PATCH] Include more cases in the optimized version --- integration/grpc-js/simple.ts | 6 +-- integration/simple-long-string/simple.ts | 12 ++---- integration/simple-long/simple.ts | 18 +++----- .../simple-optionals/import_dir/thing.ts | 6 +-- integration/simple-optionals/simple.ts | 24 ++++------- integration/simple-optionals/thing.ts | 6 +-- integration/simple-snake/import_dir/thing.ts | 6 +-- integration/simple-snake/simple.ts | 24 ++++------- .../import_dir/thing.ts | 6 +-- .../simple-unrecognized-enum/simple.ts | 24 ++++------- integration/simple/import_dir/thing.ts | 6 +-- integration/simple/simple.ts | 42 +++++++------------ integration/type-registry/foo.ts | 12 ++---- .../use-date-string/use-date-string.ts | 18 +++----- integration/use-date-true/use-date-true.ts | 18 +++----- src/main.ts | 9 +++- 16 files changed, 83 insertions(+), 154 deletions(-) diff --git a/integration/grpc-js/simple.ts b/integration/grpc-js/simple.ts index b8928314b..0f1f16bd5 100644 --- a/integration/grpc-js/simple.ts +++ b/integration/grpc-js/simple.ts @@ -85,10 +85,8 @@ 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/simple-long-string/simple.ts b/integration/simple-long-string/simple.ts index e5b66125f..e62424f93 100644 --- a/integration/simple-long-string/simple.ts +++ b/integration/simple-long-string/simple.ts @@ -274,15 +274,11 @@ export const Numbers = { { message.sfixed64 = object.sfixed64 ?? '0'; } - if (object.guint64 !== undefined && object.guint64 !== null) { - message.guint64 = object.guint64; - } else { - message.guint64 = undefined; + { + message.guint64 = object.guint64 ?? undefined; } - 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/simple-long/simple.ts b/integration/simple-long/simple.ts index 1777a8dc8..a75b0be25 100644 --- a/integration/simple-long/simple.ts +++ b/integration/simple-long/simple.ts @@ -164,20 +164,14 @@ export const SimpleWithWrappers = { fromPartial(object: DeepPartial): SimpleWithWrappers { const message = { ...baseSimpleWithWrappers } as SimpleWithWrappers; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = undefined; + { + message.name = object.name ?? undefined; } - if (object.age !== undefined && object.age !== null) { - message.age = object.age; - } else { - message.age = undefined; + { + message.age = object.age ?? undefined; } - if (object.enabled !== undefined && object.enabled !== null) { - message.enabled = object.enabled; - } else { - message.enabled = undefined; + { + message.enabled = object.enabled ?? undefined; } if (object.bananas !== undefined && object.bananas !== null) { message.bananas = object.bananas as Long | undefined; diff --git a/integration/simple-optionals/import_dir/thing.ts b/integration/simple-optionals/import_dir/thing.ts index 8acd814c2..c0eee8737 100644 --- a/integration/simple-optionals/import_dir/thing.ts +++ b/integration/simple-optionals/import_dir/thing.ts @@ -55,10 +55,8 @@ 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 c52244226..fe6844dbf 100644 --- a/integration/simple-optionals/simple.ts +++ b/integration/simple-optionals/simple.ts @@ -437,10 +437,8 @@ export const Simple = { { message.age = object.age ?? 0; } - if (object.createdAt !== undefined && object.createdAt !== null) { - message.createdAt = object.createdAt; - } else { - message.createdAt = undefined; + { + message.createdAt = object.createdAt ?? undefined; } if (object.child !== undefined && object.child !== null) { message.child = Child.fromPartial(object.child); @@ -935,20 +933,14 @@ export const SimpleWithWrappers = { fromPartial(object: DeepPartial): SimpleWithWrappers { const message = { ...baseSimpleWithWrappers } as SimpleWithWrappers; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = undefined; + { + message.name = object.name ?? undefined; } - if (object.age !== undefined && object.age !== null) { - message.age = object.age; - } else { - message.age = undefined; + { + message.age = object.age ?? undefined; } - if (object.enabled !== undefined && object.enabled !== null) { - message.enabled = object.enabled; - } else { - message.enabled = undefined; + { + message.enabled = object.enabled ?? undefined; } message.coins = []; if (object.coins !== undefined && object.coins !== null) { diff --git a/integration/simple-optionals/thing.ts b/integration/simple-optionals/thing.ts index 609e9bc3e..2a13db33f 100644 --- a/integration/simple-optionals/thing.ts +++ b/integration/simple-optionals/thing.ts @@ -55,10 +55,8 @@ 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-snake/import_dir/thing.ts b/integration/simple-snake/import_dir/thing.ts index 050b04f5a..9041fa33c 100644 --- a/integration/simple-snake/import_dir/thing.ts +++ b/integration/simple-snake/import_dir/thing.ts @@ -55,10 +55,8 @@ 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 276159648..290bdd1c3 100644 --- a/integration/simple-snake/simple.ts +++ b/integration/simple-snake/simple.ts @@ -437,10 +437,8 @@ export const Simple = { { message.age = object.age ?? 0; } - 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; } if (object.child !== undefined && object.child !== null) { message.child = Child.fromPartial(object.child); @@ -935,20 +933,14 @@ export const SimpleWithWrappers = { fromPartial(object: DeepPartial): SimpleWithWrappers { const message = { ...baseSimpleWithWrappers } as SimpleWithWrappers; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = undefined; + { + message.name = object.name ?? undefined; } - if (object.age !== undefined && object.age !== null) { - message.age = object.age; - } else { - message.age = undefined; + { + message.age = object.age ?? undefined; } - if (object.enabled !== undefined && object.enabled !== null) { - message.enabled = object.enabled; - } else { - message.enabled = undefined; + { + message.enabled = object.enabled ?? undefined; } message.coins = []; if (object.coins !== undefined && object.coins !== null) { diff --git a/integration/simple-unrecognized-enum/import_dir/thing.ts b/integration/simple-unrecognized-enum/import_dir/thing.ts index 9c777434d..527ac1a17 100644 --- a/integration/simple-unrecognized-enum/import_dir/thing.ts +++ b/integration/simple-unrecognized-enum/import_dir/thing.ts @@ -55,10 +55,8 @@ 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 a602ab9d8..cda6d70a4 100644 --- a/integration/simple-unrecognized-enum/simple.ts +++ b/integration/simple-unrecognized-enum/simple.ts @@ -428,10 +428,8 @@ export const Simple = { { message.age = object.age ?? 0; } - if (object.createdAt !== undefined && object.createdAt !== null) { - message.createdAt = object.createdAt; - } else { - message.createdAt = undefined; + { + message.createdAt = object.createdAt ?? undefined; } if (object.child !== undefined && object.child !== null) { message.child = Child.fromPartial(object.child); @@ -926,20 +924,14 @@ export const SimpleWithWrappers = { fromPartial(object: DeepPartial): SimpleWithWrappers { const message = { ...baseSimpleWithWrappers } as SimpleWithWrappers; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = undefined; + { + message.name = object.name ?? undefined; } - if (object.age !== undefined && object.age !== null) { - message.age = object.age; - } else { - message.age = undefined; + { + message.age = object.age ?? undefined; } - if (object.enabled !== undefined && object.enabled !== null) { - message.enabled = object.enabled; - } else { - message.enabled = undefined; + { + message.enabled = object.enabled ?? undefined; } message.coins = []; if (object.coins !== undefined && object.coins !== null) { diff --git a/integration/simple/import_dir/thing.ts b/integration/simple/import_dir/thing.ts index 9c777434d..527ac1a17 100644 --- a/integration/simple/import_dir/thing.ts +++ b/integration/simple/import_dir/thing.ts @@ -55,10 +55,8 @@ 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 6f7a5c1fa..e0013c70c 100644 --- a/integration/simple/simple.ts +++ b/integration/simple/simple.ts @@ -524,10 +524,8 @@ export const Simple = { { message.age = object.age ?? 0; } - if (object.createdAt !== undefined && object.createdAt !== null) { - message.createdAt = object.createdAt; - } else { - message.createdAt = undefined; + { + message.createdAt = object.createdAt ?? undefined; } if (object.child !== undefined && object.child !== null) { message.child = Child.fromPartial(object.child); @@ -1048,20 +1046,14 @@ export const SimpleWithWrappers = { fromPartial(object: DeepPartial): SimpleWithWrappers { const message = { ...baseSimpleWithWrappers } as SimpleWithWrappers; - if (object.name !== undefined && object.name !== null) { - message.name = object.name; - } else { - message.name = undefined; + { + message.name = object.name ?? undefined; } - if (object.age !== undefined && object.age !== null) { - message.age = object.age; - } else { - message.age = undefined; + { + message.age = object.age ?? undefined; } - if (object.enabled !== undefined && object.enabled !== null) { - message.enabled = object.enabled; - } else { - message.enabled = undefined; + { + message.enabled = object.enabled ?? undefined; } message.coins = []; if (object.coins !== undefined && object.coins !== null) { @@ -1075,10 +1067,8 @@ export const SimpleWithWrappers = { 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; }, @@ -1592,10 +1582,8 @@ export const SimpleWithMap_MapOfTimestampsEntry = { { message.key = object.key ?? ''; } - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = undefined; + { + message.value = object.value ?? undefined; } return message; }, @@ -2407,10 +2395,8 @@ export const SimpleButOptional = { { message.age = object.age ?? undefined; } - if (object.createdAt !== undefined && object.createdAt !== null) { - message.createdAt = object.createdAt; - } else { - message.createdAt = undefined; + { + message.createdAt = object.createdAt ?? undefined; } if (object.child !== undefined && object.child !== null) { message.child = Child.fromPartial(object.child); diff --git a/integration/type-registry/foo.ts b/integration/type-registry/foo.ts index f54103137..e7400c2a4 100644 --- a/integration/type-registry/foo.ts +++ b/integration/type-registry/foo.ts @@ -64,10 +64,8 @@ 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,10 +121,8 @@ 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/use-date-string/use-date-string.ts b/integration/use-date-string/use-date-string.ts index 7a7502eb0..dabf8d37e 100644 --- a/integration/use-date-string/use-date-string.ts +++ b/integration/use-date-string/use-date-string.ts @@ -131,10 +131,8 @@ export const Todo = { { message.id = object.id ?? ''; } - if (object.timestamp !== undefined && object.timestamp !== null) { - message.timestamp = object.timestamp; - } else { - message.timestamp = undefined; + { + message.timestamp = object.timestamp ?? undefined; } message.repeatedTimestamp = []; if (object.repeatedTimestamp !== undefined && object.repeatedTimestamp !== null) { @@ -142,10 +140,8 @@ export const Todo = { 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) { @@ -220,10 +216,8 @@ export const Todo_MapOfTimestampsEntry = { { message.key = object.key ?? ''; } - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = undefined; + { + message.value = object.value ?? undefined; } return message; }, diff --git a/integration/use-date-true/use-date-true.ts b/integration/use-date-true/use-date-true.ts index 4bd0b7db9..4ef512bdd 100644 --- a/integration/use-date-true/use-date-true.ts +++ b/integration/use-date-true/use-date-true.ts @@ -131,10 +131,8 @@ export const Todo = { { message.id = object.id ?? ''; } - if (object.timestamp !== undefined && object.timestamp !== null) { - message.timestamp = object.timestamp; - } else { - message.timestamp = undefined; + { + message.timestamp = object.timestamp ?? undefined; } message.repeatedTimestamp = []; if (object.repeatedTimestamp !== undefined && object.repeatedTimestamp !== null) { @@ -142,10 +140,8 @@ export const Todo = { 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) { @@ -220,10 +216,8 @@ export const Todo_MapOfTimestampsEntry = { { message.key = object.key ?? ''; } - if (object.value !== undefined && object.value !== null) { - message.value = object.value; - } else { - message.value = undefined; + { + message.value = object.value ?? undefined; } return message; }, diff --git a/src/main.ts b/src/main.ts index c47c1211d..9d7edf548 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1215,9 +1215,14 @@ function generateFromPartial(ctx: Context, fullName: string, messageDesc: Descri chunks.push(code`} else {`); const fallback = isWithinOneOf(field) ? 'undefined' : defaultValue(ctx, field); chunks.push(code`message.${fieldName} = ${fallback}`); - } else if (isPrimitive(field)) { - const fallback = isWithinOneOf(field) ? 'undefined' : defaultValue(ctx, field); + } 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 chunks.push(code`{`); // Without this extra scope the code generation breaks 🤷. We don't really need it. + 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) {`);