-
Notifications
You must be signed in to change notification settings - Fork 0
/
concordances.go
107 lines (73 loc) · 1.9 KB
/
concordances.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
package document
import (
"context"
"fmt"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)
func AppendConcordancesMachineTags(ctx context.Context, body []byte) ([]byte, error) {
root := gjson.ParseBytes(body)
props_rsp := gjson.GetBytes(body, "properties")
if props_rsp.Exists() {
root = props_rsp
}
concordances_rsp := root.Get("wof:concordances")
if !concordances_rsp.Exists() {
return body, nil
}
tags := make([]string, 0)
for k, v := range concordances_rsp.Map() {
mt := fmt.Sprintf("%s=%s", k, v.String())
tags = append(tags, mt)
}
updates := map[string]interface{}{
"wof:concordances_machinetags": tags,
}
var err error
for k, v := range updates {
path := k
if props_rsp.Exists() {
path = fmt.Sprintf("properties.%s", k)
}
body, err = sjson.SetBytes(body, path, v)
if err != nil {
return nil, err
}
}
return body, nil
}
// AppendConcordancesStats appends statistics about the `wof:concordances` properties in a Who's On First document.
// Specifically:
// * An array containing the set of source prefixes for concordances
// * The total number of concordances in a record.
func AppendConcordancesStats(ctx context.Context, body []byte) ([]byte, error) {
root := gjson.ParseBytes(body)
props_rsp := gjson.GetBytes(body, "properties")
if props_rsp.Exists() {
root = props_rsp
}
concordances_rsp := root.Get("wof:concordances")
if !concordances_rsp.Exists() {
return body, nil
}
sources := make([]string, 0)
for k, _ := range concordances_rsp.Map() {
sources = append(sources, k)
}
stats := map[string]interface{}{
"wof:concordances_sources": sources,
"counts:concordances_total": len(sources),
}
var err error
for k, v := range stats {
path := k
if props_rsp.Exists() {
path = fmt.Sprintf("properties.%s", k)
}
body, err = sjson.SetBytes(body, path, v)
if err != nil {
return nil, err
}
}
return body, nil
}