chore: 调整解析逻辑

This commit is contained in:
adlyq 2022-06-02 17:03:08 +08:00
parent 3b038310ab
commit fa3e0c726e
3 changed files with 28 additions and 51 deletions

View file

@ -62,21 +62,18 @@ func payloadToRule(subPayload string) (C.Rule, error) {
}
func parseRule(tp, payload string, params []string) (parsed C.Rule, parseErr error) {
parsed, parseErr = ruleparser.ParseSameRule(tp, payload, "", params)
if ruleparser.IsUnsupported(parseErr) {
switch tp {
case "AND":
parsed, parseErr = NewAND(payload, "")
case "OR":
parsed, parseErr = NewOR(payload, "")
case "NOT":
parsed, parseErr = NewNOT(payload, "")
case "RULE-SET":
noResolve := RC.HasNoResolve(params)
parsed, parseErr = RP.NewRuleSet(payload, "", noResolve)
default:
parseErr = ruleparser.NewUnsupportedError(tp)
}
switch tp {
case "AND":
parsed, parseErr = NewAND(payload, "")
case "OR":
parsed, parseErr = NewOR(payload, "")
case "NOT":
parsed, parseErr = NewNOT(payload, "")
case "RULE-SET":
noResolve := RC.HasNoResolve(params)
parsed, parseErr = RP.NewRuleSet(payload, "", noResolve)
default:
parsed, parseErr = ruleparser.ParseSameRule(tp, payload, "", params)
}
if parseErr != nil {

View file

@ -9,24 +9,21 @@ import (
)
func ParseRule(tp, payload, target string, params []string) (parsed C.Rule, parseErr error) {
parsed, parseErr = ruleparser.ParseSameRule(tp, payload, target, params)
if ruleparser.IsUnsupported(parseErr) {
switch tp {
case "AND":
parsed, parseErr = logic.NewAND(payload, target)
case "OR":
parsed, parseErr = logic.NewOR(payload, target)
case "NOT":
parsed, parseErr = logic.NewNOT(payload, target)
case "RULE-SET":
noResolve := RC.HasNoResolve(params)
parsed, parseErr = RP.NewRuleSet(payload, target, noResolve)
case "MATCH":
parsed = RC.NewMatch(target)
parseErr = nil
default:
parseErr = ruleparser.NewUnsupportedError(tp)
}
switch tp {
case "AND":
parsed, parseErr = logic.NewAND(payload, target)
case "OR":
parsed, parseErr = logic.NewOR(payload, target)
case "NOT":
parsed, parseErr = logic.NewNOT(payload, target)
case "RULE-SET":
noResolve := RC.HasNoResolve(params)
parsed, parseErr = RP.NewRuleSet(payload, target, noResolve)
case "MATCH":
parsed = RC.NewMatch(target)
parseErr = nil
default:
parsed, parseErr = ruleparser.ParseSameRule(tp, payload, target, params)
}
if parseErr != nil {

View file

@ -44,24 +44,7 @@ func ParseSameRule(tp, payload, target string, params []string) (parsed C.Rule,
case "IN-TYPE":
parsed, parseErr = RC.NewInType(payload, target)
default:
parseErr = NewUnsupportedError(tp)
parseErr = fmt.Errorf("unsupported rule type %s", tp)
}
return
}
type UnsupportedError struct {
err string
}
func (ue UnsupportedError) Error() string {
return ue.err
}
func NewUnsupportedError(tp any) *UnsupportedError {
return &UnsupportedError{err: fmt.Sprintf("unsupported rule type %s", tp)}
}
func IsUnsupported(err error) bool {
_, ok := err.(*UnsupportedError)
return ok
}