added parent node to model for references.

Signed-off-by: quobix <dave@quobix.com>
This commit is contained in:
quobix
2024-02-07 17:38:52 -05:00
parent 7afb1e9d11
commit 0eea21b150
5 changed files with 50 additions and 17 deletions

View File

@@ -1,10 +1,14 @@
package index
import "strings"
import (
"gopkg.in/yaml.v3"
"strings"
)
// CircularReferenceResult contains a circular reference found when traversing the graph.
type CircularReferenceResult struct {
Journey []*Reference
ParentNode *yaml.Node
Start *Reference
LoopIndex int
LoopPoint *Reference

View File

@@ -57,6 +57,7 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string,
_, jsonPath = utils.ConvertComponentIdIntoFriendlyPathSearch(definitionPath)
}
ref := &Reference{
ParentNode: parent,
FullDefinition: fullDefinitionPath,
Definition: definitionPath,
Node: node.Content[i+1],
@@ -132,6 +133,7 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string,
_, jsonPath = utils.ConvertComponentIdIntoFriendlyPathSearch(definitionPath)
}
ref := &Reference{
ParentNode: parent,
FullDefinition: fullDefinitionPath,
Definition: definitionPath,
Node: prop,
@@ -178,6 +180,7 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string,
}
ref := &Reference{
ParentNode: parent,
FullDefinition: fullDefinitionPath,
Definition: definitionPath,
Node: element,
@@ -337,6 +340,7 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string,
_, p := utils.ConvertComponentIdIntoFriendlyPathSearch(componentName)
ref := &Reference{
ParentNode: parent,
FullDefinition: fullDefinitionPath,
Definition: componentName,
Name: name,
@@ -364,6 +368,7 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string,
if len(node.Content) > 2 {
copiedNode := *node
copied := Reference{
ParentNode: parent,
FullDefinition: fullDefinitionPath,
Definition: ref.Definition,
Name: ref.Name,
@@ -434,10 +439,11 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string,
}
ref := &DescriptionReference{
Content: node.Content[i+1].Value,
Path: jsonPath,
Node: node.Content[i+1],
IsSummary: false,
ParentNode: parent,
Content: node.Content[i+1].Value,
Path: jsonPath,
Node: node.Content[i+1],
IsSummary: false,
}
if !utils.IsNodeMap(ref.Node) {
@@ -455,10 +461,11 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string,
b = node.Content[i+1]
}
ref := &DescriptionReference{
Content: b.Value,
Path: jsonPath,
Node: b,
IsSummary: true,
ParentNode: parent,
Content: b.Value,
Path: jsonPath,
Node: b,
IsSummary: true,
}
index.allSummaries = append(index.allSummaries, ref)
@@ -528,11 +535,11 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string,
if enumKeyValueNode != nil {
ref := &EnumReference{
ParentNode: parent,
Path: jsonPath,
Node: node.Content[i+1],
Type: enumKeyValueNode,
SchemaNode: node,
ParentNode: parent,
}
index.allEnums = append(index.allEnums, ref)

View File

@@ -67,6 +67,16 @@ func FindComponent(root *yaml.Node, componentId, absoluteFilePath string, index
resNode := res[0]
fullDef := fmt.Sprintf("%s%s", absoluteFilePath, componentId)
// extract properties
// check if we have already seen this reference and there is a parent, use it
var parentNode *yaml.Node
if index.allRefs[componentId] != nil {
parentNode = index.allRefs[componentId].ParentNode
}
if index.allRefs[fullDef] != nil {
parentNode = index.allRefs[fullDef].ParentNode
}
ref := &Reference{
FullDefinition: fullDef,
Definition: componentId,
@@ -74,6 +84,7 @@ func FindComponent(root *yaml.Node, componentId, absoluteFilePath string, index
Node: resNode,
Path: friendlySearch,
RemoteLocation: absoluteFilePath,
ParentNode: parentNode,
Index: index,
RequiredRefProperties: extractDefinitionRequiredRefProperties(resNode, map[string][]string{}, fullDef, index),
}
@@ -166,7 +177,13 @@ func (index *SpecIndex) lookupRolodex(uri []string) *Reference {
parsedDocument = parsedDocument.Content[0]
}
var parentNode *yaml.Node
if index.allRefs[absoluteFileLocation] != nil {
parentNode = index.allRefs[absoluteFileLocation].ParentNode
}
foundRef = &Reference{
ParentNode: parentNode,
FullDefinition: absoluteFileLocation,
Definition: fileName,
Name: fileName,

View File

@@ -330,10 +330,11 @@ func (i *IndexingError) Error() string {
// DescriptionReference holds data about a description that was found and where it was found.
type DescriptionReference struct {
Content string
Path string
Node *yaml.Node
IsSummary bool
Content string
Path string
Node *yaml.Node
ParentNode *yaml.Node
IsSummary bool
}
type EnumReference struct {

View File

@@ -201,7 +201,7 @@ func (resolver *Resolver) Resolve() []*ResolvingError {
if !resolver.circChecked {
resolver.resolvingErrors = append(resolver.resolvingErrors, &ResolvingError{
ErrorRef: fmt.Errorf("infinite circular reference detected: %s", circRef.Start.Definition),
Node: circRef.LoopPoint.Node,
Node: circRef.ParentNode,
Path: circRef.GenerateJourneyPath(),
})
}
@@ -221,7 +221,7 @@ func (resolver *Resolver) CheckForCircularReferences() []*ResolvingError {
if !resolver.circChecked {
resolver.resolvingErrors = append(resolver.resolvingErrors, &ResolvingError{
ErrorRef: fmt.Errorf("infinite circular reference detected: %s", circRef.Start.Name),
Node: circRef.LoopPoint.Node,
Node: circRef.ParentNode,
Path: circRef.GenerateJourneyPath(),
CircularReference: circRef,
})
@@ -361,6 +361,7 @@ func (resolver *Resolver) VisitReference(ref *Reference, seen map[string]bool, j
isArray = true
}
circRef = &CircularReferenceResult{
ParentNode: foundDup.ParentNode,
Journey: loop,
Start: foundDup,
LoopIndex: i,
@@ -641,7 +642,7 @@ func (resolver *Resolver) extractRelatives(ref *Reference, node, parent *yaml.No
// if this is a polymorphic link, we want to follow it and see if it becomes circular
if i+1 <= len(node.Content) && utils.IsNodeMap(node.Content[i+1]) { // check for nested items
// check if items is present, to indicate an array
if _, v := utils.FindKeyNodeTop("items", node.Content[i+1].Content); v != nil {
if k, v := utils.FindKeyNodeTop("items", node.Content[i+1].Content); v != nil {
if utils.IsNodeMap(v) {
if d, _, l := utils.IsNodeRefValue(v); d {
@@ -662,6 +663,7 @@ func (resolver *Resolver) extractRelatives(ref *Reference, node, parent *yaml.No
} else {
loop := append(journey, mappedRefs)
circRef := &CircularReferenceResult{
ParentNode: k,
Journey: loop,
Start: mappedRefs,
LoopIndex: i,
@@ -706,6 +708,7 @@ func (resolver *Resolver) extractRelatives(ref *Reference, node, parent *yaml.No
} else {
loop := append(journey, mappedRefs)
circRef := &CircularReferenceResult{
ParentNode: node.Content[i],
Journey: loop,
Start: mappedRefs,
LoopIndex: i,
@@ -751,6 +754,7 @@ func (resolver *Resolver) extractRelatives(ref *Reference, node, parent *yaml.No
loop := append(journey, mappedRefs)
circRef := &CircularReferenceResult{
ParentNode: node.Content[i],
Journey: loop,
Start: mappedRefs,
LoopIndex: i,