From 0647151b356d7b22f8baf72b70f5a0353259b404 Mon Sep 17 00:00:00 2001 From: roboslone Date: Thu, 29 Dec 2022 19:27:40 +0300 Subject: [PATCH] fix: Fix codegen for google.protobuf.Struct with useMapType=true (#740) * wip * fmt Co-authored-by: Alexander Khristyukhin --- src/main.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main.ts b/src/main.ts index 7828fee65..08a708f17 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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; @@ -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; }`);