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
c4e575d4
Commit
c4e575d4
authored
Jun 16, 2014
by
Daniel Smith
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
switch to different types for different parts of the label query
parent
3b980bd9
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
47 additions
and
69 deletions
+47
-69
labels.go
pkg/labels/labels.go
+1
-1
query.go
pkg/labels/query.go
+46
-68
No files found.
pkg/labels/labels.go
View file @
c4e575d4
...
@@ -29,7 +29,7 @@ type Labels interface {
...
@@ -29,7 +29,7 @@ type Labels interface {
// A map of label:value. Implements Labels.
// A map of label:value. Implements Labels.
type
Set
map
[
string
]
string
type
Set
map
[
string
]
string
// All labels listed as a human readable string. Conveiently, exactly the format
// All labels listed as a human readable string. Conve
n
iently, exactly the format
// that ParseQuery takes.
// that ParseQuery takes.
func
(
ls
Set
)
String
()
string
{
func
(
ls
Set
)
String
()
string
{
query
:=
make
([]
string
,
0
,
len
(
ls
))
query
:=
make
([]
string
,
0
,
len
(
ls
))
...
...
pkg/labels/query.go
View file @
c4e575d4
...
@@ -32,53 +32,50 @@ type Query interface {
...
@@ -32,53 +32,50 @@ type Query interface {
// Everything returns a query that matches all labels.
// Everything returns a query that matches all labels.
func
Everything
()
Query
{
func
Everything
()
Query
{
return
&
query
Term
{}
return
and
Term
{}
}
}
// A single term of a label query.
type
hasTerm
struct
{
type
queryTerm
struct
{
label
,
value
string
// Not inverts the meaning of the items in this term.
}
not
bool
// Exactly one of the below three items should be used.
func
(
t
*
hasTerm
)
Matches
(
ls
Labels
)
bool
{
return
ls
.
Get
(
t
.
label
)
==
t
.
value
}
// If non-nil, we match Set l iff l[*label] == *value.
func
(
t
*
hasTerm
)
String
()
string
{
label
,
value
*
string
return
fmt
.
Sprintf
(
"%v=%v"
,
t
.
label
,
t
.
value
)
}
// A list of terms which must all match for this query term to return true.
type
notHasTerm
struct
{
and
[]
queryTerm
label
,
value
string
}
// A list of terms, any one of which will cause this query term to return true.
func
(
t
*
notHasTerm
)
Matches
(
ls
Labels
)
bool
{
// Parsing/printing not implemented.
return
ls
.
Get
(
t
.
label
)
!=
t
.
value
or
[]
queryTerm
}
}
func
(
l
*
queryTerm
)
Matches
(
ls
Labels
)
bool
{
func
(
t
*
notHasTerm
)
String
()
string
{
matches
:=
!
l
.
not
return
fmt
.
Sprintf
(
"%v!=%v"
,
t
.
label
,
t
.
value
)
switch
{
}
case
l
.
label
!=
nil
&&
l
.
value
!=
nil
:
if
ls
.
Get
(
*
l
.
label
)
==
*
l
.
value
{
type
andTerm
[]
Query
return
matches
}
func
(
t
andTerm
)
Matches
(
ls
Labels
)
bool
{
return
!
matches
for
_
,
q
:=
range
t
{
case
len
(
l
.
and
)
>
0
:
if
!
q
.
Matches
(
ls
)
{
for
i
:=
range
l
.
and
{
return
false
if
!
l
.
and
[
i
]
.
Matches
(
ls
)
{
return
!
matches
}
}
return
matches
case
len
(
l
.
or
)
>
0
:
for
i
:=
range
l
.
or
{
if
l
.
or
[
i
]
.
Matches
(
ls
)
{
return
matches
}
}
}
return
!
matches
}
}
return
true
}
// Empty queries match everything
func
(
t
andTerm
)
String
()
string
{
return
matches
var
terms
[]
string
for
_
,
q
:=
range
t
{
terms
=
append
(
terms
,
q
.
String
())
}
return
strings
.
Join
(
terms
,
","
)
}
}
func
try
(
queryPiece
,
op
string
)
(
lhs
,
rhs
string
,
ok
bool
)
{
func
try
(
queryPiece
,
op
string
)
(
lhs
,
rhs
string
,
ok
bool
)
{
...
@@ -91,55 +88,36 @@ func try(queryPiece, op string) (lhs, rhs string, ok bool) {
...
@@ -91,55 +88,36 @@ func try(queryPiece, op string) (lhs, rhs string, ok bool) {
// Given a Set, return a Query which will match exactly that Set.
// Given a Set, return a Query which will match exactly that Set.
func
QueryFromSet
(
ls
Set
)
Query
{
func
QueryFromSet
(
ls
Set
)
Query
{
var
query
queryTerm
var
items
[]
Query
for
l
,
v
:=
range
ls
{
for
label
,
value
:=
range
ls
{
// Make a copy, because we're taking the address below
items
=
append
(
items
,
&
hasTerm
{
label
:
label
,
value
:
value
})
label
,
value
:=
l
,
v
query
.
and
=
append
(
query
.
and
,
queryTerm
{
label
:
&
label
,
value
:
&
value
})
}
}
return
&
query
if
len
(
items
)
==
1
{
return
items
[
0
]
}
return
andTerm
(
items
)
}
}
// Takes a string repsenting a label query and returns an object suitable for matching, or an error.
// Takes a string repsenting a label query and returns an object suitable for matching, or an error.
func
ParseQuery
(
query
string
)
(
Query
,
error
)
{
func
ParseQuery
(
query
string
)
(
Query
,
error
)
{
parts
:=
strings
.
Split
(
query
,
","
)
parts
:=
strings
.
Split
(
query
,
","
)
var
items
[]
queryTerm
var
items
[]
Query
for
_
,
part
:=
range
parts
{
for
_
,
part
:=
range
parts
{
if
part
==
""
{
if
part
==
""
{
continue
continue
}
}
if
lhs
,
rhs
,
ok
:=
try
(
part
,
"!="
);
ok
{
if
lhs
,
rhs
,
ok
:=
try
(
part
,
"!="
);
ok
{
items
=
append
(
items
,
queryTerm
{
not
:
true
,
label
:
&
lhs
,
value
:
&
rhs
})
items
=
append
(
items
,
&
notHasTerm
{
label
:
lhs
,
value
:
rhs
})
}
else
if
lhs
,
rhs
,
ok
:=
try
(
part
,
"=="
);
ok
{
}
else
if
lhs
,
rhs
,
ok
:=
try
(
part
,
"=="
);
ok
{
items
=
append
(
items
,
queryTerm
{
label
:
&
lhs
,
value
:
&
rhs
})
items
=
append
(
items
,
&
hasTerm
{
label
:
lhs
,
value
:
rhs
})
}
else
if
lhs
,
rhs
,
ok
:=
try
(
part
,
"="
);
ok
{
}
else
if
lhs
,
rhs
,
ok
:=
try
(
part
,
"="
);
ok
{
items
=
append
(
items
,
queryTerm
{
label
:
&
lhs
,
value
:
&
rhs
})
items
=
append
(
items
,
&
hasTerm
{
label
:
lhs
,
value
:
rhs
})
}
else
{
}
else
{
return
nil
,
fmt
.
Errorf
(
"invalid label query: '%s'; can't understand '%s'"
,
query
,
part
)
return
nil
,
fmt
.
Errorf
(
"invalid label query: '%s'; can't understand '%s'"
,
query
,
part
)
}
}
}
}
if
len
(
items
)
==
1
{
if
len
(
items
)
==
1
{
return
&
items
[
0
],
nil
return
items
[
0
],
nil
}
return
&
queryTerm
{
and
:
items
},
nil
}
// Returns this query as a string in a form that ParseQuery can parse.
func
(
l
*
queryTerm
)
String
()
(
out
string
)
{
if
len
(
l
.
and
)
>
0
{
for
_
,
part
:=
range
l
.
and
{
if
out
!=
""
{
out
+=
","
}
out
+=
part
.
String
()
}
return
}
else
if
l
.
label
!=
nil
&&
l
.
value
!=
nil
{
op
:=
"="
if
l
.
not
{
op
=
"!="
}
return
fmt
.
Sprintf
(
"%v%v%v"
,
*
l
.
label
,
op
,
*
l
.
value
)
}
}
return
""
return
andTerm
(
items
),
nil
}
}
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