/
edtf.go
60 lines (40 loc) · 1003 Bytes
/
edtf.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
package edtf
import (
"fmt"
"strings"
"github.com/sfomuseum/go-edtf"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)
func UpdateBytes(body []byte) (bool, []byte, error) {
props := gjson.GetBytes(body, "properties")
if !props.Exists() {
return false, nil, fmt.Errorf("Missing properties")
}
changed := false
for k, v := range props.Map() {
if !strings.HasPrefix(k, "edtf:") {
continue
}
path := fmt.Sprintf("properties.%s", k)
var err error
switch v.String() {
case "open":
body, err = sjson.SetBytes(body, path, edtf.OPEN)
if err != nil {
return false, nil, fmt.Errorf("Failed to assign %s, %w", path, err)
}
changed = true
case "uuuu":
body, err = sjson.SetBytes(body, path, edtf.UNKNOWN)
if err != nil {
return false, nil, fmt.Errorf("Failed to assign %s, %w", path, err)
}
changed = true
default:
// https://github.com/whosonfirst/go-whosonfirst-edtf/issues
// pass
}
}
return changed, body, nil
}