Skip to content

Commit 9c6b93c

Browse files
authored
Merge pull request #5 from Splode/refactor/optimize-generator
Refactor/optimize generator
2 parents 78db39c + fb9eca9 commit 9c6b93c

6 files changed

Lines changed: 67 additions & 61 deletions

File tree

.github/workflows/release.yml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ on:
44
push:
55
tags:
66
- "v*"
7-
permissions:
8-
contents: write
97
jobs:
108
goreleaser:
119
runs-on: ubuntu-latest
@@ -18,14 +16,6 @@ jobs:
1816
uses: actions/setup-go@v2
1917
with:
2018
go-version: 1.19
21-
- name: Create GitHub Release
22-
uses: actions/create-release@v1
23-
env:
24-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
25-
with:
26-
tag_name: ${{ github.ref }}
27-
release_name: ${{ github.ref }}
28-
body: ${{ env.CHANGELOG}}
2919
- name: Run GoReleaser
3020
uses: goreleaser/goreleaser-action@v2
3121
with:

.goreleaser.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ builds:
1010
- linux
1111
- windows
1212
- darwin
13+
goarch:
14+
- amd64
15+
- arm64
16+
- "386"
1317
ignore:
1418
- goos: windows
1519
goarch: arm64
@@ -30,3 +34,6 @@ changelog:
3034
exclude:
3135
- '^docs:'
3236
- '^test:'
37+
release:
38+
draft: true
39+
name_template: "v{{ .Version }}"

Taskfile.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ tasks:
2222
release:test:
2323
desc: Tests release process without publishing
2424
cmds:
25-
- goreleaser --snapshot --rm-dist
25+
- goreleaser --snapshot --clean
2626
data:dupe:
2727
desc: Checks dictionary data for duplicate entries
2828
cmds:

