-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauditEvent.go
More file actions
53 lines (43 loc) · 1.08 KB
/
auditEvent.go
File metadata and controls
53 lines (43 loc) · 1.08 KB
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
package types
import (
"errors"
"fmt"
"strconv"
"strings"
)
// AuditEvent is a serialization object that can be easily stored
// in a database or ledger.
type AuditEvent struct {
UserID string
ID uint64
EventType string
Content string
}
const auditEventLen = 4
// ToCSV serializes the AuditEvent as a CSV
func (ae *AuditEvent) ToCSV() string {
parts := make([]string, auditEventLen)
parts[0] = ae.UserID
parts[1] = strconv.FormatUint(ae.ID, 10)
parts[2] = ae.EventType
parts[3] = ae.Content // TODO: Escape commas somehow?
return strings.Join(parts, ",")
}
// ParseAuditEvent attempts to parse a csv as an AuditEvent
func ParseAuditEvent(csv string) (AuditEvent, error) {
parts := strings.Split(csv, ",")
if len(parts) != auditEventLen {
msg := fmt.Sprintf("Expected %d values in AuditEvent csv", auditEventLen)
return AuditEvent{}, errors.New(msg)
}
id, err := strconv.ParseUint(parts[1], 10, 64)
if err != nil {
return AuditEvent{}, err
}
return AuditEvent{
UserID: parts[0],
ID: id,
EventType: parts[2],
Content: parts[3],
}, nil
}