dubbo.apache.org/dubbo-go/v3@v3.1.1/cluster/router/condition/matcher/pattern_value/wildcard.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package pattern_value
    19  
    20  import (
    21  	"math"
    22  	"strings"
    23  )
    24  
    25  import (
    26  	"dubbo.apache.org/dubbo-go/v3/common"
    27  	"dubbo.apache.org/dubbo-go/v3/common/constant"
    28  	"dubbo.apache.org/dubbo-go/v3/protocol"
    29  )
    30  
    31  // WildcardValuePattern evaluator must be the last one being executed.
    32  // matches with patterns like 'key=hello', 'key=hello*', 'key=*hello', 'key=h*o' or 'key=*'
    33  type WildcardValuePattern struct {
    34  }
    35  
    36  func init() {
    37  	SetValuePattern(constant.Wildcard, NewWildcardValuePattern)
    38  }
    39  
    40  func NewWildcardValuePattern() ValuePattern {
    41  	return &WildcardValuePattern{}
    42  }
    43  
    44  func (w *WildcardValuePattern) Priority() int64 {
    45  	return math.MaxInt64
    46  }
    47  
    48  func (w *WildcardValuePattern) ShouldMatch(pattern string) bool {
    49  	return true
    50  }
    51  
    52  func (w *WildcardValuePattern) Match(pattern string, value string, url *common.URL, invocation protocol.Invocation, isWhenCondition bool) bool {
    53  	if url != nil && strings.HasPrefix(pattern, "$") {
    54  		pattern = url.GetRawParam(pattern[1:])
    55  	}
    56  
    57  	if "*" == pattern {
    58  		return true
    59  	}
    60  	if pattern == "" && value == "" {
    61  		return true
    62  	}
    63  	if pattern == "" || value == "" {
    64  		return false
    65  	}
    66  
    67  	i := strings.LastIndex(pattern, "*")
    68  	// doesn't find "*"
    69  	if i == -1 {
    70  		return value == pattern
    71  	} else if i == len(pattern)-1 {
    72  		// "*" is at the end
    73  		return strings.HasPrefix(value, pattern[0:i])
    74  	} else if i == 0 {
    75  		// "*" is at the beginning
    76  		return strings.HasSuffix(value, pattern[i+1:])
    77  	} else {
    78  		// "*" is in the middle
    79  		prefix := pattern[0:i]
    80  		suffix := pattern[i+1:]
    81  		return strings.HasPrefix(value, prefix) && strings.HasSuffix(value, suffix)
    82  	}
    83  }