Skip to content

Commit

Permalink
format: make groupIterable sort by row
Browse files Browse the repository at this point in the history
Before, it depended on the elements being passed in ordered by their rows.
Before #3823, the iteration
order was the same as the row order; but with sorting the keys slice (which
determines iteration order) on creation, that was changed.

Now, we'll sort the elements within `groupIterable`.

Fixes #3849.

Signed-off-by: Stephan Renatus <[email protected]>
  • Loading branch information
srenatus committed Oct 4, 2021
1 parent 5c6efb0 commit bfd984d
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 4 deletions.
13 changes: 10 additions & 3 deletions format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -810,18 +810,25 @@ func (w *writer) listWriter() entryWriter {
}
}

func groupIterable(elements []interface{}, last *ast.Location) (lines [][]interface{}) {
// groupIterable will group the `elements` slice into slices according to their
// location: anything on the same line will be put into a slice.
func groupIterable(elements []interface{}, last *ast.Location) [][]interface{} {
sort.Slice(elements, func(i, j int) bool {
return locLess(elements[i], elements[j])
})
var lines [][]interface{}
var cur []interface{}
for i, t := range elements {
loc := getLoc(t)
elem := t
loc := getLoc(elem)
lineDiff := loc.Row - last.Row
if lineDiff > 0 && i > 0 {
lines = append(lines, cur)
cur = nil
}

last = loc
cur = append(cur, t)
cur = append(cur, elem)
}
return append(lines, cur)
}
Expand Down
5 changes: 4 additions & 1 deletion format/testfiles/test.rego.formatted
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ foo[x] {
g(x, "foo") = z
}

globals = {"fizz": "buzz", "foo": "bar"}
globals = {
"foo": "bar",
"fizz": "buzz",
}

partial_obj["x"] = 1

Expand Down
33 changes: 33 additions & 0 deletions format/testfiles/test_issue_3849.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package test_issue_3849

test_require_context {
require_context("monkey", "eat", "banana") with input as {
"principal": {"id": 101, "type": "monkey"},
"action": "eat",
"entity": {"id": 102, "type": "banana"},
}
}

test_contrived {
allow with input as {
"a": 101,
"b": 101,
"z": 101,
"y": 101,
"x": 101,
"w": 101,
"v": 101,
"u": 101,
"t": 101,
"s": 101,
"r": 101,
"q": 101,
"p": 101,
"o": 101,
"n": 101,
"j": 101,
"k": 101,
"l": 101,
"m": 101,
}
}
33 changes: 33 additions & 0 deletions format/testfiles/test_issue_3849.rego.formatted
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package test_issue_3849

test_require_context {
require_context("monkey", "eat", "banana") with input as {
"principal": {"id": 101, "type": "monkey"},
"action": "eat",
"entity": {"id": 102, "type": "banana"},
}
}

test_contrived {
allow with input as {
"a": 101,
"b": 101,
"z": 101,
"y": 101,
"x": 101,
"w": 101,
"v": 101,
"u": 101,
"t": 101,
"s": 101,
"r": 101,
"q": 101,
"p": 101,
"o": 101,
"n": 101,
"j": 101,
"k": 101,
"l": 101,
"m": 101,
}
}

0 comments on commit bfd984d

Please sign in to comment.