Skip to content

Commit

Permalink
fix: Fix codegen for google.protobuf.Struct with useMapType=true (#740)
Browse files Browse the repository at this point in the history
* wip

* fmt

Co-authored-by: Alexander Khristyukhin <alexander.khristyukhin@pinely.com>
  • Loading branch information
roboslone and roboslone committed Dec 29, 2022
1 parent c6147b4 commit 0647151
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/main.ts
Expand Up @@ -1728,11 +1728,16 @@ type StructFieldNames = {
function generateWrap(ctx: Context, fullProtoTypeName: string, fieldNames: StructFieldNames): Code[] {
const chunks: Code[] = [];
if (isStructTypeName(fullProtoTypeName)) {
let setStatement = "struct.fields[key] = object[key];";
if (ctx.options.useMapType) {
setStatement = "struct.fields.set(key, object[key]);";
}

chunks.push(code`wrap(object: {[key: string]: any} | undefined): Struct {
const struct = createBaseStruct();
if (object !== undefined) {
Object.keys(object).forEach(key => {
struct.fields[key] = object[key];
${setStatement}
});
}
return struct;
Expand Down Expand Up @@ -1815,10 +1820,15 @@ function generateWrap(ctx: Context, fullProtoTypeName: string, fieldNames: Struc
function generateUnwrap(ctx: Context, fullProtoTypeName: string, fieldNames: StructFieldNames): Code[] {
const chunks: Code[] = [];
if (isStructTypeName(fullProtoTypeName)) {
let setStatement = "object[key] = message.fields[key];";
if (ctx.options.useMapType) {
setStatement = "object[key] = message.fields.get(key);";
}

chunks.push(code`unwrap(message: Struct): {[key: string]: any} {
const object: { [key: string]: any } = {};
Object.keys(message.fields).forEach(key => {
object[key] = message.fields[key];
${setStatement}
});
return object;
}`);
Expand Down

0 comments on commit 0647151

Please sign in to comment.