Added more support for YAML merge nodes, anchors and aliases

And added deeper support for Aliases. Also added in local file handling through renamed `FSHandler` configuration property for the index.

Also re-ran `go fmt`

Signed-off-by: Dave Shanley <dave@quobix.com>
This commit is contained in:
Dave Shanley
2023-07-15 10:18:49 -04:00
committed by quobix
parent 3b7cbacc44
commit 25d8de9b0e
48 changed files with 925 additions and 531 deletions

View File

@@ -6,14 +6,13 @@ package low
import (
"crypto/sha256"
"fmt"
"reflect"
"strconv"
"strings"
"github.com/pb33f/libopenapi/index"
"github.com/pb33f/libopenapi/utils"
"github.com/vmware-labs/yaml-jsonpath/pkg/yamlpath"
"gopkg.in/yaml.v3"
"reflect"
"strconv"
"strings"
)
// FindItemInMap accepts a string key and a collection of KeyReference[string] and ValueReference[T]. Every
@@ -86,14 +85,14 @@ func LocateRefNode(root *yaml.Node, idx *index.SpecIndex) (*yaml.Node, error) {
found[rv].Node.Column)
}
}
return found[rv].Node, nil
return utils.NodeAlias(found[rv].Node), nil
}
}
// perform a search for the reference in the index
foundRefs := idx.SearchIndexForReference(rv)
if len(foundRefs) > 0 {
return foundRefs[0].Node, nil
return utils.NodeAlias(foundRefs[0].Node), nil
}
// let's try something else to find our references.
@@ -106,7 +105,7 @@ func LocateRefNode(root *yaml.Node, idx *index.SpecIndex) (*yaml.Node, error) {
nodes, fErr := path.Find(idx.GetRootNode())
if fErr == nil {
if len(nodes) > 0 {
return nodes[0], nil
return utils.NodeAlias(nodes[0]), nil
}
}
}
@@ -123,6 +122,7 @@ func ExtractObjectRaw[T Buildable[N], N any](root *yaml.Node, idx *index.SpecInd
var circError error
var isReference bool
var referenceValue string
root = utils.NodeAlias(root)
if h, _, rv := utils.IsNodeRefValue(root); h {
ref, err := LocateRefNode(root, idx)
if ref != nil {
@@ -167,6 +167,7 @@ func ExtractObject[T Buildable[N], N any](label string, root *yaml.Node, idx *in
var circError error
var isReference bool
var referenceValue string
root = utils.NodeAlias(root)
if rf, rl, refVal := utils.IsNodeRefValue(root); rf {
ref, err := LocateRefNode(root, idx)
if ref != nil {
@@ -251,6 +252,7 @@ func ExtractArray[T Buildable[N], N any](label string, root *yaml.Node, idx *ind
) {
var ln, vn *yaml.Node
var circError error
root = utils.NodeAlias(root)
if rf, rl, _ := utils.IsNodeRefValue(root); rf {
ref, err := LocateRefNode(root, idx)
if ref != nil {
@@ -370,7 +372,10 @@ func ExtractMapNoLookupExtensions[PT Buildable[N], N any](
if utils.IsNodeMap(root) {
var currentKey *yaml.Node
skip := false
for i, node := range root.Content {
rlen := len(root.Content)
for i := 0; i < rlen; i++ {
node := root.Content[i]
if !includeExtensions {
if strings.HasPrefix(strings.ToLower(node.Value), "x-") {
skip = true
@@ -386,6 +391,14 @@ func ExtractMapNoLookupExtensions[PT Buildable[N], N any](
continue
}
if currentKey.Tag == "!!merge" && currentKey.Value == "<<" {
root.Content = append(root.Content, utils.NodeAlias(node).Content...)
rlen = len(root.Content)
currentKey = nil
continue
}
node = utils.NodeAlias(node)
var isReference bool
var referenceValue string
// if value is a reference, we have to look it up in the index!
@@ -470,6 +483,7 @@ func ExtractMapExtensions[PT Buildable[N], N any](
var referenceValue string
var labelNode, valueNode *yaml.Node
var circError error
root = utils.NodeAlias(root)
if rf, rl, rv := utils.IsNodeRefValue(root); rf {
// locate reference in index.
ref, err := LocateRefNode(root, idx)
@@ -515,6 +529,7 @@ func ExtractMapExtensions[PT Buildable[N], N any](
buildMap := func(label *yaml.Node, value *yaml.Node, c chan mappingResult[PT], ec chan<- error, ref string) {
var n PT = new(N)
value = utils.NodeAlias(value)
_ = BuildModel(value, n)
err := n.Build(value, idx)
if err != nil {
@@ -544,6 +559,7 @@ func ExtractMapExtensions[PT Buildable[N], N any](
totalKeys := 0
for i, en := range valueNode.Content {
en = utils.NodeAlias(en)
referenceValue = ""
if i%2 == 0 {
currentLabelNode = en
@@ -620,6 +636,7 @@ func ExtractMap[PT Buildable[N], N any](
//
// int64, float64, bool, string
func ExtractExtensions(root *yaml.Node) map[KeyReference[string]]ValueReference[any] {
root = utils.NodeAlias(root)
extensions := utils.FindExtensionNodes(root.Content)
extensionMap := make(map[KeyReference[string]]ValueReference[any])
for _, ext := range extensions {