Skip to content

Commit

Permalink
fix output on map and type changes
Browse files Browse the repository at this point in the history
  • Loading branch information
semihbkgr committed Jan 7, 2024
1 parent cd2cf3d commit 4fdce66
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions diff/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,17 @@ func compareNodes(leftNode, rightNode ast.Node, conf *DiffOptions) []*Diff {
return []*Diff{{NodeLeft: leftNode, NodeRight: rightNode}}
}

// When a map's key size is one, it is represented by MappingValueNode instead of MappingNode in ast.
// When the map's key size is one, it is just represented by MappingValueNode instead of MappingNode in AST.
// Wrap MappingValueNode by MappingNode if needed.
if leftNode.Type() == ast.MappingType && rightNode.Type() == ast.MappingValueType {
rightNode = ast.Mapping(rightNode.GetToken(), false, rightNode.(*ast.MappingValueNode))
} else if leftNode.Type() == ast.MappingValueType && rightNode.Type() == ast.MappingType {
if leftNode.Type() == ast.MappingValueType {
path := leftNode.GetPath()
leftNode = ast.Mapping(leftNode.GetToken(), false, leftNode.(*ast.MappingValueNode))
} else if leftNode.Type() == ast.MappingValueType && rightNode.Type() == ast.MappingValueType {
leftNode.SetPath(path)
}
if rightNode.Type() == ast.MappingValueType {
path := rightNode.GetPath()
rightNode = ast.Mapping(rightNode.GetToken(), false, rightNode.(*ast.MappingValueNode))
leftNode = ast.Mapping(leftNode.GetToken(), false, leftNode.(*ast.MappingValueNode))
rightNode.SetPath(path)
}

if leftNode.Type() != rightNode.Type() {
Expand Down Expand Up @@ -163,7 +165,15 @@ func compareMappingNodes(leftNode, rightNode *ast.MappingNode, conf *DiffOptions
for k, leftValue := range leftKeyValueMap {
rightValue, ok := rightKeyValueMap[k]
if !ok {
keyDiffsMap[k] = []*Diff{{NodeLeft: leftValue.Value, NodeRight: nil}}
node := leftValue.Value
// wrap the MappingValueNode by MappingNode
// todo: extract this logic into a function
if node.Type() == ast.MappingValueType {
path := node.GetPath()
node = ast.Mapping(node.GetToken(), false, node.(*ast.MappingValueNode))
node.SetPath(path)
}
keyDiffsMap[k] = []*Diff{{NodeLeft: node, NodeRight: nil}}
continue
}
keyDiffsMap[k] = compareNodes(leftValue.Value, rightValue.Value, conf)
Expand All @@ -173,7 +183,14 @@ func compareMappingNodes(leftNode, rightNode *ast.MappingNode, conf *DiffOptions
if ok {
continue
}
keyDiffsMap[k] = []*Diff{{NodeLeft: nil, NodeRight: rightValue.Value}}
node := rightValue.Value
// wrap the MappingValueNode by MappingNode
if node.Type() == ast.MappingValueType {
path := node.GetPath()
node = ast.Mapping(node.GetToken(), false, node.(*ast.MappingValueNode))
node.SetPath(path)
}
keyDiffsMap[k] = []*Diff{{NodeLeft: nil, NodeRight: node}}
}

allDiffs := make([]*Diff, 0)
Expand Down Expand Up @@ -255,6 +272,7 @@ func ignoreIndexes(diffs []*Diff, conf *DiffOptions) []*Diff {
func (d DocDiffs) OutputString(opts *OutputOptions) string {
b := strings.Builder{}
for _, diff := range d {
// todo: move this logic to Diff.OutputString
if diff.NodeLeft == nil { // Added
sign := "+"
path := nodePathString(diff.NodeRight)
Expand Down Expand Up @@ -341,8 +359,6 @@ func nodePathString(n ast.Node) string {
return path
}

// todo: fix output and comparison of MappingValueNode (when left node is nil, right node is MappingValueNode, or vice versa)

func nodeValueString(n ast.Node) string {
switch n.Type() {
case ast.MappingType, ast.SequenceType:
Expand Down

0 comments on commit 4fdce66

Please sign in to comment.