-
Notifications
You must be signed in to change notification settings - Fork 0
/
readeventsfile.go
155 lines (127 loc) · 3.79 KB
/
readeventsfile.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
package veracity
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"os"
"path/filepath"
"github.com/datatrails/go-datatrails-logverification/logverification"
)
var (
ErrInvalidV3Event = errors.New(`json is not in expected v3event format`)
)
func stdinToVerifiableEvents() ([]logverification.VerifiableEvent, error) {
return scannerToVerifiableEvents(bufio.NewScanner(os.Stdin))
}
func filePathToVerifiableEvents(filePath string) ([]logverification.VerifiableEvent, error) {
filePath, err := filepath.Abs(filePath)
if err != nil {
return nil, err
}
f, err := os.Open(filePath)
if err != nil {
return nil, err
}
return scannerToVerifiableEvents(bufio.NewScanner(f))
}
func stdinToDecodedEvents() ([]logverification.DecodedEvent, error) {
return scannerToDecodedEvents(bufio.NewScanner(os.Stdin))
}
func scannerToDecodedEvents(scanner *bufio.Scanner) ([]logverification.DecodedEvent, error) {
var data []byte
for scanner.Scan() {
data = append(data, scanner.Bytes()...)
}
if err := scanner.Err(); err != nil {
return nil, err
}
return DecodedEventsFromData(data)
}
func scannerToVerifiableEvents(scanner *bufio.Scanner) ([]logverification.VerifiableEvent, error) {
var data []byte
for scanner.Scan() {
data = append(data, scanner.Bytes()...)
}
if err := scanner.Err(); err != nil {
return nil, err
}
return VerifiableEventsFromData(data)
}
func VerifiableEventsFromData(data []byte) ([]logverification.VerifiableEvent, error) {
// Accept either the list events response format or a single event. Peak
// into the json data to pick which.
eventsJson, err := eventListFromData(data)
if err != nil {
return nil, err
}
verifiableEvents, err := logverification.NewVerifiableEvents(eventsJson)
if err != nil {
return nil, err
}
for _, event := range verifiableEvents {
validationErr := event.Validate()
if validationErr != nil {
return nil, validationErr
}
}
return verifiableEvents, nil
}
func DecodedEventsFromData(data []byte) ([]logverification.DecodedEvent, error) {
// Accept either the list events response format or a single event. Peak
// into the json data to pick which.
eventsJson, err := eventListFromData(data)
if err != nil {
return nil, err
}
decodedEvents, err := logverification.NewDecodedEvents(eventsJson)
if err != nil {
return nil, err
}
for _, event := range decodedEvents {
validationErr := event.Validate()
if validationErr != nil {
return nil, validationErr
}
}
return decodedEvents, nil
}
// eventListFromData normalises a json encoded event or *list* of events, by
// always returning a list of json encoded events.
//
// NOTE: there is no json validation done on the event or list of events given
// any valid json will be accepted, use validation logic after this function.
func eventListFromData(data []byte) ([]byte, error) {
var err error
doc := struct {
Events []json.RawMessage `json:"events,omitempty"`
NextPageToken json.RawMessage `json:"next_page_token,omitempty"`
}{}
decoder := json.NewDecoder(bytes.NewReader(data))
decoder.DisallowUnknownFields()
err = decoder.Decode(&doc)
// if we can decode the events json
// we know its in the form of a list events json response from
// the list events api, so just return data
if err == nil {
return data, nil
}
// if we get here we know that the given data doesn't represent
// a list events json response
// so we can assume its a single event response from the events api.
var event json.RawMessage
err = json.Unmarshal(data, &event)
if err != nil {
return nil, err
}
// purposefully omit the next page token for response
listEvents := struct {
Events []json.RawMessage `json:"events,omitempty"`
}{}
listEvents.Events = []json.RawMessage{event}
events, err := json.Marshal(&listEvents)
if err != nil {
return nil, err
}
return events, nil
}