Skip to content

Commit

Permalink
Merge pull request #248 from TomWright/twright/selector-parsing
Browse files Browse the repository at this point in the history
Twright/selector parsing
  • Loading branch information
TomWright authored Sep 28, 2022
2 parents 071fc3d + 6285df7 commit 1baa94c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 8 deletions.
15 changes: 15 additions & 0 deletions internal/command/root_select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,21 @@ spec:
t.Run("NullInput", selectTest(`null`, "yaml", `.`, newline("{}"), nil))
t.Run("EmptyDocument", selectTest(`---`, "yaml", `.`, newline("{}"), nil))
t.Run("BlankInput", selectTest(``, "yaml", `.`, newline("{}"), nil))

// https://github.com/TomWright/dasel/discussions/244
t.Run("DynamicSelectorContainingEqualsInValue", selectTest(`
options:
k3s:
extraArgs:
- arg: --no-deploy=traefik
nodeFilters:
- server:*
- arg: --kube-apiserver-arg=feature-gates=HPAContainerMetrics=true
nodeFilters:
- server:*
`, "yaml", `.options.k3s.extraArgs.(arg=--no-deploy=traefik)`, newline(`arg: --no-deploy=traefik
nodeFilters:
- server:*`), nil))
}

func TestRootCmd_Select_TOML(t *testing.T) {
Expand Down
12 changes: 6 additions & 6 deletions node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ func TestParseSelector(t *testing.T) {
}
})
t.Run("InvalidDynamicComparison", func(t *testing.T) {
_, err := dasel.ParseSelector(".(x<=>2)")
exp := &dasel.UnknownComparisonOperatorErr{Operator: "<=>"}
_, err := dasel.ParseSelector(".(x@2)")
exp := &dasel.UnknownComparisonOperatorErr{Operator: ""}
if err == nil || err.Error() != exp.Error() {
t.Errorf("expected error %v, got %v", exp, err)
}
Expand All @@ -152,15 +152,15 @@ func TestParseSelector(t *testing.T) {
}
})
t.Run("UnknownComparisonOperator", func(t *testing.T) {
_, err := dasel.ParseSelector(".(a<=>b)")
exp := "unknown comparison operator: <=>"
_, err := dasel.ParseSelector(".(a@b)")
exp := "unknown comparison operator: "
if err == nil || err.Error() != exp {
t.Errorf("expected error %v, got %v", exp, err)
}
})
t.Run("UnknownSearchComparisonOperator", func(t *testing.T) {
_, err := dasel.ParseSelector(".(?:a<=>b)")
exp := "unknown comparison operator: <=>"
_, err := dasel.ParseSelector(".(?:a@b)")
exp := "unknown comparison operator: "
if err == nil || err.Error() != exp {
t.Errorf("expected error %v, got %v", exp, err)
}
Expand Down
36 changes: 34 additions & 2 deletions selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dasel

import (
"errors"
"strings"
)

// ErrDynamicSelectorBracketMismatch is returned when the number of opening brackets doesn't equal that
Expand Down Expand Up @@ -85,6 +86,33 @@ type DynamicSelectorParts struct {
Value string
}

var comparisons = []string{
"=",
"!=",
"<",
"<=",
">",
">=",
}

func isBuildingComparison(comparison string) bool {
for _, c := range comparisons {
if strings.HasPrefix(c, comparison) {
return true
}
}
return false
}

func isValidComparison(comparison string) bool {
for _, c := range comparisons {
if comparison == c {
return true
}
}
return false
}

// FindDynamicSelectorParts extracts the parts from the dynamic selector given.
func FindDynamicSelectorParts(selector string) DynamicSelectorParts {
i := 0
Expand All @@ -110,8 +138,12 @@ func FindDynamicSelectorParts(selector string) DynamicSelectorParts {
parts.Value += string(v)
}

// Matches a comparison character
case i == 0 && (v == '<' || v == '>' || v == '=' || v == '!'):
// Matches a comparison operator
case i == 0 && isValidComparison(parts.Comparison+string(v)):
parts.Comparison += string(v)

// Is building a comparison character
case i == 0 && isBuildingComparison(parts.Comparison+string(v)):
parts.Comparison += string(v)

// Add to key or value based on comparison existence
Expand Down
5 changes: 5 additions & 0 deletions selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ func TestFindDynamicSelectorParts(t *testing.T) {
Comparison: "=",
Value: "b",
}))
t.Run("EqualWithEqualsInValue", testFindDynamicSelectorParts("a=b=c", dasel.DynamicSelectorParts{
Key: "a",
Comparison: "=",
Value: "b=c",
}))
t.Run("NotEqual", testFindDynamicSelectorParts("a!=b", dasel.DynamicSelectorParts{
Key: "a",
Comparison: "!=",
Expand Down

0 comments on commit 1baa94c

Please sign in to comment.