chore: refactor (#3545)

master
Kevin Wan 1 year ago committed by GitHub
parent ca5a7df5b0
commit 1ba1724c65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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…
Cancel
Save