github.com/safing/portbase@v0.19.5/config/get.go (about) 1 package config 2 3 import ( 4 "github.com/safing/portbase/log" 5 ) 6 7 type ( 8 // StringOption defines the returned function by GetAsString. 9 StringOption func() string 10 // StringArrayOption defines the returned function by GetAsStringArray. 11 StringArrayOption func() []string 12 // IntOption defines the returned function by GetAsInt. 13 IntOption func() int64 14 // BoolOption defines the returned function by GetAsBool. 15 BoolOption func() bool 16 ) 17 18 func getValueCache(name string, option *Option, requestedType OptionType) (*Option, *valueCache) { 19 // get option 20 if option == nil { 21 var err error 22 option, err = GetOption(name) 23 if err != nil { 24 log.Errorf("config: request for unregistered option: %s", name) 25 return nil, nil 26 } 27 } 28 29 // Check the option type, no locking required as 30 // OptType is immutable once it is set 31 if requestedType != option.OptType { 32 log.Errorf("config: bad type: requested %s as %s, but is %s", name, getTypeName(requestedType), getTypeName(option.OptType)) 33 return option, nil 34 } 35 36 option.Lock() 37 defer option.Unlock() 38 39 // check release level 40 if option.ReleaseLevel <= getReleaseLevel() && option.activeValue != nil { 41 return option, option.activeValue 42 } 43 44 if option.activeDefaultValue != nil { 45 return option, option.activeDefaultValue 46 } 47 48 return option, option.activeFallbackValue 49 } 50 51 // GetAsString returns a function that returns the wanted string with high performance. 52 func GetAsString(name string, fallback string) StringOption { 53 valid := getValidityFlag() 54 option, valueCache := getValueCache(name, nil, OptTypeString) 55 value := fallback 56 if valueCache != nil { 57 value = valueCache.stringVal 58 } 59 60 return func() string { 61 if !valid.IsSet() { 62 valid = getValidityFlag() 63 option, valueCache = getValueCache(name, option, OptTypeString) 64 if valueCache != nil { 65 value = valueCache.stringVal 66 } else { 67 value = fallback 68 } 69 } 70 return value 71 } 72 } 73 74 // GetAsStringArray returns a function that returns the wanted string with high performance. 75 func GetAsStringArray(name string, fallback []string) StringArrayOption { 76 valid := getValidityFlag() 77 option, valueCache := getValueCache(name, nil, OptTypeStringArray) 78 value := fallback 79 if valueCache != nil { 80 value = valueCache.stringArrayVal 81 } 82 83 return func() []string { 84 if !valid.IsSet() { 85 valid = getValidityFlag() 86 option, valueCache = getValueCache(name, option, OptTypeStringArray) 87 if valueCache != nil { 88 value = valueCache.stringArrayVal 89 } else { 90 value = fallback 91 } 92 } 93 return value 94 } 95 } 96 97 // GetAsInt returns a function that returns the wanted int with high performance. 98 func GetAsInt(name string, fallback int64) IntOption { 99 valid := getValidityFlag() 100 option, valueCache := getValueCache(name, nil, OptTypeInt) 101 value := fallback 102 if valueCache != nil { 103 value = valueCache.intVal 104 } 105 106 return func() int64 { 107 if !valid.IsSet() { 108 valid = getValidityFlag() 109 option, valueCache = getValueCache(name, option, OptTypeInt) 110 if valueCache != nil { 111 value = valueCache.intVal 112 } else { 113 value = fallback 114 } 115 } 116 return value 117 } 118 } 119 120 // GetAsBool returns a function that returns the wanted int with high performance. 121 func GetAsBool(name string, fallback bool) BoolOption { 122 valid := getValidityFlag() 123 option, valueCache := getValueCache(name, nil, OptTypeBool) 124 value := fallback 125 if valueCache != nil { 126 value = valueCache.boolVal 127 } 128 129 return func() bool { 130 if !valid.IsSet() { 131 valid = getValidityFlag() 132 option, valueCache = getValueCache(name, option, OptTypeBool) 133 if valueCache != nil { 134 value = valueCache.boolVal 135 } else { 136 value = fallback 137 } 138 } 139 return value 140 } 141 } 142 143 /* 144 func getAndFindValue(key string) interface{} { 145 optionsLock.RLock() 146 option, ok := options[key] 147 optionsLock.RUnlock() 148 if !ok { 149 log.Errorf("config: request for unregistered option: %s", key) 150 return nil 151 } 152 153 return option.findValue() 154 } 155 */ 156 157 /* 158 // findValue finds the preferred value in the user or default config. 159 func (option *Option) findValue() interface{} { 160 // lock option 161 option.Lock() 162 defer option.Unlock() 163 164 if option.ReleaseLevel <= getReleaseLevel() && option.activeValue != nil { 165 return option.activeValue 166 } 167 168 if option.activeDefaultValue != nil { 169 return option.activeDefaultValue 170 } 171 172 return option.DefaultValue 173 } 174 */