-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocument.go
95 lines (83 loc) · 2.05 KB
/
document.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
package couchdb
import (
"context"
"errors"
"net/http"
)
// Various errors.
var (
ErrMissingID = errors.New("missing id")
)
// Document implements all methods on a couchdb document.
type Document struct {
database *Database
id string
revision string
}
// NewDocument returns a new document.
func NewDocument(database *Database, id, revision string) *Document {
return &Document{
database: database,
id: id,
revision: revision,
}
}
// ID returns the document's id.
func (d *Document) ID() string {
return d.id
}
// Revision returns the document's revision.
func (d *Document) Revision() string {
return d.revision
}
// Store saves the document to the database. If the id is empty, it will be save at
// a generated id.
func (d *Document) Store(ctx context.Context, data interface{}) error {
if d.id == "" {
return d.storeWithoutID(ctx, data)
}
return d.storeWithID(ctx, data)
}
// Fetch loads the document from the database.
func (d *Document) Fetch(ctx context.Context, data interface{}) error {
if d.id == "" {
return ErrMissingID
}
if err := d.database.client.requestJSON(ctx, http.MethodGet, "/"+d.database.name+"/"+d.id, nil, nil, data); err != nil {
return err
}
return nil
}
func (d *Document) storeWithoutID(ctx context.Context, data interface{}) error {
r := struct {
OK bool `json:"ok"`
ID string `json:"id"`
Revision string `json:"rev"`
}{}
if err := d.database.client.requestJSON(ctx, http.MethodPost, "/"+d.database.name, nil, data, &r); err != nil {
return err
}
if r.OK {
d.id = r.ID
d.revision = r.Revision
}
return nil
}
func (d *Document) storeWithID(ctx context.Context, data interface{}) error {
header := http.Header{}
if d.revision != "" {
header.Add("If-Match", d.revision)
}
r := struct {
OK bool `json:"ok"`
ID string `json:"id"`
Revision string `json:"rev"`
}{}
if err := d.database.client.requestJSON(ctx, http.MethodPut, "/"+d.database.name+"/"+d.id, header, data, &r); err != nil {
return err
}
if r.OK {
d.revision = r.Revision
}
return nil
}