diff --git a/util/reflect/reflect.go b/util/reflect/reflect.go index 0670761..2fd3a5d 100644 --- a/util/reflect/reflect.go +++ b/util/reflect/reflect.go @@ -60,8 +60,45 @@ func safeAssignment(variable reflect.Value, value interface{}) (err error) { return } switch kind { + case reflect.Bool: + switch rv.Kind() { + case reflect.Bool: + variable.SetBool(rv.Bool()) + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + if rv.Int() != 0 { + variable.SetBool(true) + } else { + variable.SetBool(false) + } + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + if rv.Uint() != 0 { + variable.SetBool(true) + } else { + variable.SetBool(false) + } + case reflect.Float32, reflect.Float64: + if rv.Float() != 0 { + variable.SetBool(true) + } else { + variable.SetBool(false) + } + case reflect.String: + var tv bool + tv, err = strconv.ParseBool(rv.String()) + if err == nil { + variable.SetBool(tv) + } + default: + err = fmt.Errorf("boolean value can not assign %s", rv.Kind()) + } case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: switch rv.Kind() { + case reflect.Bool: + if rv.Bool() { + variable.SetInt(1) + } else { + variable.SetInt(0) + } case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: variable.SetInt(rv.Int()) case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: @@ -77,6 +114,12 @@ func safeAssignment(variable reflect.Value, value interface{}) (err error) { } case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: switch rv.Kind() { + case reflect.Bool: + if rv.Bool() { + variable.SetUint(1) + } else { + variable.SetUint(0) + } case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: variable.SetUint(uint64(rv.Int())) case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: @@ -92,6 +135,12 @@ func safeAssignment(variable reflect.Value, value interface{}) (err error) { } case reflect.Float32, reflect.Float64: switch rv.Kind() { + case reflect.Bool: + if rv.Bool() { + variable.SetFloat(1) + } else { + variable.SetFloat(0) + } case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: variable.SetFloat(float64(rv.Int())) case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: @@ -107,6 +156,12 @@ func safeAssignment(variable reflect.Value, value interface{}) (err error) { } case reflect.String: switch rv.Kind() { + case reflect.Bool: + if rv.Bool() { + variable.SetString("true") + } else { + variable.SetString("false") + } case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: variable.SetString(strconv.FormatInt(rv.Int(), 10)) case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: diff --git a/util/reflect/reflect_test.go b/util/reflect/reflect_test.go index f187a98..a38a3ae 100644 --- a/util/reflect/reflect_test.go +++ b/util/reflect/reflect_test.go @@ -8,8 +8,11 @@ import ( func TestSet(t *testing.T) { type hack struct { Duration time.Duration + Enable bool } h := &hack{} - Set(h, "Duration", "5s") + Set(h, "Duration", "1111111111111111") + Set(h, "Enable", "T") t.Log(h.Duration) + t.Log(h.Enable) }