Skip to content

Commit

Permalink
Struct.String() should not include optional fields that are absent
Browse files Browse the repository at this point in the history
This fixes the output for unions as well.
  • Loading branch information
abhinav committed Mar 23, 2016
1 parent 9c4c749 commit 77e9166
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 33 deletions.
8 changes: 6 additions & 2 deletions gen/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,13 @@ func structure(g Generator, spec *compile.StructSpec) error {
<range .Spec.Fields>
<$f := printf "%s.%s" $v (goCase .Name)>
<if and (not .Required) (isPrimitiveType .Type)>
<if not .Required>
if <$f> != nil {
<$fields>[<$i>] = <$fmt>.Sprintf("<goCase .Name>: %v", *(<$f>))
<if isPrimitiveType .Type>
<$fields>[<$i>] = <$fmt>.Sprintf("<goCase .Name>: %v", *(<$f>))
<else>
<$fields>[<$i>] = <$fmt>.Sprintf("<goCase .Name>: %v", <$f>)
<end>
<$i>++
}
<else>
Expand Down
15 changes: 14 additions & 1 deletion gen/struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ func TestNestedStructsOptional(t *testing.T) {
wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
{ID: 1, Value: wire.NewValueString("Foo Bar")},
}}),
"User{Contact: <nil>, Name: Foo Bar}",
"User{Name: Foo Bar}",
},
{
testdata.User{
Expand Down Expand Up @@ -405,18 +405,21 @@ func TestUnionSimple(t *testing.T) {
tests := []struct {
s testdata.Document
v wire.Value
o string
}{
{
testdata.Document{Pdf: []byte{1, 2, 3}},
wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
{ID: 1, Value: wire.NewValueBinary([]byte{1, 2, 3})},
}}),
"Document{Pdf: [1 2 3]}",
},
{
testdata.Document{PlainText: stringp("hello")},
wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
{ID: 2, Value: wire.NewValueString("hello")},
}}),
"Document{PlainText: hello}",
},
}

Expand All @@ -431,31 +434,37 @@ func TestUnionSimple(t *testing.T) {
if assert.NoError(t, s.FromWire(tt.v)) {
assert.Equal(t, tt.s, s)
}

assert.Equal(t, tt.o, tt.s.String())
}
}

func TestUnionComplex(t *testing.T) {
tests := []struct {
s testdata.ArbitraryValue
v wire.Value
o string
}{
{
testdata.ArbitraryValue{BoolValue: boolp(true)},
wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
{ID: 1, Value: wire.NewValueBool(true)},
}}),
"ArbitraryValue{BoolValue: true}",
},
{
testdata.ArbitraryValue{Int64Value: int64p(42)},
wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
{ID: 2, Value: wire.NewValueI64(42)},
}}),
"ArbitraryValue{Int64Value: 42}",
},
{
testdata.ArbitraryValue{StringValue: stringp("hello")},
wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
{ID: 3, Value: wire.NewValueString("hello")},
}}),
"ArbitraryValue{StringValue: hello}",
},
{
testdata.ArbitraryValue{ListValue: []*testdata.ArbitraryValue{
Expand All @@ -480,6 +489,7 @@ func TestUnionComplex(t *testing.T) {
}),
})},
}}),
"ArbitraryValue{ListValue: [ArbitraryValue{BoolValue: true} ArbitraryValue{Int64Value: 42} ArbitraryValue{StringValue: hello}]}",
},
{
testdata.ArbitraryValue{MapValue: map[string]*testdata.ArbitraryValue{
Expand Down Expand Up @@ -514,6 +524,7 @@ func TestUnionComplex(t *testing.T) {
}),
})},
}}),
"ArbitraryValue{MapValue: map[bool:ArbitraryValue{BoolValue: true} int64:ArbitraryValue{Int64Value: 42} string:ArbitraryValue{StringValue: hello}]}",
},
}

Expand All @@ -528,6 +539,8 @@ func TestUnionComplex(t *testing.T) {
if assert.NoError(t, s.FromWire(tt.v)) {
assert.Equal(t, tt.s, s)
}

assert.Equal(t, tt.o, tt.s.String())
}
}

Expand Down
90 changes: 60 additions & 30 deletions gen/testdata/test.thrift.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 77e9166

Please sign in to comment.