-
Notifications
You must be signed in to change notification settings - Fork 6
/
io_abstraction.go
122 lines (101 loc) · 2.1 KB
/
io_abstraction.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
package imageflow
import (
"io/ioutil"
"net/http"
)
type ioOperation interface {
toBuffer() ([]byte, error)
toOutput([]byte, map[string][]byte) map[string][]byte
setIo(id uint)
getIo() uint
}
func (file File) toBuffer() ([]byte, error) {
bytes, errorInRead := ioutil.ReadFile(file.filename)
if errorInRead != nil {
return nil, errorInRead
}
return bytes, nil
}
func (file File) toOutput(data []byte, m map[string][]byte) map[string][]byte {
ioutil.WriteFile(file.filename, data, 0644)
return m
}
func (file *File) setIo(id uint) {
file.iOID = id
}
func (file File) getIo() uint {
return file.iOID
}
func (file URL) toBuffer() ([]byte, error) {
bytes, errorInURL := http.Get(file.url)
if errorInURL != nil {
return nil, errorInURL
}
data, errorInRead := ioutil.ReadAll(bytes.Body)
if errorInRead != nil {
return nil, errorInRead
}
return data, nil
}
func (file URL) toOutput(data []byte, m map[string][]byte) map[string][]byte {
return m
}
func (file *URL) setIo(id uint) {
file.iOID = id
}
func (file URL) getIo() uint {
return file.iOID
}
// URL is used to make a http request to get file and use it
type URL struct {
url string
iOID uint
}
// NewURL is used to create a new url operation
func NewURL(url string) *URL {
return &URL{
url: url,
}
}
// NewBuffer create a buffer operation
func NewBuffer(buffer []byte) *Buffer {
return &Buffer{
buffer: buffer,
}
}
// GetBuffer is used to get key
func GetBuffer(key string) *Buffer {
return &Buffer{
key: key,
}
}
// Buffer is io operation related to []byte
type Buffer struct {
iOID uint
buffer []byte
key string
}
func (file Buffer) toBuffer() ([]byte, error) {
return file.buffer, nil
}
func (file Buffer) toOutput(data []byte, m map[string][]byte) map[string][]byte {
m[file.key] = data
return m
}
func (file *Buffer) setIo(id uint) {
file.iOID = id
}
func (file Buffer) getIo() uint {
return file.iOID
}
// File is io operation related to file
type File struct {
iOID uint
filename string
}
// NewFile is used to create a file io
func NewFile(filename string) *File {
return &File{
filename: filename,
}
}