add defaut config parser
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
		| @@ -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 { | ||||||
|   | |||||||
| @@ -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) | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user