All the minor Changes

This commit is contained in:
suhasgumma
2022-12-16 12:56:40 +05:30
parent fa5e7fef23
commit 76d2154152
4 changed files with 46 additions and 72 deletions
-13
View File
@@ -56,16 +56,3 @@ type linesToRemove struct {
startLine int
endLine int
}
type fileFixInfo struct {
contentToAdd []contentToAdd
linesToRemove []linesToRemove
}
func (fileFixInfo *fileFixInfo) addContent(content contentToAdd) {
fileFixInfo.contentToAdd = append(fileFixInfo.contentToAdd, content)
}
func (fileFixInfo *fileFixInfo) addLinesToRemove(linesToRemove linesToRemove) {
fileFixInfo.linesToRemove = append(fileFixInfo.linesToRemove, linesToRemove)
}
+2 -2
View File
@@ -234,8 +234,8 @@ func (h *FixHandler) getFilePathAndIndex(filePathWithIndex string) (filePath str
func (h *FixHandler) ApplyFix(yamlString, yamlExpression string) (fixedYamlString string, err error) {
yamlLines := strings.Split(yamlString, "\n")
originalRootNodes := constructDecodedYaml(yamlString)
fixedRootNodes, err := constructFixedYamlNodes(yamlString, yamlExpression)
originalRootNodes := decodeDocumentRoots(yamlString)
fixedRootNodes, err := getFixedNodes(yamlString, yamlExpression)
if err != nil {
return "", err
+21 -27
View File
@@ -13,7 +13,8 @@ import (
"gopkg.in/yaml.v3"
)
func constructDecodedYaml(yamlString string) *[]yaml.Node {
// decodeDocumentRoots decodes all YAML documents stored in a given `filepath` and returns a slice of their root nodes
func decodeDocumentRoots(yamlString string) *[]yaml.Node {
fileReader := strings.NewReader(yamlString)
dec := yaml.NewDecoder(fileReader)
@@ -35,7 +36,7 @@ func constructDecodedYaml(yamlString string) *[]yaml.Node {
return &nodes
}
func constructFixedYamlNodes(yamlString, yamlExpression string) (*[]yaml.Node, error) {
func getFixedNodes(yamlString, yamlExpression string) (*[]yaml.Node, error) {
preferences := yqlib.ConfiguredYamlPreferences
preferences.EvaluateTogether = true
decoder := yqlib.NewYamlDecoder(preferences)
@@ -67,29 +68,22 @@ func constructFixedYamlNodes(yamlString, yamlExpression string) (*[]yaml.Node, e
return &fixedNodes, nil
}
func constructDFSOrder(node *yaml.Node) *[]nodeInfo {
func flattenWithDFS(node *yaml.Node) *[]nodeInfo {
dfsOrder := make([]nodeInfo, 0)
constructDFSOrderHelper(node, nil, &dfsOrder, 0)
flattenWithDFSHelper(node, nil, &dfsOrder, 0)
return &dfsOrder
}
func matchNodes(nodeOne, nodeTwo *yaml.Node) int {
func flattenWithDFSHelper(node *yaml.Node, parent *yaml.Node, dfsOrder *[]nodeInfo, index int) {
dfsNode := nodeInfo{
node: node,
parent: parent,
index: index,
}
*dfsOrder = append(*dfsOrder, dfsNode)
isNewNode := nodeTwo.Line == 0 && nodeTwo.Column == 0
sameLines := nodeOne.Line == nodeTwo.Line
sameColumns := nodeOne.Column == nodeTwo.Column
isSameNode := isSameNode(nodeOne, nodeTwo)
switch {
case isSameNode:
return int(sameNodes)
case isNewNode:
return int(insertedNode)
case sameLines && sameColumns:
return int(replacedNode)
default:
return int(removedNode)
for idx, child := range node.Content {
flattenWithDFSHelper(child, node, dfsOrder, idx)
}
}
@@ -97,9 +91,9 @@ func getFixInfo(originalRootNodes, fixedRootNodes *[]yaml.Node) (*[]contentToAdd
contentToAdd := make([]contentToAdd, 0)
linesToRemove := make([]linesToRemove, 0)
for idx, _ := range *fixedRootNodes {
originalList := constructDFSOrder(&(*originalRootNodes)[idx])
fixedList := constructDFSOrder(&(*fixedRootNodes)[idx])
for idx := 0; idx < len(*fixedRootNodes); idx++ {
originalList := flattenWithDFS(&(*originalRootNodes)[idx])
fixedList := flattenWithDFS(&(*fixedRootNodes)[idx])
nodeContentToAdd, nodeLinesToRemove := getFixInfoHelper(originalList, fixedList)
contentToAdd = append(contentToAdd, *nodeContentToAdd...)
linesToRemove = append(linesToRemove, *nodeLinesToRemove...)
@@ -136,17 +130,17 @@ func getFixInfoHelper(originalList, fixedList *[]nodeInfo) (*[]contentToAdd, *[]
fixInfoMetadata.fixedListTracker = fixedListTracker
switch matchNodeResult {
case int(sameNodes):
case sameNodes:
originalListTracker += 1
fixedListTracker += 1
case int(removedNode):
case removedNode:
originalListTracker, fixedListTracker = addLinesToRemove(fixInfoMetadata)
case int(insertedNode):
case insertedNode:
originalListTracker, fixedListTracker = addLinesToInsert(fixInfoMetadata)
case int(replacedNode):
case replacedNode:
originalListTracker, fixedListTracker = updateLinesToReplace(fixInfoMetadata)
}
}
+23 -30
View File
@@ -17,13 +17,35 @@ import (
"gopkg.in/yaml.v3"
)
type NodeRelation int
const (
sameNodes = iota
sameNodes NodeRelation = iota
insertedNode
removedNode
replacedNode
)
func matchNodes(nodeOne, nodeTwo *yaml.Node) NodeRelation {
isNewNode := nodeTwo.Line == 0 && nodeTwo.Column == 0
sameLines := nodeOne.Line == nodeTwo.Line
sameColumns := nodeOne.Column == nodeTwo.Column
isSameNode := isSameNode(nodeOne, nodeTwo)
switch {
case isSameNode:
return sameNodes
case isNewNode:
return insertedNode
case sameLines && sameColumns:
return replacedNode
default:
return removedNode
}
}
func adjustContentLines(contentToAdd *[]contentToAdd, linesSlice *[]string) {
for contentIdx, content := range *contentToAdd {
line := content.line
@@ -57,35 +79,6 @@ func adjustFixedListLines(originalList, fixedList *[]nodeInfo) {
}
func constructDFSOrderHelper(node *yaml.Node, parent *yaml.Node, dfsOrder *[]nodeInfo, index int) {
dfsNode := nodeInfo{
node: node,
parent: parent,
index: index,
}
*dfsOrder = append(*dfsOrder, dfsNode)
for idx, child := range node.Content {
constructDFSOrderHelper(child, node, dfsOrder, idx)
}
}
func constructNewReader(filename string) (io.Reader, error) {
var reader *bufio.Reader
if filename == "-" {
reader = bufio.NewReader(os.Stdin)
} else {
// ignore CWE-22 gosec issue - that's more targeted for http based apps that run in a public directory,
// and ensuring that it's not possible to give a path to a file outside thar directory.
file, err := os.Open(filename) // #nosec
if err != nil {
return nil, err
}
reader = bufio.NewReader(file)
}
return reader, nil
}
func enocodeIntoYaml(parentNode *yaml.Node, nodeList *[]nodeInfo, tracker int) (string, error) {
content := make([]*yaml.Node, 0)
currentNode := (*nodeList)[tracker].node