Skip to content

Commit

Permalink
feat: Reduce code size by using nullish coalescing operator in fromPa…
Browse files Browse the repository at this point in the history
…rtial (#376)

* Move list initializers into setter block

* Regenerate code after intializer change

* Move else block up

* Duplicate undefined check and fallback

* Move Long case to top level

* Use nullish coalescing for primitive types

* Include more cases in the optimized version

* Fix weird braces.

Co-authored-by: Stephen Haberman <[email protected]>
  • Loading branch information
webmaster128 and stephenh authored Nov 2, 2021
1 parent db38120 commit 19d2ded
Show file tree
Hide file tree
Showing 61 changed files with 487 additions and 2,147 deletions.
6 changes: 1 addition & 5 deletions integration/angular/simple-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ export const SimpleMessage = {

fromPartial(object: DeepPartial<SimpleMessage>): 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;
},
};
Expand Down
18 changes: 3 additions & 15 deletions integration/avoid-import-conflicts/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,7 @@ export const Simple = {

fromPartial(object: DeepPartial<Simple>): 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 {
Expand Down Expand Up @@ -189,16 +185,8 @@ export const SimpleEnums = {

fromPartial(object: DeepPartial<SimpleEnums>): 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;
},
};
Expand Down
12 changes: 2 additions & 10 deletions integration/avoid-import-conflicts/simple2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,8 @@ export const Simple = {

fromPartial(object: DeepPartial<Simple>): 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;
},
};
Expand Down
12 changes: 2 additions & 10 deletions integration/barrel-imports/bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,8 @@ export const Bar = {

fromPartial(object: DeepPartial<Bar>): 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;
},
};
Expand Down
6 changes: 1 addition & 5 deletions integration/barrel-imports/foo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,7 @@ export const Foo = {

fromPartial(object: DeepPartial<Foo>): 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 {
Expand Down
30 changes: 5 additions & 25 deletions integration/batching-with-context/batching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,11 +358,7 @@ export const BatchMapQueryResponse_EntitiesEntry = {

fromPartial(object: DeepPartial<BatchMapQueryResponse_EntitiesEntry>): 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 {
Expand Down Expand Up @@ -418,11 +414,7 @@ export const GetOnlyMethodRequest = {

fromPartial(object: DeepPartial<GetOnlyMethodRequest>): 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;
},
};
Expand Down Expand Up @@ -528,11 +520,7 @@ export const WriteMethodRequest = {

fromPartial(object: DeepPartial<WriteMethodRequest>): 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;
},
};
Expand Down Expand Up @@ -633,16 +621,8 @@ export const Entity = {

fromPartial(object: DeepPartial<Entity>): 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;
},
};
Expand Down
30 changes: 5 additions & 25 deletions integration/batching/batching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,11 +356,7 @@ export const BatchMapQueryResponse_EntitiesEntry = {

fromPartial(object: DeepPartial<BatchMapQueryResponse_EntitiesEntry>): 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 {
Expand Down Expand Up @@ -416,11 +412,7 @@ export const GetOnlyMethodRequest = {

fromPartial(object: DeepPartial<GetOnlyMethodRequest>): 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;
},
};
Expand Down Expand Up @@ -526,11 +518,7 @@ export const WriteMethodRequest = {

fromPartial(object: DeepPartial<WriteMethodRequest>): 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;
},
};
Expand Down Expand Up @@ -631,16 +619,8 @@ export const Entity = {

fromPartial(object: DeepPartial<Entity>): 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;
},
};
Expand Down
6 changes: 1 addition & 5 deletions integration/bytes-as-base64/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ export const Message = {

fromPartial(object: DeepPartial<Message>): 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;
},
};
Expand Down
6 changes: 1 addition & 5 deletions integration/bytes-node/point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,7 @@ export const Point = {

fromPartial(object: DeepPartial<Point>): 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;
},
};
Expand Down
6 changes: 1 addition & 5 deletions integration/const-enum/const-enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,7 @@ export const DividerData = {

fromPartial(object: DeepPartial<DividerData>): 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;
},
};
Expand Down
6 changes: 1 addition & 5 deletions integration/generic-service-definitions/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ export const TestMessage = {

fromPartial(object: DeepPartial<TestMessage>): 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;
},
};
Expand Down
12 changes: 2 additions & 10 deletions integration/global-this/global-this.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,7 @@ export const Object = {

fromPartial(object: DeepPartial<Object>): 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;
},
};
Expand Down Expand Up @@ -113,11 +109,7 @@ export const Error = {

fromPartial(object: DeepPartial<Error>): 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;
},
};
Expand Down
12 changes: 2 additions & 10 deletions integration/grpc-js/google/protobuf/timestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +162,8 @@ export const Timestamp = {

fromPartial(object: DeepPartial<Timestamp>): 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;
},
};
Expand Down
Loading

0 comments on commit 19d2ded

Please sign in to comment.