Skip to content

Commit

Permalink
chore: refactor (zeromicro#3545)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan authored Sep 6, 2023
1 parent ca5a7df commit 1ba1724
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions internal/encoding/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func TomlToJson(data []byte) ([]byte, error) {
if err := toml.NewDecoder(bytes.NewReader(data)).Decode(&val); err != nil {
return nil, err
}

return encodeToJSON(val)
}

Expand All @@ -24,17 +25,8 @@ func YamlToJson(data []byte) ([]byte, error) {
if err := yaml.Unmarshal(data, &val); err != nil {
return nil, err
}
val = toStringKeyMap(val)
return encodeToJSON(val)
}

// 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
return encodeToJSON(toStringKeyMap(val))
}

// convertKeyToString ensures all keys of the map are of type string.
Expand All @@ -60,6 +52,16 @@ func convertSlice(in []any) []any {
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.
func toStringKeyMap(v any) any {
switch v := v.(type) {
Expand Down

0 comments on commit 1ba1724

Please sign in to comment.