-
Notifications
You must be signed in to change notification settings - Fork 1
/
json.go
147 lines (128 loc) · 3.28 KB
/
json.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
package main
import (
"encoding/json"
"io"
"io/ioutil"
"time"
"github.com/30x/changeagent/common"
)
var defaultTime = time.Time{}
/*
Change represents a single change represented as JSON.
*/
type Change struct {
ID uint64 `json:"_id,omitempty"`
Timestamp int64 `json:"_ts,omitempty"`
Tags []string `json:"tags,omitempty"`
Data json.RawMessage `json:"data,omitempty"`
}
/*
ChangeList represents a list of changes returned from an API call, plus
information about the position of changes on the list.
*/
type ChangeList struct {
Changes []Change `json:"changes"`
AtStart bool `json:"atStart"`
AtEnd bool `json:"atEnd"`
}
/*
JSONError represents an error represented in JSON.
*/
type JSONError struct {
Error string `json:"error"`
}
/*
* Given a Reader that contains JSON data, read all the data until EOF and then
* return an error if any of it contains invalid JSON. Return an array of
* bytes that exactly represents the original JSON, although possibly not
* including white space.
*/
func unmarshalJSON(in io.Reader) (*common.Entry, error) {
// We are going to decode twice, because we accept two JSON formats.
// That means we need to read the whole body into memory.
body, err := ioutil.ReadAll(in)
if err != nil {
return nil, err
}
var fullData Change
err = json.Unmarshal(body, &fullData)
if err != nil || (fullData.Data == nil) && (fullData.ID == 0) && (fullData.Timestamp == 0) {
// No "data" entry -- assume that this is raw JSON
var rawJSON json.RawMessage
err = json.Unmarshal(body, &rawJSON)
if err != nil {
return nil, err
}
return &common.Entry{
Data: rawJSON,
}, nil
}
entry := &common.Entry{
Index: fullData.ID,
Tags: fullData.Tags,
Data: fullData.Data,
}
if fullData.Timestamp == 0 {
entry.Timestamp = time.Time{}
} else {
entry.Timestamp = time.Unix(0, fullData.Timestamp)
}
return entry, nil
}
/*
* Given a byte array that was parsed by "unmarshalJson," write the results to
* the specified Writer. However, the previous results are moved to a field
* named "data". Any fields in "metadata" that are non-empty will also be
* added to the message.
*/
func marshalJSON(entry *common.Entry, out io.Writer) error {
jd := convertData(entry)
enc := json.NewEncoder(out)
err := enc.Encode(&jd)
return err
}
/*
* Same as above but marshal a whole array of changes.
*/
func marshalChanges(changes []common.Entry, atStart, atEnd bool, out io.Writer) error {
cl := ChangeList{
Changes: convertChanges(changes),
AtStart: atStart,
AtEnd: atEnd,
}
enc := json.NewEncoder(out)
err := enc.Encode(&cl)
return err
}
func convertChanges(changes []common.Entry) []Change {
if len(changes) == 0 {
return []Change{}
}
var changeList []Change
for _, change := range changes {
cd := convertData(&change)
changeList = append(changeList, *cd)
}
return changeList
}
func marshalError(result error) string {
msg := &JSONError{
Error: result.Error(),
}
outBody, err := json.Marshal(msg)
if err != nil {
return result.Error()
}
return string(outBody)
}
func convertData(entry *common.Entry) *Change {
ret := Change{
ID: entry.Index,
Tags: entry.Tags,
Data: entry.Data,
}
if entry.Timestamp != defaultTime {
ret.Timestamp = entry.Timestamp.UnixNano()
}
return &ret
}