validateBalancedBraces checks that all ${{ }} braces are balanced and properly closed
(group string)
| 31 | |
| 32 | // validateBalancedBraces checks that all ${{ }} braces are balanced and properly closed |
| 33 | func validateBalancedBraces(group string) error { |
| 34 | expressionValidationLog.Print("Checking balanced braces in expression") |
| 35 | openCount := 0 |
| 36 | i := 0 |
| 37 | positions := []int{} // Track positions of opening braces for error reporting |
| 38 | |
| 39 | for i < len(group) { |
| 40 | // Check for opening ${{ |
| 41 | if i+2 < len(group) && group[i:i+3] == "${{" { |
| 42 | openCount++ |
| 43 | positions = append(positions, i) |
| 44 | i += 3 |
| 45 | continue |
| 46 | } |
| 47 | |
| 48 | // Check for closing }} |
| 49 | if i+1 < len(group) && group[i:i+2] == "}}" { |
| 50 | if openCount == 0 { |
| 51 | return NewValidationError( |
| 52 | "expression", |
| 53 | "unbalanced closing braces", |
| 54 | fmt.Sprintf("found '}}' at position %d without matching opening '${{' in expression: %s", i, group), |
| 55 | "Ensure all '}}' have a corresponding opening '${{'. Check for typos or missing opening braces.", |
| 56 | ) |
| 57 | } |
| 58 | openCount-- |
| 59 | if len(positions) > 0 { |
| 60 | positions = positions[:len(positions)-1] |
| 61 | } |
| 62 | i += 2 |
| 63 | continue |
| 64 | } |
| 65 | |
| 66 | i++ |
| 67 | } |
| 68 | |
| 69 | if openCount > 0 { |
| 70 | // Find the position of the first unclosed opening brace |
| 71 | pos := positions[0] |
| 72 | expressionValidationLog.Printf("Found %d unclosed brace(s) starting at position %d", openCount, pos) |
| 73 | return NewValidationError( |
| 74 | "expression", |
| 75 | "unclosed expression braces", |
| 76 | fmt.Sprintf("found opening '${{' at position %d without matching closing '}}' in expression: %s", pos, group), |
| 77 | "Ensure all '${{' have a corresponding closing '}}'. Add the missing closing braces.", |
| 78 | ) |
| 79 | } |
| 80 | |
| 81 | expressionValidationLog.Print("Brace balance check passed") |
| 82 | return nil |
| 83 | } |
| 84 | |
| 85 | // validateExpressionSyntax validates the syntax of expressions within ${{ }} |
| 86 | func validateExpressionSyntax(group string) error { |