forked from go-pg/pg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
append_value.go
129 lines (114 loc) · 3.46 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package pg
import (
"database/sql/driver"
"encoding/json"
"reflect"
"strconv"
"time"
)
var (
stringSliceType = reflect.TypeOf([]string(nil))
)
type valueAppender func([]byte, reflect.Value, bool) []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: appendSliceValue,
reflect.String: appendStringValue,
reflect.Struct: appendStructValue,
reflect.UnsafePointer: nil,
}
func appendBoolValue(b []byte, v reflect.Value, _ bool) []byte {
return appendBool(b, v.Bool())
}
func appendIntValue(b []byte, v reflect.Value, _ bool) []byte {
return strconv.AppendInt(b, v.Int(), 10)
}
func appendUintValue(b []byte, v reflect.Value, _ bool) []byte {
return strconv.AppendUint(b, v.Uint(), 10)
}
func appendFloatValue(b []byte, v reflect.Value, _ bool) []byte {
return appendFloat(b, v.Float())
}
func appendSliceValue(b []byte, v reflect.Value, quote bool) []byte {
elemType := v.Type().Elem()
switch elemType.Kind() {
case reflect.Uint8:
return appendBytes(b, v.Bytes(), quote)
case reflect.String:
ss := v.Convert(stringSliceType).Interface().([]string)
return appendStringSlice(b, ss, quote)
case reflect.Int:
return nil
case reflect.Int64:
return nil
case reflect.Float64:
return nil
}
panic(errorf("pg: Decode(unsupported %s)", v.Type()))
}
func appendStringValue(b []byte, v reflect.Value, quote bool) []byte {
return appendString(b, v.String(), quote)
}
func appendStructValue(b []byte, v reflect.Value, quote bool) []byte {
switch v.Type() {
case timeType:
return appendTimeValue(b, v, quote)
}
bytes, err := json.Marshal(v.Interface())
if err != nil {
panic(err)
}
return appendStringBytes(b, bytes, quote)
}
func appendTimeValue(b []byte, v reflect.Value, quote bool) []byte {
tm := v.Interface().(time.Time)
return appendTime(b, tm, quote)
}
func appendAppenderValue(b []byte, v reflect.Value, quote bool) []byte {
if quote {
return v.Interface().(QueryAppender).AppendQuery(b)
} else {
return v.Interface().(RawQueryAppender).AppendRawQuery(b)
}
}
func appendDriverValuerValue(b []byte, v reflect.Value, quote bool) []byte {
return appendDriverValuer(b, v.Interface().(driver.Valuer), quote)
}
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
}