Skip to content

Commit

Permalink
enum: MarshalLogObject should support go.label
Browse files Browse the repository at this point in the history
This changes the generated MarshalLogObject for enums (thriftrw#366) to support
the `go.label` annotation for enum items (thriftrw#363).
  • Loading branch information
abhinav committed Aug 14, 2018
1 parent dcefe66 commit ba9c0a0
Show file tree
Hide file tree
Showing 5 changed files with 286 additions and 3 deletions.
4 changes: 2 additions & 2 deletions envelope/internal/tests/enums/idl.go

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

218 changes: 218 additions & 0 deletions envelope/internal/tests/enums/types.go

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

2 changes: 1 addition & 1 deletion gen/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func enum(g Generator, spec *compile.EnumSpec) error {
switch int32(<$v>) {
<range .UniqueItems ->
case <.Value>:
enc.AddString("name", "<.Name>")
enc.AddString("name", "<enumItemLabelName .>")
<end ->
}
<end ->
Expand Down
23 changes: 23 additions & 0 deletions gen/internal/tests/enums/types.go

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

42 changes: 42 additions & 0 deletions gen/zap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -540,3 +540,45 @@ func TestTypedefsZapLogging(t *testing.T) {
expected6 := o{"foo": int64(1), "bar": int64(2)}
assert.Equal(t, expected6, mapEncoder.Fields)
}

func TestEnumWithLabelZapLogging(t *testing.T) {
// These types are created to ease building map[string]interface{}
type o = map[string]interface{}
type a = []interface{}
type i = int32

tests := []struct {
desc string
p te.EnumWithLabel
v interface{}
}{
{
desc: "with label",
p: te.EnumWithLabelUsername,
v: o{"name": "surname", "value": i(0)},
},
{
desc: "empty label",
p: te.EnumWithLabelSalt,
v: o{"name": "SALT", "value": i(2)},
},
{
desc: "unspecified label",
p: te.EnumWithLabelSugar,
v: o{"name": "SUGAR", "value": i(3)},
},
{
desc: "keyword label",
p: te.EnumWithLabelNaive4N1,
v: o{"name": "function", "value": i(5)},
},
}

for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
enc := zapcore.NewMapObjectEncoder()
tt.p.MarshalLogObject(enc)
assert.Equal(t, tt.v, enc.Fields)
})
}
}

0 comments on commit ba9c0a0

Please sign in to comment.