github.com/AbsaOSS/env-binder@v1.0.1/env/field.go (about) 1 /* 2 Copyright 2021 The k8gb Contributors. 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 Generated by GoLic, for more details see: https://github.com/AbsaOSS/golic 17 */ 18 19 package env 20 21 import ( 22 "fmt" 23 "strconv" 24 "strings" 25 ) 26 27 func boolean(env env) (b bool, err error) { 28 var d bool 29 if env.def.exists { 30 d, err = strconv.ParseBool(env.def.value) 31 if err != nil { 32 err = fmt.Errorf("can't convert default value %s of '%s' to bool", env.name, env.def.value) 33 return 34 } 35 } 36 b, err = GetEnvAsBoolOrFallback(env.name, d) 37 return 38 } 39 40 func float(env env) (f float64, err error) { 41 var d float64 42 if env.def.exists { 43 d, err = strconv.ParseFloat(env.def.value, 64) 44 if err != nil { 45 err = fmt.Errorf("can't convert default value %s of '%s' to float64", env.name, env.def.value) 46 return 47 } 48 } 49 f, err = GetEnvAsFloat64OrFallback(env.name, d) 50 if err != nil { 51 err = fmt.Errorf("can't read %s and parse value '%s' to float64", env.name, env.value) 52 } 53 return 54 } 55 56 func floatSlice(env env) (fs []float64, err error) { 57 var d []float64 58 if env.def.asStringSlice() != nil { 59 d = make([]float64, 0) 60 for _, s := range env.def.asStringSlice() { 61 var fl float64 62 fl, err = strconv.ParseFloat(strings.Trim(s, " "), 64) 63 if err != nil { 64 err = fmt.Errorf("can't convert default %s to slice of float64", env.def.asStringSlice()) 65 return 66 } 67 d = append(d, fl) 68 } 69 } 70 fs, err = GetEnvAsArrayOfFloat64OrFallback(env.name, d) 71 if err != nil { 72 err = fmt.Errorf("can't parse %s as slice of float64 '%s'", env.name, env.value) 73 } 74 return 75 } 76 77 func boolSlice(env env) (bs []bool, err error) { 78 var d []bool 79 if env.def.asStringSlice() != nil { 80 d = make([]bool, 0) 81 for _, s := range env.def.asStringSlice() { 82 var b bool 83 b, err = strconv.ParseBool(strings.Trim(s, " ")) 84 if err != nil { 85 err = fmt.Errorf("can't convert default %s to slice of bool", env.def.asStringSlice()) 86 return 87 } 88 d = append(d, b) 89 } 90 } 91 bs, err = GetEnvAsArrayOfBoolOrFallback(env.name, d) 92 if err != nil { 93 err = fmt.Errorf("can't parse %s as slice of bool '%s'", env.name, env.value) 94 } 95 return 96 }