Skip to content

Commit

Permalink
Merge pull request #693 from uber/sc-binary-check-nil
Browse files Browse the repository at this point in the history
Add nil check in code-gen converter for binary
  • Loading branch information
chengs041021 authored Mar 26, 2020
2 parents 993a605 + aa38399 commit ab8c042
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
5 changes: 3 additions & 2 deletions codegen/type_converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -807,9 +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
Expand Down
43 changes: 43 additions & 0 deletions codegen/type_converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit ab8c042

Please sign in to comment.