-
Notifications
You must be signed in to change notification settings - Fork 0
/
metadata.go
58 lines (51 loc) · 1.31 KB
/
metadata.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
package nidhi
import (
jsoniter "github.com/json-iterator/go"
)
// Metadata is the metadata stored against a document.
//
// Metadata is designed to be extendible. These should typically be document agnostic information.
// Eg:
// - Activity log (create, update timestamps)
// - Common information extracted from the document
type Metadata map[string]MetadataPart
type MetadataPart interface {
// MarshalMD marshals the metadata.
MarshalMDP(w *jsoniter.Stream)
// UnmarshalMD unmarshals the metdata.
UnmarshalMDP(r *jsoniter.Iterator)
}
// MetadataField is a [Field] for metadata parts
//
// Metadata parts can return a MetadataField
type MetadataField struct {
Part string
Type string
Default string
}
func (f *MetadataField) Selector() string {
return `JSON_VALUE(` + ColMeta + `::jsonb, '$.` + f.Part + `' RETURNING ` + f.Type + ` DEFAULT ` + f.Default + ` ON EMPTY)`
}
func (m Metadata) writeJSON(w *jsoniter.Stream) {
w.WriteObjectStart()
var count int
for k, v := range m {
count++
w.WriteObjectField(k)
v.MarshalMDP(w)
if count != len(m) {
w.WriteMore()
}
}
w.WriteObjectEnd()
}
func (m Metadata) readJSON(r *jsoniter.Iterator) {
for field := r.ReadObject(); field != ""; field = r.ReadObject() {
part, ok := m[field]
if ok {
part.UnmarshalMDP(r)
} else {
r.Skip()
}
}
}