This repository has been archived by the owner on May 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
casemapper.go
172 lines (152 loc) · 3.99 KB
/
casemapper.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
// SPDX-FileCopyrightText: 2022 Weston Schmidt <[email protected]>
// SPDX-License-Identifier: Apache-2.0
// casemapper bundles up the tedious work of tagging structures with a specific
// naming convention case for configuration.
//
// # Extended Character Sets
//
// This mapping is based on strings and runes so extended character sets are
// supported by default.
package casemapper
import (
"errors"
"fmt"
"sort"
"strings"
"unicode"
"github.com/goschtalt/goschtalt"
"resenje.org/casbab"
)
// Popular names of commonly used naming conventions.
const (
FlatCase = "twowords"
UpperCase = "TWOWORDS"
CamelCase = "twoWords"
PascalCase = "TwoWords"
SnakeCase = "two_words"
ScreamingSnakeCase = "TWO_WORDS"
TitleCase = "Two_Words"
KebabCase = "two-words"
ScreamingKebabCase = "TWO-WORDS"
TrainCase = "Two-Words"
)
var (
ErrDuplicate = errors.New("duplicate adjustment")
)
var fmtToFunc = map[string]func(string) string{
"twowords": allLower,
"TWOWORDS": allUpper,
"twoWords": casbab.Camel,
"TwoWords": casbab.Pascal,
"two_words": casbab.Snake,
"TWO_WORDS": casbab.ScreamingSnake,
"two_Words": lowerCamelSnake,
"Two_Words": casbab.CamelSnake,
"two-words": casbab.Kebab,
"TWO-WORDS": casbab.ScreamingKebab,
"two-Words": lowerCamelKebab,
"Two-Words": casbab.CamelKebab,
}
type casemapper struct {
toCase func(string) string
adjustments map[string]string
}
func (c casemapper) Map(in string) string {
out, found := c.adjustments[in]
if !found {
out = c.toCase(in)
}
return out
}
// ConfigIs provides a strict field/key mapper that converts the config
// values from the specified nomenclature into the go structure name.
//
// Since the names of the different formatting styles are not standardized, only
// a few of the common ones have consts defined. The complete list is below:
//
// - twowords
// - TWOWORDS
// - twoWords
// - TwoWords
// - two_words
// - TWO_WORDS
// - two_Words
// - Two_Words
// - two-words
// - TWO-WORDS
// - Two-Words
// - two-Words
//
// This option provides a goschtalt.KeymapMapper based option that will convert
// every input string, ending the chain 100% of the time.
//
// To make adjustments pass in a map (or many) with keys being the golang
// structure field names and values being the configuration name.
func ConfigIs(format string, structToConfig ...map[string]string) goschtalt.Option {
sToC, err := merge(structToConfig)
if err != nil {
return goschtalt.WithError(err)
}
if toCase, found := fmtToFunc[format]; found {
return goschtalt.Options(
goschtalt.DefaultUnmarshalOptions(
goschtalt.KeymapMapper(&casemapper{
toCase: toCase,
adjustments: sToC,
}),
),
goschtalt.DefaultValueOptions(
goschtalt.KeymapMapper(&casemapper{
toCase: toCase,
adjustments: sToC,
}),
),
)
}
keys := make([]string, 0, len(fmtToFunc))
for k := range fmtToFunc {
keys = append(keys, k)
}
sort.Strings(keys)
return goschtalt.WithError(
fmt.Errorf("%w, '%s' unknown format by casemapper.ConfigIs(). Known formats: %s",
goschtalt.ErrInvalidInput,
format,
strings.Join(keys, ", "),
),
)
}
func merge(in []map[string]string) (map[string]string, error) {
sToC := make(map[string]string, len(in))
for i := range in {
for k, v := range in[i] {
if _, a := sToC[k]; a {
return nil, fmt.Errorf("%w, '%s' is duplicated.", ErrDuplicate, k)
}
sToC[k] = v
}
}
return sToC, nil
}
func allLower(s string) string {
return strings.Join(strings.Split(casbab.Lower(s), " "), "")
}
func allUpper(s string) string {
return strings.Join(strings.Split(casbab.Screaming(s), " "), "")
}
func lowerCamelSnake(s string) string {
if len(s) == 0 {
return ""
}
r := []rune(casbab.CamelSnake(s))
r[0] = unicode.ToLower(r[0])
return string(r)
}
func lowerCamelKebab(s string) string {
if len(s) == 0 {
return ""
}
r := []rune(casbab.CamelKebab(s))
r[0] = unicode.ToLower(r[0])
return string(r)
}