func()

in confgenerator/filter/internal/ast/ast.go [489:528]


func (r Restriction) OTTLExpression() (ottl.Value, error) {
	lhs, _ := r.LHS.OTTLAccessor()

	// TODO: Add support for numeric comparisons

	var expr ottl.Value

	switch r.Operator {
	case "GLOBAL", "<", "<=", ">", ">=":
		return nil, fmt.Errorf("unimplemented operator: %s", r.Operator)
	case ":":
		// substring match, case insensitive
		expr = ottl.IsMatch(lhs, fmt.Sprintf(`(?i)%s`, regexp.QuoteMeta(r.RHS)))
	case "=~", "!~":
		// regex match, case sensitive

		if _, err := regexp.Compile(r.RHS); err != nil {
			return nil, fmt.Errorf("unsupported regex %q: %w", r.RHS, err)
		}

		expr = ottl.IsMatch(lhs, r.RHS)
		// TODO: Support Ruby regex syntax

		if r.Operator == "!~" {
			expr = ottl.Not(expr)
		}
	case "=", "!=":
		// equality, case insensitive
		expr = ottl.IsMatch(lhs, fmt.Sprintf(`(?i)^%s$`, regexp.QuoteMeta(r.RHS)))
		if r.Operator == "!=" {
			expr = ottl.Not(expr)
		}
	}
	if expr != nil {
		// All comparisons involving a missing field are false
		return ottl.And(lhs.IsPresent(), expr), nil
	}
	// This is all the supported operators.
	return nil, fmt.Errorf("unknown operator: %s", r.Operator)
}