Skip to content

Commit 919e61c

Browse files
authored
add a file suffix option (#230)
1 parent 2f16976 commit 919e61c

File tree

6 files changed

+153
-3
lines changed

6 files changed

+153
-3
lines changed

.github/workflows/build_and_test.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,19 @@ jobs:
3333
COVERALLS_TOKEN: ${{ secrets.COVERALLS_TOKEN }}
3434
run: |
3535
make cover
36+
37+
- name: Coveralls
38+
if: (matrix.go == '1.21')
39+
env:
40+
COVERALLS_TOKEN: ${{ secrets.COVERALLS_TOKEN }}
41+
run: |
3642
if [[ -n "$COVERALLS_TOKEN" ]]; then
3743
make coveralls
3844
fi
45+
3946
- name: Upload coverage
40-
uses: actions/upload-artifact@v3
47+
if: (matrix.go == '1.21')
48+
uses: actions/upload-artifact@v4
4149
with:
4250
name: coverage
4351
path: coverage.*

example/suffix.enum.gen.go

Lines changed: 46 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/suffix.enum.gen_test.go

Lines changed: 46 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/suffix.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//go:generate ../bin/go-enum -b example --output-suffix .enum.gen
2+
3+
//go:build example
4+
// +build example
5+
6+
package example
7+
8+
// Suffix ENUM(gen)
9+
type Suffix string

example/suffix_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//go:generate ../bin/go-enum -b example --output-suffix .enum.gen
2+
3+
//go:build example
4+
// +build example
5+
6+
package example
7+
8+
import (
9+
"testing"
10+
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
// SuffixTest ENUM(some_item)
15+
type SuffixTest string
16+
17+
func TestSuffix(t *testing.T) {
18+
x := Suffix("")
19+
assert.Equal(t, "", x.String())
20+
21+
assert.Equal(t, Suffix("gen"), SuffixGen)
22+
}
23+
24+
func TestSuffixTest(t *testing.T) {
25+
x := SuffixTest("")
26+
assert.Equal(t, "", x.String())
27+
28+
assert.Equal(t, SuffixTest("some_item"), SuffixTestSomeItem)
29+
}

main.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type rootT struct {
4242
ForceLower bool
4343
ForceUpper bool
4444
NoComments bool
45+
OutputSuffix string
4546
}
4647

4748
func main() {
@@ -174,6 +175,11 @@ func main() {
174175
Usage: "Adds build tags to a generated enum file.",
175176
Destination: &argv.BuildTags,
176177
},
178+
&cli.StringFlag{
179+
Name: "output-suffix",
180+
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 ",
181+
Destination: &argv.OutputSuffix,
182+
},
177183
},
178184
Action: func(ctx *cli.Context) error {
179185
aliases, err := generator.ParseAliases(argv.Aliases.Value())
@@ -262,14 +268,20 @@ func main() {
262268
filenames = fn
263269
}
264270

271+
outputSuffix := `_enum`
272+
if argv.OutputSuffix != "" {
273+
outputSuffix = argv.OutputSuffix
274+
}
275+
265276
for _, fileName := range filenames {
266277
originalName := fileName
267278

268279
out("go-enum started. file: %s\n", color.Cyan(originalName))
269280
fileName, _ = filepath.Abs(fileName)
270-
outFilePath := fmt.Sprintf("%s_enum.go", strings.TrimSuffix(fileName, filepath.Ext(fileName)))
281+
282+
outFilePath := fmt.Sprintf("%s%s.go", strings.TrimSuffix(fileName, filepath.Ext(fileName)), outputSuffix)
271283
if strings.HasSuffix(fileName, "_test.go") {
272-
outFilePath = strings.Replace(outFilePath, "_test_enum.go", "_enum_test.go", 1)
284+
outFilePath = strings.Replace(outFilePath, "_test"+outputSuffix+".go", outputSuffix+"_test.go", 1)
273285
}
274286

275287
// Parse the file given in arguments

0 commit comments

Comments
 (0)