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