dubbo.apache.org/dubbo-go/v3@v3.1.1/cluster/router/condition/matcher/factory.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  	"math"
    22  	"strings"
    23  )
    24  
    25  import (
    26  	"dubbo.apache.org/dubbo-go/v3/common/constant"
    27  )
    28  
    29  func init() {
    30  	SetMatcherFactory(constant.Arguments, NewArgumentMatcherFactory)
    31  	SetMatcherFactory(constant.Attachments, NewAttachmentMatcherFactory)
    32  	SetMatcherFactory(constant.Param, NewParamMatcherFactory)
    33  }
    34  
    35  // ArgumentMatcherFactory matcher factory
    36  type ArgumentMatcherFactory struct {
    37  }
    38  
    39  // NewArgumentMatcherFactory constructs a new argument.ArgumentMatcherFactory
    40  func NewArgumentMatcherFactory() ConditionMatcherFactory {
    41  	return &ArgumentMatcherFactory{}
    42  }
    43  
    44  func (a *ArgumentMatcherFactory) ShouldMatch(key string) bool {
    45  	return strings.HasPrefix(key, constant.Arguments)
    46  }
    47  
    48  // NewMatcher constructs a new matcher
    49  func (a *ArgumentMatcherFactory) NewMatcher(key string) Matcher {
    50  	return NewArgumentConditionMatcher(key)
    51  }
    52  
    53  func (a *ArgumentMatcherFactory) Priority() int64 {
    54  	return 300
    55  }
    56  
    57  // AttachmentMatcherFactory matcher factory
    58  type AttachmentMatcherFactory struct {
    59  }
    60  
    61  // NewAttachmentMatcherFactory constructs a new attachment.AttachmentMatcherFactory
    62  func NewAttachmentMatcherFactory() ConditionMatcherFactory {
    63  	return &AttachmentMatcherFactory{}
    64  }
    65  
    66  func (a *AttachmentMatcherFactory) ShouldMatch(key string) bool {
    67  	return strings.HasPrefix(key, constant.Attachments)
    68  }
    69  
    70  // NewMatcher constructs a new matcher
    71  func (a *AttachmentMatcherFactory) NewMatcher(key string) Matcher {
    72  	return NewAttachmentConditionMatcher(key)
    73  }
    74  
    75  func (a *AttachmentMatcherFactory) Priority() int64 {
    76  	return 200
    77  }
    78  
    79  // ParamMatcherFactory matcher factory
    80  type ParamMatcherFactory struct {
    81  }
    82  
    83  // NewParamMatcherFactory constructs a new paramMatcherFactory
    84  func NewParamMatcherFactory() ConditionMatcherFactory {
    85  	return &ParamMatcherFactory{}
    86  }
    87  
    88  func (p *ParamMatcherFactory) ShouldMatch(key string) bool {
    89  	return true
    90  }
    91  
    92  // NewMatcher constructs a new matcher
    93  func (p *ParamMatcherFactory) NewMatcher(key string) Matcher {
    94  	return NewParamConditionMatcher(key)
    95  }
    96  
    97  // Priority make sure this is the last matcher being executed.
    98  // This instance will be loaded separately to ensure it always gets executed as the last matcher.
    99  func (p *ParamMatcherFactory) Priority() int64 {
   100  	return math.MaxInt64
   101  }