Skip to content

Commit

Permalink
fix reset.go
Browse files Browse the repository at this point in the history
Signed-off-by: Suleiman Dibirov <idsulik@gmail.com>
  • Loading branch information
idsulik authored and glours committed Oct 28, 2024
1 parent 88ca71b commit 222d93c
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions loader/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
type ResetProcessor struct {
target interface{}
paths []tree.Path
visitedNodes map[*yaml.Node]bool
visitedNodes map[*yaml.Node]string
}

// UnmarshalYAML implement yaml.Unmarshaler
Expand All @@ -43,23 +43,27 @@ func (p *ResetProcessor) UnmarshalYAML(value *yaml.Node) error {

// resolveReset detects `!reset` tag being set on yaml nodes and record position in the yaml tree
func (p *ResetProcessor) resolveReset(node *yaml.Node, path tree.Path) (*yaml.Node, error) {
pathStr := path.String()
// If the path contains "<<", removing the "<<" element and merging the path
if strings.Contains(path.String(), ".<<") {
path = tree.NewPath(strings.Replace(path.String(), ".<<", "", 1))
if strings.Contains(pathStr, ".<<") {
path = tree.NewPath(strings.Replace(pathStr, ".<<", "", 1))
}

// Check for cycle
if p.visitedNodes == nil {
p.visitedNodes = make(map[*yaml.Node]bool)
p.visitedNodes = make(map[*yaml.Node]string)
}

// Check if the current node has been visited before (cycle detection)
if p.visitedNodes[node] {
return nil, fmt.Errorf("cycle detected at path: %s", path.String())
// Check for cycle by seeing if the node has already been visited at this path
if previousPath, found := p.visitedNodes[node]; found {
// If the current node has been visited, we have a cycle if the previous path is a prefix
if strings.HasPrefix(pathStr, previousPath) {
return nil, fmt.Errorf("cycle detected at path: %s", pathStr)
}
}

// Mark the current node as visited
p.visitedNodes[node] = true
p.visitedNodes[node] = pathStr

// If the node is an alias, We need to process the alias field in order to consider the !override and !reset tags
if node.Kind == yaml.AliasNode {
Expand Down

0 comments on commit 222d93c

Please sign in to comment.