/
flowpipe_loop_query.go
110 lines (88 loc) · 3.16 KB
/
flowpipe_loop_query.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
package modconfig
import (
"reflect"
"github.com/hashicorp/hcl/v2"
"github.com/iancoleman/strcase"
"github.com/turbot/go-kit/helpers"
"github.com/turbot/pipe-fittings/error_helpers"
"github.com/turbot/pipe-fittings/schema"
"github.com/turbot/pipe-fittings/utils"
)
type LoopQueryStep struct {
LoopStep
Database *string `json:"database,omitempty" hcl:"database,optional" cty:"database"`
Sql *string `json:"sql,omitempty" hcl:"sql,optional" cty:"sql"`
Args *[]interface{} `json:"args,omitempty" hcl:"args,optional" cty:"args"`
}
func (l *LoopQueryStep) Equals(other LoopDefn) bool {
if l == nil && helpers.IsNil(other) {
return true
}
if l == nil && !helpers.IsNil(other) || !helpers.IsNil(l) && other == nil {
return false
}
otherLoopQueryStep, ok := other.(*LoopQueryStep)
if !ok {
return false
}
if !l.LoopStep.Equals(otherLoopQueryStep.LoopStep) {
return false
}
if l.Args == nil && otherLoopQueryStep.Args != nil || l.Args != nil && otherLoopQueryStep.Args == nil {
return false
}
if l.Args != nil {
if !reflect.DeepEqual(*l.Args, *otherLoopQueryStep.Args) {
return false
}
}
return utils.PtrEqual(l.Database, otherLoopQueryStep.Database) &&
utils.PtrEqual(l.Sql, otherLoopQueryStep.Sql)
}
func (l *LoopQueryStep) UpdateInput(input Input, evalContext *hcl.EvalContext) (Input, error) {
result, diags := simpleTypeInputFromAttribute(l.GetUnresolvedAttributes(), input, evalContext, schema.AttributeTypeDatabase, l.Database)
if len(diags) > 0 {
return nil, error_helpers.BetterHclDiagsToError("query", diags)
}
result, diags = simpleTypeInputFromAttribute(l.GetUnresolvedAttributes(), result, evalContext, schema.AttributeTypeSql, l.Sql)
if len(diags) > 0 {
return nil, error_helpers.BetterHclDiagsToError("query", diags)
}
result, diags = interfaceSliceInputFromAttribute(l.GetUnresolvedAttributes(), result, evalContext, schema.AttributeTypeArgs, l.Args)
if len(diags) > 0 {
return nil, error_helpers.BetterHclDiagsToError("query", diags)
}
return result, nil
}
func (*LoopQueryStep) GetType() string {
return schema.BlockTypePipelineStepQuery
}
func (l *LoopQueryStep) SetAttributes(hclAttributes hcl.Attributes, evalContext *hcl.EvalContext) hcl.Diagnostics {
diags := l.LoopStep.SetAttributes(hclAttributes, evalContext)
for name, attr := range hclAttributes {
switch name {
case schema.AttributeTypeDatabase, schema.AttributeTypeSql:
fieldName := strcase.ToCamel(name)
stepDiags := setStringAttributeWithResultReference(attr, evalContext, l, fieldName, true, true)
if stepDiags.HasErrors() {
diags = append(diags, stepDiags...)
}
case schema.AttributeTypeArgs:
fieldName := strcase.ToCamel(name)
stepDiags := setInterfaceSliceAttributeWithResultReference(attr, evalContext, l, fieldName, true, true)
if stepDiags.HasErrors() {
diags = append(diags, stepDiags...)
}
case schema.AttributeTypeUntil:
// already handled in SetAttributes
default:
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid attribute",
Detail: "Invalid attribute '" + name + "' in the step loop block",
Subject: &attr.Range,
})
}
}
return diags
}