github.com/cloudwego/kitex@v0.9.0/internal/configutil/config.go (about) 1 /* 2 * Copyright 2021 CloudWeGo Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package configutil 18 19 import "time" 20 21 // Config provides access to configurations. 22 type Config interface { 23 Get(key string) (val interface{}, ok bool) 24 } 25 26 // Container manages multiple config sources with priorities. 27 type Container interface { 28 Config 29 AddPriorSource(src Config) error 30 } 31 32 // DefaultConfig provides access to configurations. 33 type DefaultConfig interface { 34 Get(key string, def interface{}) interface{} 35 } 36 37 // RichTypeConfig provides typed get functions. 38 type RichTypeConfig interface { 39 GetBool(key string) (val, ok bool) 40 GetInt(key string) (val int, ok bool) 41 GetString(key string) (val string, ok bool) 42 GetInt64(key string) (val int64, ok bool) 43 GetFloat(key string) (val float64, ok bool) 44 GetDuration(key string) (val time.Duration, ok bool) 45 } 46 47 // RichTypeDefaultConfig provides typed get functions with default value support. 48 type RichTypeDefaultConfig interface { 49 GetBool(key string, def bool) bool 50 GetInt(key string, def int) int 51 GetString(key, def string) string 52 GetInt64(key string, def int64) int64 53 GetFloat(key string, def float64) float64 54 GetDuration(key string, def time.Duration) time.Duration 55 } 56 57 // dummyConfig is a dummy config source. 58 type dummyConfig struct{} 59 60 func (dc *dummyConfig) Get(key string) (val interface{}, ok bool) { return } 61 62 // NewDummyConfig creates a dummy config. 63 func NewDummyConfig() Config { 64 return &dummyConfig{} 65 } 66 67 // configs implements Config and Container interfaces. 68 type configs struct { 69 sources []Config 70 } 71 72 // NewConfigContainer creates an empty Container. 73 func NewConfigContainer() Container { 74 return &configs{} 75 } 76 77 func (cc *configs) Get(key string) (val interface{}, ok bool) { 78 for i := len(cc.sources); i > 0; i-- { 79 src := cc.sources[i-1] 80 if val, ok = src.Get(key); ok { 81 break 82 } 83 } 84 return 85 } 86 87 func (cc *configs) AddPriorSource(src Config) error { 88 cc.sources = append(cc.sources, src) 89 return nil 90 } 91 92 // defaultConfig wraps a Config to implement DefaultConfig. 93 type defaultConfig struct { 94 Config 95 } 96 97 // NewDefaultConfig creates a DefaultConfig with the given Config. 98 func NewDefaultConfig(c Config) DefaultConfig { 99 return &defaultConfig{c} 100 } 101 102 func (dc *defaultConfig) Get(key string, def interface{}) interface{} { 103 if val, exist := dc.Config.Get(key); exist { 104 return val 105 } 106 return def 107 } 108 109 // richTypeConfig wraps a Config to implement RichTypeConfig. 110 type richTypeConfig struct { 111 DefaultConfig 112 } 113 114 // NewRichTypeConfig creates a RichTypeConfig with the given Config. 115 func NewRichTypeConfig(c Config) RichTypeConfig { 116 return &richTypeConfig{NewDefaultConfig(c)} 117 } 118 119 func (rd *richTypeConfig) GetBool(key string) (val, ok bool) { 120 val, ok = rd.DefaultConfig.Get(key, nil).(bool) 121 return 122 } 123 124 func (rd *richTypeConfig) GetInt(key string) (val int, ok bool) { 125 val, ok = rd.DefaultConfig.Get(key, nil).(int) 126 return 127 } 128 129 func (rd *richTypeConfig) GetString(key string) (val string, ok bool) { 130 val, ok = rd.DefaultConfig.Get(key, nil).(string) 131 return 132 } 133 134 func (rd *richTypeConfig) GetInt64(key string) (val int64, ok bool) { 135 if val, ok = rd.DefaultConfig.Get(key, nil).(int64); ok { 136 return 137 } 138 if i32, ok := rd.DefaultConfig.Get(key, nil).(int); ok { 139 return int64(i32), ok 140 } 141 return 142 } 143 144 func (rd *richTypeConfig) GetFloat(key string) (val float64, ok bool) { 145 val, ok = rd.DefaultConfig.Get(key, nil).(float64) 146 return 147 } 148 149 func (rd *richTypeConfig) GetDuration(key string) (val time.Duration, ok bool) { 150 val, ok = rd.DefaultConfig.Get(key, nil).(time.Duration) 151 return 152 } 153 154 // richTypeDefaultConfig wraps a RichTypeConfig to implement RichTypeDefaultConfig. 155 type richTypeDefaultConfig struct { 156 RichTypeConfig 157 } 158 159 // NewRichTypeDefaultConfig creates a RichTypeDefaultConfig with the given RichTypeConfig. 160 func NewRichTypeDefaultConfig(rtc RichTypeConfig) RichTypeDefaultConfig { 161 return &richTypeDefaultConfig{rtc} 162 } 163 164 func (rd *richTypeDefaultConfig) GetBool(key string, def bool) bool { 165 if val, exist := rd.RichTypeConfig.GetBool(key); exist { 166 return val 167 } 168 return def 169 } 170 171 func (rd *richTypeDefaultConfig) GetInt(key string, def int) int { 172 if val, exist := rd.RichTypeConfig.GetInt(key); exist { 173 return val 174 } 175 return def 176 } 177 178 func (rd *richTypeDefaultConfig) GetString(key, def string) string { 179 if val, exist := rd.RichTypeConfig.GetString(key); exist { 180 return val 181 } 182 return def 183 } 184 185 func (rd *richTypeDefaultConfig) GetInt64(key string, def int64) int64 { 186 if val, exist := rd.RichTypeConfig.GetInt64(key); exist { 187 return val 188 } 189 return def 190 } 191 192 func (rd *richTypeDefaultConfig) GetFloat(key string, def float64) float64 { 193 if val, exist := rd.RichTypeConfig.GetFloat(key); exist { 194 return val 195 } 196 return def 197 } 198 199 func (rd *richTypeDefaultConfig) GetDuration(key string, def time.Duration) time.Duration { 200 if val, exist := rd.RichTypeConfig.GetDuration(key); exist { 201 return val 202 } 203 return def 204 }