-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathexpr.go
More file actions
371 lines (341 loc) · 9.43 KB
/
Copy pathexpr.go
File metadata and controls
371 lines (341 loc) · 9.43 KB
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
package batch
import (
"bytes"
"crypto/md5"
"crypto/rand"
b64 "encoding/base64"
"encoding/gob"
"fmt"
"html"
"math/big"
"os"
"strings"
"time"
"unicode"
force "github.com/ForceCLI/force/lib"
"github.com/expr-lang/expr"
strip "github.com/grokify/html-strip-tags-go"
"github.com/mitchellh/mapstructure"
log "github.com/sirupsen/logrus"
)
func init() {
gob.Register(force.ForceRecord{})
}
type Env struct {
Record force.ForceRecord `expr:"record"`
ApexContext any `expr:"apex"`
}
func (Env) MergePatch(a force.ForceRecord, b map[string]any) force.ForceRecord {
for k, v := range b {
a[k] = v
}
return a
}
func (Env) DeleteKey(a force.ForceRecord, b string) force.ForceRecord {
delete(a, b)
return a
}
// Escape any non-ASCII characters like Apex's String.escapeUnicode.
func escapeUnicode(s string) string {
var result strings.Builder
for _, rune := range s {
if rune > unicode.MaxASCII {
result.WriteString(fmt.Sprintf("\\u%04X", rune))
} else {
result.WriteRune(rune)
}
}
return result.String()
}
func exprFunctions(session BulkSession) []expr.Option {
var exprFunctions []expr.Option
exprFunctions = append(exprFunctions, expr.Function(
"base64",
func(params ...any) (any, error) {
return b64.StdEncoding.EncodeToString([]byte(params[0].(string))), nil
},
new(func(string) string),
))
exprFunctions = append(exprFunctions, expr.Function(
"stripHtml",
func(params ...any) (any, error) {
return strip.StripTags(params[0].(string)), nil
},
new(func(string) string),
))
exprFunctions = append(exprFunctions, expr.Function(
"escapeUnicode",
func(params ...any) (any, error) {
return escapeUnicode(params[0].(string)), nil
},
new(func(string) string),
))
exprFunctions = append(exprFunctions, expr.Function(
"escapeHtml",
func(params ...any) (any, error) {
return strings.ReplaceAll(html.EscapeString(params[0].(string)), `"`, `"`), nil
},
new(func(string) string),
))
exprFunctions = append(exprFunctions, expr.Function(
"md5",
func(params ...any) (any, error) {
return fmt.Sprintf("%x", md5.Sum([]byte(params[0].(string)))), nil
},
new(func(string) string),
))
compareAndSetStore := make(map[string]any)
// compareAndSet takes a key and value
// returns true if the key exists in the store and the stored value matches the passed value
// returns true if the key does not exist in the store, and stores the value under the key
// returns false if the key exists in the store and the stored value does not match the passed value
exprFunctions = append(exprFunctions, expr.Function(
"compareAndSet",
func(params ...any) (any, error) {
key := params[0].(string)
value := params[1]
if compareAndSetStore[key] == value {
return true, nil
} else if _, ok := compareAndSetStore[key]; !ok {
compareAndSetStore[key] = value
return true, nil
} else {
return false, nil
}
},
new(func(string, any) bool),
))
exprFunctions = append(exprFunctions, expr.Function(
"clone",
func(params ...any) (any, error) {
source := params[0].(force.ForceRecord)
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
dec := gob.NewDecoder(&buf)
err := enc.Encode(source)
if err != nil {
return nil, err
}
var copy force.ForceRecord
err = dec.Decode(©)
if err != nil {
return nil, err
}
return copy, nil
},
new(func(force.ForceRecord) force.ForceRecord),
))
changeValueStore := make(map[string]any)
// changeValue takes a key and value
// returns true if the key exists did not already exist
// returns true if the key already exists in the store, and the new value is different that the previous value
// returns false if the key exists in the store and the stored value is the same as passed value
exprFunctions = append(exprFunctions, expr.Function(
"changeValue",
func(params ...any) (any, error) {
key := params[0].(string)
value := params[1]
if changeValueStore[key] == value {
return false, nil
}
changeValueStore[key] = value
return true, nil
},
new(func(string, any) bool),
))
getSetStore := make(map[string]any)
// getSet takes a key and value
// sets the current key to the new value
// returns the previous value for the key
exprFunctions = append(exprFunctions, expr.Function(
"getSet",
func(params ...any) (any, error) {
key := params[0].(string)
value := params[1]
prevValue, ok := getSetStore[key]
getSetStore[key] = value
if ok {
return prevValue, nil
}
switch v := value.(type) {
case int:
return int(0), nil
case float64:
return float64(0), nil
case []string:
return []string{}, nil
case []any:
return []any{}, nil
case string:
return "", nil
default:
return prevValue, fmt.Errorf("Unsupported type %T", v)
}
},
new(func(string, int) int),
new(func(string, float64) float64),
new(func(string, string) string),
new(func(string, []string) []string),
new(func(string, []any) []any),
))
counters := make(map[string]int64)
// incr increments the number stored at key by one. If the key does not
// exist, it is set to 1.
exprFunctions = append(exprFunctions, expr.Function(
"incr",
func(params ...any) (any, error) {
key := params[0].(string)
if v, ok := counters[key]; ok {
counters[key] = v + 1
} else {
counters[key] = 1
}
return counters[key], nil
},
new(func(string) int64),
))
exprFunctions = append(exprFunctions, expr.Function(
"rand",
func(params ...any) (any, error) {
max := params[0].(int64)
bigInt, err := rand.Int(rand.Reader, big.NewInt(max))
if err != nil {
return bigInt, err
}
return bigInt.Int64(), nil
},
new(func(int64) int64),
))
exprFunctions = append(exprFunctions, expr.Function(
"readfile",
func(params ...any) (any, error) {
path := params[0].(string)
content, err := os.ReadFile(path)
if err != nil {
return "", err
}
return string(content), nil
},
new(func(string) string),
))
// Same as Expr's builtin date function, but with support for Salesforce's
// standard DateTime format.
exprFunctions = append(exprFunctions, expr.Function(
"date",
func(args ...any) (any, error) {
tz, ok := args[0].(*time.Location)
if ok {
args = args[1:]
}
date := args[0].(string)
if len(args) == 2 {
layout := args[1].(string)
if tz != nil {
return time.ParseInLocation(layout, date, tz)
}
return time.Parse(layout, date)
}
if len(args) == 3 {
layout := args[1].(string)
timeZone := args[2].(string)
tz, err := time.LoadLocation(timeZone)
if err != nil {
return nil, err
}
t, err := time.ParseInLocation(layout, date, tz)
if err != nil {
return nil, err
}
return t, nil
}
layouts := []string{
"2006-01-02T15:04:05-0700",
"2006-01-02",
"15:04:05",
"2006-01-02 15:04:05",
time.RFC3339,
time.RFC822,
time.RFC850,
time.RFC1123,
}
for _, layout := range layouts {
if tz == nil {
t, err := time.Parse(layout, date)
if err == nil {
return t, nil
}
} else {
t, err := time.ParseInLocation(layout, date, tz)
if err == nil {
return t, nil
}
}
}
return nil, fmt.Errorf("invalid date %s", date)
},
))
// fetch retrieves content from a URL. If the URL is relative, it's fetched from the Salesforce instance.
exprFunctions = append(exprFunctions, expr.Function(
"fetch",
func(params ...any) (any, error) {
url := params[0].(string)
var content []byte
var err error
// Check if URL is absolute or relative
if strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://") {
// Absolute URL - fetch directly
// Note: This will only work for URLs that don't require authentication
// For authenticated external URLs, you'd need to implement custom logic
return nil, fmt.Errorf("external URLs not supported in fetch function")
} else {
// Relative URL - fetch from Salesforce instance
// Ensure the URL starts with /
if !strings.HasPrefix(url, "/") {
url = "/" + url
}
content, err = session.GetAbsoluteBytes(url)
if err != nil {
return nil, fmt.Errorf("failed to fetch from Salesforce: %w", err)
}
}
// Return as string for now - can be used with base64() function
return string(content), nil
},
new(func(string) string),
))
exprFunctions = append(exprFunctions, expr.Operator("+", "MergePatch"))
exprFunctions = append(exprFunctions, expr.Operator("-", "DeleteKey"))
return exprFunctions
}
func exprConverter(expression string, context any, session BulkSession) (func(force.ForceRecord) []force.ForceRecord, error) {
program, err := expr.Compile(expression, append(exprFunctions(session), expr.Env(Env{}))...)
if err != nil {
return nil, fmt.Errorf("Invalid expression: %w", err)
}
converter := func(record force.ForceRecord) []force.ForceRecord {
env := Env{
Record: record,
ApexContext: context,
}
out, err := expr.Run(program, env)
if err != nil {
panic(err)
}
if out == nil {
return []force.ForceRecord{}
}
var singleRecord force.ForceRecord
err = mapstructure.Decode(out, &singleRecord)
if err == nil {
return []force.ForceRecord{singleRecord}
}
var multipleRecords []force.ForceRecord
err = mapstructure.Decode(out, &multipleRecords)
if err == nil {
return multipleRecords
}
log.Warnln("Unexpected value. It should be a map or array or maps. Got", out)
return []force.ForceRecord{}
}
return converter, nil
}