-
Notifications
You must be signed in to change notification settings - Fork 4
/
metadata.go
96 lines (77 loc) · 1.85 KB
/
metadata.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
package evaluate
import (
"github.com/puppetlabs/leg/relspec/pkg/ref"
)
type Annotation struct {
Name string
Attributes map[string]any
Description string
}
type Step[T ref.References[T]] struct {
*Result[T]
Annotations []Annotation
}
type Metadata[T ref.References[T]] struct {
Annotation Annotation
References T
Steps []*Step[T]
Evaluator Evaluator[T]
}
func (m *Metadata[T]) SetAnnotation(annotation Annotation) {
m.Annotation = annotation
}
func (m *Metadata[T]) MergeReferences(other *Metadata[T]) {
m.References = m.References.Merge(other.References)
}
func (m *Metadata[T]) InsertSteps(other *Metadata[T]) {
old := m.Steps
m.Steps = make([]*Step[T], 0, len(other.Steps)+len(old))
m.MergeSteps(other)
m.Steps = append(m.Steps, old...)
}
func (m *Metadata[T]) addStep(step *Step[T]) {
m.Steps = append(m.Steps, step)
m.MergeReferences(step.Metadata)
}
func (m *Metadata[T]) AddStep(step *Result[T], annotations ...Annotation) {
m.addStep(&Step[T]{
Result: step,
Annotations: annotations,
})
}
func (m *Metadata[T]) MergeSteps(other *Metadata[T]) {
for _, step := range other.Steps {
m.addStep(step)
}
}
func (m *Metadata[T]) SetEvaluator(ev Evaluator[T]) {
m.Evaluator = ev
}
func (m *Metadata[T]) MergeEvaluator(other *Metadata[T]) {
if other.Evaluator != nil {
m.SetEvaluator(other.Evaluator)
}
}
func (m *Metadata[T]) MergeEvaluatorIfNotSet(other *Metadata[T]) {
if m.Evaluator != nil {
return
}
m.MergeEvaluator(other)
}
func (m *Metadata[T]) Reduce() {
m.Steps = nil
}
func (m *Metadata[T]) Resolved() bool {
return m.References.Resolved()
}
func (m *Metadata[T]) OK() bool {
return m.References.OK()
}
func EmptyMetadata[T ref.References[T]]() *Metadata[T] {
return &Metadata[T]{}
}
func NewMetadata[T ref.References[T]](refs T) *Metadata[T] {
md := EmptyMetadata[T]()
md.References = refs
return md
}