From 71c7f97b55b77de76877609d40a91f43c3b3e9a2 Mon Sep 17 00:00:00 2001 From: guoguangwu Date: Wed, 12 Jul 2023 15:58:14 +0800 Subject: [PATCH] chore: use bytes.Equal instead bytes.Compare --- pkg/proxy/filter_cross_domain.go | 2 +- pkg/route/parser.go | 2 +- pkg/route/route.go | 4 ++-- pkg/route/route_test.go | 12 ++++++------ 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/pkg/proxy/filter_cross_domain.go b/pkg/proxy/filter_cross_domain.go index 0c96d89e..920664b1 100644 --- a/pkg/proxy/filter_cross_domain.go +++ b/pkg/proxy/filter_cross_domain.go @@ -62,7 +62,7 @@ func (f *CrossDomainFilter) Name() string { // Pre execute before proxy func (f *CrossDomainFilter) Pre(c filter.Context) (statusCode int, err error) { - if bytes.Compare(c.OriginRequest().Method(), options) != 0 { + if !bytes.Equal(c.OriginRequest().Method(), options) { return f.BaseFilter.Pre(c) } diff --git a/pkg/route/parser.go b/pkg/route/parser.go index 5c7f7388..eb1f217c 100644 --- a/pkg/route/parser.go +++ b/pkg/route/parser.go @@ -47,7 +47,7 @@ func (n *node) isMatchAllConstString() bool { func (n *node) inEnumValue(value []byte) bool { in := false for _, enum := range n.enums { - if bytes.Compare(enum, value) == 0 { + if bytes.Equal(enum, value) { in = true } } diff --git a/pkg/route/route.go b/pkg/route/route.go index efc426de..e2799072 100644 --- a/pkg/route/route.go +++ b/pkg/route/route.go @@ -52,7 +52,7 @@ func (item *routeItem) urlMatches(n node, matchAllParam *bytes.Buffer) bool { return true } - return bytes.Compare(item.node.value, n.value) == 0 + return bytes.Equal(item.node.value, n.value) case stringType: return true default: @@ -75,7 +75,7 @@ func (item *routeItem) matches(n node) bool { case stringType: return true case constType: - return bytes.Compare(item.node.value, n.value) == 0 + return bytes.Equal(item.node.value, n.value) case enumType: return true default: diff --git a/pkg/route/route_test.go b/pkg/route/route_test.go index ae15cfb3..12e1a09c 100644 --- a/pkg/route/route_test.go +++ b/pkg/route/route_test.go @@ -239,7 +239,7 @@ func TestFindMatchAll(t *testing.T) { if id != 1 { t.Errorf("expect matched 1, but %d", id) } - if bytes.Compare(params["*"], []byte("p1")) != 0 { + if !bytes.Equal(params["*"], []byte("p1")) { t.Errorf("expect params p1, but %s", params["*"]) } @@ -247,7 +247,7 @@ func TestFindMatchAll(t *testing.T) { if id != 1 { t.Errorf("expect matched 1, but %d", id) } - if bytes.Compare(params["*"], []byte("p1/p2")) != 0 { + if !bytes.Equal(params["*"], []byte("p1/p2")) { t.Errorf("expect params p1/p2, but %s", params["*"]) } } @@ -301,7 +301,7 @@ func TestFind(t *testing.T) { if id != 3 { t.Errorf("expect matched 3, but %d", id) } - if bytes.Compare(params["name"], []byte("check2")) != 0 { + if !bytes.Equal(params["name"], []byte("check2")) { t.Errorf("expect params check2, but %s", params["name"]) } @@ -310,7 +310,7 @@ func TestFind(t *testing.T) { if id != 4 { t.Errorf("expect matched 4, but %d", id) } - if bytes.Compare(params["age"], []byte("123")) != 0 { + if !bytes.Equal(params["age"], []byte("123")) { t.Errorf("expect params 123, but %s", params["age"]) } @@ -319,7 +319,7 @@ func TestFind(t *testing.T) { if id != 5 { t.Errorf("expect matched 5, but %d", id) } - if bytes.Compare(params["action"], []byte("on")) != 0 { + if !bytes.Equal(params["action"], []byte("on")) { t.Errorf("expect params on, but %s", params["action"]) } @@ -328,7 +328,7 @@ func TestFind(t *testing.T) { if id != 5 { t.Errorf("expect matched 5, but %d", id) } - if bytes.Compare(params["action"], []byte("off")) != 0 { + if !bytes.Equal(params["action"], []byte("off")) { t.Errorf("expect params off, but %s", params["action"]) }