mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-24 01:38:50 +02:00
feat(rules): introduce block DSL, phase-based execution (#203)
* 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
This commit is contained in:
@@ -4,13 +4,13 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"unsafe"
|
||||
|
||||
httputils "github.com/yusing/goutils/http"
|
||||
)
|
||||
|
||||
type templateString struct {
|
||||
string
|
||||
|
||||
isTemplate bool
|
||||
}
|
||||
|
||||
@@ -23,32 +23,28 @@ func (tmpl *keyValueTemplate) Unpack() (string, templateString) {
|
||||
return tmpl.key, tmpl.tmpl
|
||||
}
|
||||
|
||||
func (tmpl *templateString) ExpandVars(w http.ResponseWriter, req *http.Request, dstW io.Writer) error {
|
||||
func (tmpl *templateString) ExpandVars(w *httputils.ResponseModifier, req *http.Request, dst io.Writer) (phase PhaseFlag, err error) {
|
||||
if !tmpl.isTemplate {
|
||||
_, err := dstW.Write(strtobNoCopy(tmpl.string))
|
||||
return err
|
||||
_, err := asBytesBufferLike(dst).WriteString(tmpl.string)
|
||||
return PhaseNone, err
|
||||
}
|
||||
|
||||
return ExpandVars(httputils.GetInitResponseModifier(w), req, tmpl.string, dstW)
|
||||
return ExpandVars(w, req, tmpl.string, dst)
|
||||
}
|
||||
|
||||
func (tmpl *templateString) ExpandVarsToString(w http.ResponseWriter, req *http.Request) (string, error) {
|
||||
func (tmpl *templateString) ExpandVarsToString(w *httputils.ResponseModifier, r *http.Request) (string, PhaseFlag, error) {
|
||||
if !tmpl.isTemplate {
|
||||
return tmpl.string, nil
|
||||
return tmpl.string, PhaseNone, nil
|
||||
}
|
||||
|
||||
var buf strings.Builder
|
||||
err := ExpandVars(httputils.GetInitResponseModifier(w), req, tmpl.string, &buf)
|
||||
phase, err := tmpl.ExpandVars(w, r, &buf)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return "", PhaseNone, err
|
||||
}
|
||||
return buf.String(), nil
|
||||
return buf.String(), phase, nil
|
||||
}
|
||||
|
||||
func (tmpl *templateString) Len() int {
|
||||
return len(tmpl.string)
|
||||
}
|
||||
|
||||
func strtobNoCopy(s string) []byte {
|
||||
return unsafe.Slice(unsafe.StringData(s), len(s))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user