Skip to content

Commit 189bd39

Browse files
committed
adjust visitor config
1 parent 4e8612a commit 189bd39

File tree

7 files changed

+147
-31
lines changed

7 files changed

+147
-31
lines changed

pkg/tree/entry.go

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ type Entry interface {
158158
}
159159

160160
type EntryVisitor interface {
161-
Config() *EntryVisitorConfig
161+
DescendMethod() DescendMethod
162162
Visit(ctx context.Context, e Entry) error
163163
Up()
164164
}
@@ -178,21 +178,6 @@ type LeafVariantEntries interface {
178178
GetByOwner(owner string) *LeafEntry
179179
}
180180

181-
type EntryVisitorConfig struct {
182-
descendMethod DescendMethod
183-
}
184-
185-
func NewEntryVisitorConfig() *EntryVisitorConfig {
186-
return &EntryVisitorConfig{
187-
descendMethod: DescendMethodActiveChilds,
188-
}
189-
}
190-
191-
func (e *EntryVisitorConfig) SetDescendingMethod(d DescendMethod) *EntryVisitorConfig {
192-
e.descendMethod = d
193-
return e
194-
}
195-
196181
type DescendMethod int
197182

198183
const (

pkg/tree/sharedEntryAttributes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ func (s *sharedEntryAttributes) Walk(ctx context.Context, v EntryVisitor) error
430430
}
431431

