-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinencoder.go
137 lines (106 loc) · 3.14 KB
/
binencoder.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
package main
import (
"bytes"
"encoding/json"
"errors"
"net/http"
"strconv"
"time"
)
type BinRecordSetWriter struct {
bytes.Buffer
MaxResponseSizeBytes int64
ContentType string
}
func (w *BinRecordSetWriter) ToBytes() []byte {
return w.Buffer.Bytes()
}
func (w *BinRecordSetWriter) HttpRespond(hw http.ResponseWriter) {
hw.Header().Set("Content-Type", w.ContentType)
hw.WriteHeader(http.StatusOK)
hw.Write(w.ToBytes())
}
func (w *BinRecordSetWriter) BeginBatch() error {
return errors.New("Batch mode not supported by binary format.")
}
func (w *BinRecordSetWriter) EndBatch() error {
return nil
}
func (w *BinRecordSetWriter) BeginRecordSet(rs *RecordSet) error {
return errors.New("Record sets not supported by binary format.")
}
func (w *BinRecordSetWriter) EndRecordSet(rs *RecordSet) error {
return nil
}
func (w *BinRecordSetWriter) BeginRecord(rs *RecordSet) error {
return errors.New("Record sets not supported by binary format.")
}
func (w *BinRecordSetWriter) EndRecord(rs *RecordSet) error {
return nil
}
func (w *BinRecordSetWriter) BeginColumn(rs *RecordSet) error {
return errors.New("Record sets not supported by binary format.")
}
func (w *BinRecordSetWriter) EndColumn(rs *RecordSet) error {
return nil
}
func (w *BinRecordSetWriter) BeginArray(rs *RecordSet, size int) error {
return errors.New("Arrays not supported by binary format.")
}
func (w *BinRecordSetWriter) EndArray(rs *RecordSet) error {
return nil
}
func (w *BinRecordSetWriter) BeginObject(rs *RecordSet) error {
return errors.New("Objects not supported by binary format.")
}
func (w *BinRecordSetWriter) EndObject(rs *RecordSet) error {
return nil
}
func (w *BinRecordSetWriter) Null(rs *RecordSet) error {
return nil
}
func (w *BinRecordSetWriter) Bool(rs *RecordSet, v bool) error {
if v {
return w.String(rs, "true")
} else {
return w.String(rs, "false")
}
}
func (w *BinRecordSetWriter) Integer(rs *RecordSet, v int64) error {
return w.String(rs, strconv.FormatInt(v, 10))
}
func (w *BinRecordSetWriter) Float(rs *RecordSet, v float64) error {
return w.String(rs, strconv.FormatFloat(v, 'g', -1, 64))
}
func (w *BinRecordSetWriter) Numeric(rs *RecordSet, v string) error {
return w.String(rs, v)
}
func (w *BinRecordSetWriter) Date(rs *RecordSet, v time.Time) error {
return w.String(rs, v.Format("2006-01-02"))
}
func (w *BinRecordSetWriter) DateTime(rs *RecordSet, v time.Time) error {
return w.String(rs, v.Format(time.RFC3339))
}
func (w *BinRecordSetWriter) String(rs *RecordSet, v string) error {
if w.Len() > 0 {
return errors.New("Binary format may contain only one scalar value.")
}
w.WriteString(v) // strings are UTF-8 encoded byte arrays in Go
return w.checkSize()
}
func (w *BinRecordSetWriter) Bytes(rs *RecordSet, v []byte) error {
if w.Len() > 0 {
return errors.New("Binary format may contain only one scalar value.")
}
w.Write(v)
return w.checkSize()
}
func (w *BinRecordSetWriter) Json(rs *RecordSet, v json.RawMessage) error {
return w.String(rs, string(v))
}
func (w *BinRecordSetWriter) checkSize() error {
if int64(w.Len()) > w.MaxResponseSizeBytes {
return errors.New("Response too long.")
}
return nil
}