-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpayload.go
335 lines (280 loc) · 7.99 KB
/
payload.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package cc
import (
"bytes"
"errors"
"fmt"
"io"
"os"
)
type DataSourceIoType string
const (
DataSourceInput DataSourceIoType = "INPUT"
DataSourceOutput DataSourceIoType = "OUTPUT"
DataSourceAll DataSourceIoType = "" //zero value == all
)
type Payload struct {
IOManager
Actions []Action `json:"actions"`
}
type Action struct {
IOManager
Type string `json:"type,omitempty"`
Description string `json:"description,omitempty"`
}
// -----------------------------------------------
// Wrapped IOManager functions
// -----------------------------------------------
func (a Action) GetStore(name string) (*DataStore, error) {
return a.IOManager.GetStore(name)
}
func (a Action) GetDataSource(input GetDsInput) (DataSource, error) {
return a.IOManager.GetDataSource(input)
}
func (a Action) GetInputDataSource(name string) (DataSource, error) {
return a.IOManager.GetInputDataSource(name)
}
func (a Action) GetOutputDataSource(name string) (DataSource, error) {
return a.IOManager.GetOutputDataSource(name)
}
func (a Action) GetReader(input DataSourceOpInput) (io.ReadCloser, error) {
return a.IOManager.GetReader(input)
}
func (a Action) Get(input DataSourceOpInput) ([]byte, error) {
return a.IOManager.Get(input)
}
func (a Action) Put(input PutOpInput) (int, error) {
return a.IOManager.Put(input)
}
func (a Action) Copy(src DataSourceOpInput, dest DataSourceOpInput) error {
return a.IOManager.Copy(src, dest)
}
func (a Action) CopyFileToLocal(dsName string, pathkey string, dataPathKey string, localPath string) error {
return a.IOManager.CopyFileToLocal(dsName, pathkey, dataPathKey, localPath)
}
func (a Action) CopyFileToRemote(input CopyFileToRemoteInput) error {
return a.IOManager.CopyFileToRemote(input)
}
// -----------------------------------------------
// IOManager
// -----------------------------------------------
type IOManager struct {
Attributes PayloadAttributes `json:"attributes,omitempty"`
Stores []DataStore `json:"stores"`
Inputs []DataSource `json:"inputs"`
Outputs []DataSource `json:"outputs"`
}
type GetDsInput struct {
DsIoType DataSourceIoType
DsName string
}
type DataSourceOpInput struct {
DataSourceName string
PathKey string
DataPathKey string
}
type PutOpInput struct {
SrcReader io.Reader
DataSourceOpInput
}
func (im *IOManager) GetStore(name string) (*DataStore, error) {
for _, store := range im.Stores {
if store.Name == name {
return &store, nil
}
}
return nil, errors.New("Invalid store name")
}
func (im *IOManager) GetDataSource(input GetDsInput) (DataSource, error) {
sources := []DataSource{}
switch input.DsIoType {
case DataSourceInput:
sources = im.Inputs
case DataSourceOutput:
sources = im.Outputs
case DataSourceAll:
sources = append(sources, im.Inputs...)
sources = append(sources, im.Outputs...)
}
for _, ds := range sources {
if input.DsName == ds.Name {
return ds, nil
}
}
return DataSource{}, fmt.Errorf("data source %s not found", input.DsName)
}
func (im *IOManager) GetInputDataSource(name string) (DataSource, error) {
return im.GetDataSource(GetDsInput{DataSourceInput, name})
}
func (im *IOManager) GetOutputDataSource(name string) (DataSource, error) {
return im.GetDataSource(GetDsInput{DataSourceOutput, name})
}
func (im *IOManager) GetReader(input DataSourceOpInput) (io.ReadCloser, error) {
dataSource, err := im.GetInputDataSource(input.DataSourceName)
if err != nil {
return nil, err
}
dataStore, err := im.GetStore(dataSource.StoreName)
if err != nil {
return nil, err
}
if readerStore, ok := dataStore.Session.(StoreReader); ok {
path := dataSource.Paths[input.PathKey]
datapath := ""
if input.DataPathKey != "" {
datapath = dataSource.DataPaths[input.DataPathKey]
}
return readerStore.Get(path, datapath)
}
return nil, fmt.Errorf("data store %s session does not implement a StoreReader", dataStore.Name)
}
func (im *IOManager) Get(input DataSourceOpInput) ([]byte, error) {
reader, err := im.GetReader(input)
if err != nil {
return nil, err
}
buf := new(bytes.Buffer)
buf.ReadFrom(reader)
return buf.Bytes(), nil
}
func (im *IOManager) Put(input PutOpInput) (int, error) {
ds, err := im.GetOutputDataSource(input.DataSourceName)
if err != nil {
return 0, err
}
store, err := im.GetStore(ds.StoreName)
if err != nil {
return 0, err
}
if writer, ok := store.Session.(StoreWriter); ok {
path := ds.Paths[input.PathKey]
datapath := ""
if input.DataPathKey != "" {
datapath = ds.DataPaths[input.DataPathKey]
}
return writer.Put(input.SrcReader, path, datapath)
}
return 0, fmt.Errorf("Data Store %s session does not implement a StoreWriter", ds.StoreName)
}
func (im *IOManager) Copy(src DataSourceOpInput, dest DataSourceOpInput) error {
srcds, err := im.GetOutputDataSource(src.DataSourceName)
if err != nil {
return err
}
srcstore, err := im.GetStore(srcds.StoreName)
if err != nil {
return err
}
destds, err := im.GetOutputDataSource(dest.DataSourceName)
if err != nil {
return err
}
deststore, err := im.GetStore(destds.StoreName)
if err != nil {
return err
}
if srcReader, ok := srcstore.Session.(StoreReader); ok {
if destwriter, ok := deststore.Session.(StoreWriter); ok {
//get the reader
srcpath := srcds.Paths[src.PathKey]
srcdatapath := ""
if src.DataPathKey != "" {
srcdatapath = srcds.DataPaths[src.DataPathKey]
}
reader, err := srcReader.Get(srcpath, srcdatapath)
if err != nil {
return err
}
//write
destpath := destds.Paths[dest.PathKey]
destdatapath := ""
if dest.DataPathKey != "" {
destdatapath = destds.DataPaths[dest.DataPathKey]
}
_, err = destwriter.Put(reader, destpath, destdatapath)
return err
}
return fmt.Errorf("Destination Data Store %s session does not implement a StoreWriter", srcstore.Name)
}
return fmt.Errorf("Source Data Store %s session does not implement a StoreReader", srcstore.Name)
}
func (im *IOManager) CopyFileToLocal(dsName string, pathkey string, dataPathKey string, localPath string) error {
ds, err := im.GetDataSource(GetDsInput{DataSourceInput, dsName})
if err != nil {
return err
}
store, err := im.GetStore(ds.StoreName)
if err != nil {
return err
}
path := ds.Paths[pathkey]
datapath := ""
if dataPathKey != "" {
datapath = ds.DataPaths[dataPathKey]
}
if storeReader, ok := store.Session.(StoreReader); ok {
reader, err := storeReader.Get(path, datapath)
if err != nil {
return err
}
defer reader.Close()
writer, err := os.Create(localPath)
if err != nil {
return err
}
defer writer.Close()
_, err = io.Copy(writer, reader)
return err
}
return fmt.Errorf("Data Store %s session does not implement a StoreReader", store.Name)
}
type CopyFileToRemoteInput struct {
RemoteStoreName string
RemotePath string
LocalPath string
RemoteDsName string
DsPathKey string
DsDataPathKey string
}
func (im *IOManager) CopyFileToRemote(input CopyFileToRemoteInput) error {
storeName := input.RemoteStoreName
path := input.RemotePath
datapath := ""
if storeName == "" {
//get store name from datasource and use datasource semantics
ds, err := im.GetDataSource(GetDsInput{DataSourceOutput, input.RemoteDsName})
if err != nil {
return err
}
storeName = ds.StoreName
path = ds.Paths[input.DsPathKey]
if input.DsDataPathKey != "" {
datapath = ds.DataPaths[input.DsDataPathKey]
}
}
store, err := im.GetStore(storeName)
if err != nil {
return err
}
if writer, ok := store.Session.(StoreWriter); ok {
reader, err := os.Open(input.LocalPath)
if err != nil {
return err
}
_, err = writer.Put(reader, path, datapath)
return err
}
return fmt.Errorf("Data Store %s session does not implement a StoreWriter", store.Name)
}
func GetStoreAs[T any](mgr *IOManager, name string) (T, error) {
for _, s := range mgr.Stores {
if s.Name == name {
if t, ok := s.Session.(T); ok {
return t, nil
} else {
return t, errors.New("Invalid Store Type")
}
}
}
var t T
return t, errors.New(fmt.Sprintf("Session %s does not exist.\n", name))
}