/
column_transforms.go
63 lines (56 loc) · 2.13 KB
/
column_transforms.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
package transform
import (
"context"
"log"
"github.com/turbot/steampipe-plugin-sdk/plugin/quals"
)
// TransformData is the input to a transform function.
type TransformData struct {
// an optional parameter
Param interface{}
// the value to be transformed
Value interface{}
// a data object containing the source data for this column
HydrateItem interface{}
// all hydrate results
HydrateResults map[string]interface{}
// the column this transform is generating
ColumnName string
// the 'matrix item' associated with this row
MatrixItem map[string]interface{}
// KeyColumnQuals will be populated with the quals as a map of column name to an array of quals for that column
KeyColumnQuals map[string]quals.QualSlice
}
// TransformFunc is a function to transform a data value from the api value to a column value
// parameters are: value, parent json object, param
// returns the transformed HydrateItem
type TransformFunc func(context.Context, *TransformData) (interface{}, error)
type GetSourceFieldFunc func(interface{}) string
// ColumnTransforms struct defines the data transforms required to map from a JSON value to a column value
type ColumnTransforms struct {
// a list of transforms to apply to the data
Transforms []*TransformCall
// should this transform chain start with the default transform for the column
ApplyDefaultTransform bool
}
func (t *ColumnTransforms) Execute(ctx context.Context, transformData *TransformData, defaultTransform *ColumnTransforms) (interface{}, error) {
var value interface{}
var err error
if t.ApplyDefaultTransform {
log.Printf("[TRACE] ColumnTransforms.Execute - running default transforms first\n")
if value, err = callTransforms(ctx, value, transformData, defaultTransform.Transforms); err != nil {
return nil, err
}
}
return callTransforms(ctx, value, transformData, t.Transforms)
}
func callTransforms(ctx context.Context, value interface{}, transformData *TransformData, transforms []*TransformCall) (interface{}, error) {
for _, tr := range transforms {
var err error
value, err = tr.Execute(ctx, value, transformData)
if err != nil {
return nil, err
}
}
return value, nil
}