-
Notifications
You must be signed in to change notification settings - Fork 5
/
writers.go
147 lines (131 loc) · 2.78 KB
/
writers.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 (
"io"
"log"
"strconv"
as "github.com/aerospike/aerospike-client-go"
)
func writeErr(wf io.Writer, errorPrefix string, s string, args [][]byte) error {
one := ""
two := ""
if len(args) > 0 {
one = string(args[0])
}
if len(args) > 1 {
two = string(args[1])
}
log.Printf("%s Client error : %s {%s, %s}\n", errorPrefix, s, one, two)
return write(wf, []byte("-ERR "+s+"\n"))
}
func writeByteArray(wf io.Writer, buf []byte) error {
err := write(wf, []byte("$"+strconv.Itoa(len(buf))+"\r\n"))
if err != nil {
return err
}
err = write(wf, buf)
if err != nil {
return err
}
return write(wf, []byte("\r\n"))
}
func writeArray(wf io.Writer, array []interface{}) error {
if len(array) == 1 {
if array[0] == nil {
return writeLine(wf, "*0")
}
}
err := writeLine(wf, "*"+strconv.Itoa(len(array)))
if err != nil {
return err
}
for _, e := range array {
err := writeByteArray(wf, e.([]byte))
if err != nil {
return err
}
}
return nil
}
func write(wf io.Writer, b []byte) error {
_, err := wf.Write(b)
if err != nil {
return err
}
return nil
}
func writeLine(wf io.Writer, s string) error {
err := write(wf, []byte(s))
if err != nil {
return err
}
err = write(wf, []byte("\r\n"))
if err != nil {
return err
}
return nil
}
func writeValue(wf io.Writer, x interface{}) error {
switch x.(type) {
case int:
return writeByteArray(wf, []byte(strconv.Itoa(x.(int))))
case string:
return writeByteArray(wf, []byte(x.(string)))
default:
return writeByteArray(wf, x.([]byte))
}
}
func writeBin(wf io.Writer, rec *as.Record, binName string, nilValue string) error {
if rec == nil {
return writeLine(wf, nilValue)
}
x := rec.Bins[binName]
if x == nil {
return writeLine(wf, nilValue)
}
return writeValue(wf, x)
}
func writeBinInt(wf io.Writer, rec *as.Record, binName string) error {
return writeBinIntFull(wf, rec, binName, ":0", ":0")
}
func writeBinIntFull(wf io.Writer, rec *as.Record, binName string, nilValue string, nilBinValue string) error {
if rec == nil {
return writeLine(wf, nilValue)
}
x := rec.Bins[binName]
if x == nil {
return writeLine(wf, nilBinValue)
}
return writeLine(wf, ":"+strconv.Itoa(x.(int)))
}
func writeArrayBin(wf io.Writer, res []*as.Record, binName string, keyBinName string) error {
l := len(res)
if keyBinName != "" {
l *= 2
}
err := writeLine(wf, "*"+strconv.Itoa(l))
if err != nil {
return err
}
for _, e := range res {
if keyBinName != "" {
err := writeBin(wf, e, keyBinName, "$-1")
if err != nil {
return err
}
}
err := writeBin(wf, e, binName, "$-1")
if err != nil {
return err
}
}
return nil
}
func encode(ctx *context, buf []byte) interface{} {
if len(buf) < 10 {
x, err := strconv.Atoi(string(buf))
if err == nil {
return x
}
}
return buf
}