Skip to content

Commit

Permalink
Merge pull request #196 from gessnerfl/bugfix/195_logical_and_with_si…
Browse files Browse the repository at this point in the history
…ngle_element

closes #195: fix implemented
  • Loading branch information
gessnerfl authored Sep 29, 2023
2 parents b559e31 + 119a1d0 commit c5d2c68
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
8 changes: 6 additions & 2 deletions instana/tagfilter/instana-api-model-to-tag-filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ func (m *tagFilterMapper) mapRightOfLogicalOr(right *expressionHandle) *LogicalO

func (m *tagFilterMapper) mapLogicalAnd(elements []*expressionHandle) (*expressionHandle, error) {
total := len(elements)
if total < 2 {
return nil, fmt.Errorf("at least two elements are expected for logical and")
if total < 1 {
return nil, fmt.Errorf("at least one element is expected for logical and")
}
if elements[0].and != nil {
return nil, fmt.Errorf("invalid logical and expression: logical and is not allowed for first element")
Expand All @@ -135,6 +135,10 @@ func (m *tagFilterMapper) mapLogicalAnd(elements []*expressionHandle) (*expressi
operator := Operator(restapi.LogicalAnd)
var expression *LogicalAndExpression

if total == 1 {
return elements[0], nil
}

for i := total - 2; i >= 0; i-- {
if expression == nil {
expression = &LogicalAndExpression{
Expand Down
23 changes: 19 additions & 4 deletions instana/tagfilter/instana-api-model-to-tag-filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,15 +446,30 @@ func TestShouldFailToMapLogicalAndFromInstanaAPIWhenFirstElementIsAnAndExpressio
require.Contains(t, err.Error(), "logical and is not allowed for first element")
}

func TestShouldFailToMapLogicalAndFromInstanaAPIWhenOnlyOneElementIsProvided(t *testing.T) {
func TestShouldUnwrapLogicalAndFromInstanaAPIWhenOnlyOneElementIsProvided(t *testing.T) {
primaryExpression := restapi.NewUnaryTagFilter(restapi.TagFilterEntityDestination, tagFilterName, restapi.IsEmptyOperator)
input := restapi.NewLogicalAndTagFilter([]restapi.TagFilterExpressionElement{primaryExpression})

expectedResult := &FilterExpression{
Expression: &LogicalOrExpression{
Left: &LogicalAndExpression{
Left: &BracketExpression{
Primary: &PrimaryExpression{
UnaryOperation: &UnaryOperationExpression{
Entity: &EntitySpec{Identifier: tagFilterName, Origin: utils.StringPtr(EntityOriginDestination.Key())},
Operator: Operator(restapi.IsEmptyOperator),
},
},
},
},
},
}

mapper := NewMapper()
_, err := mapper.FromAPIModel(input)
result, err := mapper.FromAPIModel(input)

require.NotNil(t, err)
require.Contains(t, err.Error(), "at least two elements are expected for logical and")
require.NoError(t, err)
require.Equal(t, expectedResult, result)
}

func TestShouldMapLogicalOrWithTwoPrimaryExpressionsFromInstanaAPI(t *testing.T) {
Expand Down

0 comments on commit c5d2c68

Please sign in to comment.