aboutsummaryrefslogtreecommitdiff
path: root/plugin/rewrite/condition.go
diff options
context:
space:
mode:
Diffstat (limited to 'plugin/rewrite/condition.go')
-rw-r--r--plugin/rewrite/condition.go132
1 files changed, 132 insertions, 0 deletions
diff --git a/plugin/rewrite/condition.go b/plugin/rewrite/condition.go
new file mode 100644
index 000000000..2f20d71aa
--- /dev/null
+++ b/plugin/rewrite/condition.go
@@ -0,0 +1,132 @@
+package rewrite
+
+import (
+ "fmt"
+ "regexp"
+ "strings"
+
+ "github.com/coredns/coredns/plugin/pkg/replacer"
+
+ "github.com/miekg/dns"
+)
+
+// Operators
+const (
+ Is = "is"
+ Not = "not"
+ Has = "has"
+ NotHas = "not_has"
+ StartsWith = "starts_with"
+ EndsWith = "ends_with"
+ Match = "match"
+ NotMatch = "not_match"
+)
+
+func operatorError(operator string) error {
+ return fmt.Errorf("invalid operator %v", operator)
+}
+
+func newReplacer(r *dns.Msg) replacer.Replacer {
+ return replacer.New(r, nil, "")
+}
+
+// condition is a rewrite condition.
+type condition func(string, string) bool
+
+var conditions = map[string]condition{
+ Is: isFunc,
+ Not: notFunc,
+ Has: hasFunc,
+ NotHas: notHasFunc,
+ StartsWith: startsWithFunc,
+ EndsWith: endsWithFunc,
+ Match: matchFunc,
+ NotMatch: notMatchFunc,
+}
+
+// isFunc is condition for Is operator.
+// It checks for equality.
+func isFunc(a, b string) bool {
+ return a == b
+}
+
+// notFunc is condition for Not operator.
+// It checks for inequality.
+func notFunc(a, b string) bool {
+ return a != b
+}
+
+// hasFunc is condition for Has operator.
+// It checks if b is a substring of a.
+func hasFunc(a, b string) bool {
+ return strings.Contains(a, b)
+}
+
+// notHasFunc is condition for NotHas operator.
+// It checks if b is not a substring of a.
+func notHasFunc(a, b string) bool {
+ return !strings.Contains(a, b)
+}
+
+// startsWithFunc is condition for StartsWith operator.
+// It checks if b is a prefix of a.
+func startsWithFunc(a, b string) bool {
+ return strings.HasPrefix(a, b)
+}
+
+// endsWithFunc is condition for EndsWith operator.
+// It checks if b is a suffix of a.
+func endsWithFunc(a, b string) bool {
+ // TODO(miek): IsSubDomain
+ return strings.HasSuffix(a, b)
+}
+
+// matchFunc is condition for Match operator.
+// It does regexp matching of a against pattern in b
+// and returns if they match.
+func matchFunc(a, b string) bool {
+ matched, _ := regexp.MatchString(b, a)
+ return matched
+}
+
+// notMatchFunc is condition for NotMatch operator.
+// It does regexp matching of a against pattern in b
+// and returns if they do not match.
+func notMatchFunc(a, b string) bool {
+ matched, _ := regexp.MatchString(b, a)
+ return !matched
+}
+
+// If is statement for a rewrite condition.
+type If struct {
+ A string
+ Operator string
+ B string
+}
+
+// True returns true if the condition is true and false otherwise.
+// If r is not nil, it replaces placeholders before comparison.
+func (i If) True(r *dns.Msg) bool {
+ if c, ok := conditions[i.Operator]; ok {
+ a, b := i.A, i.B
+ if r != nil {
+ replacer := newReplacer(r)
+ a = replacer.Replace(i.A)
+ b = replacer.Replace(i.B)
+ }
+ return c(a, b)
+ }
+ return false
+}
+
+// NewIf creates a new If condition.
+func NewIf(a, operator, b string) (If, error) {
+ if _, ok := conditions[operator]; !ok {
+ return If{}, operatorError(operator)
+ }
+ return If{
+ A: a,
+ Operator: operator,
+ B: b,
+ }, nil
+}
ror.test.ts?h=feat/npm&id=ed421855d70c64f55a5cb15a1a46798457697d3e&follow=1'>commitdiff
path: root/test/bun.js/resolve-error.test.ts (unfollow)
AgeCommit message (Expand)AuthorFilesLines
2023-01-21Handle string subclasses and new String() in new BufferGravatar Jarred Sumner 1-2/+11
2023-01-21Make Buffer.alloc* 3ns fasterGravatar Jarred Sumner 1-11/+17
2023-01-21[buffer] Make Buffer.from pass more testsGravatar Jarred Sumner 4-92/+179
2023-01-20constructor parameter properties in class expressions (#1867)Gravatar Dylan Conway 2-8/+48
2023-01-20Update transpiler.test.jsbun-v0.5.1Gravatar Jarred Sumner 1-1/+1
2023-01-20Update transpiler.test.jsGravatar Jarred Sumner 1-1/+1
2023-01-20push super before generated statements (#1856)Gravatar Dylan Conway 2-29/+53
2023-01-20Clear the errorsGravatar Jarred Sumner 1-0/+2
2023-01-20one less hash tableGravatar Jarred Sumner 1-2/+13
2023-01-20Add another testGravatar Jarred Sumner 2-1/+11
2023-01-20fix hanging testGravatar Jarred Sumner 1-38/+42
2023-01-20Further cleanup buffer encodingGravatar Jarred Sumner 1-48/+26
2023-01-20Fixes #1855Gravatar Jarred Sumner 2-5/+57
2023-01-20Fix assertion failure with boringssl messagesGravatar Jarred Sumner 3-5/+132
2023-01-19Revert "ignore sighup"Gravatar Jarred Sumner 1-45/+10
2023-01-19ignore sighupGravatar Jarred Sumner 1-10/+45
2023-01-19make this code easier to readGravatar Jarred Sumner 3-29/+26
2023-01-19Update types.zigGravatar Jarred Sumner 1-4/+0
2023-01-19BumpGravatar Jarred Sumner 2-2/+2
2023-01-19Fix buffer encoding bugGravatar Jarred Sumner 2-4/+17
2023-01-19use `String.from()` (#1850)Gravatar Alex Lam S.L 4-5/+12
2023-01-19Bump zigGravatar Jarred Sumner 2-2/+2
2023-01-19make it packedGravatar Jarred Sumner 1-2/+2
2023-01-20Bugfixes to install (#1848)Gravatar Jarred Sumner 5-26/+119
2023-01-19repopulate `alias_map` correctly (#1847)Gravatar Alex Lam S.L 5-70/+240
2023-01-19Add a commentGravatar Jarred Sumner 1-0/+6
2023-01-19Add a debug safety check for UAF in AST nodesGravatar Jarred Sumner 1-0/+5
2023-01-19Fix UAF when opening workspacesGravatar Jarred Sumner 1-2/+0
2023-01-19Improve error message when a workspace is not foundGravatar Jarred Sumner 2-9/+97