-
Notifications
You must be signed in to change notification settings - Fork 2
/
values.go
202 lines (186 loc) · 4.96 KB
/
values.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package sol
import (
"encoding/json"
"fmt"
"reflect"
"sort"
"strings"
"time"
"github.com/aodin/sol/dialect"
)
// Values is a map of column names to values. It can be used both as
// a source of values in INSERT, UPDATE, and Text statements, or as
// a destination for SELECT.
type Values map[string]interface{}
// Compile outputs the Values in a format for UPDATE using the given dialect
// and parameters.
func (v Values) Compile(d dialect.Dialect, ps *Parameters) (string, error) {
keys := v.Keys()
values := make([]string, len(keys))
for i, key := range keys {
param := NewParam(v[key])
compiledParam, err := param.Compile(d, ps)
if err != nil {
return "", err
}
values[i] = fmt.Sprintf(`%s = %s`, key, compiledParam)
}
return strings.Join(values, ", "), nil
}
// Diff returns the values in v that differ from the values in other.
// ISO 31-11: v \ other
func (v Values) Diff(other Values) Values {
diff := Values{}
for key, value := range v {
if !reflect.DeepEqual(value, other[key]) {
diff[key] = value
}
}
return diff
}
// Equals returns true if both the receiver and parameter Values have
// equal keys and values
func (v Values) Equals(other Values) bool {
if len(v) != len(other) {
return false
}
for key, a := range v {
if b, ok := other[key]; !ok || !reflect.DeepEqual(a, b) {
return false
}
}
return true
}
// Exclude removes the given keys and returns the remaining Values
func (v Values) Exclude(keys ...string) Values {
out := Values{}
ValueLoop:
for key, value := range v {
for _, k := range keys {
if k == key {
continue ValueLoop
}
}
out[key] = value
}
return out
}
// Filter returns a Values type with key-values from the original Values
// that match the given keys
func (v Values) Filter(keys ...string) Values {
out := Values{}
for key, value := range v {
for _, k := range keys {
if k == key {
out[key] = value
break
}
}
}
return out
}
// Keys returns the keys of the Values map in alphabetical order.
func (v Values) Keys() []string {
keys := make([]string, len(v))
var i int
for key := range v {
keys[i] = key
i += 1
}
sort.Strings(keys)
return keys
}
// Merge combines the given Values without modifying the original Values.
// Precedence is given to the rightmost Values given as parameters.
func (v Values) Merge(others ...Values) Values {
merged := Values{}
for _, other := range append([]Values{v}, others...) {
for key, value := range other {
merged[key] = value
}
}
return merged
}
// MarshalJSON converts Values to JSON after converting all byte slices to
// a string type. By default, byte slices are JSON unmarshaled as base64.
// This is an issue since the postgres driver will scan string/varchar
// types as byte slices - the current solution is to convert before output.
func (v Values) MarshalJSON() ([]byte, error) {
for key, value := range v {
if val, ok := value.([]byte); ok {
v[key] = string(val)
}
}
// Convert to prevent recursive marshaling
return json.Marshal(map[string]interface{}(v))
}
// Reject is an alias for Exclude
func (v Values) Reject(keys ...string) Values {
return v.Exclude(keys...)
}
// Values returns the values of the Values map in the alphabetical order
// of its keys.
func (v Values) Values() []interface{} {
keys := v.Keys()
values := make([]interface{}, len(keys))
for i, key := range keys {
values[i] = v[key]
}
return values
}
// ValuesOf converts the given object to a Values type
func ValuesOf(obj interface{}) (Values, error) {
elem := reflect.Indirect(reflect.ValueOf(obj))
switch elem.Kind() {
case reflect.Map:
// If the type is already Values, convert and return
switch converted := obj.(type) {
case Values:
return converted, nil
case *Values:
return *converted, nil
case map[string]interface{}:
return Values(converted), nil
case *map[string]interface{}:
return Values(*converted), nil
default:
return nil, fmt.Errorf(
"sol: unsupported map type %T for ValuesOf()", converted,
)
}
case reflect.Struct:
values := Values{}
for _, field := range DeepFields(obj) {
if field.IsOmittable() {
continue // Skip zero values
}
values[field.Name] = field.Value.Interface()
}
return values, nil
}
return nil, fmt.Errorf("sol: unsupported type %T for ValuesOf()", obj)
}
// isEmptyValue is from Go's encoding/json package: encode.go
// Copyright 2010 The Go Authors. All rights reserved.
func isEmptyValue(v reflect.Value) bool {
switch v.Kind() {
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.String:
return v.String() == ""
case reflect.Bool:
return !v.Bool()
case reflect.Float32, reflect.Float64:
return v.Float() == 0
case reflect.Interface, reflect.Ptr:
return v.IsNil()
case reflect.Struct:
t, ok := v.Interface().(time.Time)
if ok {
return t.IsZero()
}
}
return false
}