diff --git a/internal/encoding/encoding.go b/internal/encoding/encoding.go index af56a497..736c3fd2 100644 --- a/internal/encoding/encoding.go +++ b/internal/encoding/encoding.go @@ -9,36 +9,35 @@ import ( "gopkg.in/yaml.v2" ) +// TomlToJson converts TOML data into its JSON representation. func TomlToJson(data []byte) ([]byte, error) { var val any if err := toml.NewDecoder(bytes.NewReader(data)).Decode(&val); err != nil { return nil, err } - - var buf bytes.Buffer - if err := json.NewEncoder(&buf).Encode(val); err != nil { - return nil, err - } - - return buf.Bytes(), nil + return encodeToJSON(val) } +// YamlToJson converts YAML data into its JSON representation. func YamlToJson(data []byte) ([]byte, error) { var val any 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 } +// convertKeyToString ensures all keys of the map are of type string. func convertKeyToString(in map[any]any) map[string]any { res := make(map[string]any) for k, v := range in { @@ -47,10 +46,12 @@ func convertKeyToString(in map[any]any) map[string]any { return res } +// convertNumberToJsonNumber converts numbers into json.Number type for compatibility. func convertNumberToJsonNumber(in any) json.Number { return json.Number(lang.Repr(in)) } +// convertSlice processes slice items to ensure key compatibility. func convertSlice(in []any) []any { res := make([]any, len(in)) for i, v := range in { @@ -59,6 +60,7 @@ func convertSlice(in []any) []any { return res } +// toStringKeyMap processes the data to ensure that all map keys are of type string. func toStringKeyMap(v any) any { switch v := v.(type) { case []any: