Skip to content

Commit

Permalink
Add missing support for float values
Browse files Browse the repository at this point in the history
Signed-off-by: Kimmo Lehto <[email protected]>
  • Loading branch information
kke committed Nov 12, 2024
1 parent 912cb53 commit fed4fa6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
6 changes: 5 additions & 1 deletion mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ func (m *Mapping) Dup() Mapping {
var ns []int
ns = append(ns, vt...)
new[k] = ns
case []float64:
var ns []float64
ns = append(ns, vt...)
new[k] = ns
case []bool:
var ns []bool
ns = append(ns, vt...)
Expand Down Expand Up @@ -135,7 +139,7 @@ func cleanUpMapValue(v any) any {
return cleanUpInterfaceArray(v)
case map[string]any:
return cleanUpInterfaceMap(v)
case string, int, bool, nil:
case string, bool, int, float64, nil:
return v
default:
return fmt.Sprintf("%v", v)
Expand Down
22 changes: 22 additions & 0 deletions mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ func TestDig(t *testing.T) {
t.Run("non-existing key should return nil", func(t *testing.T) {
mustBeNil(t, m.Dig("foo", "non-existing"))
})

t.Run("int value", func(t *testing.T) {
m.DigMapping("foo")["int"] = 1
mustEqual(t, 1, m.Dig("foo", "int"))
})
t.Run("float value", func(t *testing.T) {
m.DigMapping("foo")["float"] = 0.5
mustEqual(t, 0.5, m.Dig("foo", "float"))
})
t.Run("bool value", func(t *testing.T) {
m.DigMapping("foo")["bool"] = true
mustEqual(t, true, m.Dig("foo", "bool"))
})
}

func TestDigString(t *testing.T) {
Expand Down Expand Up @@ -133,6 +146,15 @@ func TestUnmarshalYamlWithNil(t *testing.T) {
mustBeNil(t, m.Dig("foo"))
}

func TestUnmarshalYamlWithFloat(t *testing.T) {
data := []byte(`{"foo": 0.5}`)
var m dig.Mapping
mustBeNil(t, json.Unmarshal(data, &m))
val, ok := m.Dig("foo").(float64)
mustEqual(t, true, ok)
mustEqual(t, 0.5, val)
}

func ExampleMapping_Dig() {
h := dig.Mapping{
"greeting": dig.Mapping{
Expand Down

0 comments on commit fed4fa6

Please sign in to comment.