From fa3e0c726e69cb4441e45161995016a36e23864d Mon Sep 17 00:00:00 2001 From: adlyq Date: Thu, 2 Jun 2022 17:03:08 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E8=B0=83=E6=95=B4=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rule/logic/common.go | 27 ++++++++++++--------------- rule/parser.go | 33 +++++++++++++++------------------ rule/ruleparser/ruleparser.go | 19 +------------------ 3 files changed, 28 insertions(+), 51 deletions(-) diff --git a/rule/logic/common.go b/rule/logic/common.go index 61dbe963..0f43e688 100644 --- a/rule/logic/common.go +++ b/rule/logic/common.go @@ -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 { diff --git a/rule/parser.go b/rule/parser.go index cc76219c..05d01e32 100644 --- a/rule/parser.go +++ b/rule/parser.go @@ -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 { diff --git a/rule/ruleparser/ruleparser.go b/rule/ruleparser/ruleparser.go index c4bd7bad..55f63f82 100644 --- a/rule/ruleparser/ruleparser.go +++ b/rule/ruleparser/ruleparser.go @@ -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 -}