-
Notifications
You must be signed in to change notification settings - Fork 1
/
value.go
38 lines (36 loc) · 781 Bytes
/
value.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
package flyline
import (
"fmt"
"reflect"
)
/*
* Scan value
* @param dest ptr object
*
*/
func ValueScan(src interface{}, dest interface{}) (err error) {
if dest == nil {
err = fmt.Errorf("dest is nil, dest = %v", dest)
return
}
dpv := reflect.ValueOf(dest)
if dpv.Kind() != reflect.Ptr {
err = fmt.Errorf("dest's type is not Ptr, dest = %v", dpv.Kind())
return
}
sv := reflect.ValueOf(src)
dv := reflect.Indirect(dpv)
if sv.Kind() == reflect.Ptr {
sv = sv.Elem()
}
if sv.IsValid() && sv.Type().AssignableTo(dv.Type()) {
dv.Set(sv)
return
}
if dv.Kind() == sv.Kind() && sv.Type().ConvertibleTo(dv.Type()) {
dv.Set(sv.Convert(dv.Type()))
return
}
err = fmt.Errorf("scan failed, not match, src = %v, dest = %v", sv.Type(), dv.Type())
return
}