Skip to content

Commit

Permalink
gen/enum: Generate <Name>_Values function (#308)
Browse files Browse the repository at this point in the history
This generates a `<Name>_Values` function for all enums which returns
all known values for that enum.
  • Loading branch information
recht authored and abhinav committed May 9, 2017
1 parent 98f883a commit 1204a69
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ v1.3.0 (unreleased)
- Enums now implement the `encoding.TextUnmarshaler` interface. This allows
retrieving enum values by name and integrates better with other encoding
formats.
- Enums now have a `<EnumName>_Values()` function which returns all known
values for that enum.


v1.2.0 (2017-04-17)
Expand Down
4 changes: 4 additions & 0 deletions gen/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ func enum(g Generator, spec *compile.EnumSpec) error {
<enumItemName $enumName .> <$enumName> = <.Value>
<end>
)
func <$enumName>_Values() []<$enumName> {
return []<$enumName>{<range .Spec.Items><enumItemName $enumName .>,<end>}
}
<end>
<$v := newVar "v">
Expand Down
6 changes: 6 additions & 0 deletions gen/enum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,12 @@ func TestInvalidJSON(t *testing.T) {
})
}
}

func TestEnumValuesCanBeListed(t *testing.T) {
values := te.EnumDefault_Values()
assert.Equal(t, values, []te.EnumDefault{te.EnumDefaultFoo, te.EnumDefaultBar, te.EnumDefaultBaz})
}

func TestUnmarshalTextReturnsValue(t *testing.T) {
var v te.EnumDefault
err := v.UnmarshalText([]byte("Foo"))
Expand Down
8 changes: 8 additions & 0 deletions gen/testdata/collision/types.go

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

4 changes: 4 additions & 0 deletions gen/testdata/enum_conflict/types.go

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

4 changes: 2 additions & 2 deletions gen/testdata/enums/idl.go

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

110 changes: 110 additions & 0 deletions gen/testdata/enums/types.go

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

3 changes: 3 additions & 0 deletions gen/testdata/thrift/enums.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ enum RecordType {
enum lowerCaseEnum {
containing, lower_case, items
}

// collision with RecordType_Values() function.
enum RecordType_Values { FOO, BAR }
4 changes: 4 additions & 0 deletions internal/envelope/exception/types.go

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

8 changes: 8 additions & 0 deletions plugin/api/types.go

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

0 comments on commit 1204a69

Please sign in to comment.