Skip to content

Commit cfc0c06

Browse files
committed
feat: parsing []byte
1 parent 20e0253 commit cfc0c06

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

envconfig.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,10 @@ func read(le func(string) (string, bool), prefix string, holder any) error {
265265
return nil
266266
}
267267

268-
var durationType = reflect.TypeOf(time.Duration(0))
268+
var (
269+
durationType = reflect.TypeOf(time.Duration(0))
270+
byteSliceType = reflect.TypeOf([]byte{})
271+
)
269272

270273
func setValue(inp reflect.Value, value string) error {
271274
if inp.Kind() == reflect.Ptr {
@@ -295,6 +298,10 @@ func setValue(inp reflect.Value, value string) error {
295298
}
296299
inp.Set(reflect.ValueOf(d))
297300
return nil
301+
302+
case byteSliceType:
303+
inp.Set(reflect.ValueOf([]byte(value)))
304+
return nil
298305
}
299306

300307
switch inp.Kind() {

envconfig_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ func TestReadValues(t *testing.T) {
2929
return "key1=value1, key2=value2", true
3030
case "Z":
3131
return "embedded_value", true
32+
case "T":
33+
return "bytes", true
3234
case "EMB_ZA":
3335
return "emb_value", true
3436
case "CUSTOM":
@@ -90,6 +92,7 @@ func TestReadValues(t *testing.T) {
9092
PtrBool: ptr(true),
9193
PtrPtrBool: ptr(ptr(true)),
9294
StringDefault: "Default Value",
95+
Bytes: []byte("bytes"),
9396
CustomTextUnmarshaler: CustomTextUnmarshaler{
9497
Value: "***custom_text***",
9598
},
@@ -303,6 +306,7 @@ type Config struct {
303306
Map map[string]string `env:"Q"`
304307
PtrBool *bool `env:"R"`
305308
PtrPtrBool **bool `env:"S"`
309+
Bytes []byte `env:"T"`
306310

307311
StringDefault string `env:"MISSING" envDefault:"Default Value"`
308312
MissingValue string `env:"MISSING"`

0 commit comments

Comments
 (0)