Skip to content

Commit

Permalink
Merge pull request #219 from thriftrw/dev
Browse files Browse the repository at this point in the history
Prepare v0.2.0
  • Loading branch information
abhinav authored Sep 9, 2016
2 parents 9484e94 + e8de1e8 commit 0edcedc
Show file tree
Hide file tree
Showing 45 changed files with 2,206 additions and 295 deletions.
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Releases
========

v0.2.0 (unreleased)
-------------------

- Added a `-v`/`--version` flag.
- Added a plugin system.

ThriftRW now provides a plugin system to allow customizing code generation.
Initially, only the generated code for `service` declarations is
customizable. Check the documentation for more details.
- **Breaking**: Fixed a bug where all-caps attributes that are not known
abbreviations were changed to PascalCase.
- **Breaking**: The `String()` method for `enum` types now returns the name
of the item as specified in the Thrift file.


v0.1.0 (2016-08-31)
-------------------

- Initial release.
18 changes: 16 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
WANT_VERSION = $(shell grep '^v[0-9]' CHANGELOG.md | head -n1 | cut -d' ' -f1)

# Minor versions of Go for which the lint check should be run.
LINTABLE_MINOR_VERSIONS := 6

Expand Down Expand Up @@ -59,8 +61,19 @@ else
@echo "Skipping linters for $(GO_VERSION)"
endif

.PHONY: verifyVersion
verifyVersion: build
@if [ "$$(./thriftrw-go --version)" != "thriftrw $(WANT_VERSION)" ]; then \
echo "Version number in version.go does not match CHANGELOG.md"; \
echo "Want: thriftrw $(WANT_VERSION)"; \
echo " Got: $$(./thriftrw-go --version)"; \
exit 1; \
else \
echo "thriftrw $(WANT_VERSION)"; \
fi

.PHONY: test
test: build
test: build verifyVersion
go test -race $(PACKAGES)

.PHONY: cover
Expand Down Expand Up @@ -89,6 +102,7 @@ endif
go get -u github.com/wadey/gocovmerge
go get -u github.com/mattn/goveralls
go get -u golang.org/x/tools/cmd/cover
go install .

.PHONY: build_ci
build_ci: build
Expand All @@ -97,5 +111,5 @@ build_ci: build
lint_ci: lint

.PHONY: test_ci
test_ci: build_ci
test_ci: build_ci verifyVersion
./scripts/cover.sh $(shell go list $(PACKAGES))
5 changes: 3 additions & 2 deletions gen/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ import (
// Constant generates code for `const` expressions in Thrift files.
func Constant(g Generator, c *compile.Constant) error {
err := g.DeclareFromTemplate(
`<if canBeConstant .Type>const<else>var<end> <goCase .Name> <typeReference .Type> = <constantValue .Value .Type>`,
`<if canBeConstant .Type>const<else>var<end> <constantName .Name> <typeReference .Type> = <constantValue .Value .Type>`,
c,
TemplateFunc("constantValue", ConstantValue),
TemplateFunc("canBeConstant", canBeConstant),
TemplateFunc("constantName", constantName),
)
return wrapGenerateError(c.Name, err)
}
Expand Down Expand Up @@ -200,7 +201,7 @@ func constantStruct(g Generator, v *compile.ConstantStruct, t compile.TypeSpec)
}

func enumItemReference(g Generator, v compile.EnumItemReference, t compile.TypeSpec) (_ string, err error) {
s, err := g.TextTemplate(`<typeName .Enum><goCase .Item.Name>`, v)
s, err := g.TextTemplate(`<enumItemName .Enum .Item.Name>`, v, TemplateFunc("enumItemName", enumItemName))
if err != nil {
return "", err
}
Expand Down
25 changes: 21 additions & 4 deletions gen/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@

package gen

import "github.com/thriftrw/thriftrw-go/compile"
import (
"strings"

"github.com/thriftrw/thriftrw-go/compile"
)

// enumGenerator generates code to serialize and deserialize enums.
type enumGenerator struct {
Expand Down Expand Up @@ -67,8 +71,9 @@ func enum(g Generator, spec *compile.EnumSpec) error {
<if .Spec.Items>
const (
<$enum := .Spec>
<range .Spec.Items>
<$enumName><goCase .Name> <$enumName> = <.Value>
<enumItemName $enum .Name> <$enumName> = <.Value>
<end>
)
<end>
Expand All @@ -90,7 +95,7 @@ func enum(g Generator, spec *compile.EnumSpec) error {
switch <$w> {
<range .UniqueItems>
case <.Value>:
return "<goCase .Name>"
return "<.Name>"
<end>
}
<end>
Expand All @@ -103,11 +108,23 @@ func enum(g Generator, spec *compile.EnumSpec) error {
}{
Spec: spec,
UniqueItems: items,
})
},
TemplateFunc("enumItemName", enumItemName),
)

return wrapGenerateError(spec.Name, err)
}

// enumItemName returns the Go name that should be used for an enum item with
// the given Thrift name.
func enumItemName(g Generator, spec compile.TypeSpec, itemName string) (string, error) {
enumName, err := typeName(g, spec)
if err != nil {
return "", err
}
return enumName + pascalCase(false /* all caps */, strings.Split(itemName, "_")...), nil
}

// enumUniqueItems returns a subset of the given list of enum items where
// there are no value collisions between items.
func enumUniqueItems(items []compile.EnumItem) []compile.EnumItem {
Expand Down
12 changes: 12 additions & 0 deletions gen/enum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,18 @@ func TestEnumString(t *testing.T) {
te.EnumWithDuplicateValuesQ,
"Q",
},
{
te.LowerCaseEnumWith,
"with",
},
{
te.LowerCaseEnumLowerCase,
"lower_case",
},
{
te.LowerCaseEnumItems,
"items",
},
}

for _, tt := range tests {
Expand Down
Loading

0 comments on commit 0edcedc

Please sign in to comment.