This repository has been archived by the owner on Jun 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathencode.go
169 lines (125 loc) · 3.44 KB
/
encode.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
package cloth
import (
"bytes"
"encoding/binary"
"fmt"
"reflect"
"time"
"github.com/fatih/structs"
"github.com/osamingo/boolconv"
"google.golang.org/cloud/bigtable"
)
// GenerateColumnsMutation generates Mutation from Struct.
func GenerateColumnsMutation(family string, t time.Time, i interface{}) (m *bigtable.Mutation, err error) {
m = bigtable.NewMutation()
err = SetColumns(family, t, m, i)
return
}
// GenerateColumnQualifiersMutation generates Mutation from Slice.
func GenerateColumnQualifiersMutation(family string, t time.Time, slice interface{}) (m *bigtable.Mutation, err error) {
m = bigtable.NewMutation()
err = SetColumnQualifiers(family, t, m, slice)
return
}
// SetColumns sets columns of Mutation by Struct.
func SetColumns(family string, t time.Time, m *bigtable.Mutation, i interface{}) (err error) {
if family == "" {
err = fmt.Errorf("cloth: family should not be empty")
return
}
if i == nil {
err = fmt.Errorf("cloth: struct should not be nil")
return
}
fs := structs.New(i).Fields()
if len(fs) == 0 {
err = fmt.Errorf("cloth: fields are not found, %v", i)
return
}
for _, f := range fs {
tg := f.Tag(BigtableTagName)
if tg == "" {
continue
}
ti := GetBigtableTagInfo(tg)
if ti.Ignore || ti.Column == "" || ti.Omitempty && f.IsZero() {
continue
}
var b []byte
b, err = getBytes(f)
if err != nil {
m = nil
break
}
m.Set(family, ti.Column, bigtable.Time(t), b)
}
return
}
// SetColumnQualifiers sets column qualifiers of Mutation by Slice.
func SetColumnQualifiers(family string, t time.Time, m *bigtable.Mutation, slice interface{}) (err error) {
if family == "" {
err = fmt.Errorf("cloth: family should not be empty")
return
}
s := reflect.ValueOf(slice)
if s.Kind() != reflect.Slice {
err = fmt.Errorf("cloth: slice should be type slice")
return
}
if s.Len() == 0 {
err = fmt.Errorf("cloth: slice should not be empty")
return
}
for i := 0; i < s.Len(); i++ {
fs := structs.New(s.Index(i).Interface()).Fields()
if len(fs) == 0 {
err = fmt.Errorf("cloth: fields are not found, %v", i)
return
}
for _, f := range fs {
tg := f.Tag(BigtableTagName)
if tg == "" {
continue
}
ti := GetBigtableTagInfo(tg)
if ti.Qualifier && !f.IsZero() {
m.Set(family, fmt.Sprintf("%s", f.Value()), bigtable.Time(t), nil)
}
}
}
return
}
func getBytes(f *structs.Field) ([]byte, error) {
var b *bytes.Buffer
switch f.Kind() {
case reflect.Slice:
if reflect.ValueOf(f.Value()).Type().Elem().Kind() == reflect.Uint8 {
// []byte
return (f.Value()).([]byte), nil
}
case reflect.String:
return []byte((f.Value()).(string)), nil
case reflect.Bool:
return boolconv.NewBool((f.Value()).(bool)).Bytes(), nil
case reflect.Int8, reflect.Uint8:
b = bytes.NewBuffer(make([]byte, 0, 2))
case reflect.Int16, reflect.Uint16:
b = bytes.NewBuffer(make([]byte, 0, binary.MaxVarintLen16))
case reflect.Int32, reflect.Uint32:
b = bytes.NewBuffer(make([]byte, 0, binary.MaxVarintLen32))
case reflect.Int64, reflect.Uint64, reflect.Int, reflect.Uint, reflect.Float32, reflect.Float64:
b = bytes.NewBuffer(make([]byte, 0, binary.MaxVarintLen64))
}
if b != nil {
i := f.Value()
if f.Kind() == reflect.Int {
i = int64(i.(int))
}
if f.Kind() == reflect.Uint {
i = uint64(i.(uint))
}
err := binary.Write(b, binary.BigEndian, i)
return b.Bytes(), err
}
return nil, fmt.Errorf("cloth: unsupported type. %v", f.Kind())
}