forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
internal.go
54 lines (42 loc) · 1.27 KB
/
internal.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
package db
import (
"reflect"
"sort"
"strings"
"github.com/jmoiron/sqlx/reflectx"
)
var mapper = reflectx.NewMapper("db")
type person struct {
Name string `db:"name"`
HungerLevel string `db:"hunger_level"`
SomethingIgnored int `db:"-"`
}
// columnsForStruct returns a slice of column names for the provided value
// (which should be a struct, a slice of structs).
func columnsForStruct(dest interface{}) []string {
typ := reflect.TypeOf(dest)
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
if typ.Kind() == reflect.Slice {
typ = typ.Elem()
}
typmap := mapper.TypeMap(typ)
var keys []string
for k := range typmap.Names {
// If a struct contains another struct (ex. sql.NullString) mapper.TypeMap
// will return the fields of an internal struct (like: "payment_id.String",
// "payment_id.Valid").
// This will break the query so skip these fields.
if strings.Contains(k, ".") {
continue
}
keys = append(keys, k)
}
// Ensure keys are sorted. keys is populated from a map, which has no
// defined iteration order. Different versions of go or different
// architectures may cause non-deterministic results to occur (and in our CI
// environment, they have). To make testing easier, we sort the keys.
sort.Strings(keys)
return keys
}