Skip to content

Commit

Permalink
fix: typescript errors for struct with optional=all
Browse files Browse the repository at this point in the history
  • Loading branch information
UnderKoen committed Feb 26, 2024
1 parent ba97f98 commit 9ddefaa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
12 changes: 7 additions & 5 deletions integration/nestjs-simple/google/protobuf/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,21 @@ export const Struct = {
wrap(object: { [key: string]: any } | undefined): Struct {
const struct = createBaseStruct();
if (object !== undefined) {
Object.keys(object).forEach((key) => {
struct.fields[key] = Value.wrap(object[key]);
});
if (struct.fields !== undefined) {
for (const key of Object.keys(object)) {
struct.fields[key] = Value.wrap(object[key]);
}
}
}
return struct;
},

unwrap(message: Struct): { [key: string]: any } {
const object: { [key: string]: any } = {};
if (message.fields) {
Object.keys(message.fields).forEach((key) => {
for (const key of Object.keys(message.fields)) {
object[key] = Value.unwrap(message.fields[key]);
});
}
}
return object;
},
Expand Down
20 changes: 12 additions & 8 deletions src/generate-struct-wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ export function generateWrapDeep(ctx: Context, fullProtoTypeName: string, fieldN
chunks.push(code`wrap(object: {[key: string]: any} | undefined): Struct {
const struct = createBaseStruct();
if (object !== undefined) {
Object.keys(object).forEach(key => {
${setStatement}
});
if (struct.fields !== undefined) {
for (const key of Object.keys(object)) {
${setStatement}
}
}
}
return struct;
}`);
Expand Down Expand Up @@ -99,18 +101,20 @@ export function generateUnwrapDeep(ctx: Context, fullProtoTypeName: string, fiel
if (ctx.options.useMapType) {
chunks.push(code`unwrap(message: Struct): {[key: string]: any} {
const object: { [key: string]: any } = {};
[...message.fields.keys()].forEach((key) => {
object[key] = Value.unwrap(message.fields.get(key));
});
if (message.fields) {
for (const key of message.fields.keys()) {
object[key] = Value.unwrap(message.fields.get(key));
}
}
return object;
}`);
} else {
chunks.push(code`unwrap(message: Struct): {[key: string]: any} {
const object: { [key: string]: any } = {};
if (message.fields) {
Object.keys(message.fields).forEach(key => {
for (const key of Object.keys(message.fields)) {
object[key] = Value.unwrap(message.fields[key]);
});
}
}
return object;
}`);
Expand Down

0 comments on commit 9ddefaa

Please sign in to comment.