-
Notifications
You must be signed in to change notification settings - Fork 0
/
struct_marshal.go
205 lines (184 loc) · 5.52 KB
/
struct_marshal.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
203
204
205
package riaken_struct
import (
"encoding/json"
"errors"
"fmt"
"reflect"
"strconv"
"strings"
)
import (
"github.com/riaken/riaken-core/rpb"
)
// This package was lifted from https://github.com/mrb/riakpbc/blob/master/coder.go
// However, it was authored by myself. - Brian Jones
// typeOfBytes is a special check against Slices of []byte.
var typeOfBytes = reflect.TypeOf([]byte(nil))
// MarshalMethod is the method signature of a marshaller.
type MarshalMethod func(interface{}, *rpb.RpbContent) error
// UnmarshalMethod is the method signature of a unmarshaller.
type UnmarshalMethod func([]byte, interface{}) error
// StructMarshal contains a tag, marshaller, and unmarshaller.
// It's primary duty is to convert data from `tag` format to and from a composed struct.
type StructMarshal struct {
tag string // the tag to match for the marshaller
marshaller MarshalMethod // the method to run on the data
unmarshaller UnmarshalMethod // the method to extra the data
}
// JsonMarshaller is an example of a MarshalMethod that is passed to NewEncode().
//
// If a different data marshaller is desired, such as XML, YAML, etc. Use this as a template.
func JsonMarshaller(in interface{}, out *rpb.RpbContent) error {
jsondata, err := json.Marshal(in)
if err != nil {
return err
}
out.Value = jsondata
out.ContentType = []byte("application/json")
return nil
}
// JsonUnmarshaller is an example of an UnmarshallMethod that is passed to NewEncode().
func JsonUnmarshaller(in []byte, out interface{}) error {
err := json.Unmarshal(in, out)
if err != nil {
return err
}
return nil
}
// NewStructMarshal requires a tag and MarshalMethod.
func NewStructMarshal(tag string, marshaller MarshalMethod, unmarshaller UnmarshalMethod) *StructMarshal {
c := new(StructMarshal)
c.tag = tag
c.marshaller = marshaller
c.unmarshaller = unmarshaller
return c
}
// Marshal takes a struct with `riak` tagged fields and builds the correct
// RpbContent to send along to Riak.
//
// Any fields of type string are set as a _bin index, and fields of any
// int type set to an _int index.
//
// Examples:
//
// // Field is a _bin index
// Field string `riak:"index"`
//
// // Field is an _int index
// Field int `riak:"index"`
//
// // Field is a _bin index and also a json field in the actual data.
// Field string `json:"field" riak:"index"`
func (c *StructMarshal) Marshal(data interface{}) (*rpb.RpbContent, error) {
t := reflect.ValueOf(data)
if t.Kind() != reflect.Ptr {
return nil, errors.New(fmt.Sprintf("Expected a pointer not %s", t.Kind()))
}
// Output
out := &rpb.RpbContent{}
e := t.Elem()
switch e.Kind() {
case reflect.Struct:
c.process("", e, out)
if err := c.marshaller(&data, out); err != nil {
return nil, err
}
break
default:
return nil, errors.New("Marshal expected a struct")
}
return out, nil
}
// Unmarshal unwraps the database data into the passed structure based on the defined marshaller.
func (c *StructMarshal) Unmarshal(in []byte, data interface{}) error {
return c.unmarshaller(in, data)
}
func (c *StructMarshal) process(carry string, e reflect.Value, out *rpb.RpbContent) {
for i := 0; i < e.NumField(); i++ {
if !e.Field(i).CanSet() {
continue
}
val := e.Field(i).Interface()
fld := e.Type().Field(i)
knd := e.Field(i).Kind()
tag := fld.Tag
// Skip anonymous fields
if fld.Anonymous {
continue
}
// Continue to process nested structs
if knd == reflect.Struct {
c.process(fld.Name+"_", e.Field(i), out)
}
if tag.Get(c.tag) == "" {
continue
}
if tdata := tag.Get("riak"); tdata != "" {
for _, tfield := range strings.Split(tdata, ",") {
switch tfield {
case "index":
index := &rpb.RpbPair{}
var key string
switch knd {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
key = carry + fld.Name + "_int"
switch knd {
case reflect.Int:
index.Value = []byte(strconv.Itoa(int(val.(int))))
break
case reflect.Int8:
index.Value = []byte(strconv.Itoa(int(val.(int8))))
break
case reflect.Int16:
index.Value = []byte(strconv.Itoa(int(val.(int16))))
break
case reflect.Int32:
index.Value = []byte(strconv.Itoa(int(val.(int32))))
break
case reflect.Int64:
index.Value = []byte(strconv.Itoa(int(val.(int64))))
break
}
index.Key = []byte(strings.ToLower(key))
break
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
key = carry + fld.Name + "_int"
switch knd {
case reflect.Uint:
index.Value = []byte(strconv.Itoa(int(val.(uint))))
break
case reflect.Uint8:
index.Value = []byte(strconv.Itoa(int(val.(uint8))))
break
case reflect.Uint16:
index.Value = []byte(strconv.Itoa(int(val.(uint16))))
break
case reflect.Uint32:
index.Value = []byte(strconv.Itoa(int(val.(uint32))))
break
case reflect.Uint64:
index.Value = []byte(strconv.Itoa(int(val.(uint64))))
break
}
index.Key = []byte(strings.ToLower(key))
break
case reflect.String:
key = carry + fld.Name + "_bin"
index.Key = []byte(strings.ToLower(key))
index.Value = []byte(val.(string))
break
case reflect.Slice:
if fld.Type == typeOfBytes {
key = carry + fld.Name + "_bin"
index.Key = []byte(strings.ToLower(key))
index.Value = []byte(val.([]byte))
}
break
}
out.Indexes = append(out.Indexes, index)
break
}
}
}
}
}