-
Notifications
You must be signed in to change notification settings - Fork 94
/
record.go
64 lines (53 loc) · 1.29 KB
/
record.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
package record
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"time"
)
const (
JSONExtension = "json"
)
// Record represents a record that will be stored as a file.
type Record struct {
Name string
Captured time.Time
Item Marshalable
// AlwaysStored marks the record as a priority - it will be always present
// in the archive regardles of the size limit. Use with caution.
AlwaysStored bool
}
// Marshal marshals the item and returns its fingerprint
func (r *Record) Marshal() (content []byte, fingerprint string, err error) {
content, err = r.Item.Marshal()
if err != nil {
return content, "", err
}
h := sha256.New()
h.Write(content)
fingerprint = hex.EncodeToString(h.Sum(nil))
return content, fingerprint, nil
}
// GetFilename with extension, if present
func (r *Record) GetFilename() string {
extension := r.Item.GetExtension()
if len(extension) > 0 {
return fmt.Sprintf("%s.%s", r.Name, extension)
}
return r.Name
}
type Marshalable interface {
Marshal() ([]byte, error)
GetExtension() string
}
type JSONMarshaller struct {
Object interface{}
}
func (m JSONMarshaller) Marshal() ([]byte, error) {
return json.Marshal(m.Object)
}
// GetExtension return extension for json marshaller
func (m JSONMarshaller) GetExtension() string {
return JSONExtension
}