-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopts.go
321 lines (289 loc) · 9.17 KB
/
opts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
* The MIT License (MIT)
*
* Copyright (c) 2022 Chance Dinkins <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, Subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or Substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package caps
import "github.com/chanced/caps/token"
// ReplaceStyle is used to indicate the case style that text should be
// transformed to when performing replacement in a Converter.
//
// For example, a call to ToCamel with a ReplaceStyleScreaming with an input of
// "MarshalJson" will return "MarshaalJSON" while ReplaceStyleCamel would return
// "MarshalJson"
type ReplaceStyle uint8
const (
ReplaceStyleNotSpecified ReplaceStyle = iota
ReplaceStyleCamel // Text should be replaced with the Camel variant (e.g. "Json").
ReplaceStyleScreaming // Text should be replaced with the screaming variant (e.g. "JSON").
ReplaceStyleLower // Text should be replaced with the lowercase variant (e.g. "json").
)
func (rs ReplaceStyle) String() string {
switch rs {
case ReplaceStyleCamel:
return "ReplaceStyleCamel"
case ReplaceStyleScreaming:
return "ReplaceStyleScreaming"
case ReplaceStyleLower:
return "ReplaceStyleLower"
}
return "ReplaceStyleNotSpecified"
}
// IsCamel returns true if rs equals ReplaceStyleCamel
func (rs ReplaceStyle) IsCamel() bool {
return rs == ReplaceStyleCamel
}
// IsScreaming returns true if rs equals ReplaceStyleScreaming
func (rs ReplaceStyle) IsScreaming() bool {
return rs == ReplaceStyleScreaming
}
// IsLower returns true if rs equals ReplaceStyleLower
func (rs ReplaceStyle) IsLower() bool {
return rs == ReplaceStyleLower
}
type Style uint8
const (
StyleNotSpecified Style = iota
StyleLower // The output should be lowercase (e.g. "an_example")
StyleScreaming // The output should be screaming (e.g. "AN_EXAMPLE")
StyleCamel // The output should be camel case (e.g. "AnExample")
StyleLowerCamel // The output should be lower camel case (e.g. "anExample")
)
func (s Style) String() string {
switch s {
case StyleLower:
return "StyleLower"
case StyleScreaming:
return "StyleScreaming"
case StyleCamel:
return "StyleCamel"
case StyleLowerCamel:
return "StyleLowerCamel"
}
return "StyleNotSpecified"
}
func (s Style) IsLower() bool {
return s == StyleLower
}
func (s Style) IsScreaming() bool {
return s == StyleScreaming
}
func (s Style) IsCamel() bool {
return s == StyleCamel
}
func (s Style) IsLowerCamel() bool {
return s == StyleLowerCamel
}
// Opts include configurable options for case conversion.
//
// See the documentation for the individual fields for more information.
type Opts struct {
// Any characters within this string will be allowed in the output.
//
// This does not affect delimiters (e.g. '_', '-', '.') as they are added
// post-tokenization.
//
// Default:
// ""
AllowedSymbols string
// The Converter to use.
//
// Default:
// DefaultConverter
Converter Converter
// ReplaceStyle overwrites the way Replacements are cased.
//
// A typical call to ToLowerCamel for "ServeJSON" with a Converter that
// contains {"Json": "JSON"} would result in "serveJSON" by using the
// ReplaceStyleScreaming variant. If ReplacementStyle was set to
// ReplaceStyleCamel then a call to ToLowerCamel then the result would be
// "serveHttp".
//
// The default ReplaceStyle is dependent upon the target Style.
ReplaceStyle ReplaceStyle
// NumberRules are used by the DefaultTokenizer to augment the standard
// rules for determining if a rune is part of a number.
//
// Note, if you add special characters here, they must be present in the
// AllowedSymbols string for them to be part of the output.
NumberRules token.NumberRules
}
// WithConverter sets the Converter to use
func WithConverter(converter Converter) Opts {
return Opts{
Converter: converter,
}
}
// WithReplaceStyle sets the ReplaceStyle to use
//
// There are also methods for each ReplaceStyle (e.g. WithReplaceStyleCamel)
func WithReplaceStyle(style ReplaceStyle) Opts {
return Opts{
ReplaceStyle: style,
}
}
// WithReplaceStyleCamel indicates Replacements should use the Camel variant
// (e.g. "Json").
func WithReplaceStyleCamel() Opts {
return Opts{
ReplaceStyle: ReplaceStyleCamel,
}
}
// WithReplaceStyleScreaming indicates Replacements should use the screaming
// variant (e.g. "JSON").
func WithReplaceStyleScreaming() Opts {
return Opts{
ReplaceStyle: ReplaceStyleScreaming,
}
}
// WithReplaceStyleLower indicates Replacements should use the lowercase variant
// (e.g. "json").
func WithReplaceStyleLower() Opts {
return Opts{
ReplaceStyle: ReplaceStyleLower,
}
}
// WithNumberRules sets the NumberRules to use
func WithNumberRules(rules NumberRules) Opts {
return Opts{
NumberRules: rules,
}
}
// WithAllowedSymbols sets the AllowedSymbols to use
func WithAllowedSymbols(symbols string) Opts {
return Opts{
AllowedSymbols: symbols,
}
}
func loadOpts(opts []Opts) Opts {
result := Opts{
AllowedSymbols: "",
Converter: DefaultConverter,
ReplaceStyle: ReplaceStyleScreaming,
}
for _, opt := range opts {
result.AllowedSymbols += opt.AllowedSymbols
if opt.Converter != nil {
result.Converter = opt.Converter
}
if opt.ReplaceStyle != ReplaceStyleNotSpecified {
result.ReplaceStyle = opt.ReplaceStyle
}
if len(opt.NumberRules) > 0 {
if result.NumberRules == nil {
result.NumberRules = make(NumberRules)
}
for k, v := range opt.NumberRules {
result.NumberRules[k] = v
}
}
}
return result
}
// Config include configurable options for Caps instances.
//
// See the documentation for the individual fields for more information.
type Config struct {
// Any characters within this string will be allowed in the output.
//
// This does not affect delimiters (e.g. "_", "-", ".") as they are added
// post-tokenization.
//
// Default:
// ""
AllowedSymbols string
// The Converter to use.
//
// Default:
// A StdConverter with the Replacements, Caser, and Tokenizer.
Converter Converter
// If not set, this will be DefaultReplacements.
Replacements []Replacement
// ReplaceStyle overwrites the way words are replaced.
//
// A typical call to ToLowerCamel for "ServeJSON" with a Converter that
// contains {"Json": "JSON"} would result in "serveJSON" by using the
// ReplaceStyleScreaming variant. If ReplacementStyle was set to
// ReplaceStyleCamel, on the call to ToLowerCamel then the result would
// be "serveHttp".
//
// The default replacement style is dependent upon the target Style.
ReplaceStyle ReplaceStyle
// NumberRules are used by the DefaultTokenizer to augment the standard
// rules for determining if a rune is part of a number.
//
// Note, if you add special characters here, they must be present in the
// AllowedSymbols string for them to be part of the output.
NumberRules token.NumberRules
// Special unicode case rules.
// See unicode.SpecialCase or token.Caser for more information.
//
// Default: token.DefaultCaser (which relies on the default unicode
// functions)
Caser token.Caser
// If not set, uses StdTokenizer with the provided delimiters and token.Caser.
Tokenizer Tokenizer
}
func loadConfig(opts []Config) Config {
result := Config{
AllowedSymbols: "",
ReplaceStyle: ReplaceStyleScreaming,
}
for _, opt := range opts {
result.AllowedSymbols += opt.AllowedSymbols
if opt.Converter != nil {
result.Converter = opt.Converter
}
if opt.ReplaceStyle != ReplaceStyleNotSpecified {
result.ReplaceStyle = opt.ReplaceStyle
}
if len(opt.NumberRules) > 0 {
if result.NumberRules == nil {
result.NumberRules = make(NumberRules)
}
for k, v := range opt.NumberRules {
result.NumberRules[k] = v
}
}
if opt.Replacements != nil {
result.Replacements = append(result.Replacements, opt.Replacements...)
}
if opt.Caser != nil {
result.Caser = opt.Caser
}
if opt.Tokenizer != nil {
result.Tokenizer = opt.Tokenizer
}
}
if result.Caser == nil {
result.Caser = token.DefaultCaser
}
if result.Replacements == nil {
result.Replacements = DefaultReplacements
}
if result.Tokenizer == nil {
result.Tokenizer = NewTokenizer(DEFAULT_DELIMITERS, result.Caser)
}
if result.Converter == nil {
result.Converter = NewConverter(result.Replacements, result.Tokenizer, result.Caser)
}
return result
}