mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-23 17:41:05 +01:00
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:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user