-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnamer.go
49 lines (37 loc) · 1.13 KB
/
namer.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
package gowalker
import (
"iter"
"strings"
)
type Namer interface {
Key(fs iter.Seq[string]) string
}
type NamerFunc func(fs iter.Seq[string]) string
func (n NamerFunc) Key(fs iter.Seq[string]) string { return n(fs) }
// Fullname namer.
func Fullname(separator string, convFn func(string) string) Namer {
return NamerFunc(func(fs iter.Seq[string]) string {
var out string
var sep string
if convFn == nil {
convFn = func(s string) string { return s }
}
for f := range fs {
out += sep
out += convFn(f)
sep = separator
}
return out
})
}
// UpperNamer - concat a uppercase parent's name with a uppercase child's one with underscore.
var UpperNamer = Fullname("_", strings.ToUpper)
// EnvNamer - STRUCT_FIELD naming.
var EnvNamer = UpperNamer
// StructFieldNamer - concat a parent's name with a child's one with dot.
var StructFieldNamer = Fullname(".", nil)
// DashToLoverNamer - concat a lowercase parent's name with a lowercase child's one with dash.
var DashToLoverNamer = Fullname("-", strings.ToLower)
// FlagNamer - struct-field naming.
var FlagNamer = DashToLoverNamer
var DefaultNamer = Fullname("", nil)