feat: accept camelcase for config keys (#2651)
* feat: accept camelcase for config keys * chore: refactor * chore: refactor * chore: add more tests * chore: refactor * fix: map elements of array
This commit is contained in:
@@ -7,9 +7,13 @@ import (
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/jsonx"
|
||||
"github.com/zeromicro/go-zero/core/mapping"
|
||||
"github.com/zeromicro/go-zero/internal/encoding"
|
||||
)
|
||||
|
||||
const distanceBetweenUpperAndLower = 32
|
||||
|
||||
var loaders = map[string]func([]byte, interface{}) error{
|
||||
".json": LoadFromJsonBytes,
|
||||
".toml": LoadFromTomlBytes,
|
||||
@@ -49,7 +53,12 @@ func LoadConfig(file string, v interface{}, opts ...Option) error {
|
||||
|
||||
// LoadFromJsonBytes loads config into v from content json bytes.
|
||||
func LoadFromJsonBytes(content []byte, v interface{}) error {
|
||||
return mapping.UnmarshalJsonBytes(content, v)
|
||||
var m map[string]interface{}
|
||||
if err := jsonx.Unmarshal(content, &m); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return mapping.UnmarshalJsonMap(toCamelCaseKeyMap(m), v, mapping.WithCanonicalKeyFunc(toCamelCase))
|
||||
}
|
||||
|
||||
// LoadConfigFromJsonBytes loads config into v from content json bytes.
|
||||
@@ -60,12 +69,22 @@ func LoadConfigFromJsonBytes(content []byte, v interface{}) error {
|
||||
|
||||
// LoadFromTomlBytes loads config into v from content toml bytes.
|
||||
func LoadFromTomlBytes(content []byte, v interface{}) error {
|
||||
return mapping.UnmarshalTomlBytes(content, v)
|
||||
b, err := encoding.TomlToJson(content)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return LoadFromJsonBytes(b, v)
|
||||
}
|
||||
|
||||
// LoadFromYamlBytes loads config into v from content yaml bytes.
|
||||
func LoadFromYamlBytes(content []byte, v interface{}) error {
|
||||
return mapping.UnmarshalYamlBytes(content, v)
|
||||
b, err := encoding.YamlToJson(content)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return LoadFromJsonBytes(b, v)
|
||||
}
|
||||
|
||||
// LoadConfigFromYamlBytes loads config into v from content yaml bytes.
|
||||
@@ -80,3 +99,66 @@ func MustLoad(path string, v interface{}, opts ...Option) {
|
||||
log.Fatalf("error: config file %s, %s", path, err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func toCamelCase(s string) string {
|
||||
var buf strings.Builder
|
||||
buf.Grow(len(s))
|
||||
var capNext bool
|
||||
boundary := true
|
||||
for _, v := range s {
|
||||
isCap := v >= 'A' && v <= 'Z'
|
||||
isLow := v >= 'a' && v <= 'z'
|
||||
if boundary && (isCap || isLow) {
|
||||
if capNext {
|
||||
if isLow {
|
||||
v -= distanceBetweenUpperAndLower
|
||||
}
|
||||
} else {
|
||||
if isCap {
|
||||
v += distanceBetweenUpperAndLower
|
||||
}
|
||||
}
|
||||
boundary = false
|
||||
}
|
||||
if isCap || isLow {
|
||||
buf.WriteRune(v)
|
||||
capNext = false
|
||||
} else if v == ' ' || v == '\t' {
|
||||
buf.WriteRune(v)
|
||||
capNext = false
|
||||
boundary = true
|
||||
} else if v == '_' {
|
||||
capNext = true
|
||||
boundary = true
|
||||
} else {
|
||||
buf.WriteRune(v)
|
||||
capNext = true
|
||||
}
|
||||
}
|
||||
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func toCamelCaseInterface(v interface{}) interface{} {
|
||||
switch vv := v.(type) {
|
||||
case map[string]interface{}:
|
||||
return toCamelCaseKeyMap(vv)
|
||||
case []interface{}:
|
||||
var arr []interface{}
|
||||
for _, vvv := range vv {
|
||||
arr = append(arr, toCamelCaseInterface(vvv))
|
||||
}
|
||||
return arr
|
||||
default:
|
||||
return v
|
||||
}
|
||||
}
|
||||
|
||||
func toCamelCaseKeyMap(m map[string]interface{}) map[string]interface{} {
|
||||
res := make(map[string]interface{})
|
||||
for k, v := range m {
|
||||
res[toCamelCase(k)] = toCamelCaseInterface(v)
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user