-
Notifications
You must be signed in to change notification settings - Fork 0
/
delegator.go
182 lines (159 loc) · 4.6 KB
/
delegator.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
176
177
178
179
180
181
182
package io2
import (
"io"
)
// Delegator implements Reader, Writer, Seeker, Closer.
type Delegator struct {
ReadFunc func(p []byte) (n int, err error)
WriteFunc func(p []byte) (n int, err error)
SeekFunc func(offset int64, whence int) (int64, error)
CloseFunc func() error
}
var (
_ io.Reader = (*Delegator)(nil)
_ io.Writer = (*Delegator)(nil)
_ io.Seeker = (*Delegator)(nil)
_ io.Closer = (*Delegator)(nil)
)
// Read calls ReadFunc(p).
func (d *Delegator) Read(p []byte) (int, error) {
if d.ReadFunc == nil {
return 0, ErrNotImplemented
}
return d.ReadFunc(p)
}
// Write calls WriteFunc(p).
func (d *Delegator) Write(p []byte) (int, error) {
if d.WriteFunc == nil {
return 0, ErrNotImplemented
}
return d.WriteFunc(p)
}
// Seek calls SeekFunc(offset, whence).
func (d *Delegator) Seek(offset int64, whence int) (int64, error) {
if d.SeekFunc == nil {
return 0, ErrNotImplemented
}
return d.SeekFunc(offset, whence)
}
// Close calls CloseFunc().
func (d *Delegator) Close() error {
if d.CloseFunc == nil {
// NOTE: return no error.
return nil
}
return d.CloseFunc()
}
// Delegate returns a Delegator with the provided io interfaces (io.Reader, io.Seeker, io.Writer, io.Closer).
func Delegate(i interface{}) *Delegator {
d := &Delegator{}
if r, ok := i.(io.Reader); ok {
d.ReadFunc = r.Read
}
if s, ok := i.(io.Seeker); ok {
d.SeekFunc = s.Seek
}
if w, ok := i.(io.Writer); ok {
d.WriteFunc = w.Write
}
if c, ok := i.(io.Closer); ok {
d.CloseFunc = c.Close
}
return d
}
// DelegateReader returns a Delegator with the provided Read function.
func DelegateReader(i io.Reader) *Delegator {
return &Delegator{
ReadFunc: i.Read,
}
}
// DelegateReadCloser returns a Delegator with the provided Read and Close functions.
func DelegateReadCloser(i io.ReadCloser) *Delegator {
return &Delegator{
ReadFunc: i.Read,
CloseFunc: i.Close,
}
}
// DelegateReadSeeker returns a Delegator with the provided Read and Seek functions.
func DelegateReadSeeker(i io.ReadSeeker) *Delegator {
return &Delegator{
ReadFunc: i.Read,
SeekFunc: i.Seek,
}
}
// DelegateReadSeekCloser returns a Delegator with the provided Read, Seek and Close functions.
func DelegateReadSeekCloser(i io.ReadSeekCloser) *Delegator {
return &Delegator{
ReadFunc: i.Read,
SeekFunc: i.Seek,
CloseFunc: i.Close,
}
}
// DelegateReadWriteCloser returns a Delegator with the provided Read, Write and Close functions.
func DelegateReadWriteCloser(i io.ReadWriteCloser) *Delegator {
return &Delegator{
ReadFunc: i.Read,
WriteFunc: i.Write,
CloseFunc: i.Close,
}
}
// DelegateReadWriteSeeker returns a Delegator with the provided Read, Write and Seek functions.
func DelegateReadWriteSeeker(i io.ReadWriteSeeker) *Delegator {
return &Delegator{
ReadFunc: i.Read,
WriteFunc: i.Write,
SeekFunc: i.Seek,
}
}
// DelegateReadWriter returns a Delegator with the provided Read and Write functions.
func DelegateReadWriter(i io.ReadWriter) *Delegator {
return &Delegator{
ReadFunc: i.Read,
WriteFunc: i.Write,
}
}
// DelegateWriter returns a Delegator with the provided Write function.
func DelegateWriter(i io.Writer) *Delegator {
return &Delegator{
WriteFunc: i.Write,
}
}
// DelegateWriteCloser returns a Delegator with the provided Write and Close functions.
func DelegateWriteCloser(i io.WriteCloser) *Delegator {
return &Delegator{
WriteFunc: i.Write,
CloseFunc: i.Close,
}
}
// DelegateWriteSeeker returns a Delegator with the provided Write and Seek functions.
func DelegateWriteSeeker(i io.WriteSeeker) *Delegator {
return &Delegator{
WriteFunc: i.Write,
SeekFunc: i.Seek,
}
}
// DelegateWriteSeekCloser returns a Delegator with the provided Write, Seek and Close functions.
func DelegateWriteSeekCloser(i WriteSeekCloser) *Delegator {
return &Delegator{
WriteFunc: i.Write,
SeekFunc: i.Seek,
CloseFunc: i.Close,
}
}
// NopReadCloser returns a ReadCloser with a no-op Close method wrapping the provided interface.
// This function like io.NopCloser(io.Reader).
func NopReadCloser(r io.Reader) io.ReadCloser {
return DelegateReader(r)
}
// NopReadWriteCloser returns a ReadWriteCloser with a no-op Close method wrapping the provided interface.
func NopReadWriteCloser(rw io.ReadWriter) io.ReadWriteCloser {
return DelegateReadWriter(rw)
}
// NopReadSeekCloser returns a ReadSeekCloser with a no-op Close method wrapping the provided interface.
func NopReadSeekCloser(r io.ReadSeeker) io.ReadSeekCloser {
return DelegateReadSeeker(r)
}
// NopWriteCloser returns a WriteCloser with a no-op Close method wrapping the provided interface.
func NopWriteCloser(w io.Writer) io.WriteCloser {
return DelegateWriter(w)
}