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
a7fa2aef
Commit
a7fa2aef
authored
Dec 20, 2016
by
Xing Zhou
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added bool type support for jsonpath.
Added keywords "true" and "false" for supporting bool type in jsonpath.
parent
16164c6a
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
57 additions
and
6 deletions
+57
-6
jsonpath.go
staging/src/k8s.io/client-go/util/jsonpath/jsonpath.go
+11
-0
jsonpath_test.go
staging/src/k8s.io/client-go/util/jsonpath/jsonpath_test.go
+13
-5
node.go
staging/src/k8s.io/client-go/util/jsonpath/node.go
+16
-0
parser.go
staging/src/k8s.io/client-go/util/jsonpath/parser.go
+17
-1
No files found.
staging/src/k8s.io/client-go/util/jsonpath/jsonpath.go
View file @
a7fa2aef
...
@@ -148,6 +148,8 @@ func (j *JSONPath) walk(value []reflect.Value, node Node) ([]reflect.Value, erro
...
@@ -148,6 +148,8 @@ func (j *JSONPath) walk(value []reflect.Value, node Node) ([]reflect.Value, erro
return
j
.
evalFilter
(
value
,
node
)
return
j
.
evalFilter
(
value
,
node
)
case
*
IntNode
:
case
*
IntNode
:
return
j
.
evalInt
(
value
,
node
)
return
j
.
evalInt
(
value
,
node
)
case
*
BoolNode
:
return
j
.
evalBool
(
value
,
node
)
case
*
FloatNode
:
case
*
FloatNode
:
return
j
.
evalFloat
(
value
,
node
)
return
j
.
evalFloat
(
value
,
node
)
case
*
WildcardNode
:
case
*
WildcardNode
:
...
@@ -181,6 +183,15 @@ func (j *JSONPath) evalFloat(input []reflect.Value, node *FloatNode) ([]reflect.
...
@@ -181,6 +183,15 @@ func (j *JSONPath) evalFloat(input []reflect.Value, node *FloatNode) ([]reflect.
return
result
,
nil
return
result
,
nil
}
}
// evalBool evaluates BoolNode
func
(
j
*
JSONPath
)
evalBool
(
input
[]
reflect
.
Value
,
node
*
BoolNode
)
([]
reflect
.
Value
,
error
)
{
result
:=
make
([]
reflect
.
Value
,
len
(
input
))
for
i
:=
range
input
{
result
[
i
]
=
reflect
.
ValueOf
(
node
.
Value
)
}
return
result
,
nil
}
// evalList evaluates ListNode
// evalList evaluates ListNode
func
(
j
*
JSONPath
)
evalList
(
value
[]
reflect
.
Value
,
node
*
ListNode
)
([]
reflect
.
Value
,
error
)
{
func
(
j
*
JSONPath
)
evalList
(
value
[]
reflect
.
Value
,
node
*
ListNode
)
([]
reflect
.
Value
,
error
)
{
var
err
error
var
err
error
...
...
staging/src/k8s.io/client-go/util/jsonpath/jsonpath_test.go
View file @
a7fa2aef
...
@@ -113,13 +113,14 @@ func (b book) String() string {
...
@@ -113,13 +113,14 @@ func (b book) String() string {
type
bicycle
struct
{
type
bicycle
struct
{
Color
string
Color
string
Price
float32
Price
float32
IsNew
bool
}
}
type
empName
string
type
empName
string
type
job
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
Employees
map
[
empName
]
job
...
@@ -134,7 +135,10 @@ func TestStructInput(t *testing.T) {
...
@@ -134,7 +135,10 @@ func TestStructInput(t *testing.T) {
{
"fiction"
,
"Evelyn Waugh"
,
"Sword of Honour"
,
12.99
},
{
"fiction"
,
"Evelyn Waugh"
,
"Sword of Honour"
,
12.99
},
{
"fiction"
,
"Herman Melville"
,
"Moby Dick"
,
8.99
},
{
"fiction"
,
"Herman Melville"
,
"Moby Dick"
,
8.99
},
},
},
Bicycle
:
bicycle
{
"red"
,
19.95
},
Bicycle
:
[]
bicycle
{
{
"red"
,
19.95
,
true
},
{
"green"
,
20.01
,
false
},
},
Labels
:
map
[
string
]
int
{
Labels
:
map
[
string
]
int
{
"engieer"
:
10
,
"engieer"
:
10
,
"web/html"
:
15
,
"web/html"
:
15
,
...
@@ -158,14 +162,15 @@ func TestStructInput(t *testing.T) {
...
@@ -158,14 +162,15 @@ func TestStructInput(t *testing.T) {
{
"dict/"
,
"{$.Employees.jason}"
,
storeData
,
"manager"
},
{
"dict/"
,
"{$.Employees.jason}"
,
storeData
,
"manager"
},
{
"dict/"
,
"{$.Employees.dan}"
,
storeData
,
"clerk"
},
{
"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 green
"
},
{
"allarray"
,
"{.Book[*].Author}"
,
storeData
,
"Nigel Rees Evelyn Waugh Herman Melville"
},
{
"allarray"
,
"{.Book[*].Author}"
,
storeData
,
"Nigel Rees Evelyn Waugh Herman Melville"
},
{
"allfileds"
,
"{.Bicycle.*}"
,
storeData
,
"
red 19.95
"
},
{
"allfileds"
,
"{.Bicycle.*}"
,
storeData
,
"
{red 19.95 true} {green 20.01 false}
"
},
{
"recurfileds"
,
"{..Price}"
,
storeData
,
"8.95 12.99 8.99 19.95"
},
{
"recurfileds"
,
"{..Price}"
,
storeData
,
"8.95 12.99 8.99 19.95
20.01
"
},
{
"lastarray"
,
"{.Book[-1:]}"
,
storeData
,
{
"lastarray"
,
"{.Book[-1:]}"
,
storeData
,
"{Category: fiction, Author: Herman Melville, Title: Moby Dick, Price: 8.99}"
},
"{Category: fiction, Author: Herman Melville, Title: Moby Dick, Price: 8.99}"
},
{
"recurarray"
,
"{..Book[2]}"
,
storeData
,
{
"recurarray"
,
"{..Book[2]}"
,
storeData
,
"{Category: fiction, Author: Herman Melville, Title: Moby Dick, Price: 8.99}"
},
"{Category: fiction, Author: Herman Melville, Title: Moby Dick, Price: 8.99}"
},
{
"bool"
,
"{.Bicycle[?(@.IsNew==true)]}"
,
storeData
,
"{red 19.95 true}"
},
}
}
testJSONPath
(
storeTests
,
false
,
t
)
testJSONPath
(
storeTests
,
false
,
t
)
...
@@ -220,6 +225,7 @@ func TestKubernetes(t *testing.T) {
...
@@ -220,6 +225,7 @@ func TestKubernetes(t *testing.T) {
},
},
"status":{
"status":{
"capacity":{"cpu":"4"},
"capacity":{"cpu":"4"},
"ready": true,
"addresses":[{"type": "LegacyHostIP", "address":"127.0.0.1"}]
"addresses":[{"type": "LegacyHostIP", "address":"127.0.0.1"}]
}
}
},
},
...
@@ -233,6 +239,7 @@ func TestKubernetes(t *testing.T) {
...
@@ -233,6 +239,7 @@ func TestKubernetes(t *testing.T) {
},
},
"status":{
"status":{
"capacity":{"cpu":"8"},
"capacity":{"cpu":"8"},
"ready": false,
"addresses":[
"addresses":[
{"type": "LegacyHostIP", "address":"127.0.0.2"},
{"type": "LegacyHostIP", "address":"127.0.0.2"},
{"type": "another", "address":"127.0.0.3"}
{"type": "another", "address":"127.0.0.3"}
...
@@ -272,6 +279,7 @@ func TestKubernetes(t *testing.T) {
...
@@ -272,6 +279,7 @@ func TestKubernetes(t *testing.T) {
{
"user password"
,
`{.users[?(@.name=="e2e")].user.password}`
,
&
nodesData
,
"secret"
},
{
"user password"
,
`{.users[?(@.name=="e2e")].user.password}`
,
&
nodesData
,
"secret"
},
{
"hostname"
,
`{.items[0].metadata.labels.kubernetes\.io/hostname}`
,
&
nodesData
,
"127.0.0.1"
},
{
"hostname"
,
`{.items[0].metadata.labels.kubernetes\.io/hostname}`
,
&
nodesData
,
"127.0.0.1"
},
{
"hostname filter"
,
`{.items[?(@.metadata.labels.kubernetes\.io/hostname=="127.0.0.1")].kind}`
,
&
nodesData
,
"None"
},
{
"hostname filter"
,
`{.items[?(@.metadata.labels.kubernetes\.io/hostname=="127.0.0.1")].kind}`
,
&
nodesData
,
"None"
},
{
"bool item"
,
`{.items[?(@..ready==true)].metadata.name}`
,
&
nodesData
,
"127.0.0.1"
},
}
}
testJSONPath
(
nodesTests
,
false
,
t
)
testJSONPath
(
nodesTests
,
false
,
t
)
...
...
staging/src/k8s.io/client-go/util/jsonpath/node.go
View file @
a7fa2aef
...
@@ -42,6 +42,7 @@ const (
...
@@ -42,6 +42,7 @@ const (
NodeWildcard
NodeWildcard
NodeRecursive
NodeRecursive
NodeUnion
NodeUnion
NodeBool
)
)
var
NodeTypeName
=
map
[
NodeType
]
string
{
var
NodeTypeName
=
map
[
NodeType
]
string
{
...
@@ -56,6 +57,7 @@ var NodeTypeName = map[NodeType]string{
...
@@ -56,6 +57,7 @@ var NodeTypeName = map[NodeType]string{
NodeWildcard
:
"NodeWildcard"
,
NodeWildcard
:
"NodeWildcard"
,
NodeRecursive
:
"NodeRecursive"
,
NodeRecursive
:
"NodeRecursive"
,
NodeUnion
:
"NodeUnion"
,
NodeUnion
:
"NodeUnion"
,
NodeBool
:
"NodeBool"
,
}
}
type
Node
interface
{
type
Node
interface
{
...
@@ -237,3 +239,17 @@ func newUnion(nodes []*ListNode) *UnionNode {
...
@@ -237,3 +239,17 @@ func newUnion(nodes []*ListNode) *UnionNode {
func
(
u
*
UnionNode
)
String
()
string
{
func
(
u
*
UnionNode
)
String
()
string
{
return
fmt
.
Sprintf
(
"%s"
,
u
.
Type
())
return
fmt
.
Sprintf
(
"%s"
,
u
.
Type
())
}
}
// BoolNode holds bool value
type
BoolNode
struct
{
NodeType
Value
bool
}
func
newBool
(
value
bool
)
*
BoolNode
{
return
&
BoolNode
{
NodeType
:
NodeBool
,
Value
:
value
}
}
func
(
b
*
BoolNode
)
String
()
string
{
return
fmt
.
Sprintf
(
"%s: %t"
,
b
.
Type
(),
b
.
Value
)
}
staging/src/k8s.io/client-go/util/jsonpath/parser.go
View file @
a7fa2aef
...
@@ -194,7 +194,18 @@ func (p *Parser) parseIdentifier(cur *ListNode) error {
...
@@ -194,7 +194,18 @@ func (p *Parser) parseIdentifier(cur *ListNode) error {
}
}
}
}
value
:=
p
.
consumeText
()
value
:=
p
.
consumeText
()
cur
.
append
(
newIdentifier
(
value
))
if
isBool
(
value
)
{
v
,
err
:=
strconv
.
ParseBool
(
value
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"can not parse bool '%s': %s"
,
value
,
err
.
Error
())
}
cur
.
append
(
newBool
(
v
))
}
else
{
cur
.
append
(
newIdentifier
(
value
))
}
return
p
.
parseInsideAction
(
cur
)
return
p
.
parseInsideAction
(
cur
)
}
}
...
@@ -431,3 +442,8 @@ func isEndOfLine(r rune) bool {
...
@@ -431,3 +442,8 @@ func isEndOfLine(r rune) bool {
func
isAlphaNumeric
(
r
rune
)
bool
{
func
isAlphaNumeric
(
r
rune
)
bool
{
return
r
==
'_'
||
unicode
.
IsLetter
(
r
)
||
unicode
.
IsDigit
(
r
)
return
r
==
'_'
||
unicode
.
IsLetter
(
r
)
||
unicode
.
IsDigit
(
r
)
}
}
// isBool reports whether s is a boolean value.
func
isBool
(
s
string
)
bool
{
return
s
==
"true"
||
s
==
"false"
}
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