-
Notifications
You must be signed in to change notification settings - Fork 69
/
comparison.go
41 lines (32 loc) 路 1 KB
/
comparison.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package expression
import (
"fmt"
"github.com/kubeshop/tracetest/server/assertions/comparator"
"github.com/kubeshop/tracetest/server/expression/value"
)
func compare(comparatorName string, leftValue, rightValue value.Value) error {
comparatorFunction, err := comparator.DefaultRegistry().Get(comparatorName)
if err != nil {
return fmt.Errorf("comparator not supported: %w", err)
}
if leftValue.IsArray() && comparatorName == "contains" {
return compareArrayContains(leftValue, rightValue)
}
err = comparatorFunction.Compare(rightValue.String(), leftValue.String())
if err == comparator.ErrNoMatch {
return ErrNoMatch
}
return nil
}
func compareArrayContains(array, expected value.Value) error {
equalComparator, err := comparator.DefaultRegistry().Get("=")
if err != nil {
return fmt.Errorf("equal operator is not supported: %w", err)
}
for _, item := range array.Items {
if err = equalComparator.Compare(item.Value, expected.String()); err == nil {
return nil
}
}
return ErrNoMatch
}