Skip to content

Commit 23d03b1

Browse files
committed
update
1 parent 2096524 commit 23d03b1

File tree

11 files changed

+136
-104
lines changed

11 files changed

+136
-104
lines changed

pkg/tree/entry.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,7 @@ type Entry interface {
127127
// - remainsToExists() returns true, because they remain to exist even though implicitly.
128128
// - shouldDelete() returns false, because no explicit delete should be issued for them.
129129
canDelete() bool
130-
// getChildren returns all the child Entries of the Entry in a map indexed by there name.
131-
// FYI: leaf values are not considered children
132-
getChildren() map[string]Entry
130+
GetChilds(DescendMethod) EntryMap
133131
FilterChilds(keys map[string]string) ([]Entry, error)
134132
// ToJson returns the Tree contained structure as JSON
135133
// use e.g. json.MarshalIndent() on the returned struct
@@ -160,6 +158,7 @@ type Entry interface {
160158
}
161159

162160
type EntryVisitor interface {
161+
Config() *EntryVisitorConfig
163162
Visit(ctx context.Context, e Entry) error
164163
Up()
165164
}
@@ -178,3 +177,25 @@ type LeafVariantEntries interface {
178177
AddExplicitDeleteEntry(owner string, priority int32) *LeafEntry
179178
GetByOwner(owner string) *LeafEntry
180179
}
180+
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+
196+
type DescendMethod int
197+
198+
const (
199+
DescendMethodAll DescendMethod = iota
200+
DescendMethodActiveChilds
201+
)

pkg/tree/entry_map.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package tree
2+
3+
type EntryMap map[string]Entry

pkg/tree/json.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (s *sharedEntryAttributes) toJsonInternal(onlyNewOrUpdated bool, ietf bool)
4141
// ancestor is a list with keys.
4242
result := map[string]any{}
4343

44-
for key, c := range s.filterActiveChoiceCaseChilds() {
44+
for key, c := range s.GetChilds(DescendMethodActiveChilds) {
4545
ancest, _ := s.GetFirstAncestorWithSchema()
4646
prefixedKey := jsonGetIetfPrefixConditional(key, c, ancest, ietf)
4747
// recurse the call
@@ -101,7 +101,7 @@ func (s *sharedEntryAttributes) toJsonInternal(onlyNewOrUpdated bool, ietf bool)
101101
default:
102102
// otherwise this is a map
103103
result := map[string]any{}
104-
for key, c := range s.filterActiveChoiceCaseChilds() {
104+
for key, c := range s.GetChilds(DescendMethodActiveChilds) {
105105
prefixedKey := jsonGetIetfPrefixConditional(key, c, s, ietf)
106106
js, err := c.toJsonInternal(onlyNewOrUpdated, ietf)
107107
if err != nil {

pkg/tree/sharedEntryAttributes.go

Lines changed: 61 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -172,15 +172,15 @@ func (s *sharedEntryAttributes) GetDeviations(ch chan<- *types.DeviationEntry, a
172172
// if s is a presence container but has active childs, it should not be treated as a presence
173173
// container, hence the leafvariants should not be processed. For presence container with
174174
// childs the TypedValue.empty_val in the presence container is irrelevant.
175-
if s.schema.GetContainer().GetIsPresence() && len(s.filterActiveChoiceCaseChilds()) > 0 {
175+
if s.schema.GetContainer().GetIsPresence() && len(s.GetChilds(DescendMethodActiveChilds)) > 0 {
176176
return
177177
}
178178

179179
// calculate Deviation on the LeafVariants
180180
s.leafVariants.GetDeviations(ch, activeCase)
181181

182182
// get all active childs
183-
activeChilds := s.filterActiveChoiceCaseChilds()
183+
activeChilds := s.GetChilds(DescendMethodActiveChilds)
184184

185185
// iterate through all childs
186186
for cName, c := range s.getChildren() {
@@ -332,7 +332,7 @@ func (s *sharedEntryAttributes) GetListChilds() ([]Entry, error) {
332332
for level := 0; level < len(keys); level++ {
333333
for _, e := range actualEntries {
334334
// add all children
335-
for _, c := range e.getChildren() {
335+
for _, c := range e.GetChilds(DescendMethodAll) {
336336
newEntries = append(newEntries, c)
337337
}
338338
}
@@ -369,7 +369,7 @@ func (s *sharedEntryAttributes) FilterChilds(keys map[string]string) ([]Entry, e
369369
// therefor we need to go through the processEntries List
370370
// and collect all the matching childs
371371
for _, entry := range processEntries {
372-
childs := entry.getChildren()
372+
childs := entry.GetChilds(DescendMethodAll)
373373
matchEntry, childExists := childs[keyVal]
374374
// so if such child, that matches the given filter value exists, we append it to the results
375375
if childExists {
@@ -380,7 +380,7 @@ func (s *sharedEntryAttributes) FilterChilds(keys map[string]string) ([]Entry, e
380380
// this is basically the wildcard case, so go through all childs and add them
381381
result = []Entry{}
382382
for _, entry := range processEntries {
383-
childs := entry.getChildren()
383+
childs := entry.GetChilds(DescendMethodAll)
384384
for _, v := range childs {
385385
// hence we add all the existing childs to the result list
386386
result = append(result, v)
@@ -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.childs.GetAll() {
433+
for _, c := range s.GetChilds(v.Config().descendMethod) {
434434
err := c.Walk(ctx, v)
435435
if err != nil {
436436
return err
@@ -533,7 +533,7 @@ func (s *sharedEntryAttributes) canDelete() bool {
533533
}
534534

535535
// handle containers
536-
for _, c := range s.filterActiveChoiceCaseChilds() {
536+
for _, c := range s.GetChilds(DescendMethodActiveChilds) {
537537
canDelete := c.canDelete()
538538
if !canDelete {
539539
s.cacheCanDelete = utils.BoolPtr(false)
@@ -560,7 +560,7 @@ func (s *sharedEntryAttributes) shouldDelete() bool {
560560
// but a real delete should only be added if there is at least one shouldDelete() == true
561561
shouldDelete := false
562562

563-
activeChilds := s.filterActiveChoiceCaseChilds()
563+
activeChilds := s.GetChilds(DescendMethodActiveChilds)
564564
// if we have no active childs, we can and should delete.
565565
if len(s.choicesResolvers) > 0 && len(activeChilds) == 0 {
566566
canDelete = true
@@ -607,7 +607,7 @@ func (s *sharedEntryAttributes) remainsToExist() bool {
607607

608608
// handle containers
609609
childsRemain := false
610-
for _, c := range s.filterActiveChoiceCaseChilds() {
610+
for _, c := range s.GetChilds(DescendMethodActiveChilds) {
611611
childsRemain = c.remainsToExist()
612612
if childsRemain {
613613
break
@@ -757,7 +757,7 @@ func (s *sharedEntryAttributes) NavigateSdcpbPath(ctx context.Context, pathElems
757757
}
758758
return entry.NavigateSdcpbPath(ctx, pathElems[1:], false)
759759
default:
760-
e, exists := s.filterActiveChoiceCaseChilds()[pathElems[0].Name]
760+
e, exists := s.GetChilds(DescendMethodActiveChilds)[pathElems[0].Name]
761761
if !exists {
762762
pth := &sdcpb.Path{Elem: pathElems}
763763
e, err = s.tryLoadingDefault(ctx, utils.ToStrings(pth, false, false))
@@ -830,7 +830,7 @@ func (s *sharedEntryAttributes) Navigate(ctx context.Context, path []string, isR
830830
}
831831
return parent.Navigate(ctx, path[1:], false, dotdotSkipKeys)
832832
default:
833-
e, exists := s.filterActiveChoiceCaseChilds()[path[0]]
833+
e, exists := s.GetChilds(DescendMethodActiveChilds)[path[0]]
834834
if !exists {
835835
e, err = s.tryLoadingDefault(ctx, append(s.Path(), path...))
836836
if err != nil {
@@ -885,7 +885,7 @@ func (s *sharedEntryAttributes) GetHighestPrecedence(result LeafVariantSlice, on
885885
}
886886

887887
// continue with childs. Childs are part of choices, process only the "active" (highes precedence) childs
888-
for _, c := range s.filterActiveChoiceCaseChilds() {
888+
for _, c := range s.GetChilds(DescendMethodActiveChilds) {
889889
result = c.GetHighestPrecedence(result, onlyNewOrUpdated, includeDefaults)
890890
}
891891
return result
@@ -950,7 +950,7 @@ func (s *sharedEntryAttributes) Validate(ctx context.Context, resultChan chan<-
950950
// recurse the call to the child elements
951951
wg := sync.WaitGroup{}
952952
defer wg.Wait()
953-
for _, c := range s.filterActiveChoiceCaseChilds() {
953+
for _, c := range s.GetChilds(DescendMethodActiveChilds) {
954954
wg.Add(1)
955955
valFunc := func(x Entry) {
956956
x.Validate(ctx, resultChan, statChan, vCfg)
@@ -1182,7 +1182,7 @@ func (s *sharedEntryAttributes) ImportConfig(ctx context.Context, t importer.Imp
11821182
return err
11831183
}
11841184
// if the child does not exist, create it
1185-
if keyChild, exists = actualEntry.getChildren()[keyElemValue]; !exists {
1185+
if keyChild, exists = actualEntry.GetChilds(DescendMethodAll)[keyElemValue]; !exists {
11861186
keyChild, err = newEntry(ctx, actualEntry, keyElemValue, s.treeContext)
11871187
if err != nil {
11881188
return err
@@ -1335,7 +1335,7 @@ func (s *sharedEntryAttributes) validateMandatoryWithKeys(ctx context.Context, l
13351335
// iterate over the attributes make sure any of these exists
13361336
for _, attr := range attributes {
13371337
// first check if the mandatory value is set via the intent, e.g. part of the tree already
1338-
v, existsInTree = s.filterActiveChoiceCaseChilds()[attr]
1338+
v, existsInTree = s.GetChilds(DescendMethodActiveChilds)[attr]
13391339
// if exists and remains to Exist
13401340
if existsInTree && v.remainsToExist() {
13411341
// set success to true and break the loop
@@ -1358,7 +1358,7 @@ func (s *sharedEntryAttributes) validateMandatoryWithKeys(ctx context.Context, l
13581358
return
13591359
}
13601360

1361-
for _, c := range s.filterActiveChoiceCaseChilds() {
1361+
for _, c := range s.GetChilds(DescendMethodActiveChilds) {
13621362
c.validateMandatoryWithKeys(ctx, level-1, attributes, choiceName, resultChan)
13631363
}
13641364
}
@@ -1411,7 +1411,7 @@ func (s *sharedEntryAttributes) FinishInsertionPhase(ctx context.Context) error
14111411

14121412
// recurse the call to all (active) entries within the tree.
14131413
// Thereby already using the choiceCaseResolver via filterActiveChoiceCaseChilds()
1414-
for _, child := range s.filterActiveChoiceCaseChilds() {
1414+
for _, child := range s.GetChilds(DescendMethodActiveChilds) {
14151415
err = child.FinishInsertionPhase(ctx)
14161416
if err != nil {
14171417
return err
@@ -1473,31 +1473,59 @@ func (s *sharedEntryAttributes) populateChoiceCaseResolvers(_ context.Context) e
14731473
return nil
14741474
}
14751475

1476-
// filterActiveChoiceCaseChilds returns the list of child elements. In case the Entry is
1477-
// a container with a / multiple choices, the list of childs is filtered to only return the
1478-
// cases that have the highest precedence.
1479-
func (s *sharedEntryAttributes) filterActiveChoiceCaseChilds() map[string]Entry {
1476+
func (s *sharedEntryAttributes) GetChilds(d DescendMethod) EntryMap {
14801477
if s.schema == nil {
14811478
return s.childs.GetAll()
14821479
}
14831480

1484-
skipAttributesList := s.choicesResolvers.GetSkipElements()
1485-
// if there are no items that should be skipped, take a shortcut
1486-
// and simply return all childs straight away
1487-
if len(skipAttributesList) == 0 {
1481+
switch d {
1482+
case DescendMethodAll:
14881483
return s.childs.GetAll()
1489-
}
1490-
result := map[string]Entry{}
1491-
// optimization option: sort the slices and forward in parallel, lifts extra burden that the contains call holds.
1492-
for childName, child := range s.childs.GetAll() {
1493-
if slices.Contains(skipAttributesList, childName) {
1494-
continue
1484+
case DescendMethodActiveChilds:
1485+
skipAttributesList := s.choicesResolvers.GetSkipElements()
1486+
// if there are no items that should be skipped, take a shortcut
1487+
// and simply return all childs straight away
1488+
if len(skipAttributesList) == 0 {
1489+
return s.childs.GetAll()
1490+
}
1491+
result := map[string]Entry{}
1492+
// optimization option: sort the slices and forward in parallel, lifts extra burden that the contains call holds.
1493+
for childName, child := range s.childs.GetAll() {
1494+
if slices.Contains(skipAttributesList, childName) {
1495+
continue
1496+
}
1497+
result[childName] = child
14951498
}
1496-
result[childName] = child
1499+
return result
14971500
}
1498-
return result
1501+
return nil
14991502
}
15001503

1504+
// // filterActiveChoiceCaseChilds returns the list of child elements. In case the Entry is
1505+
// // a container with a / multiple choices, the list of childs is filtered to only return the
1506+
// // cases that have the highest precedence.
1507+
// func (s *sharedEntryAttributes) filterActiveChoiceCaseChilds() map[string]Entry {
1508+
// if s.schema == nil {
1509+
// return s.childs.GetAll()
1510+
// }
1511+
1512+
// skipAttributesList := s.choicesResolvers.GetSkipElements()
1513+
// // if there are no items that should be skipped, take a shortcut
1514+
// // and simply return all childs straight away
1515+
// if len(skipAttributesList) == 0 {
1516+
// return s.childs.GetAll()
1517+
// }
1518+
// result := map[string]Entry{}
1519+
// // optimization option: sort the slices and forward in parallel, lifts extra burden that the contains call holds.
1520+
// for childName, child := range s.childs.GetAll() {
1521+
// if slices.Contains(skipAttributesList, childName) {
1522+
// continue
1523+
// }
1524+
// result[childName] = child
1525+
// }
1526+
// return result
1527+
// }
1528+
15011529
// StringIndent returns the sharedEntryAttributes in its string representation
15021530
// The string is intented according to the nesting level in the yang model
15031531
func (s *sharedEntryAttributes) StringIndent(result []string) []string {

pkg/tree/sharedEntryAttributes_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ func Test_sharedEntryAttributes_GetListChilds(t *testing.T) {
391391
for _, elem := range got {
392392
elemNames = append(elemNames, elem.PathName())
393393
elemChilds[elem.PathName()] = []string{}
394-
for k := range elem.getChildren() {
394+
for k := range elem.GetChilds(DescendMethodAll) {
395395
elemChilds[elem.PathName()] = append(elemChilds[elem.PathName()], k)
396396
}
397397
}

pkg/tree/sorter.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ func getListEntrySortFunc(parent Entry) func(a, b Entry) int {
66
keys := parent.GetSchemaKeys()
77
var cmpResult int
88
for _, v := range keys {
9-
achild, exists := a.getChildren()[v]
9+
achild, exists := a.GetChilds(DescendMethodAll)[v]
1010
if !exists {
1111
return 0
1212
}
13-
bchild, exists := b.getChildren()[v]
13+
bchild, exists := b.GetChilds(DescendMethodAll)[v]
1414
if !exists {
1515
return 0
1616
}

pkg/tree/types/update_insert_flags.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ func (f *UpdateInsertFlags) GetNewFlag() bool {
6363
}
6464

6565
func (f *UpdateInsertFlags) Apply(le LeafEntry) *UpdateInsertFlags {
66+
if f.explicitDelete {
67+
le.MarkExpliciteDelete()
68+
}
6669
if f.delete {
6770
le.MarkDelete(f.onlyIntended)
6871
return f
@@ -71,9 +74,6 @@ func (f *UpdateInsertFlags) Apply(le LeafEntry) *UpdateInsertFlags {
7174
le.MarkNew()
7275
return f
7376
}
74-
if f.explicitDelete {
75-
le.MarkExpliciteDelete()
76-
}
7777
return f
7878
}
7979

pkg/tree/visitor_explicit_delete.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,15 @@ func (edv *ExplicitDeleteVisitor) Visit(ctx context.Context, e Entry) error {
3535
edv.relatedLeafVariants = append(edv.relatedLeafVariants, le)
3636
return nil
3737
}
38+
3839
func (edv *ExplicitDeleteVisitor) Up() {
3940
// noop
4041
}
4142

43+
func (o *ExplicitDeleteVisitor) Config() *EntryVisitorConfig {
44+
return NewEntryVisitorConfig().SetDescendingMethod(DescendMethodAll)
45+
}
46+
4247
// GetExplicitDeleteCreationCount returns the amount of all the explicitDelete LeafVariants that where created.
4348
func (edv *ExplicitDeleteVisitor) GetExplicitDeleteCreationCount() int {
4449
return len(edv.relatedLeafVariants)

0 commit comments

Comments
 (0)