Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
d5e135e4
Commit
d5e135e4
authored
Mar 22, 2016
by
Avesh Agarwal
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix jsonpath to handle maps with key of nonstring types
parent
b50e89e3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
21 additions
and
6 deletions
+21
-6
jsonpath.go
pkg/util/jsonpath/jsonpath.go
+7
-1
jsonpath_test.go
pkg/util/jsonpath/jsonpath_test.go
+13
-4
node.go
pkg/util/jsonpath/node.go
+1
-1
No files found.
pkg/util/jsonpath/jsonpath.go
View file @
d5e135e4
...
@@ -325,7 +325,13 @@ func (j *JSONPath) evalField(input []reflect.Value, node *FieldNode) ([]reflect.
...
@@ -325,7 +325,13 @@ func (j *JSONPath) evalField(input []reflect.Value, node *FieldNode) ([]reflect.
return
nil
,
err
return
nil
,
err
}
}
}
else
if
value
.
Kind
()
==
reflect
.
Map
{
}
else
if
value
.
Kind
()
==
reflect
.
Map
{
result
=
value
.
MapIndex
(
reflect
.
ValueOf
(
node
.
Value
))
mapKeyType
:=
value
.
Type
()
.
Key
()
nodeValue
:=
reflect
.
ValueOf
(
node
.
Value
)
// node value type must be convertible to map key type
if
!
nodeValue
.
Type
()
.
ConvertibleTo
(
mapKeyType
)
{
return
results
,
fmt
.
Errorf
(
"%s is not convertible to %s"
,
nodeValue
,
mapKeyType
)
}
result
=
value
.
MapIndex
(
nodeValue
.
Convert
(
mapKeyType
))
}
}
if
result
.
IsValid
()
{
if
result
.
IsValid
()
{
results
=
append
(
results
,
result
)
results
=
append
(
results
,
result
)
...
...
pkg/util/jsonpath/jsonpath_test.go
View file @
d5e135e4
...
@@ -114,11 +114,14 @@ type bicycle struct {
...
@@ -114,11 +114,14 @@ type bicycle struct {
Price
float32
Price
float32
}
}
type
empName
string
type
job
string
type
store
struct
{
type
store
struct
{
Book
[]
book
Book
[]
book
Bicycle
bicycle
Bicycle
bicycle
Name
string
Name
string
Labels
map
[
string
]
int
Labels
map
[
string
]
int
Employees
map
[
empName
]
job
}
}
func
TestStructInput
(
t
*
testing
.
T
)
{
func
TestStructInput
(
t
*
testing
.
T
)
{
...
@@ -136,6 +139,10 @@ func TestStructInput(t *testing.T) {
...
@@ -136,6 +139,10 @@ func TestStructInput(t *testing.T) {
"web/html"
:
15
,
"web/html"
:
15
,
"k8s-app"
:
20
,
"k8s-app"
:
20
,
},
},
Employees
:
map
[
empName
]
job
{
"jason"
:
"manager"
,
"dan"
:
"clerk"
,
},
}
}
storeTests
:=
[]
jsonpathTest
{
storeTests
:=
[]
jsonpathTest
{
...
@@ -147,6 +154,8 @@ func TestStructInput(t *testing.T) {
...
@@ -147,6 +154,8 @@ func TestStructInput(t *testing.T) {
{
"array"
,
"{[0:2]}"
,
[]
string
{
"Monday"
,
"Tudesday"
},
"Monday Tudesday"
},
{
"array"
,
"{[0:2]}"
,
[]
string
{
"Monday"
,
"Tudesday"
},
"Monday Tudesday"
},
{
"variable"
,
"hello {.Name}"
,
storeData
,
"hello jsonpath"
},
{
"variable"
,
"hello {.Name}"
,
storeData
,
"hello jsonpath"
},
{
"dict/"
,
"{$.Labels.web/html}"
,
storeData
,
"15"
},
{
"dict/"
,
"{$.Labels.web/html}"
,
storeData
,
"15"
},
{
"dict/"
,
"{$.Employees.jason}"
,
storeData
,
"manager"
},
{
"dict/"
,
"{$.Employees.dan}"
,
storeData
,
"clerk"
},
{
"dict-"
,
"{.Labels.k8s-app}"
,
storeData
,
"20"
},
{
"dict-"
,
"{.Labels.k8s-app}"
,
storeData
,
"20"
},
{
"nest"
,
"{.Bicycle.Color}"
,
storeData
,
"red"
},
{
"nest"
,
"{.Bicycle.Color}"
,
storeData
,
"red"
},
{
"allarray"
,
"{.Book[*].Author}"
,
storeData
,
"Nigel Rees Evelyn Waugh Herman Melville"
},
{
"allarray"
,
"{.Book[*].Author}"
,
storeData
,
"Nigel Rees Evelyn Waugh Herman Melville"
},
...
...
pkg/util/jsonpath/node.go
View file @
d5e135e4
...
@@ -95,7 +95,7 @@ func (t *TextNode) String() string {
...
@@ -95,7 +95,7 @@ func (t *TextNode) String() string {
return
fmt
.
Sprintf
(
"%s: %s"
,
t
.
Type
(),
t
.
Text
)
return
fmt
.
Sprintf
(
"%s: %s"
,
t
.
Type
(),
t
.
Text
)
}
}
// FieldNode holds fi
le
d of struct
// FieldNode holds fi
el
d of struct
type
FieldNode
struct
{
type
FieldNode
struct
{
NodeType
NodeType
Value
string
Value
string
...
...
Write
Preview
Markdown
is supported
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