From 919e61c0174b91303753ee3898569a01abb32c97 Mon Sep 17 00:00:00 2001 From: Alex Bice Date: Mon, 18 Dec 2023 08:48:30 -0700 Subject: [PATCH] add a file suffix option (#230) --- .github/workflows/build_and_test.yml | 10 +++++- example/suffix.enum.gen.go | 46 ++++++++++++++++++++++++++++ example/suffix.enum.gen_test.go | 46 ++++++++++++++++++++++++++++ example/suffix.go | 9 ++++++ example/suffix_test.go | 29 ++++++++++++++++++ main.go | 16 ++++++++-- 6 files changed, 153 insertions(+), 3 deletions(-) create mode 100644 example/suffix.enum.gen.go create mode 100644 example/suffix.enum.gen_test.go create mode 100644 example/suffix.go create mode 100644 example/suffix_test.go diff --git a/.github/workflows/build_and_test.yml b/.github/workflows/build_and_test.yml index 55d9957..99a4776 100644 --- a/.github/workflows/build_and_test.yml +++ b/.github/workflows/build_and_test.yml @@ -33,11 +33,19 @@ jobs: COVERALLS_TOKEN: ${{ secrets.COVERALLS_TOKEN }} run: | make cover + + - name: Coveralls + if: (matrix.go == '1.21') + env: + COVERALLS_TOKEN: ${{ secrets.COVERALLS_TOKEN }} + run: | if [[ -n "$COVERALLS_TOKEN" ]]; then make coveralls fi + - name: Upload coverage - uses: actions/upload-artifact@v3 + if: (matrix.go == '1.21') + uses: actions/upload-artifact@v4 with: name: coverage path: coverage.* diff --git a/example/suffix.enum.gen.go b/example/suffix.enum.gen.go new file mode 100644 index 0000000..cd739a2 --- /dev/null +++ b/example/suffix.enum.gen.go @@ -0,0 +1,46 @@ +// Code generated by go-enum DO NOT EDIT. +// Version: example +// Revision: example +// Build Date: example +// Built By: example + +//go:build example +// +build example + +package example + +import ( + "errors" + "fmt" +) + +const ( + // SuffixGen is a Suffix of type gen. + SuffixGen Suffix = "gen" +) + +var ErrInvalidSuffix = errors.New("not a valid Suffix") + +// String implements the Stringer interface. +func (x Suffix) String() string { + return string(x) +} + +// IsValid provides a quick way to determine if the typed value is +// part of the allowed enumerated values +func (x Suffix) IsValid() bool { + _, err := ParseSuffix(string(x)) + return err == nil +} + +var _SuffixValue = map[string]Suffix{ + "gen": SuffixGen, +} + +// ParseSuffix attempts to convert a string to a Suffix. +func ParseSuffix(name string) (Suffix, error) { + if x, ok := _SuffixValue[name]; ok { + return x, nil + } + return Suffix(""), fmt.Errorf("%s is %w", name, ErrInvalidSuffix) +} diff --git a/example/suffix.enum.gen_test.go b/example/suffix.enum.gen_test.go new file mode 100644 index 0000000..265a805 --- /dev/null +++ b/example/suffix.enum.gen_test.go @@ -0,0 +1,46 @@ +// Code generated by go-enum DO NOT EDIT. +// Version: example +// Revision: example +// Build Date: example +// Built By: example + +//go:build example +// +build example + +package example + +import ( + "errors" + "fmt" +) + +const ( + // SuffixTestSomeItem is a SuffixTest of type some_item. + SuffixTestSomeItem SuffixTest = "some_item" +) + +var ErrInvalidSuffixTest = errors.New("not a valid SuffixTest") + +// String implements the Stringer interface. +func (x SuffixTest) String() string { + return string(x) +} + +// IsValid provides a quick way to determine if the typed value is +// part of the allowed enumerated values +func (x SuffixTest) IsValid() bool { + _, err := ParseSuffixTest(string(x)) + return err == nil +} + +var _SuffixTestValue = map[string]SuffixTest{ + "some_item": SuffixTestSomeItem, +} + +// ParseSuffixTest attempts to convert a string to a SuffixTest. +func ParseSuffixTest(name string) (SuffixTest, error) { + if x, ok := _SuffixTestValue[name]; ok { + return x, nil + } + return SuffixTest(""), fmt.Errorf("%s is %w", name, ErrInvalidSuffixTest) +} diff --git a/example/suffix.go b/example/suffix.go new file mode 100644 index 0000000..e757bf2 --- /dev/null +++ b/example/suffix.go @@ -0,0 +1,9 @@ +//go:generate ../bin/go-enum -b example --output-suffix .enum.gen + +//go:build example +// +build example + +package example + +// Suffix ENUM(gen) +type Suffix string diff --git a/example/suffix_test.go b/example/suffix_test.go new file mode 100644 index 0000000..d8f0344 --- /dev/null +++ b/example/suffix_test.go @@ -0,0 +1,29 @@ +//go:generate ../bin/go-enum -b example --output-suffix .enum.gen + +//go:build example +// +build example + +package example + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +// SuffixTest ENUM(some_item) +type SuffixTest string + +func TestSuffix(t *testing.T) { + x := Suffix("") + assert.Equal(t, "", x.String()) + + assert.Equal(t, Suffix("gen"), SuffixGen) +} + +func TestSuffixTest(t *testing.T) { + x := SuffixTest("") + assert.Equal(t, "", x.String()) + + assert.Equal(t, SuffixTest("some_item"), SuffixTestSomeItem) +} diff --git a/main.go b/main.go index 10c166e..94ec6c3 100644 --- a/main.go +++ b/main.go @@ -42,6 +42,7 @@ type rootT struct { ForceLower bool ForceUpper bool NoComments bool + OutputSuffix string } func main() { @@ -174,6 +175,11 @@ func main() { Usage: "Adds build tags to a generated enum file.", Destination: &argv.BuildTags, }, + &cli.StringFlag{ + Name: "output-suffix", + Usage: "Changes the default filename suffix of _enum to something else. `.go` will be appended to the end of the string no matter what, so that `_test.go` cases can be accommodated ", + Destination: &argv.OutputSuffix, + }, }, Action: func(ctx *cli.Context) error { aliases, err := generator.ParseAliases(argv.Aliases.Value()) @@ -262,14 +268,20 @@ func main() { filenames = fn } + outputSuffix := `_enum` + if argv.OutputSuffix != "" { + outputSuffix = argv.OutputSuffix + } + for _, fileName := range filenames { originalName := fileName out("go-enum started. file: %s\n", color.Cyan(originalName)) fileName, _ = filepath.Abs(fileName) - outFilePath := fmt.Sprintf("%s_enum.go", strings.TrimSuffix(fileName, filepath.Ext(fileName))) + + outFilePath := fmt.Sprintf("%s%s.go", strings.TrimSuffix(fileName, filepath.Ext(fileName)), outputSuffix) if strings.HasSuffix(fileName, "_test.go") { - outFilePath = strings.Replace(outFilePath, "_test_enum.go", "_enum_test.go", 1) + outFilePath = strings.Replace(outFilePath, "_test"+outputSuffix+".go", outputSuffix+"_test.go", 1) } // Parse the file given in arguments