-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsalesforce.go
43 lines (39 loc) · 1.26 KB
/
salesforce.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
package batch
import (
"fmt"
"strings"
force "github.com/ForceCLI/force/lib"
"github.com/ForceCLI/force/lib/query"
"github.com/octoberswimmer/batchforce/soql"
)
func makeFlatteningConverter(query string, converter Converter) (Converter, error) {
subQueryRelationships, err := soql.SubQueryRelationships(query)
if err != nil {
return nil, fmt.Errorf("Failed to parse query for subqueries: %w", err)
}
flatteningConverter := func(record force.ForceRecord) []force.ForceRecord {
flattened := flattenRecord(record, subQueryRelationships)
return converter(flattened)
}
return flatteningConverter, nil
}
// Replace subquery results with the records for the sub-query
func flattenRecord(r force.ForceRecord, subQueryRelationships soql.Relationships) force.ForceRecord {
for k, v := range r {
if v == nil {
continue
}
if subRelationships, found := subQueryRelationships[soql.Relationship(strings.ToLower(k))]; found {
subQueryResults := v.([]query.Record)
records := make([]force.ForceRecord, 0, len(subQueryResults))
for _, s := range subQueryResults {
records = append(records, flattenRecord(s.Fields, subRelationships))
}
r[k] = records
} else if m, ok := v.(map[string]any); ok {
delete(m, "attributes")
r[k] = m
}
}
return r
}