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) { func parseRule(tp, payload string, params []string) (parsed C.Rule, parseErr error) {
parsed, parseErr = ruleparser.ParseSameRule(tp, payload, "", params) switch tp {
if ruleparser.IsUnsupported(parseErr) { case "AND":
switch tp { parsed, parseErr = NewAND(payload, "")
case "AND": case "OR":
parsed, parseErr = NewAND(payload, "") parsed, parseErr = NewOR(payload, "")
case "OR": case "NOT":
parsed, parseErr = NewOR(payload, "") parsed, parseErr = NewNOT(payload, "")
case "NOT": case "RULE-SET":
parsed, parseErr = NewNOT(payload, "") noResolve := RC.HasNoResolve(params)
case "RULE-SET": parsed, parseErr = RP.NewRuleSet(payload, "", noResolve)
noResolve := RC.HasNoResolve(params) default:
parsed, parseErr = RP.NewRuleSet(payload, "", noResolve) parsed, parseErr = ruleparser.ParseSameRule(tp, payload, "", params)
default:
parseErr = ruleparser.NewUnsupportedError(tp)
}
} }
if parseErr != nil { if parseErr != nil {

View file

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

View file

@ -44,24 +44,7 @@ func ParseSameRule(tp, payload, target string, params []string) (parsed C.Rule,
case "IN-TYPE": case "IN-TYPE":
parsed, parseErr = RC.NewInType(payload, target) parsed, parseErr = RC.NewInType(payload, target)
default: default:
parseErr = NewUnsupportedError(tp) parseErr = fmt.Errorf("unsupported rule type %s", tp)
} }
return 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
}