-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathto.string.go
57 lines (50 loc) · 1.28 KB
/
to.string.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package cast
import (
"encoding/json"
"fmt"
"reflect"
"github.com/bdlm/errors/v2"
)
// toString casts an interface to a string type.
//
// Options:
// - DEFAULT: slice, default return value on error.
func toString(from reflect.Value, ops Ops) (any, error) {
var err error
var ret any
var ok bool
if _, ok = ops[DEFAULT]; ok {
if ret, ok = ops[DEFAULT].(string); !ok {
return ret, errors.Errorf(ErrorInvalidOption, "DEFAULT", ops[DEFAULT])
}
}
switch from.Type().Kind() {
case
reflect.Bool,
reflect.Complex128, reflect.Complex64,
reflect.Float32, reflect.Float64,
reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int8,
reflect.String,
reflect.Uint, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint8, reflect.Uintptr,
reflect.Chan:
ret = fmt.Sprintf("%v", from.Interface())
default:
if s, ok := from.Interface().(fmt.Stringer); ok || nil == from.Interface() {
ret = fmt.Sprintf("%v", s)
} else {
var b = []byte{}
b, err = json.Marshal(from.Interface())
ret = string(b)
}
}
if err != nil {
return ret, errors.WrapE(Error, err)
}
if nil == ret {
return "", nil
}
if ret, ok = ret.(string); ok {
return ret, nil
}
return ret, errors.WrapE(Error, errors.Errorf(ErrorStrUnableToCast, from, from.Interface(), ""))
}