github.com/binkynet/BinkyNet@v1.12.1-0.20240421190447-da4e34c20be0/apis/v1/connectionhelper.go (about) 1 // Copyright 2018-2021 Ewout Prangsma 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 // Author Ewout Prangsma 16 // 17 18 package v1 19 20 import "strconv" 21 22 // GetStringConfig returns the configuration value for the given key. 23 // If not found, the default value for the key is returned. 24 func (c Connection) GetStringConfig(key ConfigKey) string { 25 value, found := c.Configuration[key] 26 if found { 27 return value 28 } 29 return key.DefaultValue() 30 } 31 32 // GetBoolConfig returns the bool-typed configuration value for the given key. 33 // If not found or not an int, the default value for the key is returned. 34 func (c Connection) GetBoolConfig(key ConfigKey) bool { 35 value, found := c.Configuration[key] 36 if !found { 37 value = key.DefaultValue() 38 } 39 if tValue, err := strconv.ParseBool(value); err == nil { 40 return tValue 41 } 42 return false 43 } 44 45 // GetIntConfig returns the int-typed configuration value for the given key. 46 // If not found or not an int, the default value for the key is returned. 47 func (c Connection) GetIntConfig(key ConfigKey) int { 48 value, found := c.Configuration[key] 49 if !found { 50 value = key.DefaultValue() 51 } 52 if tValue, err := strconv.Atoi(value); err == nil { 53 return tValue 54 } 55 return 0 56 }