diff --git a/cvts.go b/cvts.go index 4a6c5c6..1c26364 100644 --- a/cvts.go +++ b/cvts.go @@ -249,6 +249,12 @@ func anyToBool(data any) bool { case bool: return z default: + rv := reflect.ValueOf(data) + rv = ref.Rindirect(rv) + if k := rv.Kind(); k == reflect.Bool { + // eg: flag.stringValue (string) + return rv.Bool() + } return toBool(anyToString(data)) } } @@ -532,6 +538,7 @@ func anyToString(data any) string { return fmt.Sprint(data) default: + rv = ref.Rindirect(rv) // *string -> string if k := rv.Kind(); k == reflect.String { // eg: flag.stringValue (string) return rv.String() @@ -596,9 +603,9 @@ func anyToComplex[R Complexes](data any) R { return R(complex(float64(z), 0)) case float64: - return R(complex(float64(z), 0)) + return R(complex(z, 0)) case float32: - return R(complex(float32(z), 0)) + return R(complex(z, 0)) case string: return R(mustParseComplex(z)) @@ -606,6 +613,12 @@ func anyToComplex[R Complexes](data any) R { return R(mustParseComplex(z.String())) default: + rv := reflect.ValueOf(data) + rv = ref.Rindirect(rv) // *string -> string + if k := rv.Kind(); k == reflect.Complex128 || k == reflect.Complex64 { + // eg: flag.complexValue (complex) + return R(rv.Complex()) + } str := fmt.Sprintf("%v", data) return R(mustParseComplex(str)) } diff --git a/cvts1.go b/cvts1.go index 95dd0d9..77694f6 100644 --- a/cvts1.go +++ b/cvts1.go @@ -294,6 +294,13 @@ func anyToInt(data any) int64 { return z.UnixNano() default: + rv := reflect.ValueOf(data) + rv = ref.Rindirect(rv) // *string -> string + if k := rv.Kind(); k >= reflect.Uint || k <= reflect.Uint64 { + return int64(rv.Uint()) + } else if k >= reflect.Int || k <= reflect.Int64 { + return rv.Int() + } return atoi(fmt.Sprint(data)) } @@ -353,6 +360,13 @@ func anyToUint(data any) uint64 { return uint64(z.UnixNano()) default: + rv := reflect.ValueOf(data) + rv = ref.Rindirect(rv) // *string -> string + if k := rv.Kind(); k >= reflect.Uint || k <= reflect.Uint64 { + return rv.Uint() + } else if k >= reflect.Int || k <= reflect.Int64 { + return uint64(rv.Int()) + } return atou(fmt.Sprint(data)) } } @@ -541,6 +555,12 @@ func anyToFloat[R Floats](data any) R { return R(mustParseFloat(z.String())) default: + rv := reflect.ValueOf(data) + rv = ref.Rindirect(rv) // *string -> string + if k := rv.Kind(); k == reflect.Float64 || k == reflect.Float32 { + // eg: flag.floatValue (float) + return R(rv.Float()) + } str := fmt.Sprintf("%v", data) return R(mustParseFloat(str)) }