mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-05-19 01:47:43 +08:00
157 lines
4.0 KiB
Go
157 lines
4.0 KiB
Go
/*
|
|
Copyright 2020 The Compose Specification Authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package loader
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/compose-spec/compose-go/v2/tree"
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
type ResetProcessor struct {
|
|
target interface{}
|
|
paths []tree.Path
|
|
visitedNodes map[*yaml.Node]string
|
|
}
|
|
|
|
// UnmarshalYAML implement yaml.Unmarshaler
|
|
func (p *ResetProcessor) UnmarshalYAML(value *yaml.Node) error {
|
|
resolved, err := p.resolveReset(value, tree.NewPath())
|
|
p.visitedNodes = nil
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return resolved.Decode(p.target)
|
|
}
|
|
|
|
// 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(pathStr, ".<<") {
|
|
path = tree.NewPath(strings.Replace(pathStr, ".<<", "", 1))
|
|
}
|
|
|
|
// Check for cycle
|
|
if p.visitedNodes == nil {
|
|
p.visitedNodes = make(map[*yaml.Node]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] = 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 {
|
|
return p.resolveReset(node.Alias, path)
|
|
}
|
|
|
|
if node.Tag == "!reset" {
|
|
p.paths = append(p.paths, path)
|
|
return nil, nil
|
|
}
|
|
if node.Tag == "!override" {
|
|
p.paths = append(p.paths, path)
|
|
return node, nil
|
|
}
|
|
switch node.Kind {
|
|
case yaml.SequenceNode:
|
|
var nodes []*yaml.Node
|
|
for idx, v := range node.Content {
|
|
next := path.Next(strconv.Itoa(idx))
|
|
resolved, err := p.resolveReset(v, next)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if resolved != nil {
|
|
nodes = append(nodes, resolved)
|
|
}
|
|
}
|
|
node.Content = nodes
|
|
case yaml.MappingNode:
|
|
var key string
|
|
var nodes []*yaml.Node
|
|
for idx, v := range node.Content {
|
|
if idx%2 == 0 {
|
|
key = v.Value
|
|
} else {
|
|
resolved, err := p.resolveReset(v, path.Next(key))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if resolved != nil {
|
|
nodes = append(nodes, node.Content[idx-1], resolved)
|
|
}
|
|
}
|
|
}
|
|
node.Content = nodes
|
|
}
|
|
return node, nil
|
|
}
|
|
|
|
// Apply finds the go attributes matching recorded paths and reset them to zero value
|
|
func (p *ResetProcessor) Apply(target any) error {
|
|
return p.applyNullOverrides(target, tree.NewPath())
|
|
}
|
|
|
|
// applyNullOverrides set val to Zero if it matches any of the recorded paths
|
|
func (p *ResetProcessor) applyNullOverrides(target any, path tree.Path) error {
|
|
switch v := target.(type) {
|
|
case map[string]any:
|
|
KEYS:
|
|
for k, e := range v {
|
|
next := path.Next(k)
|
|
for _, pattern := range p.paths {
|
|
if next.Matches(pattern) {
|
|
delete(v, k)
|
|
continue KEYS
|
|
}
|
|
}
|
|
err := p.applyNullOverrides(e, next)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
case []any:
|
|
ITER:
|
|
for i, e := range v {
|
|
next := path.Next(fmt.Sprintf("[%d]", i))
|
|
for _, pattern := range p.paths {
|
|
if next.Matches(pattern) {
|
|
continue ITER
|
|
// TODO(ndeloof) support removal from sequence
|
|
}
|
|
}
|
|
err := p.applyNullOverrides(e, next)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|