From fed4fa691e66207bc27c873cc20e5f3d5a855020 Mon Sep 17 00:00:00 2001 From: Kimmo Lehto Date: Tue, 12 Nov 2024 10:29:36 +0200 Subject: [PATCH] Add missing support for float values Signed-off-by: Kimmo Lehto --- mapping.go | 6 +++++- mapping_test.go | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/mapping.go b/mapping.go index cae51ea..8942004 100644 --- a/mapping.go +++ b/mapping.go @@ -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...) @@ -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) diff --git a/mapping_test.go b/mapping_test.go index cb2288a..e907535 100644 --- a/mapping_test.go +++ b/mapping_test.go @@ -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) { @@ -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{