-
Notifications
You must be signed in to change notification settings - Fork 1
/
event.go
116 lines (93 loc) · 2.76 KB
/
event.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package sinks
import (
"regexp"
"slices"
"github.com/ttab/elephant-repository/repository"
"github.com/ttab/newsdoc"
)
type EventDetail struct {
Event repository.Event `json:"event"`
Document *DocumentDetail `json:"document"`
}
type DocumentDetail struct {
UUID string `json:"uuid"`
URI string `json:"uri"`
Type string `json:"type"`
Title string `json:"title"`
LinkTypes []string `json:"link_types,omitempty"`
Links map[string][]DocumentLink `json:"rels"`
MetaTypes []string `json:"meta_types,omitempty"`
Meta map[string][]DocumentMeta `json:"meta"`
ContentTypes []string `json:"content_types,omitempty"`
ContentUUIDs []string `json:"content_uuids,omitempty"`
ContentURIs []string `json:"content_uris,omitempty"`
}
type DocumentMeta struct {
Role string `json:"role,omitempty"`
Value string `json:"value,omitempty"`
Data newsdoc.DataMap `json:"data,omitempty"`
}
type DocumentLink struct {
UUID string `json:"uuid,omitempty"`
URI string `json:"uri,omitempty"`
Type string `json:"type,omitempty"`
Role string `json:"role,omitempty"`
Value string `json:"value,omitempty"`
}
var nonAlphaNum = regexp.MustCompile(`[^a-zA-Z0-9 ]+`)
func DetailFromDocument(d newsdoc.Document) *DocumentDetail {
e := DocumentDetail{
UUID: d.UUID,
URI: d.URI,
Type: d.Type,
Title: d.Title,
Links: make(map[string][]DocumentLink),
Meta: make(map[string][]DocumentMeta),
}
for _, l := range d.Links {
dl := DocumentLink{
UUID: l.UUID,
URI: l.URI,
Type: l.Type,
Role: l.Role,
Value: l.Value,
}
if !slices.Contains(e.LinkTypes, l.Type) {
e.LinkTypes = append(e.LinkTypes, l.Type)
}
relKey := nonAlphaNum.ReplaceAllString(l.Rel, "_")
e.Links[relKey] = append(e.Links[relKey], dl)
}
for _, m := range d.Meta {
dm := DocumentMeta{
Role: m.Role,
Value: m.Value,
}
if !slices.Contains(e.MetaTypes, m.Type) {
e.MetaTypes = append(e.MetaTypes, m.Type)
}
for k, v := range m.Data {
if k == "text" {
continue
}
if dm.Data == nil {
dm.Data = make(newsdoc.DataMap)
}
dm.Data[k] = v
}
typeKey := nonAlphaNum.ReplaceAllString(m.Type, "_")
e.Meta[typeKey] = append(e.Meta[typeKey], dm)
}
for _, c := range d.Content {
if !slices.Contains(e.ContentTypes, c.Type) {
e.ContentTypes = append(e.ContentTypes, c.Type)
}
if c.URI != "" && !slices.Contains(e.ContentURIs, c.URI) {
e.ContentURIs = append(e.ContentURIs, c.URI)
}
if c.UUID != "" && !slices.Contains(e.ContentUUIDs, c.UUID) {
e.ContentUUIDs = append(e.ContentUUIDs, c.UUID)
}
}
return &e
}