cmd/fname/fname.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func main() {
7373

7474
opts := []fname.GeneratorOption{}
7575

76-
c, err := fname.ParseCasing(casing)
76+
c, err := fname.CasingFromString(casing)
7777
if err != nil {
7878
fmt.Fprintf(os.Stderr, "error: %s", err)
7979
os.Exit(1)

generator.go

Lines changed: 55 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,40 @@ import (
1010
"golang.org/x/text/language"
1111
)
1212

13-
type Casing string
13+
type Casing int
1414

1515
const (
16-
Lower Casing = "lower"
17-
Upper Casing = "upper"
18-
Title Casing = "title"
16+
Lower Casing = iota
17+
Upper
18+
Title
1919
)
2020

21-
// Generator is a random name generator.
21+
func (c Casing) String() string {
22+
switch c {
23+
case Lower:
24+
return "lower"
25+
case Upper:
26+
return "upper"
27+
case Title:
28+
return "title"
29+
default:
30+
return "unknown"
31+
}
32+
}
33+
34+
func CasingFromString(casing string) (Casing, error) {
35+
switch strings.ToLower(casing) {
36+
case Lower.String():
37+
return Lower, nil
38+
case Upper.String():
39+
return Upper, nil
40+
case Title.String():
41+
return Title, nil
42+
default:
43+
return -1, fmt.Errorf("invalid casing: %s", casing)
44+
}
45+
}
46+
2247
type Generator struct {
2348
casing Casing
2449
dict *Dictionary
@@ -47,7 +72,7 @@ func WithDelimiter(delimiter string) GeneratorOption {
4772
// WithSeed sets the seed used to generate random numbers.
4873
func WithSeed(seed int64) GeneratorOption {
4974
return func(g *Generator) {
50-
g.rand.Seed(seed)
75+
g.rand = rand.New(rand.NewSource(seed))
5176
}
5277
}
5378

@@ -75,59 +100,43 @@ func NewGenerator(opts ...GeneratorOption) *Generator {
75100

76101
// Generate generates a random name.
77102
func (g *Generator) Generate() (string, error) {
78-
// Keep generating adjective and noun pairs until they are not the same.
79-
var adjective, noun string
80-
for adjective == noun {
81-
adjective = g.dict.adjectives[g.rand.Intn(g.dict.LengthAdjective())]
82-
noun = g.dict.nouns[g.rand.Intn(g.dict.LengthNoun())]
103+
if g.size < 2 || g.size > 4 {
104+
return "", fmt.Errorf("invalid size: %d", g.size)
83105
}
84106

85-
words := []string{adjective, noun}
86-
87-
switch g.size {
88-
case 2:
89-
// do nothing
90-
case 3:
91-
verb := g.dict.verbs[g.rand.Intn(g.dict.LengthVerb())]
92-
words = append(words, verb)
93-
case 4:
94-
verb := g.dict.verbs[g.rand.Intn(g.dict.LengthVerb())]
95-
words = append(words, verb)
96-
adverb := g.dict.adverbs[g.rand.Intn(g.dict.LengthAdverb())]
97-
words = append(words, adverb)
98-
default:
99-
return "", fmt.Errorf("invalid size: %d", g.size)
107+
words := make([]string, 0, g.size)
108+
adjectiveIndex := g.rand.Intn(g.dict.LengthAdjective())
109+
nounIndex := g.rand.Intn(g.dict.LengthNoun())
110+
for adjectiveIndex == nounIndex {
111+
nounIndex = g.rand.Intn(g.dict.LengthNoun())
100112
}
101-
return strings.Join(g.applyCasing(words...), g.delimiter), nil
102-
}
103113

104-
// ParseCasing parses a string into a casing.
105-
func ParseCasing(casing string) (Casing, error) {
106-
switch casing {
107-
case "lower":
108-
return Lower, nil
109-
case "upper":
110-
return Upper, nil
111-
case "title":
112-
return Title, nil
113-
default:
114-
return "", fmt.Errorf("invalid casing: %s", casing)
114+
words = append(words, g.dict.adjectives[adjectiveIndex], g.dict.nouns[nounIndex])
115+
116+
if g.size >= 3 {
117+
words = append(words, g.dict.verbs[g.rand.Intn(g.dict.LengthVerb())])
115118
}
116-
}
117119

118-
var titleCaser = cases.Title(language.English)
120+
if g.size == 4 {
121+
words = append(words, g.dict.adverbs[g.rand.Intn(g.dict.LengthAdverb())])
122+
}
119123

120-
var casingMap = map[Casing]func(string) string{
121-
Lower: strings.ToLower,
122-
Upper: strings.ToUpper,
123-
Title: titleCaser.String,
124+
return strings.Join(g.applyCasing(words), g.delimiter), nil
124125
}
125126

126-
func (g *Generator) applyCasing(words ...string) []string {
127+
func (g *Generator) applyCasing(words []string) []string {
127128
if fn, ok := casingMap[g.casing]; ok {
128129
for i, word := range words {
129130
words[i] = fn(word)
130131
}
131132
}
132133
return words
133134
}
135+
136+
var titleCaser = cases.Title(language.English)
137+
138+
var casingMap = map[Casing]func(string) string{
139+
Lower: strings.ToLower,
140+
Upper: strings.ToUpper,
141+
Title: titleCaser.String,
142+
}

generator_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func TestGenerate(t *testing.T) {
185185
}
186186
}
187187

188-
func TestParseCasing(t *testing.T) {
188+
func TestCasingFromString(t *testing.T) {
189189
t.Log("Given the need to parse casing strings")
190190
{
191191
t.Log("\tWhen parsing a valid casing string")
@@ -199,7 +199,7 @@ func TestParseCasing(t *testing.T) {
199199
{"title", Title},
200200
}
201201
for _, tc := range testCases {
202-
c, err := ParseCasing(tc.name)
202+
c, err := CasingFromString(tc.name)
203203
if err != nil {
204204
t.Fatalf("\t\tShould be able to parse a valid casing string : %v", err)
205205
}
@@ -214,7 +214,7 @@ func TestParseCasing(t *testing.T) {
214214

215215
t.Log("\tWhen parsing an invalid casing string")
216216
{
217-
_, err := ParseCasing("invalid")
217+
_, err := CasingFromString("invalid")
218218
if err == nil {
219219
t.Fatal("\t\tShould not be able to parse an invalid casing string")
220220
}

0 commit comments

Comments
 (0)