-
Notifications
You must be signed in to change notification settings - Fork 4
/
udf.go
379 lines (350 loc) · 10.8 KB
/
udf.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package neatly
import (
"bytes"
"compress/gzip"
"crypto/md5"
"encoding/json"
"fmt"
"github.com/gomarkdown/markdown"
"github.com/klauspost/pgzip"
"github.com/viant/toolbox"
"github.com/viant/toolbox/data"
"github.com/viant/toolbox/data/udf"
"github.com/viant/toolbox/storage"
"github.com/viant/toolbox/url"
"io"
"io/ioutil"
"os"
"path"
"strings"
"time"
)
//Md5 computes source md5
func Md5(source interface{}, state data.Map) (interface{}, error) {
hash := md5.New()
_, err := io.WriteString(hash, toolbox.AsString(source))
if err != nil {
return nil, err
}
var result = fmt.Sprintf("%x", hash.Sum(nil))
return result, nil
}
//GetOwnerDirectory returns owner neatly document directory
func GetOwnerDirectory(state data.Map) (string, error) {
if !state.Has(OwnerURL) {
return "", fmt.Errorf("OwnerURL was empty")
}
var resource = url.NewResource(state.GetString(OwnerURL))
return resource.DirectoryPath(), nil
}
//HasResource check if patg/url to external resource exists
func HasResource(source interface{}, state data.Map) (interface{}, error) {
filename := toolbox.AsString(source)
if !strings.HasPrefix(filename, "/") {
var parentDirectory = ""
if state.Has(OwnerURL) {
parentDirectory, _ = GetOwnerDirectory(state)
}
candidate := path.Join(parentDirectory, toolbox.AsString(source))
if toolbox.FileExists(candidate) {
return true, nil
}
}
var result = url.NewResource(filename).ParsedURL.Path
return toolbox.FileExists(result), nil
}
//LoadNeatly loads neatly document as data structure, source represents path to nearly document
func LoadNeatly(source interface{}, state data.Map) (interface{}, error) {
var filename = toolbox.AsString(source)
var parentDirectory = ""
if !strings.HasPrefix(filename, "/") {
if state.Has(OwnerURL) {
parentDirectory, _ = GetOwnerDirectory(state)
}
filename = path.Join(parentDirectory, filename)
}
if !toolbox.FileExists(filename) {
return nil, fmt.Errorf("File %v does not exists", filename)
}
var documentResource = url.NewResource(filename)
var dao, ok = state.Get(NeatlyDao).(*Dao)
if !ok {
return nil, fmt.Errorf("failed to get neatly loader %T", state.Get(NeatlyDao))
}
var aMap = make(map[string]interface{})
newState := data.NewMap()
newState.Put(OwnerURL, state.Get(OwnerURL))
newState.Put(NeatlyDao, state.Get(NeatlyDao))
for k, v := range state {
if toolbox.IsFunc(v) {
newState.Put(k, v)
}
}
err := dao.Load(newState, documentResource, &aMap)
return aMap, err
}
//WorkingDirectory return joined path with current directory, ../ is supported as subpath
func WorkingDirectory(source interface{}, state data.Map) (interface{}, error) {
currentDirectory, err := os.Getwd()
if err != nil {
return nil, err
}
var subPath = toolbox.AsString(source)
for strings.HasSuffix(subPath, "../") {
currentDirectory, _ = path.Split(currentDirectory)
if len(subPath) == 3 {
subPath = ""
} else {
subPath = string(subPath[3:])
}
}
if subPath == "" {
return currentDirectory, nil
}
return path.Join(currentDirectory, subPath), nil
}
//Unzip uncompress supplied []byte or error
func Unzip(source interface{}, state data.Map) (interface{}, error) {
payload, ok := source.([]byte)
if !ok {
return nil, fmt.Errorf("invalid Unzip input, expected %T, but had %T", []byte{}, source)
}
reader, err := pgzip.NewReader(bytes.NewReader(payload))
if err != nil {
return nil, fmt.Errorf("failed to create gzip reader %v", err)
}
payload, err = ioutil.ReadAll(reader)
if err != nil {
return nil, fmt.Errorf("failed to read gzip reader %v", err)
}
return payload, err
}
//UnzipText uncompress supplied []byte into text or error
func UnzipText(source interface{}, state data.Map) (interface{}, error) {
payload, err := Unzip(source, state)
if err != nil {
return nil, err
}
return toolbox.AsString(payload), nil
}
//Zip compresses supplied []byte or test or error
func Zip(source interface{}, state data.Map) (interface{}, error) {
payload, ok := source.([]byte)
if !ok {
if text, ok := source.(string); ok {
payload = []byte(text)
} else {
return nil, fmt.Errorf("invalid Zip input, expected %T, but had %T", []byte{}, source)
}
}
buffer := new(bytes.Buffer)
writer, err := pgzip.NewWriterLevel(buffer, gzip.BestSpeed)
if err != nil {
return nil, err
}
_, err = writer.Write(payload)
if err != nil {
return nil, fmt.Errorf("error in Zip, failed to write %v", err)
}
_ = writer.Flush()
err = writer.Close()
return buffer.Bytes(), err
}
//Markdown returns html fot supplied markdown
func Markdown(source interface{}, state data.Map) (interface{}, error) {
var input = toolbox.AsString(source)
response, err := Cat(input, state)
if err == nil && response != nil {
input = toolbox.AsString(response)
}
result := markdown.ToHTML([]byte(input), nil, nil)
return string(result), nil
}
//Cat returns content of supplied file name
func Cat(source interface{}, state data.Map) (interface{}, error) {
content, err := LoadBinary(source, state)
if err != nil {
return nil, err
}
return toolbox.AsString(content), err
}
//LoadBinary returns []byte content of supplied file name
func LoadBinary(source interface{}, state data.Map) (interface{}, error) {
filename := toolbox.AsString(source)
candidate := url.NewResource(filename)
if candidate != nil || candidate.ParsedURL != nil {
filename = candidate.ParsedURL.Path
}
if !toolbox.FileExists(filename) {
var parentDirectory = ""
if state.Has(OwnerURL) {
parentDirectory, _ = GetOwnerDirectory(state)
}
filename = path.Join(parentDirectory, toolbox.AsString(source))
}
if !toolbox.FileExists(filename) {
filename := toolbox.AsString(source)
var resource = url.NewResource(state.GetString(OwnerURL))
parentURL, _ := toolbox.URLSplit(resource.URL)
var URL = toolbox.URLPathJoin(parentURL, filename)
service, err := storage.NewServiceForURL(URL, "")
if err == nil {
if exists, _ := service.Exists(URL); exists {
resource = url.NewResource(URL)
if text, err := resource.DownloadText(); err == nil {
return text, nil
}
}
}
return nil, fmt.Errorf("no such file or directory %v", filename)
}
file, err := toolbox.OpenFile(filename)
if err != nil {
return nil, err
}
defer file.Close()
content, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}
return content, nil
}
//AssetsToMap loads assets into map[string]string, it takes url, with optional list of extension as filter
func AssetsToMap(source interface{}, state data.Map) (interface{}, error) {
if source == nil {
return nil, nil
}
var result = make(map[string]string)
updator := func(key string, data []byte) {
result[key] = string(data)
}
return assetToMap(source, state, updator, result)
}
//BinaryAssetsToMap loads binary assets into map[string]string, it takes url, with optional list of extension as filter
func BinaryAssetsToMap(source interface{}, state data.Map) (interface{}, error) {
if source == nil {
return nil, nil
}
var result = make(map[string][]byte)
updator := func(key string, data []byte) {
result[key] = data
}
return assetToMap(source, state, updator, result)
}
func assetToMap(source interface{}, state data.Map, updator func(key string, data []byte), result interface{}) (interface{}, error) {
URL, ok := source.(string) //URL param case
if ok {
return result, loadAssetToMap(url.NewResource(URL), updator)
}
//url.Resource param case
resource := &url.Resource{}
if toolbox.IsStruct(source) || toolbox.IsMap(source) {
if err := toolbox.DefaultConverter.AssignConverted(&resource, source); err == nil {
return result, loadAssetToMap(resource, updator)
}
}
if toolbox.IsSlice(source) { //URL, credentials params case
params := toolbox.AsSlice(source)
return result, loadAssetToMap(url.NewResource(params...), updator)
}
return nil, fmt.Errorf("unsupported source %T", source)
}
func loadAssetToMap(resource *url.Resource, updator func(key string, data []byte)) error {
storageService, err := storage.NewServiceForURL(resource.URL, resource.Credentials)
if err != nil {
return err
}
objects, err := storageService.List(resource.URL)
if err != nil {
return err
}
for _, object := range objects {
if object.IsFolder() {
continue
}
reader, err := storageService.Download(object)
if err != nil {
return err
}
defer reader.Close()
content, err := ioutil.ReadAll(reader)
if err != nil {
return err
}
info := object.FileInfo()
updator(info.Name(), content)
}
return err
}
// Validate if JSON file is a well-formed JSON
// Returns true if file content is valid JSON
func IsJSON(fileName interface{}, state data.Map) (interface{}, error) {
content, err := Cat(fileName, state)
if err != nil {
return false, err
}
var m json.RawMessage
if err := json.Unmarshal([]byte(toolbox.AsString(content)), &m); err != nil {
return false, err
}
return true, nil
}
// No parameters
// Returns the numeric current hour [0,23]
func CurrentHour(none interface{}, state data.Map) (interface{}, error) {
return time.Now().Hour(), nil
}
const pathKey = "path"
const valueKey = "value"
// valueAndPath expects a map with keys "path" being the path to the file which contains the rows to match and "value" containing the value to match
// Return value is a boolean. True if the compare value matches any row in the file. False if there is an error or no match is found
func MatchAnyRow(valueAndPath interface{}, state data.Map) (interface{}, error) {
if toolbox.IsMap(valueAndPath) { //URL, credentials params case
argumentsMap := toolbox.AsMap(valueAndPath)
if validateMatchAnyRowKeys(argumentsMap) {
value := toolbox.AsString(argumentsMap[valueKey])
path := toolbox.AsString(argumentsMap[pathKey])
binaryRows, err := LoadBinary(path, state)
if err != nil {
return false, nil
}
rows := strings.Split(strings.ReplaceAll(string(binaryRows.([]byte)), "\r\n", "\n"), "\n")
for _, row := range rows {
if row == value {
return true, nil
}
}
return false, nil
}
}
return false, fmt.Errorf("unsupported filename and value %T", valueAndPath)
}
func validateMatchAnyRowKeys(argumentsMap map[string]interface{}) bool {
if _, exists := argumentsMap[valueKey]; !exists {
return false
}
if _, exists := argumentsMap[pathKey]; !exists {
return false
}
return true
}
//AddStandardUdf register building udf to the context
func AddStandardUdf(aMap data.Map) {
udf.Register(aMap)
aMap.Put("IsJSON", IsJSON)
aMap.Put("WorkingDirectory", WorkingDirectory)
aMap.Put("Pwd", WorkingDirectory)
aMap.Put("HasResource", HasResource)
aMap.Put("Md5", Md5)
aMap.Put("LoadNeatly", LoadNeatly)
aMap.Put("Zip", Zip)
aMap.Put("Unzip", Unzip)
aMap.Put("UnzipText", UnzipText)
aMap.Put("Markdown", Markdown)
aMap.Put("Cat", Cat)
aMap.Put("LoadBinary", LoadBinary)
aMap.Put("AssetsToMap", AssetsToMap)
aMap.Put("BinaryAssetsToMap", BinaryAssetsToMap)
aMap.Put("CurrentHour", CurrentHour)
aMap.Put("MatchAnyRow", MatchAnyRow)
}