github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/utils/config/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 import ( 18 "errors" 19 "strconv" 20 ) 21 22 // ErrConfigParamNotFound - Error returned when the config does not contain the parameter requested 23 var ErrConfigParamNotFound = errors.New("Param not found") 24 25 // ReadableConfig interface provides a mechanisms for getting key value pairs from a config 26 type ReadableConfig interface { 27 28 // GetString retrieves a string given a key. If there is no config property with the given key then 29 // config.ErrConfigParamNotFound will be returned. Other errors may be returned depending on the 30 // ReadableConfig implementation. 31 GetString(key string) (value string, err error) 32 33 // Iter will perform a callback for each value in a config until all values have been exhausted or until the 34 // callback returns true indicating that it should stop. 35 Iter(func(string, string) (stop bool)) 36 37 // Size returns the number of properties contained within the config 38 Size() int 39 } 40 41 // WritableConfig interface provides a mechanism for setting key value pairs in a config 42 type WritableConfig interface { 43 44 // SetStrings uses the updates map to set configuration parameter values. 45 SetStrings(updates map[string]string) error 46 47 // Unset removes a configuration parameter from the config 48 Unset(params []string) error 49 } 50 51 // ReadWriteConfig interface provides a mechanism for both getting and setting key value pairs in a config 52 type ReadWriteConfig interface { 53 ReadableConfig 54 WritableConfig 55 } 56 57 // GetString retrieves a string value from a ReadableConfig 58 func GetString(cs ReadableConfig, k string) (string, error) { 59 return cs.GetString(k) 60 } 61 62 // GetInt retrieves a string value from a ReadableConfig and converts it to an integer. 63 func GetInt(cs ReadableConfig, k string) (int64, error) { 64 if s, err := cs.GetString(k); err == nil { 65 if val, err := strconv.ParseInt(s, 10, 64); err != nil { 66 return 0, err 67 } else { 68 return val, nil 69 } 70 } else { 71 return 0, err 72 } 73 } 74 75 // GetUint retrieves a string value from a ReadableConfig and converts it to an unsigned integer. 76 func GetUint(cs ReadableConfig, k string) (uint64, error) { 77 if s, err := cs.GetString(k); err == nil { 78 if val, err := strconv.ParseUint(s, 10, 64); err != nil { 79 return 0, err 80 } else { 81 return val, nil 82 } 83 } else { 84 return 0, err 85 } 86 } 87 88 // GetFloat retrieves a string value from a ReadableConfig and converts it to a float. 89 func GetFloat(cs ReadableConfig, k string) (float64, error) { 90 if s, err := cs.GetString(k); err == nil { 91 if val, err := strconv.ParseFloat(s, 64); err != nil { 92 return 0, err 93 } else { 94 return val, nil 95 } 96 } else { 97 return 0, err 98 } 99 } 100 101 // SetStrings sets configuration values from the values in the updates map 102 func SetStrings(c WritableConfig, updates map[string]string) error { 103 return c.SetStrings(updates) 104 } 105 106 // SetInt sets a value in the WritableConfig for a given key to the string converted value of an integer 107 func SetInt(c WritableConfig, key string, val int64) error { 108 s := strconv.FormatInt(val, 10) 109 return c.SetStrings(map[string]string{key: s}) 110 } 111 112 // SetUint sets a value in the Writable for a given key to the string converted value of an unsigned int 113 func SetUint(c WritableConfig, key string, val uint64) error { 114 s := strconv.FormatUint(val, 10) 115 return c.SetStrings(map[string]string{key: s}) 116 } 117 118 // SetFloat sets a value in the WritableConfig for a given key to the string converted value of a float 119 func SetFloat(c WritableConfig, key string, val float64) error { 120 s := strconv.FormatFloat(val, byte('f'), 8, 64) 121 return c.SetStrings(map[string]string{key: s}) 122 } 123 124 // Equals compares a config against a map and returns whether the map contains all the values of the 125 // config with the same values. 126 func Equals(cfg ReadableConfig, compareProps map[string]string) bool { 127 if cfg.Size() != len(compareProps) { 128 return false 129 } 130 131 isEqual := true 132 cfg.Iter(func(name, value string) (stop bool) { 133 if compareVal, ok := compareProps[name]; ok { 134 isEqual = (compareVal == value) 135 } else { 136 isEqual = false 137 } 138 139 return !isEqual 140 }) 141 142 return isEqual 143 }