1 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 2 // use this file except in compliance with the License. You may obtain a copy of 3 // the License at 4 // 5 // http://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 // License for the specific language governing permissions and limitations under 11 // the License. 12 13 package driver 14 15 import "encoding/json" 16 17 // Changes is an iterator of the database changes feed. 18 type Changes interface { 19 // Next is called to populate change with the next value in the feed. 20 // 21 // Next should return [io.EOF] when the changes feed is closed by request. 22 Next(change *Change) error 23 // Close closes the changes feed iterator. 24 Close() error 25 // LastSeq returns the last change update sequence. 26 LastSeq() string 27 // Pending returns the count of remaining items in the feed 28 Pending() int64 29 // ETag returns the unquoted ETag header, if present. 30 ETag() string 31 } 32 33 // Change represents the changes to a single document. 34 type Change struct { 35 // ID is the document ID to which the change relates. 36 ID string `json:"id"` 37 // Seq is the update sequence for the changes feed. 38 Seq string `json:"seq"` 39 // Deleted is set to true for the changes feed, if the document has been 40 // deleted. 41 Deleted bool `json:"deleted"` 42 // Changes represents a list of document leaf revisions for the /_changes 43 // endpoint. 44 Changes ChangedRevs `json:"changes"` 45 // Doc is the raw, un-decoded JSON document. This is only populated when 46 // include_docs=true is set. 47 Doc json.RawMessage `json:"doc"` 48 } 49 50 // ChangedRevs represents a "changes" field of a result in the /_changes stream. 51 type ChangedRevs []string 52 53 // UnmarshalJSON satisfies the json.Unmarshaler interface 54 func (c *ChangedRevs) UnmarshalJSON(data []byte) error { 55 var changes []struct { 56 Rev string `json:"rev"` 57 } 58 if err := json.Unmarshal(data, &changes); err != nil { 59 return err 60 } 61 revs := ChangedRevs(make([]string, len(changes))) 62 for i, change := range changes { 63 revs[i] = change.Rev 64 } 65 *c = revs 66 return nil 67 } 68