-
Notifications
You must be signed in to change notification settings - Fork 453
/
pickle_writer.go
175 lines (143 loc) · 3.82 KB
/
pickle_writer.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright (c) 2019 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package pickle
import (
"bufio"
"encoding/binary"
"io"
"math"
)
var (
programStart = []uint8{opProto, 0x2}
programEnd = []uint8{opStop}
listStart = []uint8{opEmptyList, opMark}
dictStart = []uint8{opEmptyDict, opMark}
)
// A Writer is capable writing out the opcodes required by the pickle format.
// Note that this is a very limited implementation of pickling; just enough for
// us to implement the opcodes required by graphite /render.
type Writer struct {
w *bufio.Writer
buf [8]byte
err error
}
// NewWriter creates a new pickle writer.
func NewWriter(w io.Writer) *Writer {
pw := &Writer{
w: bufio.NewWriter(w),
}
_, pw.err = pw.w.Write(programStart)
return pw
}
// BeginDict starts marshalling a python dict.
func (p *Writer) BeginDict() {
if p.err != nil {
return
}
if _, p.err = p.w.Write(dictStart); p.err != nil {
return
}
}
// WriteDictKey writes a dictionary key.
func (p *Writer) WriteDictKey(s string) {
p.WriteString(s)
}
// EndDict ends marshalling a python dict.
func (p *Writer) EndDict() {
if p.err != nil {
return
}
p.err = p.w.WriteByte(opSetItems)
}
// BeginList begins writing a new python list.
func (p *Writer) BeginList() {
if p.err != nil {
return
}
_, p.err = p.w.Write(listStart)
}
// EndList ends writing a python list.
func (p *Writer) EndList() {
if p.err != nil {
return
}
p.w.WriteByte(opAppends)
}
// WriteNone writes a python `None`.
func (p *Writer) WriteNone() {
if p.err != nil {
return
}
p.err = p.w.WriteByte(opNone)
}
// WriteFloat64 writes a float64 value. NaNs are converted in `None`.
func (p *Writer) WriteFloat64(v float64) {
if math.IsNaN(v) {
p.WriteNone()
return
}
if p.err != nil {
return
}
if p.err = p.w.WriteByte(opBinFloat); p.err != nil {
return
}
binary.BigEndian.PutUint64(p.buf[:], math.Float64bits(v))
_, p.err = p.w.Write(p.buf[:])
}
// WriteString writes a python string.
func (p *Writer) WriteString(s string) {
if p.err != nil {
return
}
if p.err = p.w.WriteByte(opBinUnicode); p.err != nil {
return
}
binary.LittleEndian.PutUint32(p.buf[:4], uint32(len(s)))
if _, p.err = p.w.Write(p.buf[:4]); p.err != nil {
return
}
_, p.err = p.w.WriteString(s)
}
// WriteInt writes an int value.
func (p *Writer) WriteInt(n int) {
if p.err != nil {
return
}
if p.err = p.w.WriteByte(opBinInt); p.err != nil {
return
}
binary.LittleEndian.PutUint32(p.buf[:4], uint32(n))
_, p.err = p.w.Write(p.buf[:4])
}
// Close closes the writer, marking the end of the stream and flushing any
// pending values.
func (p *Writer) Close() error {
if p.err != nil {
return p.err
}
if _, p.err = p.w.Write(programEnd); p.err != nil {
return p.err
}
if p.err = p.w.Flush(); p.err != nil {
return p.err
}
return nil
}