add defaut config parser

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2020-12-13 13:17:38 +03:00
parent a7a3c679d1
commit 2dcd30b21c
2 changed files with 11 additions and 1 deletions

View File

@ -173,6 +173,12 @@ func (c *defaultConfig) fillValues(ctx context.Context, valueOf reflect.Value) e
continue continue
} }
switch value.Kind() { switch value.Kind() {
case reflect.Struct:
value.Set(reflect.Indirect(reflect.New(value.Type())))
if err := c.fillValues(ctx, value); err != nil {
return err
}
continue
case reflect.Ptr: case reflect.Ptr:
if value.IsNil() { if value.IsNil() {
if value.Type().Elem().Kind() != reflect.Struct { if value.Type().Elem().Kind() != reflect.Struct {

View File

@ -11,6 +11,9 @@ import (
type Cfg struct { type Cfg struct {
StringValue string `default:"string_value"` StringValue string `default:"string_value"`
IntValue int `default:"99"` IntValue int `default:"99"`
StructValue struct {
StringValue string `default:"string_value"`
}
} }
func TestDefault(t *testing.T) { func TestDefault(t *testing.T) {
@ -50,7 +53,7 @@ func TestDefault(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
if conf.StringValue != "string_value" || conf.IntValue != 99 { if conf.StringValue != "string_value" || conf.IntValue != 99 || conf.StructValue.StringValue != "string_value" {
t.Fatalf("load failed: %#+v", conf) t.Fatalf("load failed: %#+v", conf)
} }
@ -63,4 +66,5 @@ func TestDefault(t *testing.T) {
t.Fatal("AfterLoad option not working") t.Fatal("AfterLoad option not working")
} }
t.Logf("%#+v\n", conf)
} }