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
/
decode.go
154 lines (116 loc) · 2.8 KB
/
decode.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
package cloth
import (
"bytes"
"encoding/binary"
"fmt"
"reflect"
"strings"
"github.com/fatih/structs"
"github.com/osamingo/boolconv"
"google.golang.org/cloud/bigtable"
)
// ReadColumnQualifier returns column qualifiers.
func ReadColumnQualifier(ris []bigtable.ReadItem) (cqs []string) {
for i := range ris {
cs := strings.Split(ris[i].Column, ColumnQualifierDelimiter)
cqs = append(cqs, cs[len(cs)-1])
}
return
}
// ReadItems converts Mutation into Struct.
func ReadItems(ris []bigtable.ReadItem, s interface{}) (err error) {
if len(ris) == 0 || s == nil {
return
}
fs := structs.New(s).Fields()
if len(fs) == 0 {
return
}
for i := range ris {
for _, f := range fs {
t := f.Tag(BigtableTagName)
if t == "" {
continue
}
ti := GetBigtableTagInfo(t)
if ti.RowKey {
if err = setValue(f, []byte(ris[i].Row)); err != nil {
return
}
continue
}
cs := strings.Split(ris[i].Column, ColumnQualifierDelimiter)
if cs[len(cs)-1] == ti.Column {
if err = setValue(f, ris[i].Value); err != nil {
return
}
continue
}
}
}
return
}
func setValue(f *structs.Field, val []byte) (err error) {
switch f.Kind() {
case reflect.Slice:
if reflect.ValueOf(f.Value()).Type().Elem().Kind() == reflect.Uint8 {
// []byte
f.Set(val)
}
case reflect.String:
f.Set(string(val))
case reflect.Bool:
f.Set(boolconv.BtoB(val).Tob())
case reflect.Int:
var n int64
err = binary.Read(bytes.NewReader(val), binary.BigEndian, &n)
f.Set(int(n))
case reflect.Uint:
var n uint64
err = binary.Read(bytes.NewReader(val), binary.BigEndian, &n)
f.Set(uint(n))
case reflect.Int8:
var n int8
err = binary.Read(bytes.NewReader(val), binary.BigEndian, &n)
f.Set(n)
case reflect.Uint8:
var n uint8
err = binary.Read(bytes.NewReader(val), binary.BigEndian, &n)
f.Set(n)
case reflect.Int16:
var n int16
err = binary.Read(bytes.NewReader(val), binary.BigEndian, &n)
f.Set(n)
case reflect.Uint16:
var n uint16
err = binary.Read(bytes.NewReader(val), binary.BigEndian, &n)
f.Set(n)
case reflect.Int32:
var n int32
err = binary.Read(bytes.NewReader(val), binary.BigEndian, &n)
f.Set(n)
case reflect.Uint32:
var n uint32
err = binary.Read(bytes.NewReader(val), binary.BigEndian, &n)
f.Set(n)
case reflect.Int64:
var n int64
err = binary.Read(bytes.NewReader(val), binary.BigEndian, &n)
f.Set(n)
case reflect.Uint64:
var n uint64
err = binary.Read(bytes.NewReader(val), binary.BigEndian, &n)
f.Set(n)
case reflect.Float32:
var n float32
err = binary.Read(bytes.NewReader(val), binary.BigEndian, &n)
f.Set(n)
case reflect.Float64:
var n float64
err = binary.Read(bytes.NewReader(val), binary.BigEndian, &n)
f.Set(n)
default:
err = fmt.Errorf("cloth: unsupported type. %v", f.Kind())
}
return
}