Skip to content

Commit

Permalink
feat: grammar rename and golines on pkg/
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Jan 23, 2025
1 parent c544cb1 commit b074373
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 20 deletions.
18 changes: 15 additions & 3 deletions pkg/kube/labels/labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ var _ = ginkgo.Describe("Labels", func() {
}{
{map[string]string{}, map[string]string{}, map[string]string{}},
{map[string]string{"infra": "true"}, map[string]string{}, map[string]string{"infra": "true"}},
{map[string]string{"infra": "true"}, map[string]string{"env": "test", "color": "blue"}, map[string]string{"infra": "true", "env": "test", "color": "blue"}},
{
map[string]string{"infra": "true"},
map[string]string{"env": "test", "color": "blue"},
map[string]string{"infra": "true", "env": "test", "color": "blue"},
},
}
for _, test := range tests {
Expect(Merge(Set(test.labels1), Set(test.labels2))).To(MatchMap(test.mergedLabels))
Expand All @@ -96,8 +100,16 @@ var _ = ginkgo.Describe("Labels", func() {
{"x=a", map[string]string{"x": "a"}, true},
{"x=a,y=b,z=c", map[string]string{"x": "a", "y": "b", "z": "c"}, true},
{" x = a , y = b , z = c ", map[string]string{"x": "a", "y": "b", "z": "c"}, true},
{"color=green,env=test,service=front", map[string]string{"color": "green", "env": "test", "service": "front"}, true},
{"color=green, env=test, service=front", map[string]string{"color": "green", "env": "test", "service": "front"}, true},
{
"color=green,env=test,service=front",
map[string]string{"color": "green", "env": "test", "service": "front"},
true,
},
{
"color=green, env=test, service=front",
map[string]string{"color": "green", "env": "test", "service": "front"},
true,
},
{",", map[string]string{}, false},
{"x", map[string]string{}, false},
{"x,y", map[string]string{}, false},
Expand Down
29 changes: 22 additions & 7 deletions pkg/kube/labels/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,23 +166,35 @@ func NewRequirement(key string, op selection.Operator, vals []string, opts ...fi
switch op {
case selection.In, selection.NotIn:
if len(vals) == 0 {
allErrs = append(allErrs, field.Invalid(valuePath, vals, "for 'in', 'notin' operators, values set can't be empty"))
allErrs = append(
allErrs,
field.Invalid(valuePath, vals, "for 'in', 'notin' operators, values set can't be empty"),
)
}
case selection.Equals, selection.DoubleEquals, selection.NotEquals:
if len(vals) != 1 {
allErrs = append(allErrs, field.Invalid(valuePath, vals, "exact-match compatibility requires one single value"))
}
case selection.Exists, selection.DoesNotExist:
if len(vals) != 0 {
allErrs = append(allErrs, field.Invalid(valuePath, vals, "values set must be empty for exists and does not exist"))
allErrs = append(
allErrs,
field.Invalid(valuePath, vals, "values set must be empty for exists and does not exist"),
)
}
case selection.GreaterThan, selection.LessThan:
if len(vals) != 1 {
allErrs = append(allErrs, field.Invalid(valuePath, vals, "for 'Gt', 'Lt' operators, exactly one value is required"))
allErrs = append(
allErrs,
field.Invalid(valuePath, vals, "for 'Gt', 'Lt' operators, exactly one value is required"),
)
}
for i := range vals {
if _, err := strconv.ParseInt(vals[i], 10, 64); err != nil {
allErrs = append(allErrs, field.Invalid(valuePath.Index(i), vals[i], "for 'Gt', 'Lt' operators, the value must be an integer"))
allErrs = append(
allErrs,
field.Invalid(valuePath.Index(i), vals[i], "for 'Gt', 'Lt' operators, the value must be an integer"),
)
}
}
default:
Expand Down Expand Up @@ -240,19 +252,22 @@ func (r *Requirement) Matches(ls Labels) bool {

// There should be only one strValue in r.strValues, and can be converted to an integer.
if len(r.strValues) != 1 {
klog.V(10).Infof("Invalid values count %+v of requirement %#v, for 'Gt', 'Lt' operators, exactly one value is required", len(r.strValues), r)
klog.V(10).
Infof("Invalid values count %+v of requirement %#v, for 'Gt', 'Lt' operators, exactly one value is required", len(r.strValues), r)
return false
}

var rValue int64
for i := range r.strValues {
rValue, err = strconv.ParseInt(r.strValues[i], 10, 64)
if err != nil {
klog.V(10).Infof("ParseInt failed for value %+v in requirement %#v, for 'Gt', 'Lt' operators, the value must be an integer", r.strValues[i], r)
klog.V(10).
Infof("ParseInt failed for value %+v in requirement %#v, for 'Gt', 'Lt' operators, the value must be an integer", r.strValues[i], r)
return false
}
}
return (r.operator == selection.GreaterThan && lsValue > rValue) || (r.operator == selection.LessThan && lsValue < rValue)
return (r.operator == selection.GreaterThan && lsValue > rValue) ||
(r.operator == selection.LessThan && lsValue < rValue)
default:
return false
}
Expand Down
61 changes: 52 additions & 9 deletions pkg/kube/labels/selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,16 @@ func expectMatch(selector string, ls Set) {
}

func expectMatchDirect(selector, ls Set) {
Expect(SelectorFromSet(selector).Matches(ls)).To(BeTrue(), fmt.Sprintf("Wanted %s to match '%s', but ginkgo.It did not", selector, ls))
Expect(
SelectorFromSet(selector).Matches(ls),
).To(BeTrue(), fmt.Sprintf("Wanted %s to match '%s', but ginkgo.It did not", selector, ls))
}

//nolint:staticcheck,unused //iccheck // U1000 currently commented out in TODO of TestSetMatches
func expectNoMatchDirect(selector, ls Set) {
Expect(SelectorFromSet(selector).Matches(ls)).To(BeFalse(), fmt.Sprintf("Wanted %s to not match '%s', but ginkgo.It did ", selector, ls))
Expect(
SelectorFromSet(selector).Matches(ls),
).To(BeFalse(), fmt.Sprintf("Wanted %s to not match '%s', but ginkgo.It did ", selector, ls))
}

var _ = ginkgo.Describe("Selectors", func() {
Expand Down Expand Up @@ -216,12 +220,18 @@ var _ = ginkgo.Describe("Lexer", func() {
}{
{"key in ( value )", []Token{IdentifierToken, InToken, OpenParToken, IdentifierToken, ClosedParToken}},
{"key notin ( value )", []Token{IdentifierToken, NotInToken, OpenParToken, IdentifierToken, ClosedParToken}},
{"key in ( value1, value2 )", []Token{IdentifierToken, InToken, OpenParToken, IdentifierToken, CommaToken, IdentifierToken, ClosedParToken}},
{
"key in ( value1, value2 )",
[]Token{IdentifierToken, InToken, OpenParToken, IdentifierToken, CommaToken, IdentifierToken, ClosedParToken},
},
{"key", []Token{IdentifierToken}},
{"!key", []Token{DoesNotExistToken, IdentifierToken}},
{"()", []Token{OpenParToken, ClosedParToken}},
{"x in (),y", []Token{IdentifierToken, InToken, OpenParToken, ClosedParToken, CommaToken, IdentifierToken}},
{"== != (), = notin", []Token{DoubleEqualsToken, NotEqualsToken, OpenParToken, ClosedParToken, CommaToken, EqualsToken, NotInToken}},
{
"== != (), = notin",
[]Token{DoubleEqualsToken, NotEqualsToken, OpenParToken, ClosedParToken, CommaToken, EqualsToken, NotInToken},
},
{"key>2", []Token{IdentifierToken, GreaterThanToken, IdentifierToken}},
{"key<1", []Token{IdentifierToken, LessThanToken, IdentifierToken}},
}
Expand Down Expand Up @@ -250,15 +260,48 @@ var _ = ginkgo.Describe("Parser", func() {
s string
t []Token
}{
{"key in ( value )", []Token{IdentifierToken, InToken, OpenParToken, IdentifierToken, ClosedParToken, EndOfStringToken}},
{"key notin ( value )", []Token{IdentifierToken, NotInToken, OpenParToken, IdentifierToken, ClosedParToken, EndOfStringToken}},
{"key in ( value1, value2 )", []Token{IdentifierToken, InToken, OpenParToken, IdentifierToken, CommaToken, IdentifierToken, ClosedParToken, EndOfStringToken}},
{
"key in ( value )",
[]Token{IdentifierToken, InToken, OpenParToken, IdentifierToken, ClosedParToken, EndOfStringToken},
},
{
"key notin ( value )",
[]Token{IdentifierToken, NotInToken, OpenParToken, IdentifierToken, ClosedParToken, EndOfStringToken},
},
{
"key in ( value1, value2 )",
[]Token{
IdentifierToken,
InToken,
OpenParToken,
IdentifierToken,
CommaToken,
IdentifierToken,
ClosedParToken,
EndOfStringToken,
},
},
{"key", []Token{IdentifierToken, EndOfStringToken}},
{"!key", []Token{DoesNotExistToken, IdentifierToken, EndOfStringToken}},
{"()", []Token{OpenParToken, ClosedParToken, EndOfStringToken}},
{"", []Token{EndOfStringToken}},
{"x in (),y", []Token{IdentifierToken, InToken, OpenParToken, ClosedParToken, CommaToken, IdentifierToken, EndOfStringToken}},
{"== != (), = notin", []Token{DoubleEqualsToken, NotEqualsToken, OpenParToken, ClosedParToken, CommaToken, EqualsToken, NotInToken, EndOfStringToken}},
{
"x in (),y",
[]Token{IdentifierToken, InToken, OpenParToken, ClosedParToken, CommaToken, IdentifierToken, EndOfStringToken},
},
{
"== != (), = notin",
[]Token{
DoubleEqualsToken,
NotEqualsToken,
OpenParToken,
ClosedParToken,
CommaToken,
EqualsToken,
NotInToken,
EndOfStringToken,
},
},
{"key>2", []Token{IdentifierToken, GreaterThanToken, IdentifierToken, EndOfStringToken}},
{"key<1", []Token{IdentifierToken, LessThanToken, IdentifierToken, EndOfStringToken}},
}
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion query/grammar/grammar_parser.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:generate go run github.com/mna/[email protected] -o grammer.go grammer.peg
//go:generate go run github.com/mna/[email protected] -o grammar.go grammar.peg
package grammar

import (
Expand Down

0 comments on commit b074373

Please sign in to comment.