/
document.go
84 lines (64 loc) · 1.6 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
package index
import (
"strconv"
"time"
"github.com/ttab/newsdoc"
"golang.org/x/exp/slices"
)
// DocumentState is the full state that we want to index.
type DocumentState struct {
Created time.Time `json:"created"`
Modified time.Time `json:"modified"`
CurrentVersion int64 `json:"current_version"`
ACL []ACLEntry `json:"acl"`
Heads map[string]Status `json:"heads"`
Document newsdoc.Document `json:"document"`
}
type Document struct {
Fields map[string]Field
}
func NewDocument() *Document {
return &Document{
Fields: make(map[string]Field),
}
}
func (d *Document) AddTime(name string, value time.Time) {
v := value.Format(time.RFC3339)
d.AddField(name, TypeDate, v)
}
func (d *Document) AddInteger(name string, value int64) {
v := strconv.FormatInt(value, 10)
d.AddField(name, TypeLong, v)
}
func (d *Document) SetField(name string, t FieldType, values ...string) {
d.Fields[name] = Field{
Type: t,
Values: values,
}
}
func (d *Document) AddField(name string, t FieldType, value string) {
e := d.Fields[name]
if t.Priority() > e.Type.Priority() {
e.Type = t
}
if !slices.Contains(e.Values, value) {
e.Values = append(e.Values, value)
}
d.Fields[name] = e
}
func (d *Document) Mappings() Mappings {
m := Mappings{
Properties: make(map[string]Mapping),
}
for name, def := range d.Fields {
m.Properties[name] = Mapping{Type: def.Type}
}
return m
}
func (d *Document) Values() map[string][]string {
v := make(map[string][]string)
for k := range d.Fields {
v[k] = d.Fields[k].Values
}
return v
}