Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gen/enum: Generate <Name>_Values function #308

Merged
merged 4 commits into from
May 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.