github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/utils/config/map_config.go (about) 1 // Copyright 2019 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package config 16 17 // MapConfig is a simple config for in memory or test configuration. Calls to SetStrings will are valid for the 18 // lifecycle of a program, but are not persisted anywhere and will return to their default values on the next run 19 // of a program. 20 type MapConfig struct { 21 properties map[string]string 22 } 23 24 // NewMapConfig creates a config from a map. 25 func NewMapConfig(properties map[string]string) *MapConfig { 26 return &MapConfig{properties} 27 } 28 29 // GetString retrieves a value for a given key. 30 func (mc *MapConfig) GetString(k string) (string, error) { 31 if val, ok := mc.properties[k]; ok { 32 return val, nil 33 } 34 35 return "", ErrConfigParamNotFound 36 } 37 38 // SetString sets the values for a map of updates. 39 func (mc *MapConfig) SetStrings(updates map[string]string) error { 40 for k, v := range updates { 41 mc.properties[k] = v 42 } 43 44 return nil 45 } 46 47 // Iter will perform a callback for ech value in a config until all values have been exhausted or until the 48 // callback returns true indicating that it should stop. 49 func (mc *MapConfig) Iter(cb func(string, string) (stop bool)) { 50 for k, v := range mc.properties { 51 stop := cb(k, v) 52 53 if stop { 54 break 55 } 56 } 57 } 58 59 // Unset removes a configuration parameter from the config 60 func (mc *MapConfig) Unset(params []string) error { 61 for _, param := range params { 62 delete(mc.properties, param) 63 } 64 65 return nil 66 } 67 68 // Size returns the number of properties contained within the config 69 func (mc *MapConfig) Size() int { 70 return len(mc.properties) 71 }