mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-02-02 16:28:39 +08:00
chore: refactor (#3545)
This commit is contained in:
parent
ca5a7df5b0
commit
1ba1724c65
@ -15,6 +15,7 @@ func TomlToJson(data []byte) ([]byte, error) {
|
|||||||
if err := toml.NewDecoder(bytes.NewReader(data)).Decode(&val); err != nil {
|
if err := toml.NewDecoder(bytes.NewReader(data)).Decode(&val); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return encodeToJSON(val)
|
return encodeToJSON(val)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -24,17 +25,8 @@ func YamlToJson(data []byte) ([]byte, error) {
|
|||||||
if err := yaml.Unmarshal(data, &val); err != nil {
|
if err := yaml.Unmarshal(data, &val); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
val = toStringKeyMap(val)
|
|
||||||
return encodeToJSON(val)
|
|
||||||
}
|
|
||||||
|
|
||||||
// encodeToJSON encodes the given value into its JSON representation.
|
return encodeToJSON(toStringKeyMap(val))
|
||||||
func encodeToJSON(val any) ([]byte, error) {
|
|
||||||
var buf bytes.Buffer
|
|
||||||
if err := json.NewEncoder(&buf).Encode(val); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return buf.Bytes(), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// convertKeyToString ensures all keys of the map are of type string.
|
// convertKeyToString ensures all keys of the map are of type string.
|
||||||
@ -60,6 +52,16 @@ func convertSlice(in []any) []any {
|
|||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// encodeToJSON encodes the given value into its JSON representation.
|
||||||
|
func encodeToJSON(val any) ([]byte, error) {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
if err := json.NewEncoder(&buf).Encode(val); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
// toStringKeyMap processes the data to ensure that all map keys are of type string.
|
// toStringKeyMap processes the data to ensure that all map keys are of type string.
|
||||||
func toStringKeyMap(v any) any {
|
func toStringKeyMap(v any) any {
|
||||||
switch v := v.(type) {
|
switch v := v.(type) {
|
||||||
|
Loading…
Reference in New Issue
Block a user