Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add missing support for float64 and several other value types #6

Merged
merged 3 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 48 additions & 40 deletions mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
// It can be used for example to access and manipulate arbitrary nested YAML/JSON structures.
package dig

import "fmt"
import (
"reflect"
)

// Mapping is a nested key-value map where the keys are strings and values are any. In Ruby it is called a Hash (with string keys), in YAML it's called a "mapping".
type Mapping map[string]any
Expand All @@ -22,6 +24,9 @@ func (m *Mapping) UnmarshalYAML(unmarshal func(any) error) error {
//
// It returns a value from a (deeply) nested tree structure.
func (m *Mapping) Dig(keys ...string) any {
if len(keys) == 0 {
return nil
}
v, ok := (*m)[keys[0]]
if !ok {
return nil
Expand Down Expand Up @@ -72,49 +77,54 @@ func (m *Mapping) DigMapping(keys ...string) Mapping {

// Dup creates a dereferenced copy of the Mapping
func (m *Mapping) Dup() Mapping {
new := make(Mapping, len(*m))
newMap := make(Mapping, len(*m))
for k, v := range *m {
switch vt := v.(type) {
case Mapping:
new[k] = vt.Dup()
case *Mapping:
new[k] = vt.Dup()
case []Mapping:
var ns []Mapping
for _, sv := range vt {
ns = append(ns, sv.Dup())
}
new[k] = ns
case []*Mapping:
var ns []Mapping
for _, sv := range vt {
ns = append(ns, sv.Dup())
}
new[k] = ns
case []string:
var ns []string
ns = append(ns, vt...)
new[k] = ns
case []int:
var ns []int
ns = append(ns, vt...)
new[k] = ns
case []bool:
var ns []bool
ns = append(ns, vt...)
new[k] = ns
default:
new[k] = vt
newMap[k] = deepCopy(v)
}
return newMap
}

// deepCopy performs a deep copy of the value using reflection
func deepCopy(value any) any {
if value == nil {
return nil
}

val := reflect.ValueOf(value)

switch val.Kind() {
case reflect.Map:
newMap := reflect.MakeMap(val.Type())
for _, key := range val.MapKeys() {
newMap.SetMapIndex(key, reflect.ValueOf(deepCopy(val.MapIndex(key).Interface())))
}
if plainmap, ok := newMap.Interface().(map[string]any); ok {
return cleanUpInterfaceMap(plainmap)
}
return newMap.Interface()
case reflect.Slice:
if val.IsNil() {
return nil
}
newSlice := reflect.MakeSlice(val.Type(), val.Len(), val.Cap())
for i := 0; i < val.Len(); i++ {
newSlice.Index(i).Set(reflect.ValueOf(deepCopy(val.Index(i).Interface())))
}
if plainslice, ok := newSlice.Interface().([]any); ok {
return cleanUpInterfaceArray(plainslice)
}
return newSlice.Interface()

default:
return value
}
return new
}

// Cleans up a slice of interfaces into slice of actual values
func cleanUpInterfaceArray(in []any) []any {
result := make([]any, len(in))
for i, v := range in {
result[i] = cleanUpMapValue(v)
result[i] = cleanUpValue(v)
}
return result
}
Expand All @@ -123,21 +133,19 @@ func cleanUpInterfaceArray(in []any) []any {
func cleanUpInterfaceMap(in map[string]any) Mapping {
result := make(Mapping)
for k, v := range in {
result[fmt.Sprintf("%v", k)] = cleanUpMapValue(v)
result[k] = cleanUpValue(v)
}
return result
}

// Cleans up the value in the map, recurses in case of arrays and maps
func cleanUpMapValue(v any) any {
func cleanUpValue(v any) any {
switch v := v.(type) {
case []any:
return cleanUpInterfaceArray(v)
case map[string]any:
return cleanUpInterfaceMap(v)
case string, int, bool, nil:
return v
default:
return fmt.Sprintf("%v", v)
return v
}
}
24 changes: 23 additions & 1 deletion 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 @@ -126,13 +139,22 @@ func TestDup(t *testing.T) {
})
}

func TestUnmarshalYamlWithNil(t *testing.T) {
func TestUnmarshalJSONWithNil(t *testing.T) {
data := []byte(`{"foo": null}`)
var m dig.Mapping
mustBeNil(t, json.Unmarshal(data, &m))
mustBeNil(t, m.Dig("foo"))
}

func TestUnmarshalJSONWithFloat(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
Loading