forked from leesper/couchdb-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapping.go
226 lines (186 loc) · 5.61 KB
/
mapping.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package couchdb
import (
"encoding/json"
"errors"
"reflect"
)
var (
// ErrSetID for setting ID to document which already has one.
ErrSetID = errors.New("id can only be set on new documents")
// ErrNotStruct for not a struct value
ErrNotStruct = errors.New("value not of struct type")
// ErrNotDocumentEmbedded for not a document-embedded value
ErrNotDocumentEmbedded = errors.New("value not Document-embedded")
zero = reflect.Value{}
)
// Document represents a document object in database.
type Document struct {
id string
rev string
ID string `json:"_id,omitempty"` // for json only, call SetID/GetID instead
Rev string `json:"_rev,omitempty"` // for json only, call GetRev instead
}
// DocumentWithID returns a new Document with ID.
func DocumentWithID(id string) Document {
return Document{
id: id,
}
}
// SetID sets ID for new document or return error.
func (d *Document) SetID(id string) error {
if d.id != "" {
return ErrSetID
}
d.id = id
return nil
}
// GetID returns the document ID.
func (d *Document) GetID() string {
return d.id
}
// SetRev sets revision for document.
func (d *Document) SetRev(rev string) {
d.rev = rev
}
// GetRev returns the document revision.
func (d *Document) GetRev() string {
return d.rev
}
// Store stores the document in specified database.
// obj: a Document-embedded struct value, its id and rev will be updated after stored,
// so caller must pass a pointer value.
func Store(db *Database, obj interface{}) error {
ptrValue := reflect.ValueOf(obj)
if ptrValue.Kind() != reflect.Ptr || ptrValue.Elem().Kind() != reflect.Struct {
return ErrNotStruct
}
if ptrValue.Elem().FieldByName("Document") == zero {
return ErrNotDocumentEmbedded
}
jsonIDField := ptrValue.Elem().FieldByName("ID")
getIDMethod := ptrValue.MethodByName("GetID")
idStr := getIDMethod.Call([]reflect.Value{})[0].Interface().(string)
if idStr != "" {
jsonIDField.SetString(idStr)
}
jsonRevField := ptrValue.Elem().FieldByName("Rev")
getRevMethod := ptrValue.MethodByName("GetRev")
revStr := getRevMethod.Call([]reflect.Value{})[0].Interface().(string)
if revStr != "" {
jsonRevField.SetString(revStr)
}
doc, err := ToJSONCompatibleMap(ptrValue.Elem().Interface())
if err != nil {
return err
}
id, rev, err := db.Save(doc, nil)
if err != nil {
return err
}
setIDMethod := ptrValue.MethodByName("SetID")
setRevMethod := ptrValue.MethodByName("SetRev")
if idStr == "" {
setIDMethod.Call([]reflect.Value{reflect.ValueOf(id)})
}
setRevMethod.Call([]reflect.Value{reflect.ValueOf(rev)})
jsonRevField.SetString(rev)
return nil
}
// Load loads the document in specified database.
func Load(db *Database, docID string, obj interface{}) error {
ptrValue := reflect.ValueOf(obj)
if ptrValue.Kind() != reflect.Ptr || ptrValue.Elem().Kind() != reflect.Struct {
return ErrNotStruct
}
if ptrValue.Elem().FieldByName("Document") == zero {
return ErrNotDocumentEmbedded
}
doc, err := db.Get(docID, nil)
if err != nil {
return err
}
err = FromJSONCompatibleMap(obj, doc)
if err != nil {
return err
}
if id, ok := doc["_id"]; ok {
setIDMethod := ptrValue.MethodByName("SetID")
setIDMethod.Call([]reflect.Value{reflect.ValueOf(id)})
}
if rev, ok := doc["_rev"]; ok {
setRevMethod := ptrValue.MethodByName("SetRev")
setRevMethod.Call([]reflect.Value{reflect.ValueOf(rev)})
}
return nil
}
// FromJSONCompatibleMap constructs a Document-embedded struct from a JSON-compatible map.
func FromJSONCompatibleMap(obj interface{}, docMap map[string]interface{}) error {
ptrValue := reflect.ValueOf(obj)
if ptrValue.Kind() != reflect.Ptr || ptrValue.Elem().Kind() != reflect.Struct {
return ErrNotStruct
}
if ptrValue.Elem().FieldByName("Document") == zero {
return ErrNotDocumentEmbedded
}
data, err := json.Marshal(docMap)
if err != nil {
return err
}
err = json.Unmarshal(data, obj)
if err != nil {
return err
}
if id, ok := docMap["_id"]; ok {
setIDMethod := ptrValue.MethodByName("SetID")
setIDMethod.Call([]reflect.Value{reflect.ValueOf(id)})
}
if rev, ok := docMap["_rev"]; ok {
setRevMethod := ptrValue.MethodByName("SetRev")
setRevMethod.Call([]reflect.Value{reflect.ValueOf(rev)})
}
return nil
}
// ToJSONCompatibleMap converts a Document-embedded struct into a JSON-compatible map,
// e.g. anything that cannot be jsonified will be ignored silently.
func ToJSONCompatibleMap(obj interface{}) (map[string]interface{}, error) {
structValue := reflect.ValueOf(obj)
if structValue.Kind() != reflect.Struct {
return nil, ErrNotStruct
}
zero := reflect.Value{}
if structValue.FieldByName("Document") == zero {
return nil, ErrNotDocumentEmbedded
}
data, err := json.Marshal(obj)
if err != nil {
return nil, err
}
doc := map[string]interface{}{}
err = json.Unmarshal(data, &doc)
if err != nil {
return nil, err
}
return doc, nil
}
// ViewField represents a view definition value bound to Document.
type ViewField func() (*ViewDefinition, error)
// NewViewField returns a ViewField function.
// design: the name of the design document.
//
// name: the name of the view.
//
// mapFun: the map function code.
//
// reduceFun: the reduce function code(optional).
//
// language: the name of the programming language used, default is javascript.
//
// wrapper: an optional function for processing the result rows after retrieved.
//
// options: view specific options.
func NewViewField(design, name, mapFun, reduceFun, language string, wrapper func(Row) Row, options map[string]interface{}) ViewField {
f := func() (*ViewDefinition, error) {
return NewViewDefinition(design, name, mapFun, reduceFun, language, wrapper, options)
}
return ViewField(f)
}