Skip to content

Commit 985314b

Browse files
committed
fix panic
1 parent 9494539 commit 985314b

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

config/env.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,29 @@ func walkStruct(iface any, prefix string, fn func(fieldv reflect.Value, path, en
3030
for i := 0; i < ift.NumField(); i++ {
3131
fieldv := ifv.Field(i)
3232
t := ift.Field(i)
33+
34+
if !t.IsExported() {
35+
continue
36+
}
37+
38+
if !fieldv.CanInterface() {
39+
continue
40+
}
41+
3342
name := t.Name
3443
tag, ok := t.Tag.Lookup("mapstructure")
3544
if ok {
45+
if tag == "-" {
46+
continue
47+
}
3648
name = tag
3749
}
3850

51+
jsonTag := t.Tag.Get("json")
52+
if jsonTag == "-" {
53+
continue
54+
}
55+
3956
path := name
4057
if prefix != "" {
4158
path = prefix + "." + name
@@ -44,8 +61,10 @@ func walkStruct(iface any, prefix string, fn func(fieldv reflect.Value, path, en
4461
envKey := strings.ToUpper(strings.ReplaceAll(path, ".", "_"))
4562

4663
if fieldv.Kind() == reflect.Struct {
47-
if err := walkStruct(fieldv.Addr().Interface(), path, fn); err != nil {
48-
return err
64+
if fieldv.CanAddr() && fieldv.Addr().CanInterface() {
65+
if err := walkStruct(fieldv.Addr().Interface(), path, fn); err != nil {
66+
return err
67+
}
4968
}
5069
} else {
5170
if err := fn(fieldv, path, envKey); err != nil {

0 commit comments

Comments
 (0)