From 119a1d0059c6781548f38554b78c85e1e9d17cbe Mon Sep 17 00:00:00 2001 From: gessnerfl Date: Fri, 29 Sep 2023 10:14:18 +0200 Subject: [PATCH] #195: add support for logical ANDs used as wrappers. --- .../instana-api-model-to-tag-filter.go | 8 +++++-- .../instana-api-model-to-tag-filter_test.go | 23 +++++++++++++++---- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/instana/tagfilter/instana-api-model-to-tag-filter.go b/instana/tagfilter/instana-api-model-to-tag-filter.go index 88d8cda..d088a49 100644 --- a/instana/tagfilter/instana-api-model-to-tag-filter.go +++ b/instana/tagfilter/instana-api-model-to-tag-filter.go @@ -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") @@ -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{ diff --git a/instana/tagfilter/instana-api-model-to-tag-filter_test.go b/instana/tagfilter/instana-api-model-to-tag-filter_test.go index 4f9b783..d4eab8c 100644 --- a/instana/tagfilter/instana-api-model-to-tag-filter_test.go +++ b/instana/tagfilter/instana-api-model-to-tag-filter_test.go @@ -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) {