mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-20 00:03:53 +01:00
* chore(deps): update submodule goutils * docs(http): remove default client from README.md * refactor(rules): introduce block DSL, phase-based execution, and flow validation - add block syntax parser/scanner with nested @blocks and elif/else support - restructure rule execution into explicit pre/post phases with phase flags - classify commands by phase and termination behavior - enforce flow semantics (default rule handling, dead-rule detection) - expand HTTP flow coverage with block + YAML parity tests and benches - refresh rules README/spec and update playground/docs integration - Default rules act as fallback handlers that execute only when no matching non-default rule exists in the pre phase - IfElseBlockCommand now returns early when a condition matches with a nil Do block, instead of falling through to else blocks - Add nil check for auth handler to allow requests when no auth is configured * fix(rules): buffer log output before writing to stdout/stderr * refactor(api/rules): remove IsResponseRule field from ParsedRule and related logic * docs(rules): update examples to use block syntax
141 lines
2.8 KiB
Go
141 lines
2.8 KiB
Go
package rules
|
|
|
|
import (
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/gobwas/glob"
|
|
"github.com/puzpuzpuz/xsync/v4"
|
|
gperr "github.com/yusing/goutils/errs"
|
|
)
|
|
|
|
type (
|
|
Matcher func(string) bool
|
|
MatcherType string
|
|
)
|
|
|
|
var matcherCache = xsync.NewMap[string, Matcher]() // map[string]Matcher
|
|
|
|
const (
|
|
MatcherTypeString MatcherType = "string"
|
|
MatcherTypeGlob MatcherType = "glob"
|
|
MatcherTypeRegex MatcherType = "regex"
|
|
)
|
|
|
|
func unquoteExpr(s string) (string, gperr.Error) {
|
|
if s == "" {
|
|
return "", nil
|
|
}
|
|
switch s[0] {
|
|
case '"', '\'', '`':
|
|
if s[0] != s[len(s)-1] {
|
|
return "", ErrUnterminatedQuotes
|
|
}
|
|
return s[1 : len(s)-1], nil
|
|
default:
|
|
return s, nil
|
|
}
|
|
}
|
|
|
|
func ExtractExpr(s string) (matcherType MatcherType, expr string, err gperr.Error) {
|
|
idx := strings.IndexByte(s, '(')
|
|
if idx == -1 {
|
|
return MatcherTypeString, s, nil
|
|
}
|
|
idxEnd := strings.LastIndexByte(s, ')')
|
|
if idxEnd == -1 {
|
|
return "", "", ErrUnterminatedBrackets
|
|
}
|
|
|
|
expr, err = unquoteExpr(s[idx+1 : idxEnd])
|
|
if err != nil {
|
|
return "", "", err
|
|
}
|
|
matcherType = MatcherType(strings.ToLower(s[:idx]))
|
|
|
|
switch matcherType {
|
|
case MatcherTypeGlob, MatcherTypeRegex, MatcherTypeString:
|
|
return
|
|
default:
|
|
return "", "", ErrInvalidArguments.Withf("invalid matcher type: %s", matcherType)
|
|
}
|
|
}
|
|
|
|
func ParseMatcher(expr string) (Matcher, gperr.Error) {
|
|
if cached, ok := matcherCache.Load(expr); ok {
|
|
return cached, nil
|
|
}
|
|
|
|
negate := false
|
|
origExpr := expr
|
|
if strings.HasPrefix(expr, "!") {
|
|
negate = true
|
|
expr = expr[1:]
|
|
}
|
|
|
|
t, expr, err := ExtractExpr(expr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
switch t {
|
|
case MatcherTypeString:
|
|
m, err := StringMatcher(expr, negate)
|
|
if err == nil {
|
|
matcherCache.Store(origExpr, m)
|
|
}
|
|
return m, err
|
|
case MatcherTypeGlob:
|
|
m, err := GlobMatcher(expr, negate)
|
|
if err == nil {
|
|
matcherCache.Store(origExpr, m)
|
|
}
|
|
return m, err
|
|
case MatcherTypeRegex:
|
|
m, err := RegexMatcher(expr, negate)
|
|
if err == nil {
|
|
matcherCache.Store(origExpr, m)
|
|
}
|
|
return m, err
|
|
}
|
|
// won't reach here
|
|
return nil, ErrInvalidArguments.Withf("invalid matcher type: %s", t)
|
|
}
|
|
|
|
func StringMatcher(s string, negate bool) (Matcher, gperr.Error) {
|
|
if negate {
|
|
return func(s2 string) bool {
|
|
return s != s2
|
|
}, nil
|
|
}
|
|
return func(s2 string) bool {
|
|
return s == s2
|
|
}, nil
|
|
}
|
|
|
|
func GlobMatcher(expr string, negate bool) (Matcher, gperr.Error) {
|
|
g, err := glob.Compile(expr)
|
|
if err != nil {
|
|
return nil, ErrInvalidArguments.With(err)
|
|
}
|
|
if negate {
|
|
return func(s string) bool {
|
|
return !g.Match(s)
|
|
}, nil
|
|
}
|
|
return g.Match, nil
|
|
}
|
|
|
|
func RegexMatcher(expr string, negate bool) (Matcher, gperr.Error) {
|
|
re, err := regexp.Compile(expr)
|
|
if err != nil {
|
|
return nil, ErrInvalidArguments.With(err)
|
|
}
|
|
if negate {
|
|
return func(s string) bool {
|
|
return !re.MatchString(s)
|
|
}, nil
|
|
}
|
|
return re.MatchString, nil
|
|
}
|