Skip to content
This repository was archived by the owner on Jun 2, 2024. It is now read-only.

Commit

Permalink
#1 use same operator names in filter expression as in instana api
Browse files Browse the repository at this point in the history
  • Loading branch information
gessnerfl committed Apr 18, 2019
1 parent 1fde923 commit dccdc5e
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 132 deletions.
7 changes: 1 addition & 6 deletions instana/filterexpression/filter-expression-mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,8 @@ func NewMapper() Mapper {
//Mapper interface of the filter expression mapper
type Mapper interface {
FromAPIModel(input restapi.MatchExpression) (*FilterExpression, error)
ToAPIModel(input *FilterExpression) (*restapi.MatchExpression, error)
ToAPIModel(input *FilterExpression) restapi.MatchExpression
}

//struct for the filter expression mapper implementation
type mapperImpl struct{}

//ToAPIModel Implementation of the mapping form filter expression model to the Instana API model
func (m *mapperImpl) ToAPIModel(input *FilterExpression) (*restapi.MatchExpression, error) {
return nil, nil
}
21 changes: 6 additions & 15 deletions instana/filterexpression/filter-expression-parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,6 @@ func (c *Operator) Capture(values []string) error {
return nil
}

//UnaryOperator Custom type for a unary operations
type UnaryOperator string

//Capture captures the string representation of a unary operation from the given slice of strings. Interface of participle
func (c *UnaryOperator) Capture(values []string) error {
*c = UnaryOperator(strings.ToUpper(strings.Join(values, " ")))
return nil
}

//ExpressionRenderer interface definition for all types of the Filter expression to render the corresponding value
type ExpressionRenderer interface {
Render() string
Expand Down Expand Up @@ -96,10 +87,10 @@ func (e *PrimaryExpression) Render() string {
return e.UnaryOperation.Render()
}

//ComparisionExpression representation of a comparision expression. Supported types: EQ (Equals), NE (Not Equal), CO (Contains), NC (Not Contain)
//ComparisionExpression representation of a comparision expression. Supported types: EQUALS (Equals), NOT_EQUAL (Not Equal), CONTAINS (Contains), NOT_CONTAIN (Not Contain)
type ComparisionExpression struct {
Key string `parser:"@Ident"`
Operator Operator `parser:"@( \"EQ\" | \"NE\" | \"CO\" | \"NC\" )"`
Operator Operator `parser:"@( \"EQUALS\" | \"NOT_EQUAL\" | \"CONTAINS\" | \"NOT_CONTAIN\" )"`
Value string `parser:"@String"`
}

Expand All @@ -110,8 +101,8 @@ func (e *ComparisionExpression) Render() string {

//UnaryOperationExpression representation of a unary expression representing a unary operator
type UnaryOperationExpression struct {
Key string `parser:"@Ident"`
Operator UnaryOperator `parser:"@( \"IS\" (\"EMPTY\" | \"BLANK\") | \"NOT\" (\"EMPTY\" | \"BLANK\") )"`
Key string `parser:"@Ident"`
Operator Operator `parser:"@( \"IS_EMPTY\" | \"IS_BLANK\" | \"NOT_EMPTY\" | \"NOT_BLANK\" )"`
}

//Render implementation of ExpressionRenderer.Render
Expand All @@ -121,11 +112,11 @@ func (e *UnaryOperationExpression) Render() string {

var (
filterLexer = lexer.Must(lexer.Regexp(`(\s+)` +
`|(?P<Keyword>(?i)OR|AND|TRUE|FALSE|IS|NOT|EMPTY|BLANK|EQ|NE|CO|NC)` +
`|(?P<Keyword>(?i)OR|AND|TRUE|FALSE|IS_EMPTY|NOT_EMPTY|IS_BLANK|NOT_BLANK|EQUALS|NOT_EQUAL|CONTAINS|NOT_CONTAIN)` +
`|(?P<Ident>[a-zA-Z_][\.a-zA-Z0-9_]*)` +
`|(?P<Number>[-+]?\d+(\.\d+)?)` +
`|(?P<String>'[^']*'|"[^"]*")` +
`|(?P<Operators>EQ|NE|CO|NC|[()])`,
`|(?P<Operators>EQUALS|NOT_EQUAL|CONTAINS|NOT_CONTAIN|IS_EMPTY|NOT_EMPTY|IS_BLANK|NOT_BLANK|[()])`,
))
filterParser = participle.MustBuild(
&FilterExpression{},
Expand Down
68 changes: 34 additions & 34 deletions instana/filterexpression/filter-expression-parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestShouldSuccessfullyParseComplexExpression(t *testing.T) {
expression := "entity.name CO 'foo bar' OR entity.kind EQ '2.34' AND entity.type EQ 'true' AND span.name NOT EMPTY OR ( span.id NE '1234' OR span.id NE '6789' )"
expression := "entity.name CONTAINS 'foo bar' OR entity.kind EQUALS '2.34' AND entity.type EQUALS 'true' AND span.name NOT_EMPTY OR ( span.id NOT_EQUAL '1234' OR span.id NOT_EQUAL '6789' )"

logicalAnd := Operator("AND")
logicalOr := Operator("OR")
Expand All @@ -18,7 +18,7 @@ func TestShouldSuccessfullyParseComplexExpression(t *testing.T) {
Left: &PrimaryExpression{
Comparision: &ComparisionExpression{
Key: "entity.name",
Operator: "CO",
Operator: "CONTAINS",
Value: "foo bar",
},
},
Expand All @@ -29,7 +29,7 @@ func TestShouldSuccessfullyParseComplexExpression(t *testing.T) {
Left: &PrimaryExpression{
Comparision: &ComparisionExpression{
Key: "entity.kind",
Operator: "EQ",
Operator: "EQUALS",
Value: "2.34",
},
},
Expand All @@ -38,7 +38,7 @@ func TestShouldSuccessfullyParseComplexExpression(t *testing.T) {
Left: &PrimaryExpression{
Comparision: &ComparisionExpression{
Key: "entity.type",
Operator: "EQ",
Operator: "EQUALS",
Value: "true",
},
},
Expand All @@ -47,7 +47,7 @@ func TestShouldSuccessfullyParseComplexExpression(t *testing.T) {
Left: &PrimaryExpression{
UnaryOperation: &UnaryOperationExpression{
Key: "span.name",
Operator: "NOT EMPTY",
Operator: "NOT_EMPTY",
},
},
},
Expand All @@ -62,7 +62,7 @@ func TestShouldSuccessfullyParseComplexExpression(t *testing.T) {
Left: &PrimaryExpression{
Comparision: &ComparisionExpression{
Key: "span.id",
Operator: "NE",
Operator: "NOT_EQUAL",
Value: "1234",
},
},
Expand All @@ -73,7 +73,7 @@ func TestShouldSuccessfullyParseComplexExpression(t *testing.T) {
Left: &PrimaryExpression{
Comparision: &ComparisionExpression{
Key: "span.id",
Operator: "NE",
Operator: "NOT_EQUAL",
Value: "6789",
},
},
Expand All @@ -91,7 +91,7 @@ func TestShouldSuccessfullyParseComplexExpression(t *testing.T) {
}

func TestShouldParseKeywordsCaseInsensitive(t *testing.T) {
expression := "entity.name co 'foo' and entity.type EQ 'bar'"
expression := "entity.name CONTAINS 'foo' and entity.type EQUALS 'bar'"

logicalAnd := Operator("AND")
expectedResult := &FilterExpression{
Expand All @@ -100,7 +100,7 @@ func TestShouldParseKeywordsCaseInsensitive(t *testing.T) {
Left: &PrimaryExpression{
Comparision: &ComparisionExpression{
Key: "entity.name",
Operator: "CO",
Operator: "CONTAINS",
Value: "foo",
},
},
Expand All @@ -109,7 +109,7 @@ func TestShouldParseKeywordsCaseInsensitive(t *testing.T) {
Left: &PrimaryExpression{
Comparision: &ComparisionExpression{
Key: "entity.type",
Operator: "EQ",
Operator: "EQUALS",
Value: "bar",
},
},
Expand All @@ -122,15 +122,15 @@ func TestShouldParseKeywordsCaseInsensitive(t *testing.T) {
}

func TestShouldParseComparisionOperationsCaseInsensitive(t *testing.T) {
expression := "entity.name eQ 'foo'"
expression := "entity.name EQUALS 'foo'"

expectedResult := &FilterExpression{
Expression: &LogicalOrExpression{
Left: &LogicalAndExpression{
Left: &PrimaryExpression{
Comparision: &ComparisionExpression{
Key: "entity.name",
Operator: "EQ",
Operator: "EQUALS",
Value: "foo",
},
},
Expand All @@ -142,15 +142,15 @@ func TestShouldParseComparisionOperationsCaseInsensitive(t *testing.T) {
}

func TestShouldParseUnaryOperationsCaseInsensitive(t *testing.T) {
expression := "entity.name Not EmptY"
expression := "entity.name NOT_EMPTY"

expectedResult := &FilterExpression{
Expression: &LogicalOrExpression{
Left: &LogicalAndExpression{
Left: &PrimaryExpression{
UnaryOperation: &UnaryOperationExpression{
Key: "entity.name",
Operator: "NOT EMPTY",
Operator: "NOT_EMPTY",
},
},
},
Expand Down Expand Up @@ -185,8 +185,8 @@ func TestShouldFailToParseInvalidExpression(t *testing.T) {
}

func TestShouldRenderComplexExpressionNormalizedForm(t *testing.T) {
expression := "entity.name co 'foo' OR entity.kind EQ '2.34' and entity.type EQ 'true' AND span.name NOT empTy OR span.id NE '1234'"
normalizedExpression := "entity.name CO 'foo' OR entity.kind EQ '2.34' AND entity.type EQ 'true' AND span.name NOT EMPTY OR span.id NE '1234'"
expression := "entity.name CONTAINS 'foo' OR entity.kind EQUALS '2.34' and entity.type EQUALS 'true' AND span.name NOT_EMPTY OR span.id NOT_EQUAL '1234'"
normalizedExpression := "entity.name CONTAINS 'foo' OR entity.kind EQUALS '2.34' AND entity.type EQUALS 'true' AND span.name NOT_EMPTY OR span.id NOT_EQUAL '1234'"

sut := NewParser()
result, err := sut.Parse(expression)
Expand All @@ -202,15 +202,15 @@ func TestShouldRenderComplexExpressionNormalizedForm(t *testing.T) {
}

func TestShouldRenderLogicalOrExpressionWhenOrIsSet(t *testing.T) {
expectedResult := "foo EQ 'bar' OR foo CO 'bar'"
expectedResult := "foo EQUALS 'bar' OR foo CONTAINS 'bar'"

logicalOr := Operator("OR")
sut := LogicalOrExpression{
Left: &LogicalAndExpression{
Left: &PrimaryExpression{
Comparision: &ComparisionExpression{
Key: "foo",
Operator: "EQ",
Operator: "EQUALS",
Value: "bar",
},
},
Expand All @@ -221,7 +221,7 @@ func TestShouldRenderLogicalOrExpressionWhenOrIsSet(t *testing.T) {
Left: &PrimaryExpression{
Comparision: &ComparisionExpression{
Key: "foo",
Operator: "CO",
Operator: "CONTAINS",
Value: "bar",
},
},
Expand All @@ -237,14 +237,14 @@ func TestShouldRenderLogicalOrExpressionWhenOrIsSet(t *testing.T) {
}

func TestShouldRenderPrimaryExpressionOnLogicalOrExpressionWhenNeitherOrNorAndIsSet(t *testing.T) {
expectedResult := "foo EQ 'bar'"
expectedResult := "foo EQUALS 'bar'"

sut := LogicalOrExpression{
Left: &LogicalAndExpression{
Left: &PrimaryExpression{
Comparision: &ComparisionExpression{
Key: "foo",
Operator: "EQ",
Operator: "EQUALS",
Value: "bar",
},
},
Expand All @@ -259,14 +259,14 @@ func TestShouldRenderPrimaryExpressionOnLogicalOrExpressionWhenNeitherOrNorAndIs
}

func TestShouldRenderLogicalAndExpressionWhenAndIsSet(t *testing.T) {
expectedResult := "foo EQ 'bar' AND foo CO 'bar'"
expectedResult := "foo EQUALS 'bar' AND foo CONTAINS 'bar'"

logicalAnd := Operator("AND")
sut := LogicalAndExpression{
Left: &PrimaryExpression{
Comparision: &ComparisionExpression{
Key: "foo",
Operator: "EQ",
Operator: "EQUALS",
Value: "bar",
},
},
Expand All @@ -275,7 +275,7 @@ func TestShouldRenderLogicalAndExpressionWhenAndIsSet(t *testing.T) {
Left: &PrimaryExpression{
Comparision: &ComparisionExpression{
Key: "foo",
Operator: "CO",
Operator: "CONTAINS",
Value: "bar",
},
},
Expand All @@ -290,13 +290,13 @@ func TestShouldRenderLogicalAndExpressionWhenAndIsSet(t *testing.T) {
}

func TestShouldRenderPrimaryExpressionOnLogicalAndExpressionWhenAndIsNotSet(t *testing.T) {
expectedResult := "foo EQ 'bar'"
expectedResult := "foo EQUALS 'bar'"

sut := LogicalAndExpression{
Left: &PrimaryExpression{
Comparision: &ComparisionExpression{
Key: "foo",
Operator: "EQ",
Operator: "EQUALS",
Value: "bar",
},
},
Expand All @@ -310,12 +310,12 @@ func TestShouldRenderPrimaryExpressionOnLogicalAndExpressionWhenAndIsNotSet(t *t
}

func TestShouldRenderComparisionOnPrimaryExpressionWhenComparsionIsSet(t *testing.T) {
expectedResult := "foo EQ 'bar'"
expectedResult := "foo EQUALS 'bar'"

sut := PrimaryExpression{
Comparision: &ComparisionExpression{
Key: "foo",
Operator: "EQ",
Operator: "EQUALS",
Value: "bar",
},
}
Expand All @@ -328,12 +328,12 @@ func TestShouldRenderComparisionOnPrimaryExpressionWhenComparsionIsSet(t *testin
}

func TestShouldRenderUnaryOperationExpressionOnPrimaryExpressionWhenUnaryOperationIsSet(t *testing.T) {
expectedResult := "foo IS EMPTY"
expectedResult := "foo IS_EMPTY"

sut := PrimaryExpression{
UnaryOperation: &UnaryOperationExpression{
Key: "foo",
Operator: "IS EMPTY",
Operator: "IS_EMPTY",
},
}

Expand All @@ -345,15 +345,15 @@ func TestShouldRenderUnaryOperationExpressionOnPrimaryExpressionWhenUnaryOperati
}

func TestShouldRenderSubExpression(t *testing.T) {
expectedResult := "foo IS EMPTY AND ( a EQ 'b' OR a EQ 'c' )"
expectedResult := "foo IS_EMPTY AND ( a EQUALS 'b' OR a EQUALS 'c' )"

logicalOr := Operator("OR")
logicalAnd := Operator("AND")
sut := &LogicalAndExpression{
Left: &PrimaryExpression{
UnaryOperation: &UnaryOperationExpression{
Key: "foo",
Operator: "IS EMPTY",
Operator: "IS_EMPTY",
},
},
Operator: &logicalAnd,
Expand All @@ -364,7 +364,7 @@ func TestShouldRenderSubExpression(t *testing.T) {
Left: &PrimaryExpression{
Comparision: &ComparisionExpression{
Key: "a",
Operator: "EQ",
Operator: "EQUALS",
Value: "b",
},
},
Expand All @@ -375,7 +375,7 @@ func TestShouldRenderSubExpression(t *testing.T) {
Left: &PrimaryExpression{
Comparision: &ComparisionExpression{
Key: "a",
Operator: "EQ",
Operator: "EQUALS",
Value: "c",
},
},
Expand Down
Loading

0 comments on commit dccdc5e

Please sign in to comment.