From 1ba1724c65cefcc26450502b5e38cc1c88f87304 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Wed, 6 Sep 2023 22:36:43 +0800 Subject: [PATCH] chore: refactor (#3545) --- internal/encoding/encoding.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/internal/encoding/encoding.go b/internal/encoding/encoding.go index 736c3fd2..e1f82fb0 100644 --- a/internal/encoding/encoding.go +++ b/internal/encoding/encoding.go @@ -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) } @@ -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. @@ -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) {