-
Notifications
You must be signed in to change notification settings - Fork 2
/
metadata.go
176 lines (146 loc) · 4.13 KB
/
metadata.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* Copyright (C) 2019, 2020. Genome Research Ltd. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License,
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @file metadata.go
* @author Keith James <[email protected]>
*/
package extendo
import (
"fmt"
"os/user"
"time"
dcterms "github.com/wtsi-npg/extendo/v2/dublincore"
)
const ChecksumAttr string = "md5"
type AVUFilter func(avu AVU) bool
// MakeAVU returns a new AVU instance.
//
// Deprecated: Make structs using named fields, as normal.
func MakeAVU(attr string, value string, units ...string) AVU {
var unit string
if len(units) > 0 {
unit = units[0]
}
return AVU{Attr: attr, Value: value, Units: unit}
}
// MakeCreationMetadata returns the standard metadata to be added to a newly
// created data object. The AVUs describe:
//
// - Who (which organisation) was responsible for creation of the data object
// - Who (which user) published (wrote) the data object.
// - What the checksum of the new data object was at the time of creation.
// - When the data object was created.
//
// These are expressed (with the exception of the checksum) as Dublin Core
// metadata https://www.dublincore.org/specifications/dublin-core/dcmi-terms/
func MakeCreationMetadata(checksum string) []AVU {
when := time.Now().Format(time.RFC3339)
who, err := user.Current()
if err != nil {
panic("failed to lookup the current system user")
}
publisher := fmt.Sprintf("ldap://ldap.internal.sanger.ac.uk/"+
"ou=people,dc=sanger,dc=ac,dc=uk?title?sub?(uid=%s)", who.Name)
return []AVU{
{Attr: dcterms.Created, Value: when},
{Attr: dcterms.Creator, Value: "http://www.sanger.ac.uk"},
{Attr: dcterms.Publisher, Value: publisher},
{Attr: ChecksumAttr, Value: checksum},
}
}
// SearchAVU returns true if avu is found in the slice of AVUs.
func SearchAVU(avu AVU, avus []AVU) bool {
m := make(map[AVU]struct{})
for _, avu := range avus {
m[avu] = struct{}{}
}
_, ok := m[avu]
return ok
}
func FilterAVUs(avus []AVU, f AVUFilter) []AVU {
var match []AVU
for _, avu := range avus {
if f(avu) {
match = append(match, avu)
}
}
return match
}
// SetIntersectAVUs returns a sorted slice of AVUs containing the intersection
// of the two slice arguments.
func SetIntersectAVUs(x []AVU, y []AVU) []AVU {
mx := make(map[AVU]struct{})
for _, avu := range x {
mx[avu] = struct{}{}
}
var intersection []AVU
for _, avu := range y {
if _, ok := mx[avu]; ok {
intersection = append(intersection, avu)
}
}
SortAVUs(intersection)
return intersection
}
// SetUnionAVUs returns a sorted slice of AVUs containing the union
// of the two slice arguments.
func SetUnionAVUs(x []AVU, y []AVU) []AVU {
mx := make(map[AVU]struct{})
var union []AVU
for _, avu := range x {
if _, ok := mx[avu]; !ok {
mx[avu] = struct{}{}
union = append(union, avu)
}
}
for _, avu := range y {
if _, ok := mx[avu]; !ok {
union = append(union, avu)
}
}
SortAVUs(union)
return union
}
// SetDiffAVUs returns a sorted slice of AVUs containing the set difference
// between the x and y slice arguments.
func SetDiffAVUs(x []AVU, y []AVU) []AVU {
my := make(map[AVU]struct{})
for _, avu := range y {
my[avu] = struct{}{}
}
var diff []AVU
for _, avu := range x {
if _, ok := my[avu]; !ok {
diff = append(diff, avu)
}
}
SortAVUs(diff)
return diff
}
// UniqAVUs returns a newly allocated, sorted slice of AVUs containing no
// duplicates.
func UniqAVUs(avus []AVU) []AVU {
m := make(map[AVU]struct{})
for _, avu := range avus {
m[avu] = struct{}{}
}
var uniq []AVU
for avu := range m {
uniq = append(uniq, avu)
}
SortAVUs(uniq)
return uniq
}