This repository has been archived by the owner on Sep 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
versioning.go
116 lines (92 loc) · 3.24 KB
/
versioning.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 metax
import (
"time"
"github.com/tidwall/gjson"
)
var (
// IdentifierKey is the key pointing to the primary identifier, if any.
IdentifierKey = "identifier"
// NewVersionKey is the key to the value indicating a new version has been created.
NewVersionKey = "new_version_created"
// NewVersionId is the key holding the identifier of the new version or an empty string.
NewVersionIdKey = "new_version_created.identifier"
// Editor is the key pointing to Qvain's private object inside the dataset.
EditorKey = "editor"
// QvainId is the key with the Qvain record id.
QvainIdKey = "record_id"
// QvainId is the key with the identifier "qvain".
QvainIdentifierKey = "identifier"
// DateCreatedKey is the key for the Metax dataset creation timestamp.
DateCreatedKey = "date_created"
// DateModifiedKey is the key for the Metax dataset modification timestamp.
DateModifiedKey = "date_modified"
// DateDeprecatedKey is the key for the Metax dataset deprecation timestamp.
DateDeprecatedKey = "date_deprecated"
// DateRemovedKey is the key for the Metax dataset removal timestamp.
DateRemovedKey = "date_removed"
)
func GetIdentifier(blob []byte) string {
if len(blob) < 1 {
return ""
}
return gjson.GetBytes(blob, IdentifierKey).String()
}
// GetModificationDate returns the date of last modification (created, modified, deprecated, removed) for a dataset.
func GetModificationDate(blob []byte) time.Time {
if len(blob) < 1 {
return time.Time{}
}
date := time.Time{}
results := gjson.GetManyBytes(blob, DateCreatedKey, DateModifiedKey, DateDeprecatedKey, DateRemovedKey)
for _, result := range results {
d := result.Time()
if d.After(date) {
date = d
}
}
return date
}
func IsPublished(blob []byte) bool {
if len(blob) < 1 {
return false
}
return gjson.GetBytes(blob, IdentifierKey).Exists()
}
// CreatedNewVersion returns a boolean indicating whether the new version created key exists.
// Note this doesn't check if its value is valid.
func CreatedNewVersion(blob []byte) bool {
if len(blob) < 1 {
return false
}
result := gjson.GetBytes(blob, NewVersionKey)
return result.Exists()
}
// MaybeNewVersionId returns the id of a new Metax version if a new version was created, otherwise an empty string.
func MaybeNewVersionId(blob []byte) string {
if len(blob) < 1 {
return ""
}
return gjson.GetBytes(blob, NewVersionIdKey).String()
}
// GetQvainId returns the Qvain id of the dataset from the editor object or empty string.
func GetQvainId(blob []byte) string {
result := gjson.GetBytes(blob, EditorKey)
if result.IsObject() {
ident, id := result.Get(QvainIdentifierKey).String(), result.Get(QvainIdKey).String()
if ident == "qvain" || ident == "QVAIN" {
return id
}
}
return ""
}
// GetIdentifiers returns the Metax identifier, Metax "new version" identifier and Qvain record_id fields.
func GetIdentifiers(blob []byte) (string, string, string) {
results := gjson.GetManyBytes(blob, IdentifierKey, NewVersionIdKey, EditorKey)
if results[2].IsObject() {
ident, qid := results[2].Get(QvainIdentifierKey).String(), results[2].Get(QvainIdKey).String()
if ident == "qvain" || ident == "QVAIN" {
return results[0].String(), results[1].String(), qid
}
}
return results[0].String(), results[1].String(), ""
}