Skip to content

Commit

Permalink
feat: Improve map reading (fromJSON/fromPartial) (#410)
Browse files Browse the repository at this point in the history
* Reduce code output for reading maps

* Create maps in a single assignment
  • Loading branch information
webmaster128 committed Nov 24, 2021
1 parent c2e6f81 commit 057d438
Show file tree
Hide file tree
Showing 12 changed files with 422 additions and 378 deletions.
24 changes: 10 additions & 14 deletions integration/batching-with-context/batching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},

Expand All @@ -258,14 +256,12 @@ export const BatchMapQueryResponse = {

fromPartial(object: DeepPartial<BatchMapQueryResponse>): 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;
},
};
Expand Down
24 changes: 10 additions & 14 deletions integration/batching/batching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},

Expand All @@ -256,14 +254,12 @@ export const BatchMapQueryResponse = {

fromPartial(object: DeepPartial<BatchMapQueryResponse>): 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;
},
};
Expand Down
78 changes: 42 additions & 36 deletions integration/simple-long/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},

Expand Down Expand Up @@ -257,30 +260,33 @@ export const SimpleWithMap = {

fromPartial(object: DeepPartial<SimpleWithMap>): 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;
},
};
Expand Down
104 changes: 56 additions & 48 deletions integration/simple-optionals/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},

Expand Down Expand Up @@ -938,30 +941,33 @@ export const SimpleWithMap = {

fromPartial(object: DeepPartial<SimpleWithMap>): 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;
},
};
Expand Down Expand Up @@ -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;
},

Expand All @@ -1190,14 +1197,15 @@ export const SimpleWithSnakeCaseMap = {

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

0 comments on commit 057d438

Please sign in to comment.