-
Notifications
You must be signed in to change notification settings - Fork 13
/
ingest.go
47 lines (42 loc) · 1.03 KB
/
ingest.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
package oplog
import (
"encoding/json"
"strings"
"time"
)
// inOperation represents an Operation ingested as JSON.
type inOperation struct {
Event string `json:"event"`
Parents []string `json:"parents"`
Type string `json:"type"`
ID string `json:"id"`
Timestamp *time.Time `json:"timestamp,omniempty"`
}
// decodeOperation parses JSON data and returns an Operation on success.
func decodeOperation(data []byte) (*Operation, error) {
operation := inOperation{}
err := json.Unmarshal(data, &operation)
if err != nil {
return nil, err
}
// The timestamp field is optional
var timestamp time.Time
if operation.Timestamp != nil {
timestamp = *operation.Timestamp
} else {
timestamp = time.Now()
}
op := &Operation{
Event: strings.ToLower(operation.Event),
Data: &OperationData{
Timestamp: timestamp,
Parents: operation.Parents,
Type: strings.ToLower(operation.Type),
ID: operation.ID,
},
}
if err := op.Validate(); err != nil {
return nil, err
}
return op, nil
}