Skip to content
Snippets Groups Projects
Commit 4ca4e0ce authored by pantianying's avatar pantianying
Browse files

delete router util

parent ffd76850
No related branches found
No related tags found
No related merge requests found
......@@ -27,7 +27,6 @@ import (
)
import (
matcher "github.com/apache/dubbo-go/cluster/router/util"
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/common/constant"
"github.com/apache/dubbo-go/common/logger"
......@@ -301,7 +300,7 @@ func (pair MatchPair) isMatch(value string, param *common.URL) bool {
if !pair.Matches.Empty() && pair.Mismatches.Empty() {
for match := range pair.Matches.Items {
if matcher.IsMatchGlobalPattern(match.(string), value, param) {
if isMatchGlobalPattern(match.(string), value, param) {
return true
}
}
......@@ -310,7 +309,7 @@ func (pair MatchPair) isMatch(value string, param *common.URL) bool {
if !pair.Mismatches.Empty() && pair.Matches.Empty() {
for mismatch := range pair.Mismatches.Items {
if matcher.IsMatchGlobalPattern(mismatch.(string), value, param) {
if isMatchGlobalPattern(mismatch.(string), value, param) {
return false
}
}
......@@ -319,12 +318,12 @@ func (pair MatchPair) isMatch(value string, param *common.URL) bool {
if !pair.Mismatches.Empty() && !pair.Matches.Empty() {
//when both mismatches and matches contain the same value, then using mismatches first
for mismatch := range pair.Mismatches.Items {
if matcher.IsMatchGlobalPattern(mismatch.(string), value, param) {
if isMatchGlobalPattern(mismatch.(string), value, param) {
return false
}
}
for match := range pair.Matches.Items {
if matcher.IsMatchGlobalPattern(match.(string), value, param) {
if isMatchGlobalPattern(match.(string), value, param) {
return true
}
}
......
......@@ -17,8 +17,17 @@
package condition
import (
"strings"
)
import (
gxstrings "github.com/dubbogo/gost/strings"
)
import (
"github.com/apache/dubbo-go/cluster/router"
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/common/yaml"
)
......@@ -54,3 +63,11 @@ func getRule(rawRule string) (*RouterRule, error) {
return r, nil
}
// isMatchGlobalPattern Match value to param content by pattern
func isMatchGlobalPattern(pattern string, value string, param *common.URL) bool {
if param != nil && strings.HasPrefix(pattern, "$") {
pattern = param.GetRawParam(pattern[1:])
}
return gxstrings.IsMatchPattern(pattern, value)
}
......@@ -18,6 +18,7 @@
package condition
import (
"github.com/apache/dubbo-go/common"
"testing"
)
import (
......@@ -50,3 +51,8 @@ conditions:
assert.Equal(t, false, rule.Dynamic)
assert.Equal(t, "", rule.Key)
}
func TestIsMatchGlobPattern(t *testing.T) {
url, _ := common.NewURL("dubbo://localhost:8080/Foo?key=v*e")
assert.Equal(t, true, isMatchGlobalPattern("$key", "value", &url))
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package util
import (
"strings"
)
import (
"github.com/apache/dubbo-go/common"
)
// IsMatchGlobalPattern Match value to param content by pattern
func IsMatchGlobalPattern(pattern string, value string, param *common.URL) bool {
if param != nil && strings.HasPrefix(pattern, "$") {
pattern = param.GetRawParam(pattern[1:])
}
return isMatchInternalPattern(pattern, value)
}
func isMatchInternalPattern(pattern string, value string) bool {
if "*" == pattern {
return true
}
if len(pattern) == 0 && len(value) == 0 {
return true
}
if len(pattern) == 0 || len(value) == 0 {
return false
}
i := strings.LastIndex(pattern, "*")
switch i {
case -1:
// doesn't find "*"
return value == pattern
case len(pattern) - 1:
// "*" is at the end
return strings.HasPrefix(value, pattern[0:i])
case 0:
// "*" is at the beginning
return strings.HasSuffix(value, pattern[i+1:])
default:
// "*" is in the middle
prefix := pattern[0:1]
suffix := pattern[i+1:]
return strings.HasPrefix(value, prefix) && strings.HasSuffix(value, suffix)
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package util
import (
"testing"
)
import (
"github.com/stretchr/testify/assert"
)
import (
"github.com/apache/dubbo-go/common"
)
func TestIsMatchInternalPattern(t *testing.T) {
assert.Equal(t, true, isMatchInternalPattern("*", "value"))
assert.Equal(t, true, isMatchInternalPattern("", ""))
assert.Equal(t, false, isMatchInternalPattern("", "value"))
assert.Equal(t, true, isMatchInternalPattern("value", "value"))
assert.Equal(t, true, isMatchInternalPattern("v*", "value"))
assert.Equal(t, true, isMatchInternalPattern("*ue", "value"))
assert.Equal(t, true, isMatchInternalPattern("*e", "value"))
assert.Equal(t, true, isMatchInternalPattern("v*e", "value"))
}
func TestIsMatchGlobPattern(t *testing.T) {
url, _ := common.NewURL("dubbo://localhost:8080/Foo?key=v*e")
assert.Equal(t, true, IsMatchGlobalPattern("$key", "value", &url))
}
......@@ -14,7 +14,7 @@ require (
github.com/creasty/defaults v1.3.0
github.com/dubbogo/getty v1.3.3
github.com/dubbogo/go-zookeeper v1.0.0
github.com/dubbogo/gost v1.5.2
github.com/dubbogo/gost v1.8.0
github.com/emicklei/go-restful/v3 v3.0.0
github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239 // indirect
github.com/go-errors/errors v1.0.1 // indirect
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment