• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

abice / go-enum / 16393245158

19 Jul 2025 10:13PM UTC coverage: 64.871% (-26.9%) from 91.721%
16393245158

Pull #258

github

abice
chore: convert to options struct
Pull Request #258: chore: update go versions

92 of 144 new or added lines in 3 files covered. (63.89%)

17 existing lines in 1 file now uncovered.

578 of 891 relevant lines covered (64.87%)

1007.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.84
/generator/options.go
1
package generator
2

3
import (
4
        "sort"
5
        "text/template"
6
)
7

8
// GeneratorOptions holds all the configuration options for the Generator
9
type GeneratorOptions struct {
10
        noPrefix          bool
11
        lowercaseLookup   bool
12
        caseInsensitive   bool
13
        marshal           bool
14
        sql               bool
15
        sqlint            bool
16
        flag              bool
17
        names             bool
18
        values            bool
19
        leaveSnakeCase    bool
20
        jsonPkg           string
21
        prefix            string
22
        sqlNullInt        bool
23
        sqlNullStr        bool
24
        ptr               bool
25
        mustParse         bool
26
        forceLower        bool
27
        forceUpper        bool
28
        noComments        bool
29
        buildTags         []string
30
        replacementNames  map[string]string
31
        userTemplateNames []string
32
}
33

34
// Option is a function that modifies a Generator
35
type Option func(*Generator)
36

37
// WithNoPrefix is used to change the enum const values generated to not have the enum on them.
38
func WithNoPrefix() Option {
21✔
39
        return func(g *Generator) {
42✔
40
                g.noPrefix = true
21✔
41
        }
21✔
42
}
43

44
// WithLowercaseVariant is used to change the enum const values generated to not have the enum on them.
45
func WithLowercaseVariant() Option {
21✔
46
        return func(g *Generator) {
42✔
47
                g.lowercaseLookup = true
21✔
48
        }
21✔
49
}
50

51
// WithCaseInsensitiveParse is used to change the enum const values generated to not have the enum on them.
52
func WithCaseInsensitiveParse() Option {
12✔
53
        return func(g *Generator) {
24✔
54
                g.lowercaseLookup = true
12✔
55
                g.caseInsensitive = true
12✔
56
        }
12✔
57
}
58

59
// WithMarshal is used to add marshalling to the enum
60
func WithMarshal() Option {
36✔
61
        return func(g *Generator) {
72✔
62
                g.marshal = true
36✔
63
        }
36✔
64
}
65

66
// WithSQLDriver is used to add marshalling to the enum
67
func WithSQLDriver() Option {
15✔
68
        return func(g *Generator) {
30✔
69
                g.sql = true
15✔
70
        }
15✔
71
}
72

73
// WithSQLInt is used to signal a string to be stored as an int.
NEW
74
func WithSQLInt() Option {
×
NEW
75
        return func(g *Generator) {
×
NEW
76
                g.sqlint = true
×
NEW
77
        }
×
78
}
79

80
// WithFlag is used to add flag methods to the enum
81
func WithFlag() Option {
21✔
82
        return func(g *Generator) {
42✔
83
                g.flag = true
21✔
84
        }
21✔
85
}
86

87
// WithNames is used to add Names methods to the enum
88
func WithNames() Option {
15✔
89
        return func(g *Generator) {
30✔
90
                g.names = true
15✔
91
        }
15✔
92
}
93

94
// WithValues is used to add Values methods to the enum
NEW
95
func WithValues() Option {
×
NEW
96
        return func(g *Generator) {
×
NEW
97
                g.values = true
×
NEW
98
        }
×
99
}
100

101
// WithoutSnakeToCamel is used to add flag methods to the enum
102
func WithoutSnakeToCamel() Option {
66✔
103
        return func(g *Generator) {
132✔
104
                g.leaveSnakeCase = true
66✔
105
        }
66✔
106
}
107

108
// WithJsonPkg is used to add a custom json package to the imports
NEW
109
func WithJsonPkg(pkg string) Option {
×
NEW
110
        return func(g *Generator) {
×
NEW
111
                g.jsonPkg = pkg
×
NEW
112
        }
×
113
}
114

115
// WithPrefix is used to add a custom prefix to the enum constants
116
func WithPrefix(prefix string) Option {
9✔
117
        return func(g *Generator) {
18✔
118
                g.prefix = prefix
9✔
119
        }
9✔
120
}
121

122
// WithPtr adds a way to get a pointer value straight from the const value.
123
func WithPtr() Option {
6✔
124
        return func(g *Generator) {
12✔
125
                g.ptr = true
6✔
126
        }
6✔
127
}
128

129
// WithSQLNullInt is used to add a null int option for SQL interactions.
130
func WithSQLNullInt() Option {
6✔
131
        return func(g *Generator) {
12✔
132
                g.sqlNullInt = true
6✔
133
        }
6✔
134
}
135

136
// WithSQLNullStr is used to add a null string option for SQL interactions.
137
func WithSQLNullStr() Option {
6✔
138
        return func(g *Generator) {
12✔
139
                g.sqlNullStr = true
6✔
140
        }
6✔
141
}
142

143
// WithMustParse is used to add a method `MustParse` that will panic on failure.
144
func WithMustParse() Option {
9✔
145
        return func(g *Generator) {
18✔
146
                g.mustParse = true
9✔
147
        }
9✔
148
}
149

150
// WithForceLower is used to force enums names to lower case while keeping variable names the same.
151
func WithForceLower() Option {
6✔
152
        return func(g *Generator) {
12✔
153
                g.forceLower = true
6✔
154
        }
6✔
155
}
156

157
// WithForceUpper is used to force enums names to upper case while keeping variable names the same.
158
func WithForceUpper() Option {
3✔
159
        return func(g *Generator) {
6✔
160
                g.forceUpper = true
3✔
161
        }
3✔
162
}
163

164
// WithNoComments is used to remove auto generated comments from the enum.
NEW
165
func WithNoComments() Option {
×
NEW
166
        return func(g *Generator) {
×
NEW
167
                g.noComments = true
×
NEW
168
        }
×
169
}
170

171
// WithBuildTags will add build tags to the generated file.
NEW
172
func WithBuildTags(tags ...string) Option {
×
NEW
173
        return func(g *Generator) {
×
NEW
174
                g.buildTags = append(g.buildTags, tags...)
×
NEW
175
        }
×
176
}
177

178
// WithAliases will set up aliases for the generator.
179
func WithAliases(aliases map[string]string) Option {
6✔
180
        return func(g *Generator) {
12✔
181
                if aliases == nil {
6✔
NEW
182
                        return
×
NEW
183
                }
×
184
                g.replacementNames = aliases
6✔
185
        }
186
}
187

188
// WithTemplates is used to provide the filenames of additional templates.
189
func WithTemplates(filenames ...string) Option {
9✔
190
        return func(g *Generator) {
18✔
191
                for _, ut := range template.Must(g.t.ParseFiles(filenames...)).Templates() {
18✔
192
                        if _, ok := g.knownTemplates[ut.Name()]; !ok {
18✔
193
                                g.userTemplateNames = append(g.userTemplateNames, ut.Name())
9✔
194
                        }
9✔
195
                }
196
                g.updateTemplates()
9✔
197
                sort.Strings(g.userTemplateNames)
9✔
198
        }
199
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc