github.com/alex123012/deckhouse-controller-tools@v0.0.0-20230510090815-d594daf1af8c/pkg/schemapatcher/internal/yaml/set.go (about)

     1  /*
     2  Copyright 2019 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package yaml
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"gopkg.in/yaml.v3"
    23  )
    24  
    25  // SetNode sets the given path to the given yaml Node, creating mapping nodes along the way.
    26  func SetNode(root *yaml.Node, val yaml.Node, path ...string) error {
    27  	currNode, path, err := asCloseAsPossible(root, path...)
    28  	if err != nil {
    29  		return err
    30  	}
    31  
    32  	if len(path) > 0 {
    33  		if currNode.Kind != yaml.MappingNode {
    34  			return fmt.Errorf("unexpected non-mapping before path %v", path)
    35  		}
    36  
    37  		for ; len(path) > 0; path = path[1:] {
    38  			keyNode := yaml.Node{Kind: yaml.ScalarNode, Tag: "!!str", Style: yaml.DoubleQuotedStyle, Value: path[0]}
    39  			nextNode := &yaml.Node{Kind: yaml.MappingNode}
    40  			currNode.Content = append(currNode.Content, &keyNode, nextNode)
    41  
    42  			currNode = nextNode
    43  		}
    44  	}
    45  
    46  	*currNode = val
    47  	return nil
    48  }
    49  
    50  // DeleteNode deletes the node at the given path in the given tree of mapping nodes.
    51  // It's a noop if the path doesn't exist.
    52  func DeleteNode(root *yaml.Node, path ...string) error {
    53  	if len(path) == 0 {
    54  		return fmt.Errorf("must specify a path to delete")
    55  	}
    56  	pathToParent, keyToDelete := path[:len(path)-1], path[len(path)-1]
    57  	parentNode, path, err := asCloseAsPossible(root, pathToParent...)
    58  	if err != nil {
    59  		return err
    60  	}
    61  	if len(path) > 0 {
    62  		// no-op, parent node doesn't exist
    63  		return nil
    64  	}
    65  
    66  	if parentNode.Kind != yaml.MappingNode {
    67  		return fmt.Errorf("unexpected non-mapping node")
    68  	}
    69  
    70  	for i := 0; i < len(parentNode.Content)/2; i++ {
    71  		keyNode := parentNode.Content[i*2]
    72  		if keyNode.Value == keyToDelete {
    73  			parentNode.Content = append(parentNode.Content[:i*2], parentNode.Content[i*2+2:]...)
    74  			return nil
    75  		}
    76  	}
    77  
    78  	// no-op, key not found in parent node
    79  	return nil
    80  }