-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument.go
More file actions
49 lines (41 loc) · 1.62 KB
/
document.go
File metadata and controls
49 lines (41 loc) · 1.62 KB
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
package sofa
import "encoding/json"
// Document is the interface which represents a CouchDB document. Contains methods to retrieve the
// metadata and the database containing this document.
type Document interface {
Metadata() DocumentMetadata
}
// DocumentMetadata is the minimum amount of content which all documents have. It is the
// identity and revision of the document.
type DocumentMetadata struct {
ID string `json:"_id,omitempty"`
Rev string `json:"_rev,omitempty"`
}
// Metadata provides a default implementation of the Document interface which is used when
// a DocumentMetadata is embedded in another struct.
func (md DocumentMetadata) Metadata() DocumentMetadata {
return md
}
// GenericDocument implements the Document API and can be used to represent any type
// of document. For all but simple cases it is better to implement this yourself ona struct
// for easier access to the unmarshalled data.
type GenericDocument struct {
document map[string]interface{}
}
// Metadata gets the DocumentMetadata for this Document
func (gen *GenericDocument) Metadata() DocumentMetadata {
return DocumentMetadata{
ID: gen.document["_id"].(string),
Rev: gen.document["_rev"].(string),
}
}
// MarshalJSON provides an implementation of json.Marshaler by marshaling the stored map document
// into JSON data.
func (gen *GenericDocument) MarshalJSON() ([]byte, error) {
return json.Marshal(gen.document)
}
// UnmarshalJSON provides an implementation of json.Unmarshaler by unmarshaling the provides
// data into the stored map.
func (gen *GenericDocument) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, &gen.document)
}