dubbo.apache.org/dubbo-go/v3@v3.1.1/common/extension/configurator.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 extension
    19  
    20  import (
    21  	"dubbo.apache.org/dubbo-go/v3/common"
    22  	"dubbo.apache.org/dubbo-go/v3/config_center"
    23  )
    24  
    25  const (
    26  	// DefaultKey for default Configurator
    27  	DefaultKey = "default"
    28  )
    29  
    30  type getConfiguratorFunc func(url *common.URL) config_center.Configurator
    31  
    32  var configurator = make(map[string]getConfiguratorFunc)
    33  
    34  // SetConfigurator sets the getConfiguratorFunc with @name
    35  func SetConfigurator(name string, v getConfiguratorFunc) {
    36  	configurator[name] = v
    37  }
    38  
    39  // GetConfigurator finds the Configurator with @name
    40  func GetConfigurator(name string, url *common.URL) config_center.Configurator {
    41  	if configurator[name] == nil {
    42  		panic("configurator for " + name + " is not existing, make sure you have import the package.")
    43  	}
    44  	return configurator[name](url)
    45  }
    46  
    47  // SetDefaultConfigurator sets the default Configurator
    48  func SetDefaultConfigurator(v getConfiguratorFunc) {
    49  	configurator[DefaultKey] = v
    50  }
    51  
    52  // GetDefaultConfigurator gets default configurator
    53  func GetDefaultConfigurator(url *common.URL) config_center.Configurator {
    54  	if configurator[DefaultKey] == nil {
    55  		panic("configurator for default is not existing, make sure you have import the package.")
    56  	}
    57  	return configurator[DefaultKey](url)
    58  }
    59  
    60  // GetDefaultConfiguratorFunc gets default configurator function
    61  func GetDefaultConfiguratorFunc() getConfiguratorFunc {
    62  	if configurator[DefaultKey] == nil {
    63  		panic("configurator for default is not existing, make sure you have import the package.")
    64  	}
    65  	return configurator[DefaultKey]
    66  }