Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle comparisons of boolean values correctly #335

Merged
merged 1 commit into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pkg/dyff/compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ some:
Expect(result[0]).To(BeSameDiffAs(singleDiff("/some/yaml/structure/name", dyff.MODIFICATION, 1, 2)))
})

It("should return that different representations of true are treated as the same", func() {
from := yml("---\nkey: true")

to := yml("---\nkey: True")

result, err := compare(from, to)
Expect(err).To(BeNil())
Expect(result).To(BeNil())
})

It("should return that a float was modified", func() {
from := yml(`---
some:
Expand Down
45 changes: 45 additions & 0 deletions pkg/dyff/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@
case "!!null":
// Ignore different ways to define a null value

case "!!bool":
diffs, err = compare.boolValues(path, from, to)

default:
if from.Value != to.Value {
diffs, err = []Diff{{
Expand Down Expand Up @@ -607,6 +610,48 @@
return result, nil
}

func (compare *compare) boolValues(path ytbx.Path, from *yamlv3.Node, to *yamlv3.Node) ([]Diff, error) {
boolFrom, err := toBool(from.Value)
if err != nil {
return nil, err
}

Check warning on line 617 in pkg/dyff/core.go

View check run for this annotation

Codecov / codecov/patch

pkg/dyff/core.go#L616-L617

Added lines #L616 - L617 were not covered by tests
boolTo, err := toBool(to.Value)
if err != nil {
return nil, err
}

Check warning on line 621 in pkg/dyff/core.go

View check run for this annotation

Codecov / codecov/patch

pkg/dyff/core.go#L620-L621

Added lines #L620 - L621 were not covered by tests
result := make([]Diff, 0)
if boolFrom != boolTo {
result = append(result, Diff{
&path,
[]Detail{{
Kind: MODIFICATION,
From: from,
To: to,
}},
})
}

return result, nil
}

// this uses the various values mentioned in https://yaml.org/type/bool.html
var trueValues = [...]string{"y", "Y", "yes", "Yes", "YES", "true", "True", "TRUE", "on", "On", "ON"}
var falseValues = [...]string{"n", "N", "no", "No", "NO", "false", "False", "FALSE", "off", "Off", "OFF"}

func toBool(input string) (bool, error) {
for _, t := range trueValues {
if input == t {
return true, nil
}
}
for _, f := range falseValues {
if input == f {
return false, nil
}
}
return false, fmt.Errorf("not a valid boolean value: '%s'", input)

Check warning on line 652 in pkg/dyff/core.go

View check run for this annotation

Codecov / codecov/patch

pkg/dyff/core.go#L652

Added line #L652 was not covered by tests
}

func (compare *compare) findOrderChangesInSimpleList(fromCommon, toCommon []*yamlv3.Node) []Detail {
// Try to find order changes ...
if len(fromCommon) == len(toCommon) {
Expand Down