forked from go-pg/pg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
append_value.go
107 lines (93 loc) · 2.82 KB
/
append_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
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package pg
import (
"database/sql/driver"
"encoding/json"
"reflect"
"strconv"
"time"
"gopkg.in/pg.v3/pgutil"
)
type valueAppender func([]byte, reflect.Value) []byte
var valueAppenders = [...]valueAppender{
reflect.Bool: appendBoolValue,
reflect.Int: appendIntValue,
reflect.Int8: appendIntValue,
reflect.Int16: appendIntValue,
reflect.Int32: appendIntValue,
reflect.Int64: appendIntValue,
reflect.Uint: appendUintValue,
reflect.Uint8: appendUintValue,
reflect.Uint16: appendUintValue,
reflect.Uint32: appendUintValue,
reflect.Uint64: appendUintValue,
reflect.Uintptr: nil,
reflect.Float32: appendFloatValue,
reflect.Float64: appendFloatValue,
reflect.Complex64: nil,
reflect.Complex128: nil,
reflect.Array: nil,
reflect.Chan: nil,
reflect.Func: nil,
reflect.Interface: nil,
reflect.Map: nil,
reflect.Ptr: nil,
reflect.Slice: nil,
reflect.String: appendStringValue,
reflect.Struct: appendStructValue,
reflect.UnsafePointer: nil,
}
func appendBoolValue(dst []byte, v reflect.Value) []byte {
return appendBool(dst, v.Bool())
}
func appendIntValue(dst []byte, v reflect.Value) []byte {
return strconv.AppendInt(dst, v.Int(), 10)
}
func appendUintValue(dst []byte, v reflect.Value) []byte {
return strconv.AppendUint(dst, v.Uint(), 10)
}
func appendFloatValue(dst []byte, v reflect.Value) []byte {
return appendFloat(dst, v.Float())
}
func appendStringValue(dst []byte, v reflect.Value) []byte {
return appendString(dst, v.String())
}
func appendStructValue(dst []byte, v reflect.Value) []byte {
switch v.Type() {
case timeType:
return appendTimeValue(dst, v)
}
b, err := json.Marshal(v.Interface())
if err != nil {
panic(err)
}
return appendStringBytes(dst, b)
}
func appendTimeValue(dst []byte, v reflect.Value) []byte {
dst = append(dst, '\'')
dst = pgutil.AppendTime(dst, v.Interface().(time.Time))
dst = append(dst, '\'')
return dst
}
func appendAppenderValue(dst []byte, v reflect.Value) []byte {
return v.Interface().(QueryAppender).AppendQuery(dst)
}
func appendDriverValuerValue(dst []byte, v reflect.Value) []byte {
return appendDriverValuer(dst, v.Interface().(driver.Valuer))
}
func isEmptyValue(v reflect.Value) bool {
switch v.Kind() {
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
return v.Len() == 0
case reflect.Bool:
return !v.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v.Int() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return v.Uint() == 0
case reflect.Float32, reflect.Float64:
return v.Float() == 0
case reflect.Interface, reflect.Ptr:
return v.IsNil()
}
return false
}