Skip to content

Commit

Permalink
Merge pull request #421 from kubeshop/feat/first-last-selectors
Browse files Browse the repository at this point in the history
feat: support :first and :last pseudo classes
  • Loading branch information
mathnogueira authored May 4, 2022
2 parents afa9a8a + a35231e commit 9ab8e38
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
4 changes: 4 additions & 0 deletions server/assertions/selectors/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ func createPseudoClass(parserPseudoClass parserPseudoClass) (PseudoClass, error)
return &NthChildPseudoClass{
N: *parserPseudoClass.Value.Int,
}, nil
case "first":
return &FirstPseudoClass{}, nil
case "last":
return &LastPseudoClass{}, nil
case "":
// No pseudoClass
return nil, nil
Expand Down
4 changes: 2 additions & 2 deletions server/assertions/selectors/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ type parserValue struct {
}

type parserPseudoClass struct {
Type string `":" @("nth_child")`
Value *parserValue `"(" @@* ")"`
Type string `":" @("nth_child" | "first" | "last")`
Value *parserValue `("(" @@* ")")*`
}

func CreateParser() (*participle.Parser, error) {
Expand Down
23 changes: 22 additions & 1 deletion server/assertions/selectors/pseudo_classes.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,30 @@ type NthChildPseudoClass struct {
}

func (nc NthChildPseudoClass) Filter(spans []traces.Span) []traces.Span {
if len(spans) < int(nc.N) {
if int(nc.N) < 1 || int(nc.N) > len(spans) {
return []traces.Span{}
}

return []traces.Span{spans[int(nc.N-1)]}
}

type FirstPseudoClass struct{}

func (fpc FirstPseudoClass) Filter(spans []traces.Span) []traces.Span {
if len(spans) == 0 {
return []traces.Span{}
}

return []traces.Span{spans[0]}
}

type LastPseudoClass struct{}

func (lpc LastPseudoClass) Filter(spans []traces.Span) []traces.Span {
length := len(spans)
if length == 0 {
return []traces.Span{}
}

return []traces.Span{spans[length-1]}
}
12 changes: 11 additions & 1 deletion server/assertions/selectors/selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,17 @@ func TestSelector(t *testing.T) {
ExpectedSpanIds: []trace.SpanID{updatePokemonDatabaseSpanID},
},
{
Name: "Selector with pseudo class",
Name: "Selector with first pseudo class",
Expression: "span[tracetest.span.type=\"db\"]:first",
ExpectedSpanIds: []trace.SpanID{insertPokemonDatabaseSpanID},
},
{
Name: "Selector with first pseudo class",
Expression: "span[tracetest.span.type=\"db\"]:last",
ExpectedSpanIds: []trace.SpanID{updatePokemonDatabaseSpanID},
},
{
Name: "Selector with nth_child pseudo class",
Expression: "span[tracetest.span.type=\"db\"]:nth_child(2)",
ExpectedSpanIds: []trace.SpanID{updatePokemonDatabaseSpanID},
},
Expand Down

0 comments on commit 9ab8e38

Please sign in to comment.