From 0eea21b150c8eba65b4a394675f5793581588f3d Mon Sep 17 00:00:00 2001 From: quobix Date: Wed, 7 Feb 2024 17:38:52 -0500 Subject: [PATCH] added parent node to model for references. Signed-off-by: quobix --- index/circular_reference_result.go | 6 +++++- index/extract_refs.go | 25 ++++++++++++++++--------- index/find_component.go | 17 +++++++++++++++++ index/index_model.go | 9 +++++---- index/resolver.go | 10 +++++++--- 5 files changed, 50 insertions(+), 17 deletions(-) diff --git a/index/circular_reference_result.go b/index/circular_reference_result.go index 6538b57..858a26b 100644 --- a/index/circular_reference_result.go +++ b/index/circular_reference_result.go @@ -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 diff --git a/index/extract_refs.go b/index/extract_refs.go index aa0e20d..8cbffa9 100644 --- a/index/extract_refs.go +++ b/index/extract_refs.go @@ -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) diff --git a/index/find_component.go b/index/find_component.go index 472cb4f..2a153a5 100644 --- a/index/find_component.go +++ b/index/find_component.go @@ -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, diff --git a/index/index_model.go b/index/index_model.go index 9fea661..f0f2166 100644 --- a/index/index_model.go +++ b/index/index_model.go @@ -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 { diff --git a/index/resolver.go b/index/resolver.go index 272659d..eeccae3 100644 --- a/index/resolver.go +++ b/index/resolver.go @@ -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,