func parseSimpleExpression()

in internal/resources/providers/aws_cis/monitoring/filter_pattern_parser.go [241:283]


func parseSimpleExpression(s string) (MetricFilterPattern, error) {
	buf := strings.Builder{}
	buf.Grow(len(s))

	var left string
	var operator comparisonOperator
	foundOp := false

	for i, r := range s { // for each rune in string
		if buf.Len() == 0 && (r == ' ') { // ignore trailing spaces
			continue
		}

		// append the rune to the buffer
		if _, err := buf.WriteRune(r); err != nil {
			return MetricFilterPattern{}, fmt.Errorf("could not write rune %v", err)
		}

		tmpString := buf.String()
		// If the current buffer value has a Comparison ComparisonOperator as suffix, it means the right side of the expression
		// is finished
		if contains, op := hasSuffixComparisonOp(tmpString); contains {
			if foundOp { // if there was already a found operator for this simple expression, return error
				return MetricFilterPattern{}, errors.New("got multiple comparison operators")
			}

			// Remove the operator suffix and trailing spaces
			left = strings.TrimSpace(strings.TrimSuffix(tmpString, string(op)))
			operator = op
			foundOp = true
			buf.Reset()
			buf.Grow(len(s) - i)
		}
	}

	if !foundOp {
		return MetricFilterPattern{}, errors.New("could not find a ComparisonOperator for this MetricFilterPattern")
	}

	// Trim trailing spaces
	right := strings.TrimSpace(buf.String())
	return newSimpleExpression(left, operator, right), nil
}