feat(rules): replace go templates with custom variable expansion

- Replace template syntax ({{ .Request.Method }}) with $-prefixed variables ($req_method)
- Implement custom variable parser with static ($req_method, $status_code) and dynamic ($header(), $arg(), $form()) variables
- Replace templateOrStr interface with templateString struct and ExpandVars methods
- Add parser improvements for reliable quote handling
- Add new error types: ErrUnterminatedParenthesis, ErrUnexpectedVar, ErrExpectOneOrTwoArgs
- Update all tests and help text to use new variable syntax
- Add comprehensive unit and benchmark tests for variable expansion
This commit is contained in:
yusing
2025-10-25 22:43:47 +08:00
parent 9c3346dd9d
commit 1ec2872f3d
16 changed files with 1253 additions and 161 deletions

View File

@@ -19,6 +19,12 @@ var escapedChars = map[rune]rune{
' ': ' ',
}
var quoteChars = [256]bool{
'"': true,
'\'': true,
'`': true,
}
// parse expression to subject and args
// with support for quotes, escaped chars, and env substitution, e.g.
//
@@ -74,6 +80,19 @@ func parse(v string) (subject string, args []string, err gperr.Error) {
buf.WriteRune('$')
expectingBrace = false
}
if quoteChars[r] {
switch {
case quote == 0 && brackets == 0:
quote = r
flush(false)
case r == quote:
quote = 0
flush(true)
default:
buf.WriteRune(r)
}
continue
}
switch r {
case '\\':
escaped = true
@@ -106,17 +125,6 @@ func parse(v string) (subject string, args []string, err gperr.Error) {
} else {
buf.WriteRune(r)
}
case '"', '\'', '`':
switch {
case quote == 0 && brackets == 0:
quote = r
flush(false)
case r == quote:
quote = 0
flush(true)
default:
buf.WriteRune(r)
}
case '(':
brackets++
buf.WriteRune(r)