dubbo.apache.org/dubbo-go/v3@v3.1.1/cluster/router/condition/matcher/argument.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 matcher
    19  
    20  import (
    21  	"fmt"
    22  	"regexp"
    23  	"strconv"
    24  	"strings"
    25  )
    26  
    27  import (
    28  	"github.com/dubbogo/gost/log/logger"
    29  )
    30  
    31  import (
    32  	"dubbo.apache.org/dubbo-go/v3/common"
    33  	"dubbo.apache.org/dubbo-go/v3/protocol"
    34  )
    35  
    36  var (
    37  	argumentsPattern      = regexp.MustCompile("arguments\\[([0-9]+)\\]")
    38  	notFoundArgumentValue = "dubbo internal not found argument condition value"
    39  )
    40  
    41  // ArgumentConditionMatcher analysis the arguments in the rule.
    42  // Examples would be like this:
    43  // "arguments[0]=1", whenCondition is that the first argument is equal to '1'.
    44  // "arguments[1]=a", whenCondition is that the second argument is equal to 'a'.
    45  type ArgumentConditionMatcher struct {
    46  	BaseConditionMatcher
    47  }
    48  
    49  func NewArgumentConditionMatcher(key string) *ArgumentConditionMatcher {
    50  	return &ArgumentConditionMatcher{
    51  		*NewBaseConditionMatcher(key),
    52  	}
    53  }
    54  
    55  func (a *ArgumentConditionMatcher) GetValue(sample map[string]string, url *common.URL, invocation protocol.Invocation) string {
    56  	// split the rule
    57  	expressArray := strings.Split(a.key, "\\.")
    58  	argumentExpress := expressArray[0]
    59  	matcher := argumentsPattern.FindStringSubmatch(argumentExpress)
    60  	if len(matcher) == 0 {
    61  		logger.Warn(notFoundArgumentValue)
    62  		return ""
    63  	}
    64  
    65  	// extract the argument index
    66  	index, err := strconv.Atoi(matcher[1])
    67  	if err != nil {
    68  		logger.Warn(notFoundArgumentValue)
    69  		return ""
    70  	}
    71  
    72  	if index < 0 || index > len(invocation.Arguments()) {
    73  		logger.Warn(notFoundArgumentValue)
    74  		return ""
    75  	}
    76  	return fmt.Sprint(invocation.Arguments()[index])
    77  }