-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
67 lines (60 loc) · 1.88 KB
/
util.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
58
59
60
61
62
63
64
65
66
67
package golang
import (
"html"
"reflect"
"strconv"
)
// string to number receiver
func convertStringToNumericReceiver(receiver reflect.Value, value string) (reflect.Value, error) {
// reflect.Value is struct not pointer return a new one
switch receiver.Kind() {
case reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int8:
val, err := strconv.ParseInt(value, 10, 64)
return reflect.ValueOf(val).Convert(receiver.Type()), err
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
val, err := strconv.ParseUint(value, 10, 64)
return reflect.ValueOf(val).Convert(receiver.Type()), err
default:
return reflect.Value{}, ErrorUnsupportedDataKind
}
}
// number reflect.value to string reflect.value
func convertNumericToString(value reflect.Value) (string, error) {
switch value.Kind() {
case reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int8:
return strconv.FormatInt(value.Int(), 10), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return strconv.FormatUint(value.Uint(), 10), nil
case reflect.Float32:
return strconv.FormatFloat(value.Float(), 'f', -1, 32), nil
case reflect.Float64:
return strconv.FormatFloat(value.Float(), 'f', -1, 64), nil
default:
return "", ErrorUnsupportedDataKind
}
}
func getMemoryAddress(v reflect.Value) uintptr {
switch v.Kind() {
case reflect.Ptr, reflect.Slice, reflect.Map, reflect.Chan, reflect.Func, reflect.UnsafePointer:
return v.Pointer()
case reflect.Interface:
if !v.IsNil() && v.Elem().CanAddr() {
return v.Elem().UnsafeAddr()
}
return v.UnsafeAddr()
default:
return v.UnsafeAddr()
}
}
func htmlEscape(s string) string {
return html.EscapeString(s)
}
func isUint8Array(v reflect.Value) bool {
if v.Kind() != reflect.Slice {
return false
}
if v.Type().Elem().Kind() != reflect.Uint8 {
return false
}
return true
}