-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstarkRecords.go
205 lines (175 loc) · 4.94 KB
/
starkRecords.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package stark
import (
"fmt"
"io"
"io/ioutil"
"strings"
"github.com/gogo/protobuf/jsonpb"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/timestamp"
"github.com/google/uuid"
starkcrypto "github.com/will-rowe/stark/src/crypto"
"google.golang.org/protobuf/proto"
)
// SetAlias is an option setter for the NewRecord constructor
// that sets the human readable label of a Record.
// Spaces are replaced with underscores.
//
// Note: the Record's alias is used as the key in the
// StarkDB.
func SetAlias(alias string) RecordOption {
return func(x *Record) error {
return x.setAlias(alias)
}
}
// SetDescription is an option setter for the NewRecord constructor
// that sets the description of a Record.
func SetDescription(description string) RecordOption {
return func(x *Record) error {
return x.setDescription(description)
}
}
// NewComment creates a comment.
func NewComment(comment, prevCID string) *RecordComment {
return &RecordComment{
Timestamp: ptypes.TimestampNow(),
Text: comment,
PreviousCID: prevCID,
}
}
// NewRecord creates a record.
func NewRecord(options ...RecordOption) (*Record, error) {
// create the base record
x := &Record{
Uuid: uuid.New().String(),
PreviousCID: "",
History: []*RecordComment{},
LinkedSamples: make(map[string]string),
LinkedLibraries: make(map[string]string),
Barcodes: make(map[string]int32),
}
// start the Record history
x.AddComment("record created.")
// add the user provided options
for _, option := range options {
err := option(x)
if err != nil {
return nil, err
}
}
return x, nil
}
// NewRecordFromReader creates a Record from a reader.
// Accepts either JSON or Protobuf encoded Record.
func NewRecordFromReader(data io.Reader, ienc string) (*Record, error) {
x := &Record{}
switch ienc {
case "json":
um := &jsonpb.Unmarshaler{}
if err := um.Unmarshal(data, x); err != nil {
return nil, err
}
case "proto":
buf, err := ioutil.ReadAll(data)
if err != nil {
return nil, err
}
if err := proto.Unmarshal(buf, x); err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("unsupported format: %s", ienc)
}
return x, nil
}
// AddComment adds a timestamped comment to the Record's history, along with the last known CID to enable rollbacks.
func (x *Record) AddComment(text string) {
x.History = append(x.History, NewComment(text, x.PreviousCID))
return
}
// LinkSample links a Sample to a Record.
func (x *Record) LinkSample(sampleUUID uuid.UUID, sampleLocation string) error {
// convert the UUID to string and check if it's already linked
uuid := sampleUUID.String()
if _, exists := x.LinkedSamples[uuid]; exists {
return ErrLinkExists
}
// link it
x.LinkedSamples[uuid] = sampleLocation
x.AddComment(fmt.Sprintf("linked record to sample (%s)", uuid))
return nil
}
// LinkLibrary links a Library to a Record.
func (x *Record) LinkLibrary(libraryUUID uuid.UUID, libraryLocation string) error {
// convert the UUID to string and check if it's already linked
uuid := libraryUUID.String()
if _, exists := x.LinkedLibraries[uuid]; exists {
return ErrLinkExists
}
// link it
x.LinkedLibraries[uuid] = libraryLocation
x.AddComment(fmt.Sprintf("linked record to library (%s)", uuid))
return nil
}
// GetCreatedTimestamp returns the timestamp for when the record was created.
func (x *Record) GetCreatedTimestamp() *timestamp.Timestamp {
return x.GetHistory()[0].Timestamp
}
// Encrypt will encrypt certain fields of a Record.
//
// Note: Currently only the Record UUID is encrypted.
func (x *Record) Encrypt(cipherKey []byte) error {
if x.Encrypted {
return ErrEncrypted
}
// encrypt the UUID
encField, err := starkcrypto.Encrypt(x.Uuid, cipherKey)
if err != nil {
return err
}
x.Uuid = encField
// TODO: encrypt other fields
// set the Record to encrypted
x.Encrypted = true
return nil
}
// Decrypt will decrypt certain fields of a Record.
// Unencrypted Records are ignored and errors are
// reported for unsuccessful decrypts.
//
// Note: Currently only the Record UUID is decrypted.
func (x *Record) Decrypt(cipherKey []byte) error {
if !x.Encrypted {
return nil
}
// decrypt the UUID
decField, err := starkcrypto.Decrypt(x.Uuid, cipherKey)
if err != nil {
return err
}
x.Uuid = decField
// TODO: decrypt other fields
// set the Record to decrypted
x.Encrypted = false
return nil
}
// GetLastUpdatedTimestamp returns the timestamp for when the record was created.
func (x *Record) GetLastUpdatedTimestamp() *timestamp.Timestamp {
histLength := len(x.GetHistory())
return x.GetHistory()[histLength-1].Timestamp
}
func (x *Record) setAlias(alias string) error {
alias = strings.ReplaceAll(alias, " ", "_")
if len(alias) != 0 {
x.Alias = alias
x.AddComment("alias updated.")
}
return nil
}
func (x *Record) setDescription(description string) error {
if len(description) != 0 {
x.Description = description
x.AddComment("description updated.")
}
return nil
}