432432
// trigger the execution on all childs
433-
for _, c := range s.GetChilds(v.Config().descendMethod) {
433+
for _, c := range s.GetChilds(v.DescendMethod()) {
434434
err := c.Walk(ctx, v)
435435
if err != nil {
436436
return err

pkg/tree/visitor_base.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package tree
2+
3+
// BaseVisitor abstract base visitor implementation that all the concrete visitory are ment to embed.
4+
type BaseVisitor struct{}
5+
6+
func (b *BaseVisitor) Up() {
7+
// noop
8+
}
9+
10+
func (b *BaseVisitor) DescendMethod() DescendMethod {
11+
return DescendMethodAll
12+
}

pkg/tree/visitor_blame_config.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package tree
2+
3+
import (
4+
"context"
5+
"slices"
6+
"strings"
7+
8+
sdcpb "github.com/sdcio/sdc-protos/sdcpb"
9+
"google.golang.org/protobuf/proto"
10+
)
11+
12+
type BlameConfigVisitor struct {
13+
BaseVisitor
14+
stack []*sdcpb.BlameTreeElement
15+
includeDefaults bool
16+
}
17+
18+
var _ EntryVisitor = (*BlameConfigVisitor)(nil)
19+
20+
func NewBlameConfigVisitor(includeDefaults bool) *BlameConfigVisitor {
21+
return &BlameConfigVisitor{
22+
stack: []*sdcpb.BlameTreeElement{},
23+
includeDefaults: includeDefaults,
24+
}
25+
}
26+
27+
func (b *BlameConfigVisitor) Visit(ctx context.Context, e Entry) error {
28+
name := e.PathName()
29+
if e.IsRoot() {
30+
name = "root"
31+
}
32+
result := sdcpb.NewBlameTreeElement(name)
33+
skipAdd := false
34+
35+
// process Value
36+
highestLe := e.GetLeafVariantEntries().GetHighestPrecedence(false, true)
37+
if highestLe != nil {
38+
if highestLe.Update.Owner() != DefaultsIntentName || b.includeDefaults {
39+
result.SetValue(highestLe.Update.Value()).SetOwner(highestLe.Update.Owner())
40+
41+
// check if running equals the expected
42+
runningLe := e.GetLeafVariantEntries().GetRunning()
43+
if runningLe != nil {
44+
if !proto.Equal(runningLe.Update.Value(), highestLe.Update.Value()) {
45+
result.DeviationValue = runningLe.Value()
46+
}
47+
}
48+
} else {
49+
// if it is default but no default is meant to be returned
50+
skipAdd = true
51+
}
52+
}
53+
// add to the result tree as a child
54+
if !skipAdd && len(b.stack) > 0 {
55+
b.stack[len(b.stack)-1].AddChild(result)
56+
}
57+
// add to the stack as last element
58+
b.stack = append(b.stack, result)
59+
return nil
60+
}
61+
62+
func (b *BlameConfigVisitor) Up() {
63+
// sort to make te output stable
64+
slices.SortFunc(b.stack[len(b.stack)-1].Childs, func(a *sdcpb.BlameTreeElement, b *sdcpb.BlameTreeElement) int {
65+
return strings.Compare(a.GetName(), b.GetName())
66+
})
67+
// remove the last elem from stack
68+
if len(b.stack) > 1 {
69+
b.stack = b.stack[:len(b.stack)-1]
70+
}
71+
}
72+
73+
func (o *BlameConfigVisitor) DescendMethod() DescendMethod {
74+
return DescendMethodAll
75+
}
76+
77+
func (b *BlameConfigVisitor) GetResult() *sdcpb.BlameTreeElement {
78+
if len(b.stack) == 1 {
79+
return b.stack[0]
80+
}
81+
return nil
82+
}

pkg/tree/visitor_explicit_delete.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
)
66

77
type ExplicitDeleteVisitor struct {
8+
BaseVisitor
89
owner string
910
priority int32
1011

@@ -36,14 +37,6 @@ func (edv *ExplicitDeleteVisitor) Visit(ctx context.Context, e Entry) error {
3637
return nil
3738
}
3839

39-
func (edv *ExplicitDeleteVisitor) Up() {
40-
// noop
41-
}
42-
43-
func (o *ExplicitDeleteVisitor) Config() *EntryVisitorConfig {
44-
return NewEntryVisitorConfig().SetDescendingMethod(DescendMethodAll)
45-
}
46-
4740
// GetExplicitDeleteCreationCount returns the amount of all the explicitDelete LeafVariants that where created.
4841
func (edv *ExplicitDeleteVisitor) GetExplicitDeleteCreationCount() int {
4942
return len(edv.relatedLeafVariants)

pkg/tree/visitor_explicit_delete_test.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ func TestExplicitDeleteVisitor_Visit(t *testing.T) {
8080
t.Error(err)
8181
}
8282

83-
err = testhelper.LoadYgotStructIntoTreeRoot(ctx, config1(), root, owner1, owner1Prio, flagsNew)
83+
err = testhelper.LoadYgotStructIntoTreeRoot(ctx, config1(), root, owner1, owner1Prio, flagsExisting)
8484
if err != nil {
8585
t.Error(err)
8686
}
8787

88-
err = testhelper.LoadYgotStructIntoTreeRoot(ctx, config1(), root, RunningIntentName, RunningValuesPrio, flagsNew)
88+
err = testhelper.LoadYgotStructIntoTreeRoot(ctx, config1(), root, RunningIntentName, RunningValuesPrio, flagsExisting)
8989
if err != nil {
9090
t.Error(err)
9191
}
@@ -126,12 +126,12 @@ func TestExplicitDeleteVisitor_Visit(t *testing.T) {
126126
t.Error(err)
127127
}
128128

129-
err = testhelper.LoadYgotStructIntoTreeRoot(ctx, config1(), root, owner1, owner1Prio, flagsNew)
129+
err = testhelper.LoadYgotStructIntoTreeRoot(ctx, config1(), root, owner1, owner1Prio, flagsExisting)
130130
if err != nil {
131131
t.Error(err)
132132
}
133133

134-
err = testhelper.LoadYgotStructIntoTreeRoot(ctx, config1(), root, RunningIntentName, RunningValuesPrio, flagsNew)
134+
err = testhelper.LoadYgotStructIntoTreeRoot(ctx, config1(), root, RunningIntentName, RunningValuesPrio, flagsExisting)
135135
if err != nil {
136136
t.Error(err)
137137
}
@@ -190,14 +190,18 @@ func TestExplicitDeleteVisitor_Visit(t *testing.T) {
190190
}
191191
}
192192

193-
//
193+
// get the resulting deletes
194194
dels, err := root.GetDeletes(true)
195195
if err != nil {
196196
t.Error(err)
197197
return
198198
}
199199
t.Logf("Deletes:\n%s", dels.PathSlices().StringSlice())
200-
t.Errorf("foo")
200+
updates := root.GetHighestPrecedence(true)
201+
202+
t.Logf("Updates:\n%s", updates.String())
203+
204+
t.Error(nil)
201205
})
202206
}
203207
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package tree
2+
3+
import (
4+
"context"
5+
)
6+
7+
type MarkOwnerDeleteVisitor struct {
8+
BaseVisitor
9+
owner string
10+
onlyIntended bool
11+
leafVariantsMatched LeafVariantSlice
12+
}
13+
14+
var _ EntryVisitor = (*MarkOwnerDeleteVisitor)(nil)
15+
16+
func NewMarkOwnerDeleteVisitor(owner string, onlyIntended bool) *MarkOwnerDeleteVisitor {
17+
return &MarkOwnerDeleteVisitor{
18+
owner: owner,
19+
onlyIntended: onlyIntended,
20+
leafVariantsMatched: LeafVariantSlice{},
21+
}
22+
}
23+
24+
func (o *MarkOwnerDeleteVisitor) Visit(ctx context.Context, e Entry) error {
25+
le := e.GetLeafVariantEntries().MarkOwnerForDeletion(o.owner, o.onlyIntended)
26+
if le != nil {
27+
o.leafVariantsMatched = append(o.leafVariantsMatched, le)
28+
}
29+
return nil
30+
}
31+
32+
// GetHitCount returns the number of entries marked for deletion
33+
func (o *MarkOwnerDeleteVisitor) GetHitCount() int {
34+
return len(o.leafVariantsMatched)
35+
}
36+
37+
// GetMatches return all the altered LeafVariants
38+
func (o *MarkOwnerDeleteVisitor) GetMatches() LeafVariantSlice {
39+
return o.leafVariantsMatched
40+
}

0 commit comments

Comments
 (0)