diff --git a/core/pkg/fixhandler/datastructures.go b/core/pkg/fixhandler/datastructures.go index 3d4264e2..d444ac20 100644 --- a/core/pkg/fixhandler/datastructures.go +++ b/core/pkg/fixhandler/datastructures.go @@ -23,13 +23,13 @@ type ResourceFixInfo struct { DocumentIndex int } -type FileFixInfo struct { - ContentToAdd []ContentToAdd - LinesToRemove []LinesToRemove +type fileFixInfo struct { + contentToAdd []contentToAdd + linesToRemove []linesToRemove } // NodeInfo holds extra information about the node -type NodeInfo struct { +type nodeInfo struct { node *yaml.Node parent *yaml.Node @@ -39,25 +39,25 @@ type NodeInfo struct { // FixInfoMetadata holds the arguments "getFixInfo" function needs to pass to the // functions it uses -type FixInfoMetadata struct { - originalList *[]NodeInfo - fixedList *[]NodeInfo +type fixInfoMetadata struct { + originalList *[]nodeInfo + fixedList *[]nodeInfo originalListTracker int fixedListTracker int - contentToAdd *[]ContentToAdd - linesToRemove *[]LinesToRemove + contentToAdd *[]contentToAdd + linesToRemove *[]linesToRemove } // ContentToAdd holds the information about where to insert the new changes in the existing yaml file -type ContentToAdd struct { +type contentToAdd struct { // Line where the fix should be applied to - Line int + line int // Content is a string representation of the YAML node that describes a suggested fix - Content string + content string } // LinesToRemove holds the line numbers to remove from the existing yaml file -type LinesToRemove struct { - StartLine int - EndLine int +type linesToRemove struct { + startLine int + endLine int } diff --git a/core/pkg/fixhandler/fixhandler.go b/core/pkg/fixhandler/fixhandler.go index 5e748e7e..5fbc41ec 100644 --- a/core/pkg/fixhandler/fixhandler.go +++ b/core/pkg/fixhandler/fixhandler.go @@ -187,17 +187,17 @@ func (h *FixHandler) ApplyChanges(resourcesToFix []ResourceFixInfo) (int, []erro updatedFiles := make(map[string]bool) errors := make([]error, 0) // Map with key as filepath - filePathFixInfo := make(map[string]*FileFixInfo) + filePathFixInfo := make(map[string]*fileFixInfo) for _, resourceToFix := range resourcesToFix { singleExpression := reduceYamlExpressions(&resourceToFix) resourceFilePath := resourceToFix.FilePath if _, pathExistsInMap := filePathFixInfo[resourceFilePath]; !pathExistsInMap { - contentToAdd := make([]ContentToAdd, 0) - linesToRemove := make([]LinesToRemove, 0) - filePathFixInfo[resourceFilePath] = &FileFixInfo{ - ContentToAdd: contentToAdd, - LinesToRemove: linesToRemove, + contentToAdd := make([]contentToAdd, 0) + linesToRemove := make([]linesToRemove, 0) + filePathFixInfo[resourceFilePath] = &fileFixInfo{ + contentToAdd: contentToAdd, + linesToRemove: linesToRemove, } } @@ -235,7 +235,7 @@ func (h *FixHandler) getFilePathAndIndex(filePathWithIndex string) (filePath str } } -func (h *FixHandler) updateFileFixInfo(filePath string, yamlExpression string, documentIdx int, fileFixInfo *FileFixInfo) error { +func (h *FixHandler) updateFileFixInfo(filePath string, yamlExpression string, documentIdx int, fileFixInfo *fileFixInfo) error { originalYamlNode := (*constructDecodedYaml(filePath))[documentIdx] fixedYamlNodes, err := constructFixedYamlNodes(filePath, yamlExpression) if err != nil { @@ -249,16 +249,16 @@ func (h *FixHandler) updateFileFixInfo(filePath string, yamlExpression string, d contentToAdd, linesToRemove := getFixInfo(originalList, fixedList) - fileFixInfo.ContentToAdd = append(fileFixInfo.ContentToAdd, *contentToAdd...) - fileFixInfo.LinesToRemove = append(fileFixInfo.LinesToRemove, *linesToRemove...) + fileFixInfo.contentToAdd = append(fileFixInfo.contentToAdd, *contentToAdd...) + fileFixInfo.linesToRemove = append(fileFixInfo.linesToRemove, *linesToRemove...) return nil } -func (h *FixHandler) applyFixToFiles(filePathFixInfo map[string]*FileFixInfo) error { +func (h *FixHandler) applyFixToFiles(filePathFixInfo map[string]*fileFixInfo) error { for filepath, fixInfo := range filePathFixInfo { - err := applyFixesToFile(filepath, &fixInfo.ContentToAdd, &fixInfo.LinesToRemove) + err := applyFixesToFile(filepath, &fixInfo.contentToAdd, &fixInfo.linesToRemove) if err != nil { return err } diff --git a/core/pkg/fixhandler/fixhandler_test.go b/core/pkg/fixhandler/fixhandler_test.go index 17f27039..59552aea 100644 --- a/core/pkg/fixhandler/fixhandler_test.go +++ b/core/pkg/fixhandler/fixhandler_test.go @@ -66,11 +66,11 @@ func testDirectoryApplyFixHelper(t *testing.T, yamlExpressions *[][]string, dire // make changes to temp file h, _ := NewFixHandlerMock() - filePathFixInfo := make(map[string]*FileFixInfo) + filePathFixInfo := make(map[string]*fileFixInfo) filePath := tempFile.Name() - filePathFixInfo[filePath] = &FileFixInfo{ - ContentToAdd: make([]ContentToAdd, 0), - LinesToRemove: make([]LinesToRemove, 0), + filePathFixInfo[filePath] = &fileFixInfo{ + contentToAdd: make([]contentToAdd, 0), + linesToRemove: make([]linesToRemove, 0), } fixInfo := filePathFixInfo[filePath] diff --git a/core/pkg/fixhandler/yamlhandler.go b/core/pkg/fixhandler/yamlhandler.go index 316b8b14..39e85832 100644 --- a/core/pkg/fixhandler/yamlhandler.go +++ b/core/pkg/fixhandler/yamlhandler.go @@ -77,8 +77,8 @@ func constructFixedYamlNodes(filePath, yamlExpression string) (*[]yaml.Node, err return &fixedNodes, nil } -func constructDFSOrder(node *yaml.Node) *[]NodeInfo { - dfsOrder := make([]NodeInfo, 0) +func constructDFSOrder(node *yaml.Node) *[]nodeInfo { + dfsOrder := make([]nodeInfo, 0) constructDFSOrderHelper(node, nil, &dfsOrder, 0) return &dfsOrder } @@ -103,19 +103,19 @@ func matchNodes(nodeOne, nodeTwo *yaml.Node) int { } } -func getFixInfo(originalList, fixedList *[]NodeInfo) (*[]ContentToAdd, *[]LinesToRemove) { +func getFixInfo(originalList, fixedList *[]nodeInfo) (*[]contentToAdd, *[]linesToRemove) { // While obtaining fixedYamlNode, comments and empty lines at the top are ignored. // This causes a difference in Line numbers across the tree structure. In order to // counter this, line numbers are adjusted in fixed list. adjustFixedListLines(originalList, fixedList) - contentToAdd := make([]ContentToAdd, 0) - linesToRemove := make([]LinesToRemove, 0) + contentToAdd := make([]contentToAdd, 0) + linesToRemove := make([]linesToRemove, 0) originalListTracker, fixedListTracker := 0, 0 - fixInfoMetadata := &FixInfoMetadata{ + fixInfoMetadata := &fixInfoMetadata{ originalList: originalList, fixedList: fixedList, originalListTracker: originalListTracker, @@ -165,7 +165,7 @@ func getFixInfo(originalList, fixedList *[]NodeInfo) (*[]ContentToAdd, *[]LinesT } // Adds the lines to remove and returns the updated originalListTracker -func addLinesToRemove(fixInfoMetadata *FixInfoMetadata) (int, int) { +func addLinesToRemove(fixInfoMetadata *fixInfoMetadata) (int, int) { isOneLine, line := isOneLineSequenceNode(fixInfoMetadata.originalList, fixInfoMetadata.originalListTracker) if isOneLine { @@ -177,16 +177,16 @@ func addLinesToRemove(fixInfoMetadata *FixInfoMetadata) (int, int) { currentDFSNode := (*fixInfoMetadata.originalList)[fixInfoMetadata.originalListTracker] newOriginalListTracker := updateTracker(fixInfoMetadata.originalList, fixInfoMetadata.originalListTracker) - *fixInfoMetadata.linesToRemove = append(*fixInfoMetadata.linesToRemove, LinesToRemove{ - StartLine: currentDFSNode.node.Line, - EndLine: getNodeLine(fixInfoMetadata.originalList, newOriginalListTracker), + *fixInfoMetadata.linesToRemove = append(*fixInfoMetadata.linesToRemove, linesToRemove{ + startLine: currentDFSNode.node.Line, + endLine: getNodeLine(fixInfoMetadata.originalList, newOriginalListTracker), }) return newOriginalListTracker, fixInfoMetadata.fixedListTracker } // Adds the lines to insert and returns the updated fixedListTracker -func addLinesToInsert(fixInfoMetadata *FixInfoMetadata) (int, int) { +func addLinesToInsert(fixInfoMetadata *fixInfoMetadata) (int, int) { isOneLine, line := isOneLineSequenceNode(fixInfoMetadata.fixedList, fixInfoMetadata.fixedListTracker) @@ -201,16 +201,16 @@ func addLinesToInsert(fixInfoMetadata *FixInfoMetadata) (int, int) { newFixedTracker := updateTracker(fixInfoMetadata.fixedList, fixInfoMetadata.fixedListTracker) - *fixInfoMetadata.contentToAdd = append(*fixInfoMetadata.contentToAdd, ContentToAdd{ - Line: lineToInsert, - Content: contentToInsert, + *fixInfoMetadata.contentToAdd = append(*fixInfoMetadata.contentToAdd, contentToAdd{ + line: lineToInsert, + content: contentToInsert, }) return fixInfoMetadata.originalListTracker, newFixedTracker } // Adds the lines to remove and insert and updates the fixedListTracker and originalListTracker -func updateLinesToReplace(fixInfoMetadata *FixInfoMetadata) (int, int) { +func updateLinesToReplace(fixInfoMetadata *fixInfoMetadata) (int, int) { isOneLine, line := isOneLineSequenceNode(fixInfoMetadata.fixedList, fixInfoMetadata.fixedListTracker) @@ -232,7 +232,7 @@ func updateLinesToReplace(fixInfoMetadata *FixInfoMetadata) (int, int) { return updatedOriginalTracker, updatedFixedTracker } -func applyFixesToFile(filePath string, contentToAdd *[]ContentToAdd, linesToRemove *[]LinesToRemove) error { +func applyFixesToFile(filePath string, contentToAdd *[]contentToAdd, linesToRemove *[]linesToRemove) error { // Read contents of the file line by line and store in a list linesSlice, err := getLinesSlice(filePath) @@ -270,7 +270,7 @@ func applyFixesToFile(filePath string, contentToAdd *[]ContentToAdd, linesToRemo adjustContentLines(contentToAdd, &linesSlice) for lineToAddIdx < len(*contentToAdd) { - for lineIdx <= (*contentToAdd)[lineToAddIdx].Line { + for lineIdx <= (*contentToAdd)[lineToAddIdx].line { // Check if the current line is not removed if linesSlice[lineIdx-1] != "*" { _, err := writer.WriteString(linesSlice[lineIdx-1] + "\n") @@ -281,7 +281,7 @@ func applyFixesToFile(filePath string, contentToAdd *[]ContentToAdd, linesToRemo lineIdx += 1 } - content := (*contentToAdd)[lineToAddIdx].Content + content := (*contentToAdd)[lineToAddIdx].content writer.WriteString(content) lineToAddIdx += 1 diff --git a/core/pkg/fixhandler/yamlhelper.go b/core/pkg/fixhandler/yamlhelper.go index 3f7cdfbd..e817e503 100644 --- a/core/pkg/fixhandler/yamlhelper.go +++ b/core/pkg/fixhandler/yamlhelper.go @@ -24,14 +24,14 @@ const ( replacedNode ) -func adjustContentLines(contentToAdd *[]ContentToAdd, linesSlice *[]string) { +func adjustContentLines(contentToAdd *[]contentToAdd, linesSlice *[]string) { for contentIdx, content := range *contentToAdd { - line := content.Line + line := content.line // Adjust line numbers such that there are no "empty lines or comment lines of next nodes" before them for idx := line - 1; idx >= 0; idx-- { if isEmptyLineOrComment((*linesSlice)[idx]) { - (*contentToAdd)[contentIdx].Line -= 1 + (*contentToAdd)[contentIdx].line -= 1 } else { break } @@ -39,7 +39,7 @@ func adjustContentLines(contentToAdd *[]ContentToAdd, linesSlice *[]string) { } } -func adjustFixedListLines(originalList, fixedList *[]NodeInfo) { +func adjustFixedListLines(originalList, fixedList *[]nodeInfo) { differenceAtTop := (*originalList)[0].node.Line - (*fixedList)[0].node.Line if differenceAtTop <= 0 { @@ -47,7 +47,7 @@ func adjustFixedListLines(originalList, fixedList *[]NodeInfo) { } for _, node := range *fixedList { - // Line numbers should not be changed for new nodes. + // line numbers should not be changed for new nodes. if node.node.Line != 0 { node.node.Line += differenceAtTop } @@ -57,8 +57,8 @@ func adjustFixedListLines(originalList, fixedList *[]NodeInfo) { } -func constructDFSOrderHelper(node *yaml.Node, parent *yaml.Node, dfsOrder *[]NodeInfo, index int) { - dfsNode := NodeInfo{ +func constructDFSOrderHelper(node *yaml.Node, parent *yaml.Node, dfsOrder *[]nodeInfo, index int) { + dfsNode := nodeInfo{ node: node, parent: parent, index: index, @@ -86,7 +86,7 @@ func constructNewReader(filename string) (io.Reader, error) { return reader, nil } -func enocodeIntoYaml(parentNode *yaml.Node, nodeList *[]NodeInfo, tracker int) (string, error) { +func enocodeIntoYaml(parentNode *yaml.Node, nodeList *[]nodeInfo, tracker int) (string, error) { content := make([]*yaml.Node, 0) currentNode := (*nodeList)[tracker].node content = append(content, currentNode) @@ -119,7 +119,7 @@ func enocodeIntoYaml(parentNode *yaml.Node, nodeList *[]NodeInfo, tracker int) ( return fmt.Sprintf(`%v`, buf.String()), nil } -func constructContent(parentNode *yaml.Node, nodeList *[]NodeInfo, tracker int) string { +func constructContent(parentNode *yaml.Node, nodeList *[]nodeInfo, tracker int) string { content, err := enocodeIntoYaml(parentNode, nodeList, tracker) if err != nil { logger.L().Fatal("Cannot Encode into YAML") @@ -168,7 +168,7 @@ func getLinesSlice(filePath string) ([]string, error) { return lineSlice, err } -func getLineToInsert(fixInfoMetadata *FixInfoMetadata) int { +func getLineToInsert(fixInfoMetadata *fixInfoMetadata) int { var lineToInsert int // Check if lineToInsert is last line if fixInfoMetadata.originalListTracker < 0 { @@ -181,18 +181,18 @@ func getLineToInsert(fixInfoMetadata *FixInfoMetadata) int { return lineToInsert } -func assignLastLine(contentsToAdd *[]ContentToAdd, linesToRemove *[]LinesToRemove, linesSlice *[]string) { +func assignLastLine(contentsToAdd *[]contentToAdd, linesToRemove *[]linesToRemove, linesSlice *[]string) { for idx, contentToAdd := range *contentsToAdd { - if contentToAdd.Line < 0 { - currentLine := int(math.Abs(float64(contentToAdd.Line))) - (*contentsToAdd)[idx].Line, _ = getLastLineOfResource(linesSlice, currentLine) + if contentToAdd.line < 0 { + currentLine := int(math.Abs(float64(contentToAdd.line))) + (*contentsToAdd)[idx].line, _ = getLastLineOfResource(linesSlice, currentLine) } } for idx, lineToRemove := range *linesToRemove { - if lineToRemove.EndLine < 0 { - endLine, _ := getLastLineOfResource(linesSlice, lineToRemove.StartLine) - (*linesToRemove)[idx].EndLine = endLine + if lineToRemove.endLine < 0 { + endLine, _ := getLastLineOfResource(linesSlice, lineToRemove.startLine) + (*linesToRemove)[idx].endLine = endLine } } } @@ -231,7 +231,7 @@ func getLastLineOfResource(linesSlice *[]string, currentLine int) (int, error) { return 0, fmt.Errorf("Provided line is greater than the length of YAML file") } -func getNodeLine(nodeList *[]NodeInfo, tracker int) int { +func getNodeLine(nodeList *[]nodeInfo, tracker int) int { if tracker < len(*nodeList) { return (*nodeList)[tracker].node.Line } else { @@ -240,7 +240,7 @@ func getNodeLine(nodeList *[]NodeInfo, tracker int) int { } // Checks if the node is value node in "key-value" pairs of mapping node -func isValueNodeinMapping(node *NodeInfo) bool { +func isValueNodeinMapping(node *nodeInfo) bool { if node.parent.Kind == yaml.MappingNode && node.index%2 != 0 { return true } @@ -248,13 +248,13 @@ func isValueNodeinMapping(node *NodeInfo) bool { } // Checks if the node is part of single line sequence node and returns the line -func isOneLineSequenceNode(list *[]NodeInfo, currentTracker int) (bool, int) { +func isOneLineSequenceNode(list *[]nodeInfo, currentTracker int) (bool, int) { parentNode := (*list)[currentTracker].parent if parentNode.Kind != yaml.SequenceNode { return false, -1 } - var currentNode, prevNode NodeInfo + var currentNode, prevNode nodeInfo currentTracker -= 1 for (*list)[currentTracker].node != parentNode { @@ -346,7 +346,7 @@ func safelyCloseFile(file *os.File) { // Remove the entire line and replace it with the sequence node in fixed info. This way, // the original formatting is lost. -func replaceSingleLineSequence(fixInfoMetadata *FixInfoMetadata, line int) (int, int) { +func replaceSingleLineSequence(fixInfoMetadata *fixInfoMetadata, line int) (int, int) { originalListTracker := getFirstNodeInLine(fixInfoMetadata.originalList, line) fixedListTracker := getFirstNodeInLine(fixInfoMetadata.fixedList, line) @@ -354,15 +354,15 @@ func replaceSingleLineSequence(fixInfoMetadata *FixInfoMetadata, line int) (int, contentToInsert := constructContent(currentDFSNode.parent, fixInfoMetadata.fixedList, fixedListTracker) // Remove the Single line - *fixInfoMetadata.linesToRemove = append(*fixInfoMetadata.linesToRemove, LinesToRemove{ - StartLine: line, - EndLine: line, + *fixInfoMetadata.linesToRemove = append(*fixInfoMetadata.linesToRemove, linesToRemove{ + startLine: line, + endLine: line, }) // Encode entire Sequence Node and Insert - *fixInfoMetadata.contentToAdd = append(*fixInfoMetadata.contentToAdd, ContentToAdd{ - Line: line, - Content: contentToInsert, + *fixInfoMetadata.contentToAdd = append(*fixInfoMetadata.contentToAdd, contentToAdd{ + line: line, + content: contentToInsert, }) originalListTracker = updateTracker(fixInfoMetadata.originalList, originalListTracker) @@ -372,7 +372,7 @@ func replaceSingleLineSequence(fixInfoMetadata *FixInfoMetadata, line int) (int, } // Returns the first node in the given line that is not mapping node -func getFirstNodeInLine(list *[]NodeInfo, line int) int { +func getFirstNodeInLine(list *[]nodeInfo, line int) int { tracker := 0 currentNode := (*list)[tracker].node @@ -385,11 +385,11 @@ func getFirstNodeInLine(list *[]NodeInfo, line int) int { } // To not mess with the line number while inserting, removed lines are not deleted but replaced with "*" -func removeLines(linesToRemove *[]LinesToRemove, linesSlice *[]string) { +func removeLines(linesToRemove *[]linesToRemove, linesSlice *[]string) { var startLine, endLine int for _, lineToRemove := range *linesToRemove { - startLine = lineToRemove.StartLine - 1 - endLine = lineToRemove.EndLine - 1 + startLine = lineToRemove.startLine - 1 + endLine = lineToRemove.endLine - 1 for line := startLine; line <= endLine; line++ { lineContent := (*linesSlice)[line] @@ -471,7 +471,7 @@ func truncateContentAtHead(filePath string) (string, error) { // The current node along with it's children is skipped and the tracker is moved to next sibling // of current node. If parent is mapping node, "value" in "key-value" pairs is also skipped. -func updateTracker(nodeList *[]NodeInfo, tracker int) int { +func updateTracker(nodeList *[]nodeInfo, tracker int) int { currentNode := (*nodeList)[tracker] var updatedTracker int