dubbo.apache.org/dubbo-go/v3@v3.1.1/config/custom_config.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 config
    19  
    20  import (
    21  	"github.com/creasty/defaults"
    22  )
    23  
    24  import (
    25  	"dubbo.apache.org/dubbo-go/v3/common/constant"
    26  )
    27  
    28  type CustomConfig struct {
    29  	ConfigMap map[string]interface{} `yaml:"config-map" json:"config-map,omitempty" property:"config-map"`
    30  }
    31  
    32  func (*CustomConfig) Prefix() string {
    33  	return constant.CustomConfigPrefix
    34  }
    35  
    36  func (c *CustomConfig) Init() error {
    37  	return c.check()
    38  }
    39  
    40  func (c *CustomConfig) check() error {
    41  	if err := defaults.Set(c); err != nil {
    42  		return err
    43  	}
    44  	return verify(c)
    45  }
    46  
    47  func (c *CustomConfig) GetDefineValue(key string, default_value interface{}) interface{} {
    48  	if define_value, ok := c.ConfigMap[key]; ok {
    49  		return define_value
    50  	}
    51  	return default_value
    52  }
    53  
    54  func GetDefineValue(key string, default_value interface{}) interface{} {
    55  	rt := GetRootConfig()
    56  	if rt.Custom == nil {
    57  		return default_value
    58  	}
    59  	return rt.Custom.GetDefineValue(key, default_value)
    60  }
    61  
    62  type CustomConfigBuilder struct {
    63  	customConfig *CustomConfig
    64  }
    65  
    66  func NewCustomConfigBuilder() *CustomConfigBuilder {
    67  	return &CustomConfigBuilder{customConfig: &CustomConfig{}}
    68  }
    69  
    70  func (ccb *CustomConfigBuilder) SetDefineConfig(key string, val interface{}) *CustomConfigBuilder {
    71  	if ccb.customConfig.ConfigMap == nil {
    72  		ccb.customConfig.ConfigMap = make(map[string]interface{})
    73  	}
    74  	ccb.customConfig.ConfigMap[key] = val
    75  	return ccb
    76  }
    77  
    78  func (ccb *CustomConfigBuilder) Build() *CustomConfig {
    79  	return ccb.customConfig
    80  }