Skip to content

Commit

Permalink
Merge pull request #156 from marcinc/get-map-support-complex-keys
Browse files Browse the repository at this point in the history
Adds funk.Get support for complex map keys
  • Loading branch information
thoas committed Dec 26, 2022
2 parents df1ff15 + ad87c80 commit 05e703f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
12 changes: 8 additions & 4 deletions README.rst
Expand Up @@ -449,11 +449,15 @@ Retrieves the value at path of struct(s) or map(s).
"FirstName": "Dark",
"LastName": "Vador",
"Age": 30,
"Labels": map[string]interface{} {
"example.com/hello": "world",
},
} // foo2.Bar is nil
funk.Get(bar, "Name") // "Test"
funk.Get([]map[string]interface{}{foo1, foo2}, "Bar.Name") // []string{"Test"}
funk.Get(foo2, "Bar.Name") // nil
funk.Get(foo2, `Labels."example.com/hello"`) // world
``funk.Get`` also handles ``nil`` values:
Expand Down Expand Up @@ -503,7 +507,7 @@ Set value at a path of a struct
.. code-block:: go
var bar Bar = Bar{
Name: "level-0",
Name: "level-0",
Bar: &Bar{
Name: "level-1",
Bars: []*Bar{
Expand Down Expand Up @@ -825,14 +829,14 @@ Generates a sharded string with a fixed length and depth.
funk.Subset
.............

Returns true if a collection is a subset of another
Returns true if a collection is a subset of another

.. code-block:: go
funk.Subset([]int{1, 2, 4}, []int{1, 2, 3, 4, 5}) // true
funk.Subset([]string{"foo", "bar"},[]string{"foo", "bar", "hello", "bar", "hi"}) //true
Performance
-----------

Expand Down
12 changes: 11 additions & 1 deletion retrieve.go
Expand Up @@ -82,7 +82,17 @@ func get(value reflect.Value, path string, opts ...option) reflect.Value {
return resultSlice
}

parts := strings.Split(path, ".")
quoted := false
parts := strings.FieldsFunc(path, func(r rune) bool {
if r == '"' {
quoted = !quoted
}
return !quoted && r == '.'
})

for i, part := range parts {
parts[i] = strings.Trim(part, "\"")
}

for _, part := range parts {
value = redirectValue(value)
Expand Down
5 changes: 4 additions & 1 deletion retrieve_test.go
Expand Up @@ -50,12 +50,15 @@ func TestGetMap(t *testing.T) {
is := assert.New(t)
m := map[string]interface{}{
"bar": map[string]interface{}{
"name": "foobar",
"name": "foobar",
"example.com/hello": "world",
},
}

is.Equal("foobar", Get(m, "bar.name"))
is.Equal("world", Get(m, `bar."example.com/hello"`))
is.Equal(nil, Get(m, "foo.name"))
is.Equal(nil, Get(m, `foo."example.com/hello"`))
is.Equal([]interface{}{"dark", "dark"}, Get([]map[string]interface{}{m1, m2}, "firstname"))
is.Equal([]interface{}{"test"}, Get([]map[string]interface{}{m1, m2}, "bar.name"))
}
Expand Down

0 comments on commit 05e703f

Please sign in to comment.