@@ -21,6 +21,7 @@ import (
2121func Source [T any ](envPrefix string ) clicfg.SourceFunc [T ] {
2222 return func (_ context.Context , cfg * T ) error {
2323 _ , err := recursivelyWalkThroughReflectValue (os .LookupEnv , reflect .ValueOf (cfg ).Elem (), envPrefix , nil )
24+
2425 return err
2526 }
2627}
@@ -50,13 +51,15 @@ func recursivelyWalkThroughReflectValue(lookupEnv func(string) (string, bool), v
5051 if atLeastOneEnvFound {
5152 v .Set (newV )
5253 }
54+
5355 return atLeastOneEnvFound , err
5456
5557 case reflect .Struct : // if it's a struct, iterate over its fields
5658 var (
5759 errs []error
5860 atLeastOneFound bool
5961 )
62+
6063 for i := range v .NumField () {
6164 tfield := t .Field (i )
6265 tag := tfield .Tag .Get ("env" )
@@ -80,12 +83,15 @@ func recursivelyWalkThroughReflectValue(lookupEnv func(string) (string, bool), v
8083 if envFound {
8184 atLeastOneFound = true
8285 }
86+
8387 errs = append (errs , err )
8488 }
89+
8590 return atLeastOneFound , multierr .Combine (errs ... )
8691
8792 default : // for primitive types, try to find the corresponding environment variable
8893 var rawEnv string
94+
8995 for _ , envToLookup := range append (additionalEnvsToLookup , envPrefix ) {
9096 envToLookup = strings .TrimSpace (envToLookup )
9197 if envToLookup != "" {
@@ -105,65 +111,81 @@ func recursivelyWalkThroughReflectValue(lookupEnv func(string) (string, bool), v
105111 case reflect .Bool :
106112 vv , err := strconv .ParseBool (rawEnv )
107113 v .SetBool (vv )
114+
108115 return true , err
109116 case reflect .Int :
110117 vv , err := strconv .ParseInt (rawEnv , 10 , 0 )
111118 v .SetInt (vv )
119+
112120 return true , err
113121 case reflect .Int8 :
114122 vv , err := strconv .ParseInt (rawEnv , 10 , 8 )
115123 v .SetInt (vv )
124+
116125 return true , err
117126 case reflect .Int16 :
118127 vv , err := strconv .ParseInt (rawEnv , 10 , 16 )
119128 v .SetInt (vv )
129+
120130 return true , err
121131 case reflect .Int32 :
122132 vv , err := strconv .ParseInt (rawEnv , 10 , 32 )
123133 v .SetInt (vv )
134+
124135 return true , err
125136 case reflect .Int64 :
126137 vv , err := strconv .ParseInt (rawEnv , 10 , 64 )
127138 v .SetInt (vv )
139+
128140 return true , err
129141 case reflect .Uint :
130142 vv , err := strconv .ParseUint (rawEnv , 10 , 0 )
131143 v .SetUint (vv )
144+
132145 return true , err
133146 case reflect .Uint8 :
134147 vv , err := strconv .ParseUint (rawEnv , 10 , 8 )
135148 v .SetUint (vv )
149+
136150 return true , err
137151 case reflect .Uint16 :
138152 vv , err := strconv .ParseUint (rawEnv , 10 , 16 )
139153 v .SetUint (vv )
154+
140155 return true , err
141156 case reflect .Uint32 :
142157 vv , err := strconv .ParseUint (rawEnv , 10 , 32 )
143158 v .SetUint (vv )
159+
144160 return true , err
145161 case reflect .Uint64 :
146162 vv , err := strconv .ParseUint (rawEnv , 10 , 64 )
147163 v .SetUint (vv )
164+
148165 return true , err
149166 case reflect .Float32 :
150167 vv , err := strconv .ParseFloat (rawEnv , 32 )
151168 v .SetFloat (vv )
169+
152170 return true , err
153171 case reflect .Float64 :
154172 vv , err := strconv .ParseFloat (rawEnv , 64 )
155173 v .SetFloat (vv )
174+
156175 return true , err
157176 case reflect .Complex64 :
158177 vv , err := strconv .ParseComplex (rawEnv , 64 )
159178 v .SetComplex (vv )
179+
160180 return true , err
161181 case reflect .Complex128 :
162182 vv , err := strconv .ParseComplex (rawEnv , 128 )
163183 v .SetComplex (vv )
184+
164185 return true , err
165186 case reflect .String :
166187 v .SetString (rawEnv )
188+
167189 return true , nil
168190 default :
169191 return true , fmt .Errorf ("unhandled type %s" , k )
0 commit comments