forked from go-kivik/kivik
-
Notifications
You must be signed in to change notification settings - Fork 0
/
changes.go
56 lines (51 loc) · 1.65 KB
/
changes.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
package driver
import "encoding/json"
// Changes is an iterator of the database changes feed.
type Changes interface {
// Next is called to populate *Change with the next value in the changes
// feed.
//
// Next should return io.EOF when the changes feed is closed by request.
Next(*Change) error
// Close closes the changes feed iterator.
Close() error
// LastSeq returns the last change update sequence.
LastSeq() string
// Pending returns the count of remaining items in the feed
Pending() int64
// ETag returns the unquoted ETag header, if present.
ETag() string
}
// Change represents the changes to a single document.
type Change struct {
// ID is the document ID to which the change relates.
ID string `json:"id"`
// Seq is the update sequence for the changes feed.
Seq string `json:"seq"`
// Deleted is set to true for the changes feed, if the document has been
// deleted.
Deleted bool `json:"deleted"`
// Changes represents a list of document leaf revisions for the /_changes
// endpoint.
Changes ChangedRevs `json:"changes"`
// Doc is the raw, un-decoded JSON document. This is only populated when
// include_docs=true is set.
Doc json.RawMessage `json:"doc"`
}
// ChangedRevs represents a "changes" field of a result in the /_changes stream.
type ChangedRevs []string
// UnmarshalJSON satisfies the json.Unmarshaler interface
func (c *ChangedRevs) UnmarshalJSON(data []byte) error {
var changes []struct {
Rev string `json:"rev"`
}
if err := json.Unmarshal(data, &changes); err != nil {
return err
}
revs := ChangedRevs(make([]string, len(changes)))
for i, change := range changes {
revs[i] = change.Rev
}
*c = revs
return nil
}