Skip to content

Commit

Permalink
Generate "Decode" for "enums" that will directly decode
Browse files Browse the repository at this point in the history
Add templating for generating code for "enums" that will utilize the new
`stream.Reader`, performing the same duties as `binary.Decode` and `FromWire`,
but without going through an intermediary format.
  • Loading branch information
witriew committed Jun 22, 2021
1 parent 262b6e4 commit bc1e0ce
Show file tree
Hide file tree
Showing 10 changed files with 381 additions and 2 deletions.
21 changes: 21 additions & 0 deletions gen/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func enum(g Generator, spec *compile.EnumSpec) error {
<$strconv := import "strconv">
<$wire := import "go.uber.org/thriftrw/wire">
<$stream := import "go.uber.org/thriftrw/protocol/stream">
<$enumName := goName .Spec>
<formatDoc .Spec.Doc>type <$enumName> int32
Expand Down Expand Up @@ -190,6 +191,26 @@ func enum(g Generator, spec *compile.EnumSpec) error {
return nil
}
<$sr := newVar "sr">
// Decode reads off the encoded <$enumName> directly off of the wire.
//
// sReader := BinaryStreamer.Reader(reader)
//
// var <$v> <$enumName>
// if err := <$v>.Decode(sReader); err != nil {
// return <$enumName>(0), err
// }
// return <$v>, nil
func (<$v> *<$enumName>) Decode(<$sr> <$stream>.Reader) error {
<- $rVal := newVar "rVal" ->
<$rVal>, err := <$sr>.ReadInt32()
if err != nil {
return err
}
*<$v> = (<$enumName>)(<$rVal>)
return nil
}
// String returns a readable string representation of <$enumName>.
func (<$v> <$enumName>) String() string {
<$w> := int32(<$v>)
Expand Down
5 changes: 5 additions & 0 deletions gen/enum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func TestEnumDefaultWire(t *testing.T) {

for _, tt := range tests {
assertRoundTrip(t, &tt.e, tt.v, "EnumDefault")
assertStreamingRoundTrip(t, &tt.e, tt.v, "EnumDefault")
}
}

Expand Down Expand Up @@ -154,6 +155,7 @@ func TestEnumWithDuplicateValuesWire(t *testing.T) {

for _, tt := range tests {
assertRoundTrip(t, &tt.e, tt.v, "EnumWithDuplicateValues")
assertStreamingRoundTrip(t, &tt.e, tt.v, "EnumWithDuplicateValues")
}
}

Expand Down Expand Up @@ -496,6 +498,9 @@ func TestEnumLabelValid(t *testing.T) {
t.Run("wire", func(t *testing.T) {
assertRoundTrip(t, &tt.item, wire.NewValueI32(int32(tt.item)),
"%v", tt.item)
assertStreamingRoundTrip(t, &tt.item, wire.NewValueI32(int32(tt.item)),
"%v", tt.item)

})
})
}
Expand Down
37 changes: 37 additions & 0 deletions gen/internal/tests/collision/collision.go

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

19 changes: 19 additions & 0 deletions gen/internal/tests/enum_conflict/enum_conflict.go

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

163 changes: 163 additions & 0 deletions gen/internal/tests/enums/enums.go

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

Loading

0 comments on commit bc1e0ce

Please sign in to comment.