Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
Christoph Conrads
objx
Commits
35313a95
Commit
35313a95
authored
Apr 15, 2019
by
earncef
Committed by
Hanzei
Apr 15, 2019
Browse files
Fixes #88 (#95)
parent
a4ea3d61
Changes
2
Hide whitespace changes
Inline
Side-by-side
accessors.go
View file @
35313a95
package
objx
import
(
"reflect"
"regexp"
"strconv"
"strings"
...
...
@@ -141,9 +142,10 @@ func access(current interface{}, selector string, value interface{}, isSet bool)
default
:
current
=
nil
}
// do we need to access the item of an array?
if
index
>
-
1
{
if
array
,
ok
:=
current
.
([]
interface
{}
);
ok
{
if
array
,
ok
:=
interSlice
(
current
);
ok
{
if
index
<
len
(
array
)
{
current
=
array
[
index
]
}
else
{
...
...
@@ -156,3 +158,22 @@ func access(current interface{}, selector string, value interface{}, isSet bool)
}
return
current
}
func
interSlice
(
slice
interface
{})
([]
interface
{},
bool
)
{
if
array
,
ok
:=
slice
.
([]
interface
{});
ok
{
return
array
,
ok
}
s
:=
reflect
.
ValueOf
(
slice
)
if
s
.
Kind
()
!=
reflect
.
Slice
{
return
nil
,
false
}
ret
:=
make
([]
interface
{},
s
.
Len
())
for
i
:=
0
;
i
<
s
.
Len
();
i
++
{
ret
[
i
]
=
s
.
Index
(
i
)
.
Interface
()
}
return
ret
,
true
}
accessors_test.go
View file @
35313a95
...
...
@@ -24,11 +24,21 @@ func TestAccessorsAccessGetDeep(t *testing.T) {
"name"
:
objx
.
Map
{
"first"
:
"Tyler"
,
"last"
:
"Bunnell"
,
"friends"
:
[]
string
{
"Capitol"
,
"Bollocks"
,
},
"ifriends"
:
[]
interface
{}{
"Capitol"
,
"Bollocks"
,
},
},
}
assert
.
Equal
(
t
,
"Tyler"
,
m
.
Get
(
"name.first"
)
.
Data
())
assert
.
Equal
(
t
,
"Bunnell"
,
m
.
Get
(
"name.last"
)
.
Data
())
assert
.
Equal
(
t
,
"Capitol"
,
m
.
Get
(
"name.friends[0]"
)
.
Data
())
assert
.
Equal
(
t
,
"Capitol"
,
m
.
Get
(
"name.ifriends[0]"
)
.
Data
())
}
func
TestAccessorsAccessGetDeepDeep
(
t
*
testing
.
T
)
{
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment