@@ -28,7 +28,7 @@ import (
28
28
type ResetProcessor struct {
29
29
target interface {}
30
30
paths []tree.Path
31
- visitedNodes map [* yaml.Node ]bool
31
+ visitedNodes map [* yaml.Node ]string
32
32
}
33
33
34
34
// UnmarshalYAML implement yaml.Unmarshaler
@@ -43,23 +43,27 @@ func (p *ResetProcessor) UnmarshalYAML(value *yaml.Node) error {
43
43
44
44
// resolveReset detects `!reset` tag being set on yaml nodes and record position in the yaml tree
45
45
func (p * ResetProcessor ) resolveReset (node * yaml.Node , path tree.Path ) (* yaml.Node , error ) {
46
+ pathStr := path .String ()
46
47
// If the path contains "<<", removing the "<<" element and merging the path
47
- if strings .Contains (path . String () , ".<<" ) {
48
- path = tree .NewPath (strings .Replace (path . String () , ".<<" , "" , 1 ))
48
+ if strings .Contains (pathStr , ".<<" ) {
49
+ path = tree .NewPath (strings .Replace (pathStr , ".<<" , "" , 1 ))
49
50
}
50
51
51
52
// Check for cycle
52
53
if p .visitedNodes == nil {
53
- p .visitedNodes = make (map [* yaml.Node ]bool )
54
+ p .visitedNodes = make (map [* yaml.Node ]string )
54
55
}
55
56
56
- // Check if the current node has been visited before (cycle detection)
57
- if p .visitedNodes [node ] {
58
- return nil , fmt .Errorf ("cycle detected at path: %s" , path .String ())
57
+ // Check for cycle by seeing if the node has already been visited at this path
58
+ if previousPath , found := p .visitedNodes [node ]; found {
59
+ // If the current node has been visited, we have a cycle if the previous path is a prefix
60
+ if strings .HasPrefix (pathStr , previousPath ) {
61
+ return nil , fmt .Errorf ("cycle detected at path: %s" , pathStr )
62
+ }
59
63
}
60
64
61
65
// Mark the current node as visited
62
- p .visitedNodes [node ] = true
66
+ p .visitedNodes [node ] = pathStr
63
67
64
68
// If the node is an alias, We need to process the alias field in order to consider the !override and !reset tags
65
69
if node .Kind == yaml .AliasNode {
0 commit comments