From 2ab7101ca5cda70746a70c626cb9e78dc2f656f3 Mon Sep 17 00:00:00 2001 From: chengs041021 <48456910+chengs041021@users.noreply.github.com> Date: Wed, 25 Mar 2020 12:47:57 -0700 Subject: [PATCH 1/3] Add nil check in code-gen converter for binary --- codegen/type_converter.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/codegen/type_converter.go b/codegen/type_converter.go index 16e05f325..a66ebb0d1 100644 --- a/codegen/type_converter.go +++ b/codegen/type_converter.go @@ -808,6 +808,9 @@ func (c *TypeConverter) genStructConverter( } case *compile.BinarySpec: // TODO: handle override. Check if binarySpec can be optional. + for _, line := range checkOptionalNil(indent, c.uninitialized, toIdentifier, prevKeyPrefixes, c.useRecurGen) { + c.append(line) + } c.append(toIdentifier, " = []byte(", fromIdentifier, ")") case *compile.StructSpec: From df0f78818bb8f8fa1c04dd8fae98e07085e9046a Mon Sep 17 00:00:00 2001 From: chengs041021 <48456910+chengs041021@users.noreply.github.com> Date: Wed, 25 Mar 2020 12:49:11 -0700 Subject: [PATCH 2/3] Add tests --- codegen/type_converter_test.go | 43 ++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/codegen/type_converter_test.go b/codegen/type_converter_test.go index 231f27719..d2daa4146 100644 --- a/codegen/type_converter_test.go +++ b/codegen/type_converter_test.go @@ -3592,3 +3592,46 @@ func TestNestedMapOfListOfMapInvalid(t *testing.T) { assert.Error(t, err) } + +func TestConverterMapNestedBinary(t *testing.T) { + fieldMap := make(map[string]codegen.FieldMapperEntry) + fieldMap["NestedTwo.NestedNestedTwo.Two"] = codegen.FieldMapperEntry{ + QualifiedName: "Two", + Override: false, + } + + lines, err := convertTypes( + "Foo", "Bar", + `struct Foo { + 1: optional string one + 2: optional binary two + } + + struct NestedBar { + 1: optional NestedNestedBar nestedNestedTwo + } + + struct NestedNestedBar { + 1: optional binary two + } + + struct Bar { + 1: optional string one + 2: optional NestedBar nestedTwo + }`, + nil, + fieldMap, + ) + + assert.NoError(t, err) + assertPrettyEqual(t, trim(` + out.One = (*string)(in.One) + if out.NestedTwo == nil { + out.NestedTwo = &structs.NestedBar{} + } + if out.NestedTwo.NestedNestedTwo == nil { + out.NestedTwo.NestedNestedTwo = &structs.NestedNestedBar{} + } + out.NestedTwo.NestedNestedTwo.Two = []byte(in.Two) + `), lines) +} From aa38399cb6c93facd60919e667b8c8c0563aaa0d Mon Sep 17 00:00:00 2001 From: chengs041021 <48456910+chengs041021@users.noreply.github.com> Date: Thu, 26 Mar 2020 09:56:06 -0700 Subject: [PATCH 3/3] remove todo --- codegen/type_converter.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/codegen/type_converter.go b/codegen/type_converter.go index a66ebb0d1..d5b8a9e17 100644 --- a/codegen/type_converter.go +++ b/codegen/type_converter.go @@ -807,12 +807,10 @@ func (c *TypeConverter) genStructConverter( return err } case *compile.BinarySpec: - // TODO: handle override. Check if binarySpec can be optional. for _, line := range checkOptionalNil(indent, c.uninitialized, toIdentifier, prevKeyPrefixes, c.useRecurGen) { c.append(line) } c.append(toIdentifier, " = []byte(", fromIdentifier, ")") - case *compile.StructSpec: var ( stFromPrefix = fromPrefix