Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Improve map reading (fromJSON/fromPartial) #410

Merged
merged 2 commits into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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