-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
74 lines (62 loc) · 1.76 KB
/
helper.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
package monkeywrench
import (
"fmt"
"reflect"
"strings"
"cloud.google.com/go/spanner"
"google.golang.org/api/iterator"
)
// TODO: Add tests and example usage for GetColsFromStruct.
// GetColsFromStruct - Get the names of all fields in a struct.
//
// Params:
// src interface{} - The struct to get field names from.
//
// Return:
// []string - Slice of field names from the struct.
// error - An error if it occurred.
func GetColsFromStruct(src interface{}) ([]string, error) {
// Get the reflected structure.
reflectStruct := reflect.ValueOf(src).Type()
// If we have a pointer, get the addressed element type instead.
if reflectStruct.Kind() == reflect.Ptr {
reflectStruct = reflectStruct.Elem()
}
// Check we have a structure and not something else.
if dataType := reflectStruct.Kind(); dataType != reflect.Struct {
return nil, fmt.Errorf("Unsupported data type %s", dataType.String())
}
// Get the columns.
var cols []string
for i := 0; i < reflectStruct.NumField(); i++ {
// Skip this field if it is marked as ignored.
tag := string(reflectStruct.Field(i).Tag)
if strings.Contains(tag, "spanner:\"-\"") {
continue
}
cols = append(cols, reflectStruct.Field(i).Name)
}
return cols, nil
}
// getResultSlice - Get the results of a row iterator as a slice.
//
// Params:
// iter *spanner.RowIterator - The iterator from a query or read.
//
// Return:
// []*spanner.Row - A list of all rows returned by the query.
// error - An error if it occurred.
func getResultSlice(iter *spanner.RowIterator) ([]*spanner.Row, error) {
var results []*spanner.Row
defer iter.Stop()
for {
row, err := iter.Next()
if err == iterator.Done {
return results, nil
}
if err != nil {
return nil, err
}
results = append(results, row)
}
}