Skip to content
Snippets Groups Projects
Commit fd137b3b authored by LaurenceLiZhixin's avatar LaurenceLiZhixin
Browse files

fix

parent 40e827f5
No related branches found
No related tags found
No related merge requests found
Showing
with 2 additions and 1207 deletions
/*
* 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 uniform
import (
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/config"
"github.com/apache/dubbo-go/protocol"
)
type DubboRouterRule struct {
uniformRules []*UniformRule
}
func newDubboRouterRule(dubboRoutes []*config.DubboRoute, destinationMap map[string]map[string]string) (*DubboRouterRule, error) {
uniformRules := make([]*UniformRule, 0)
for _, v := range dubboRoutes {
uniformRule, err := newUniformRule(v, destinationMap)
if err != nil {
return nil, err
}
uniformRules = append(uniformRules, uniformRule)
}
return &DubboRouterRule{
uniformRules: uniformRules,
}, nil
}
func (drr *DubboRouterRule) route(invokers []protocol.Invoker, url *common.URL, invocation protocol.Invocation) []protocol.Invoker {
resultInvokers := make([]protocol.Invoker, 0)
for _, v := range drr.uniformRules {
if resultInvokers = v.route(invokers, url, invocation); len(resultInvokers) == 0 {
continue
}
// once there is a uniformRule successfully get target invoker lists, return it
return resultInvokers
}
// return s empty invoker list
return resultInvokers
}
......@@ -21,6 +21,7 @@ import (
"github.com/apache/dubbo-go/cluster/router"
"github.com/apache/dubbo-go/common/constant"
"github.com/apache/dubbo-go/common/extension"
v3router "github.com/dubbogo/v3router/pkg"
)
func init() {
......@@ -39,5 +40,5 @@ func newUniformRouterFactory() router.PriorityRouterFactory {
// NewPriorityRouter construct a new NewConnCheckRouter via url
func (f *UniformRouteFactory) NewPriorityRouter(vsConfigBytes, distConfigBytes []byte, notify chan struct{}) (router.PriorityRouter, error) {
return NewUniformRouterChain(vsConfigBytes, distConfigBytes, notify)
return v3router.NewUniformRouterChain(vsConfigBytes, distConfigBytes, notify)
}
apiVersion: service.dubbo.apache.org/v1alpha1
kind: DestinationRule
metadata:
name: demo-route
spec:
host: demo
subsets:
- name: v1
labels:
sigma.ali/mg: v1-host
generic: false
- name: v2
labels:
generic: false
- name: v3
labels:
sigma.ali/mg: v3-host
\ No newline at end of file
apiVersion: service.dubbo.apache.org/v1alpha1
kind: VirtualService
metadata:
name: demo-route
spec:
hosts:
- demo # app name
dubbo:
- service:
- exact: com.taobao.hsf.demoService:1.0.0
- exact: com.taobao.hsf.demoService:2.0.0
routedetail:
- name: sayHello-String-method-route
match:
- method:
name_match:
exact: "sayHello"
# argp:
# - string
route:
- destination:
host: demo
subset: v1
fallback:
destination:
host: demo
subset: v2
fallback:
destination:
host: demo
subset: v3
- name: sayHello-method-route
match:
- method:
name_match:
exact: "s-method"
route:
- destination:
host: demo
subset: v2
fallback:
destination:
host: demo
subset: v3
- name: some-method-route
match:
- method:
name_match:
exact: "some-method"
route:
- destination:
host: demo
subset: v4
# - name: interface-route
# route:
# - destination:
# host: demo
# subset: v3
- name: final
match:
- method:
name_match:
exact: "GetUser"
route:
- destination:
host: demo
subset: v1
fallback:
destination:
host: demo
subset: v2
fallback:
destination:
host: demo
subset: v3
- destination:
host: demo
subset: v3
fallback:
destination:
host: demo
subset: v2
fallback:
destination:
host: demo
subset: v1
- service:
- exact: com.taobao.hsf.demoService:1.0.0
- exact: org.apache.dubbo.UserProvider
routedetail:
- name: sayHello-String-method-route
match:
- method:
name_match:
exact: "sayHello"
# argp:
# - string
route:
- destination:
host: demo
subset: v1
fallback:
destination:
host: demo
subset: v2
fallback:
destination:
host: demo
subset: v3
- name: sayHello-method-route
match:
- method:
name_match:
exact: "s-method"
route:
- destination:
host: demo
subset: v2
fallback:
destination:
host: demo
subset: v3
- name: some-method-route
match:
- method:
name_match:
exact: "some-method"
route:
- destination:
host: demo
subset: v4
# - name: interface-route
# route:
# - destination:
# host: demo
# subset: v3
- name: final
match:
- method:
name_match:
exact: "GetUser"
route:
- destination:
host: demo
subset: v1
fallback:
destination:
host: demo
subset: v2
fallback:
destination:
host: demo
subset: v3
- destination:
host: demo
subset: v3
fallback:
destination:
host: demo
subset: v2
fallback:
destination:
host: demo
subset: v1
\ No newline at end of file
/*
* 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 k8s_api
import (
"github.com/apache/dubbo-go/config_center"
"github.com/apache/dubbo-go/remoting/k8sCRD"
)
const (
GroupName = "service.dubbo.apache.org"
GroupVersion = "v1alpha1"
Namespace = "dubbo-workplace"
)
func SetK8sEventListener(listener config_center.ConfigurationListener) error {
vsUniformRouterListenerHandler := newVirtualServiceListenerHandler(listener)
drUniformRouterListenerHandler := newDestRuleListenerHandler(listener)
k8sCRDClient, err := k8sCRD.NewK8sCRDClient(GroupName, GroupVersion, Namespace, vsUniformRouterListenerHandler, drUniformRouterListenerHandler)
if err != nil {
return err
}
k8sCRDClient.WatchResources()
return nil
}
/*
* 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 k8s_api
import (
"fmt"
)
import (
metav "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
)
import (
dubboConfig "github.com/apache/dubbo-go/config"
"github.com/apache/dubbo-go/config_center"
"github.com/apache/dubbo-go/remoting"
"github.com/apache/dubbo-go/remoting/k8sCRD"
)
const VirtualServiceEventKey = "virtualServiceEventKey"
const DestinationRuleEventKey = "destinationRuleEventKe3y"
const VirtualServiceResource = "virtualservices"
const DestRuleResource = "destinationrules"
type VirtualServiceListenerHandler struct {
listener config_center.ConfigurationListener
}
func (r *VirtualServiceListenerHandler) AddFunc(obj interface{}) {
fmt.Println("addFunc")
if vsc, ok := obj.(*dubboConfig.VirtualServiceConfig); ok {
fmt.Printf("in add func: get asserted VirtualServiceConfig = %+v\n", *vsc)
event := &config_center.ConfigChangeEvent{
Key: VirtualServiceEventKey,
Value: vsc,
ConfigType: remoting.EventTypeAdd,
}
r.listener.Process(event)
}
}
func (r *VirtualServiceListenerHandler) UpdateFunc(oldObj, newObj interface{}) {
fmt.Println("update func = ")
if vsc, ok := newObj.(*dubboConfig.VirtualServiceConfig); ok {
event := &config_center.ConfigChangeEvent{
Key: VirtualServiceEventKey,
Value: vsc,
ConfigType: remoting.EventTypeUpdate,
}
r.listener.Process(event)
}
}
func (r *VirtualServiceListenerHandler) DeleteFunc(obj interface{}) {
if vsc, ok := obj.(*dubboConfig.VirtualServiceConfig); ok {
event := &config_center.ConfigChangeEvent{
Key: VirtualServiceEventKey,
Value: vsc,
ConfigType: remoting.EventTypeDel,
}
r.listener.Process(event)
}
}
func (r *VirtualServiceListenerHandler) Watch(opts metav.ListOptions, restClient *rest.RESTClient, ns string) (watch.Interface, error) {
fmt.Println("Call Watch")
opts.Watch = true
return restClient.
Get().
Namespace(ns).
Resource(VirtualServiceResource).
VersionedParams(&opts, scheme.ParameterCodec).
Watch()
}
func (r *VirtualServiceListenerHandler) List(opts metav.ListOptions, restClient *rest.RESTClient, ns string) (runtime.Object, error) {
fmt.Println("Call List")
result := dubboConfig.VirtualServiceConfigList{}
err := restClient.
Get().
Namespace(ns).
Resource(VirtualServiceResource).
VersionedParams(&opts, scheme.ParameterCodec).
Do().
Into(&result)
return &result, err
}
func (r *VirtualServiceListenerHandler) GetObject() runtime.Object {
return &dubboConfig.VirtualServiceConfig{}
}
func newVirtualServiceListenerHandler(listener config_center.ConfigurationListener) k8sCRD.ListenerHandler {
return &VirtualServiceListenerHandler{
listener: listener,
}
}
type DestRuleListenerHandler struct {
listener config_center.ConfigurationListener
}
func (r *DestRuleListenerHandler) AddFunc(obj interface{}) {
fmt.Println("dest rule addFunc")
if drc, ok := obj.(*dubboConfig.DestinationRuleConfig); ok {
event := &config_center.ConfigChangeEvent{
Key: DestinationRuleEventKey,
Value: drc,
ConfigType: remoting.EventTypeAdd,
}
r.listener.Process(event)
}
}
func (r *DestRuleListenerHandler) UpdateFunc(oldObj, newObj interface{}) {
fmt.Println("dest rule update func = ")
if drc, ok := newObj.(*dubboConfig.DestinationRuleConfig); ok {
event := &config_center.ConfigChangeEvent{
Key: DestinationRuleEventKey,
Value: drc,
ConfigType: remoting.EventTypeUpdate,
}
r.listener.Process(event)
}
}
func (r *DestRuleListenerHandler) DeleteFunc(obj interface{}) {
if drc, ok := obj.(*dubboConfig.DestinationRuleConfig); ok {
event := &config_center.ConfigChangeEvent{
Key: DestinationRuleEventKey,
Value: drc,
ConfigType: remoting.EventTypeDel,
}
r.listener.Process(event)
}
}
func (r *DestRuleListenerHandler) Watch(opts metav.ListOptions, restClient *rest.RESTClient, ns string) (watch.Interface, error) {
fmt.Println("dest rule Call Watch")
opts.Watch = true
return restClient.
Get().
Namespace(ns).
Resource(DestRuleResource).
VersionedParams(&opts, scheme.ParameterCodec).
Watch()
}
func (r *DestRuleListenerHandler) List(opts metav.ListOptions, restClient *rest.RESTClient, ns string) (runtime.Object, error) {
fmt.Println("Call List")
result := dubboConfig.DestinationRuleConfigList{}
err := restClient.
Get().
Namespace(ns).
Resource(DestRuleResource).
VersionedParams(&opts, scheme.ParameterCodec).
Do().
Into(&result)
return &result, err
}
func (r *DestRuleListenerHandler) GetObject() runtime.Object {
return &dubboConfig.DestinationRuleConfig{}
}
func newDestRuleListenerHandler(listener config_center.ConfigurationListener) k8sCRD.ListenerHandler {
return &DestRuleListenerHandler{
listener: listener,
}
}
/*
* 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 match_judger
import (
"github.com/apache/dubbo-go/config"
"github.com/apache/dubbo-go/protocol"
)
type AttachmentMatchJudger struct {
config.DubboAttachmentMatch
}
func (amj *AttachmentMatchJudger) Judge(invocation protocol.Invocation) bool {
invAttaMap := invocation.Attachments()
if amj.EagleeyeContext != nil {
for k, v := range amj.EagleeyeContext {
invAttaValue, ok := invAttaMap[k]
if !ok {
if v.Empty == "" {
return false
}
}
// exist this key
str, ok := invAttaValue.(string)
if !ok {
return false
}
strJudger := NewStringMatchJudger(v)
if !strJudger.Judge(str) {
return false
}
}
}
if amj.DubboContext != nil {
for k, v := range amj.DubboContext {
invAttaValue, ok := invAttaMap[k]
if !ok {
if v.Empty == "" {
return false
}
}
// exist this key
str, ok := invAttaValue.(string)
if !ok {
return false
}
strJudger := NewStringMatchJudger(v)
if !strJudger.Judge(str) {
return false
}
}
}
return true
}
func NewAttachmentMatchJudger(matchConf *config.DubboAttachmentMatch) *AttachmentMatchJudger {
return &AttachmentMatchJudger{
DubboAttachmentMatch: *matchConf,
}
}
package match_judger
import (
"github.com/apache/dubbo-go/config"
"github.com/apache/dubbo-go/protocol/invocation"
"github.com/stretchr/testify/assert"
"testing"
)
func TestAttachmentMatchJudger(t *testing.T) {
dubboCtxMap := make(map[string]*config.StringMatch)
dubboIvkMap := make(map[string]interface{})
dubboCtxMap["test-key"] = &config.StringMatch{
Exact: "abc",
}
dubboIvkMap["test-key"] = "abc"
assert.True(t, NewAttachmentMatchJudger(&config.DubboAttachmentMatch{
DubboContext: dubboCtxMap,
}).Judge(invocation.NewRPCInvocation("method", nil, dubboIvkMap)))
dubboIvkMap["test-key"] = "abd"
assert.False(t, NewAttachmentMatchJudger(&config.DubboAttachmentMatch{
DubboContext: dubboCtxMap,
}).Judge(invocation.NewRPCInvocation("method", nil, dubboIvkMap)))
}
/*
* 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 match_judger
import "github.com/apache/dubbo-go/config"
type BoolMatchJudger struct {
config.BoolMatch
}
func (lsmj *BoolMatchJudger) Judge(input bool) bool {
return input == lsmj.Exact
}
func newBoolMatchJudger(matchConf *config.BoolMatch) *BoolMatchJudger {
return &BoolMatchJudger{
BoolMatch: *matchConf,
}
}
/*
* 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 match_judger
import (
"github.com/apache/dubbo-go/config"
"github.com/stretchr/testify/assert"
"testing"
)
func TestBoolMatchJudger(t *testing.T) {
assert.True(t, newBoolMatchJudger(&config.BoolMatch{
Exact: true,
}).Judge(true))
assert.True(t, newBoolMatchJudger(&config.BoolMatch{
Exact: false,
}).Judge(false))
assert.False(t, newBoolMatchJudger(&config.BoolMatch{
Exact: true,
}).Judge(false))
assert.False(t, newBoolMatchJudger(&config.BoolMatch{
Exact: false,
}).Judge(true))
}
/*
* 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 match_judger
import (
"github.com/apache/dubbo-go/config"
)
type DoubleMatchJudger struct {
config.DoubleMatch
}
func (dmj *DoubleMatchJudger) Judge(input float64) bool {
if dmj.Exact != 0 {
return input == dmj.Exact
}
if dmj.Range != nil {
return newDoubleRangeMatchJudger(dmj.Range).Judge(input)
}
// todo mod match ??
//if dmj.Mode != 0 {
//
//}
return true
}
func newDoubleMatchJudger(matchConf *config.DoubleMatch) *DoubleMatchJudger {
return &DoubleMatchJudger{
DoubleMatch: *matchConf,
}
}
/*
* 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 match_judger
import (
"github.com/apache/dubbo-go/config"
"github.com/stretchr/testify/assert"
"testing"
)
func TestDoubleMatchJudger(t *testing.T) {
assert.True(t, newDoubleMatchJudger(&config.DoubleMatch{
Exact: 3.14159,
}).Judge(3.14159))
assert.False(t, newDoubleMatchJudger(&config.DoubleMatch{
Exact: 3.14159,
}).Judge(3.14155927))
assert.True(t, newDoubleMatchJudger(&config.DoubleMatch{
Range: &config.DoubleRangeMatch{
Start: 1.0,
End: 1.5,
},
}).Judge(1.3))
assert.False(t, newDoubleMatchJudger(&config.DoubleMatch{
Range: &config.DoubleRangeMatch{
Start: 1.0,
End: 1.5,
},
}).Judge(1.9))
assert.False(t, newDoubleMatchJudger(&config.DoubleMatch{
Range: &config.DoubleRangeMatch{
Start: 1.0,
End: 1.5,
},
}).Judge(0.9))
}
/*
* 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 match_judger
import (
"github.com/apache/dubbo-go/config"
)
type DoubleRangeMatchJudger struct {
config.DoubleRangeMatch
}
func (drmj *DoubleRangeMatchJudger) Judge(input float64) bool {
return input >= drmj.Start && input < drmj.End
}
func newDoubleRangeMatchJudger(matchConf *config.DoubleRangeMatch) *DoubleRangeMatchJudger {
return &DoubleRangeMatchJudger{
DoubleRangeMatch: *matchConf,
}
}
/*
* 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 match_judger
import (
"github.com/apache/dubbo-go/config"
"github.com/stretchr/testify/assert"
"testing"
)
func TestDoubleRangeMatchJudger(t *testing.T) {
assert.True(t, newDoubleRangeMatchJudger(&config.DoubleRangeMatch{
Start: 1.0,
End: 1.5,
}).Judge(1.3))
assert.False(t, newDoubleRangeMatchJudger(&config.DoubleRangeMatch{
Start: 1.0,
End: 1.5,
}).Judge(1.9))
assert.False(t, newDoubleRangeMatchJudger(&config.DoubleRangeMatch{
Start: 1.0,
End: 1.5,
}).Judge(0.9))
}
/*
* 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 match_judger
import (
"github.com/apache/dubbo-go/config"
)
type ListDoubleMatchJudger struct {
config.ListDoubleMatch
}
func (lsmj *ListDoubleMatchJudger) Judge(input float64) bool {
for _, v := range lsmj.Oneof {
if newDoubleMatchJudger(v).Judge(input) {
return true
}
}
return false
}
func newListDoubleMatchJudger(matchConf *config.ListDoubleMatch) *ListDoubleMatchJudger {
return &ListDoubleMatchJudger{
ListDoubleMatch: *matchConf,
}
}
/*
* 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 match_judger
import (
"github.com/apache/dubbo-go/config"
"github.com/stretchr/testify/assert"
"testing"
)
func TestListDoubleMatchJudger_Judge(t *testing.T) {
assert.True(t, newListDoubleMatchJudger(&config.ListDoubleMatch{
Oneof: []*config.DoubleMatch{
{
Exact: 3.14,
},
{
Range: &config.DoubleRangeMatch{
Start: 1.5,
End: 1.9,
},
},
{
Exact: 1.3,
},
},
}).Judge(1.3))
assert.False(t, newListDoubleMatchJudger(&config.ListDoubleMatch{
Oneof: []*config.DoubleMatch{
{
Exact: 3.14,
},
{
Range: &config.DoubleRangeMatch{
Start: 1.5,
End: 1.9,
},
},
{
Exact: 1.2,
},
},
}).Judge(1.3))
assert.True(t, newListDoubleMatchJudger(&config.ListDoubleMatch{
Oneof: []*config.DoubleMatch{
{
Exact: 3.14,
},
{
Range: &config.DoubleRangeMatch{
Start: 1.2,
End: 1.9,
},
},
{
Exact: 1.4,
},
},
}).Judge(1.3))
assert.False(t, newListDoubleMatchJudger(&config.ListDoubleMatch{
Oneof: []*config.DoubleMatch{
{
Exact: 3.14,
},
{
Range: &config.DoubleRangeMatch{
Start: 1.5,
End: 1.9,
},
},
{
Exact: 1.0,
},
},
}).Judge(1.3))
}
/*
* 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 match_judger
import (
"github.com/apache/dubbo-go/config"
)
type ListStringMatchJudger struct {
config.ListStringMatch
}
func (lsmj *ListStringMatchJudger) Judge(input string) bool {
for _, v := range lsmj.Oneof {
if NewStringMatchJudger(v).Judge(input) {
return true
}
}
return false
}
func newListStringMatchJudger(matchConf *config.ListStringMatch) *ListStringMatchJudger {
return &ListStringMatchJudger{
ListStringMatch: *matchConf,
}
}
/*
* 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 match_judger
import (
"github.com/apache/dubbo-go/config"
"github.com/apache/dubbo-go/protocol"
)
type MethodMatchJudger struct {
config.DubboMethodMatch
}
// Judge Method Match Judger only judge on
func (mmj *MethodMatchJudger) Judge(invocation protocol.Invocation) bool {
if mmj.NameMatch != nil {
strJudger := NewStringMatchJudger(mmj.NameMatch)
if !strJudger.Judge(invocation.MethodName()) {
return false
}
}
// todo now argc Must not be zero, else it will cause unexpected result
if mmj.Argc != 0 && len(invocation.ParameterValues()) != mmj.Argc {
return false
}
if mmj.Args != nil {
params := invocation.ParameterValues()
for _, v := range mmj.Args {
index := int(v.Index)
if index > len(params) || index < 1 {
return false
}
value := params[index-1]
if value.Type().String() != v.Type {
return false
}
switch v.Type {
case "string":
if !newListStringMatchJudger(v.StrValue).Judge(value.String()) {
return false
}
case "float", "int":
// todo now numbers Must not be zero, else it will ignore this match
if !newListDoubleMatchJudger(v.NumValue).Judge(value.Float()) {
return false
}
case "bool":
if !newBoolMatchJudger(v.BoolValue).Judge(value.Bool()) {
return false
}
default:
}
}
}
// todo Argp match judge ??? conflict to args?
//if mmj.Argp != nil {
//
//}
// todo Headers match judge: reserve for triple
//if mmj.Headers != nil {
//
//}
return true
}
func NewMethodMatchJudger(matchConf *config.DubboMethodMatch) *MethodMatchJudger {
return &MethodMatchJudger{
DubboMethodMatch: *matchConf,
}
}
/*
* 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 match_judger
import (
"regexp"
"strings"
)
import (
"github.com/apache/dubbo-go/config"
)
type StringMatchJudger struct {
config.StringMatch
}
func (smj *StringMatchJudger) Judge(input string) bool {
if smj.Exact != "" {
return input == smj.Exact
}
if smj.Prefix != "" {
return strings.HasPrefix(input, smj.Prefix)
}
if smj.Regex != "" {
ok, err := regexp.MatchString(smj.Regex, input)
return ok && err == nil
}
if smj.NoEmpty != "" {
return input != ""
}
if smj.Empty != "" {
return input == ""
}
return true
}
func NewStringMatchJudger(matchConf *config.StringMatch) *StringMatchJudger {
return &StringMatchJudger{
StringMatch: *matchConf,
}
}
/*
* 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 match_judger
import (
"github.com/apache/dubbo-go/config"
"github.com/stretchr/testify/assert"
"testing"
)
func TestNewStringMatchJudger(t *testing.T) {
assert.True(t, NewStringMatchJudger(&config.StringMatch{
Exact: "abc",
}).Judge("abc"))
assert.False(t, NewStringMatchJudger(&config.StringMatch{
Exact: "abcd",
}).Judge("abc"))
assert.True(t, NewStringMatchJudger(&config.StringMatch{
Prefix: "abc",
}).Judge("abcd"))
assert.False(t, NewStringMatchJudger(&config.StringMatch{
Exact: "abcd",
}).Judge("abdc"))
assert.True(t, NewStringMatchJudger(&config.StringMatch{
Empty: "true",
}).Judge(""))
assert.False(t, NewStringMatchJudger(&config.StringMatch{
NoEmpty: "true",
}).Judge(""))
}
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