-
Notifications
You must be signed in to change notification settings - Fork 86
/
arguments.go
150 lines (128 loc) · 4.3 KB
/
arguments.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
// Copyright (c) 2015 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 tchannel
import (
"bufio"
"encoding/json"
"io"
"io/ioutil"
"github.com/uber/tchannel-go/internal/argreader"
)
// ArgReader is the interface for the arg2 and arg3 streams on an
// OutboundCallResponse and an InboundCall
type ArgReader io.ReadCloser
// ArgWriter is the interface for the arg2 and arg3 streams on an OutboundCall
// and an InboundCallResponse
type ArgWriter interface {
io.WriteCloser
// Flush flushes the currently written bytes without waiting for the frame
// to be filled.
Flush() error
}
// ArgWritable is an interface for providing arg2 and arg3 writer streams;
// implemented by reqResWriter e.g. OutboundCall and InboundCallResponse
type ArgWritable interface {
Arg2Writer() (ArgWriter, error)
Arg3Writer() (ArgWriter, error)
}
// ArgReadable is an interface for providing arg2 and arg3 reader streams;
// implemented by reqResReader e.g. InboundCall and OutboundCallResponse.
type ArgReadable interface {
Arg2Reader() (ArgReader, error)
Arg3Reader() (ArgReader, error)
}
// ArgReadHelper providers a simpler interface to reading arguments.
type ArgReadHelper struct {
reader ArgReader
err error
}
// NewArgReader wraps the result of calling ArgXReader to provide a simpler
// interface for reading arguments.
func NewArgReader(reader ArgReader, err error) ArgReadHelper {
return ArgReadHelper{reader, err}
}
func (r ArgReadHelper) read(f func() error) error {
if r.err != nil {
return r.err
}
if err := f(); err != nil {
return err
}
if err := argreader.EnsureEmpty(r.reader, "read arg"); err != nil {
return err
}
return r.reader.Close()
}
// Read reads from the reader into the byte slice.
func (r ArgReadHelper) Read(bs *[]byte) error {
return r.read(func() error {
var err error
*bs, err = ioutil.ReadAll(r.reader)
return err
})
}
// ReadJSON deserializes JSON from the underlying reader into data.
func (r ArgReadHelper) ReadJSON(data interface{}) error {
return r.read(func() error {
// TChannel allows for 0 length values (not valid JSON), so we use a bufio.Reader
// to check whether data is of 0 length.
reader := bufio.NewReader(r.reader)
if _, err := reader.Peek(1); err == io.EOF {
// If the data is 0 length, then we don't try to read anything.
return nil
} else if err != nil {
return err
}
d := json.NewDecoder(reader)
return d.Decode(data)
})
}
// ArgWriteHelper providers a simpler interface to writing arguments.
type ArgWriteHelper struct {
writer io.WriteCloser
err error
}
// NewArgWriter wraps the result of calling ArgXWriter to provider a simpler
// interface for writing arguments.
func NewArgWriter(writer io.WriteCloser, err error) ArgWriteHelper {
return ArgWriteHelper{writer, err}
}
func (w ArgWriteHelper) write(f func() error) error {
if w.err != nil {
return w.err
}
if err := f(); err != nil {
return err
}
return w.writer.Close()
}
// Write writes the given bytes to the underlying writer.
func (w ArgWriteHelper) Write(bs []byte) error {
return w.write(func() error {
_, err := w.writer.Write(bs)
return err
})
}
// WriteJSON writes the given object as JSON.
func (w ArgWriteHelper) WriteJSON(data interface{}) error {
return w.write(func() error {
e := json.NewEncoder(w.writer)
return e.Encode(data)
})
}