forked from viant/neatly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
udf.go
329 lines (304 loc) · 9.15 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
package neatly
import (
"bytes"
"compress/gzip"
"crypto/md5"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"strings"
"time"
"github.com/klauspost/pgzip"
"github.com/viant/toolbox"
"github.com/viant/toolbox/data"
"github.com/viant/toolbox/storage"
"github.com/viant/toolbox/url"
"gopkg.in/russross/blackfriday.v2"
)
//AsMap converts source into map
func AsMap(source interface{}, state data.Map) (interface{}, error) {
if source == nil || toolbox.IsMap(source) {
return source, nil
}
if toolbox.IsString(source) {
aMap := make(map[string]interface{})
err := toolbox.NewJSONDecoderFactory().Create(strings.NewReader(toolbox.AsString(source))).Decode(&aMap)
if err != nil {
return nil, err
}
return aMap, nil
}
return toolbox.ToMap(source)
}
//AsInt converts source into int
func AsInt(source interface{}, state data.Map) (interface{}, error) {
return toolbox.ToInt(source)
}
//Add increment supplied state key with delta ['key', -2]
func Increment(args interface{}, state data.Map) (interface{}, error) {
if toolbox.IsSlice(args) {
return nil, fmt.Errorf("args were not slice: %T", args)
}
aSlice := toolbox.AsSlice(args)
if len(aSlice) != 2 {
return nil, fmt.Errorf("expeted 2 arguments but had: %v", len(aSlice))
}
var delta = toolbox.AsInt(aSlice[1])
var exrp = toolbox.AsString(aSlice[0])
value, has := state.GetValue(exrp)
if !has {
state.SetValue(exrp, delta)
} else {
state.SetValue(exrp, delta+toolbox.AsInt(value))
}
value, _ = state.GetValue(exrp)
return value, nil
}
//AsFloat converts source into float64
func AsFloat(source interface{}, state data.Map) (interface{}, error) {
return toolbox.AsFloat(source), nil
}
//Length returns length of slice or string
func Length(source interface{}, state data.Map) (interface{}, error) {
if toolbox.IsSlice(source) {
return len(toolbox.AsSlice(source)), nil
}
if toolbox.IsMap(source) {
return len(toolbox.AsMap(source)), nil
}
if text, ok := source.(string); ok {
return len(text), nil
}
return 0, nil
}
//AsBool converts source into bool
func AsBool(source interface{}, state data.Map) (interface{}, error) {
return toolbox.AsBoolean(source), nil
}
//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
}
//FormatTime return formatted time, it takes an array of two arguments, the first id time, or now followed by java style time format.
func FormatTime(source interface{}, state data.Map) (interface{}, error) {
if !toolbox.IsSlice(source) {
return nil, fmt.Errorf("unable to run FormatTime: expected %T, but had: %T", []interface{}{}, source)
}
aSlice := toolbox.AsSlice(source)
if len(aSlice) < 2 {
return nil, fmt.Errorf("unable to run FormatTime, expected 2 parameters, but had: %v", len(aSlice))
}
var err error
var timeText = toolbox.AsString(aSlice[0])
var timeFormat = toolbox.AsString(aSlice[1])
var timeLayout = toolbox.DateFormatToLayout(timeFormat)
var timeValue *time.Time
if timeText == "now" {
var now = time.Now()
timeValue = &now
} else {
timeValue, err = toolbox.ToTime(aSlice[0], timeLayout)
}
if err != nil {
return nil, err
}
if len(aSlice) > 2 {
timeLocation, err := time.LoadLocation(toolbox.AsString(aSlice[2]))
if err != nil {
return nil, err
}
timeInLocation := timeValue.In(timeLocation)
timeValue = &timeInLocation
}
return timeValue.Format(timeLayout), 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 := blackfriday.Run([]byte(input))
return string(result), nil
}
//Cat returns content of supplied file name
func Cat(source interface{}, state data.Map) (interface{}, error) {
candidate := url.NewResource(toolbox.AsString(source))
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) {
var resource = url.NewResource(state.GetString(OwnerURL))
parentURL, _ := toolbox.URLSplit(resource.URL)
var URL = toolbox.URLPathJoin(parentURL, toolbox.AsString(source))
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 string(content), nil
}
// 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
}