-
Notifications
You must be signed in to change notification settings - Fork 4
/
apex.go
51 lines (46 loc) · 1.16 KB
/
apex.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
package batch
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"github.com/benhoyt/goawk/interp"
anon "github.com/octoberswimmer/batchforce/apex"
)
func (e *Execution) getApexContext() (map[string]any, error) {
apex := e.Apex
apexVars, err := anon.Vars(apex)
if err != nil {
return nil, err
}
lines := []string{"\n" + `Map<String, Object> b_f_c_t_x = new Map<String, Object>();`}
for _, v := range apexVars {
lines = append(lines, fmt.Sprintf(`b_f_c_t_x.put('%s', %s);`, v, v))
}
lines = append(lines, `System.debug(JSON.serialize(b_f_c_t_x));`)
apex = apex + strings.Join(lines, "\n")
session := e.session()
debugLog, err := session.Partner.ExecuteAnonymous(apex)
if err != nil {
return nil, err
}
val, err := varFromDebugLog(debugLog)
if err != nil {
return nil, err
}
var n map[string]any
err = json.Unmarshal(val, &n)
if err != nil {
return nil, err
}
return n, nil
}
func varFromDebugLog(log string) ([]byte, error) {
input := strings.NewReader(log)
output := new(bytes.Buffer)
err := interp.Exec(`$2~/USER_DEBUG/ { var = $5 } END { print var }`, "|", input, output)
if err != nil {
return nil, err
}
return output.Bytes(), nil
}