config: export method to init new empty struct
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
150e8ad698
commit
b50855855b
@ -1,6 +1,8 @@
|
||||
package config
|
||||
|
||||
import "reflect"
|
||||
import (
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func IsEmpty(v reflect.Value) bool {
|
||||
switch v.Kind() {
|
||||
@ -26,3 +28,19 @@ func IsEmpty(v reflect.Value) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func Zero(src interface{}) (interface{}, error) {
|
||||
sv := reflect.ValueOf(src)
|
||||
|
||||
if sv.Kind() == reflect.Ptr {
|
||||
sv = sv.Elem()
|
||||
}
|
||||
|
||||
if sv.Kind() == reflect.Invalid {
|
||||
return nil, ErrInvalidStruct
|
||||
}
|
||||
|
||||
dst := reflect.New(sv.Type())
|
||||
|
||||
return dst.Interface(), nil
|
||||
}
|
||||
|
26
config/reflect_test.go
Normal file
26
config/reflect_test.go
Normal file
@ -0,0 +1,26 @@
|
||||
package config_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/unistack-org/micro/v3/config"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Value string
|
||||
SubConfig *SubConfig
|
||||
Config *Config
|
||||
}
|
||||
|
||||
type SubConfig struct {
|
||||
Value string
|
||||
}
|
||||
|
||||
func TestReflect(t *testing.T) {
|
||||
cfg1 := &Config{Value: "cfg1", Config: &Config{Value: "cfg1_1"}, SubConfig: &SubConfig{Value: "cfg1"}}
|
||||
cfg2, err := config.Clone(cfg1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Logf("dst: %#+v\n", cfg2)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user