-
Notifications
You must be signed in to change notification settings - Fork 262
/
resolved_runtime_dependency.go
79 lines (63 loc) · 2.18 KB
/
resolved_runtime_dependency.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
package dashboardtypes
import (
"fmt"
"log"
"sync"
"github.com/turbot/go-kit/helpers"
"github.com/turbot/steampipe/pkg/steampipeconfig/modconfig"
"github.com/turbot/steampipe/pkg/type_conversion"
)
// ResolvedRuntimeDependency is a wrapper for RuntimeDependency which contains the resolved value
// we must wrap it so that we do not mutate the underlying workspace data when resolving dependency values
type ResolvedRuntimeDependency struct {
Dependency *modconfig.RuntimeDependency
valueLock sync.Mutex
Value any
// the name of the run which publishes this dependency
publisherName string
valueChannel chan *ResolvedRuntimeDependencyValue
}
func NewResolvedRuntimeDependency(dep *modconfig.RuntimeDependency, valueChannel chan *ResolvedRuntimeDependencyValue, publisherName string) *ResolvedRuntimeDependency {
return &ResolvedRuntimeDependency{
Dependency: dep,
valueChannel: valueChannel,
publisherName: publisherName,
}
}
// ScopedName returns is a unique name for the dependency by prepending the publisher name
// this is used to uniquely identify which `with` is used - for the snapshot data
func (d *ResolvedRuntimeDependency) ScopedName() string {
return fmt.Sprintf("%s.%s", d.publisherName, d.Dependency.SourceResourceName())
}
func (d *ResolvedRuntimeDependency) IsResolved() bool {
d.valueLock.Lock()
defer d.valueLock.Unlock()
return d.hasValue()
}
func (d *ResolvedRuntimeDependency) Resolve() error {
d.valueLock.Lock()
defer d.valueLock.Unlock()
log.Printf("[TRACE] ResolvedRuntimeDependency Resolve dep %s chan %p", d.Dependency.PropertyPath, d.valueChannel)
// if we are already resolved, do nothing
if d.hasValue() {
return nil
}
// wait for value
val := <-d.valueChannel
d.Value = val.Value
// TACTICAL if the desired value is an array, wrap in an array
if d.Dependency.IsArray {
d.Value = type_conversion.AnySliceToTypedSlice([]any{d.Value})
}
if val.Error != nil {
return val.Error
}
// we should have a non nil value now
if !d.hasValue() {
return fmt.Errorf("nil value recevied for runtime dependency %s", d.Dependency.String())
}
return nil
}
func (d *ResolvedRuntimeDependency) hasValue() bool {
return !helpers.IsNil(d.Value)
}