Convert json numbers to numbers (#5496)

This commit is contained in:
WithoutPants
2024-11-22 08:27:23 +11:00
committed by GitHub
parent 5f690d96bd
commit 6c5bf5f052
4 changed files with 104 additions and 0 deletions

View File

@@ -1,8 +1,11 @@
package utils
import (
"encoding/json"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNestedMapGet(t *testing.T) {
@@ -279,3 +282,55 @@ func TestMergeMaps(t *testing.T) {
})
}
}
func TestConvertMapJSONNumbers(t *testing.T) {
tests := []struct {
name string
input map[string]interface{}
expected map[string]interface{}
}{
{
name: "Convert JSON numbers to numbers",
input: map[string]interface{}{
"int": json.Number("12"),
"float": json.Number("12.34"),
"string": "foo",
},
expected: map[string]interface{}{
"int": int64(12),
"float": 12.34,
"string": "foo",
},
},
{
name: "Convert JSON numbers to numbers in nested maps",
input: map[string]interface{}{
"foo": map[string]interface{}{
"int": json.Number("56"),
"float": json.Number("56.78"),
"nested-string": "bar",
},
"int": json.Number("12"),
"float": json.Number("12.34"),
"string": "foo",
},
expected: map[string]interface{}{
"foo": map[string]interface{}{
"int": int64(56),
"float": 56.78,
"nested-string": "bar",
},
"int": int64(12),
"float": 12.34,
"string": "foo",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := ConvertMapJSONNumbers(tt.input)
assert.Equal(t, tt.expected, result)
})
}
}