-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add EqualProperties rule (#54)
## Release Notes Added `rules.EqualProperties` rule which helps ensure selected properties are equal. The equality check is performed via a configurable function. Two builtin functions are provided out of the box: `rules.CompareFunc` which operates on `comparable` types and `rules.CompareDeepEqualFunc` which uses `reflect.DeepEqual` and operates on any type.
- Loading branch information
1 parent
aba1b13
commit d65c01e
Showing
9 changed files
with
287 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package collections | ||
|
||
import ( | ||
"slices" | ||
|
||
"golang.org/x/exp/constraints" | ||
"golang.org/x/exp/maps" | ||
) | ||
|
||
// SortedKeys returns a sorted slice of keys of the input map. | ||
// The keys must meet [constraints.Ordered] type constraint. | ||
func SortedKeys[M ~map[K]V, K constraints.Ordered, V any](m M) []K { | ||
keys := maps.Keys(m) | ||
slices.Sort(keys) | ||
return keys | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package collections | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/nobl9/govy/internal/assert" | ||
) | ||
|
||
func TestSortedKeys(t *testing.T) { | ||
t.Run("ints", func(t *testing.T) { | ||
m := map[int]string{ | ||
3: "c", | ||
1: "a", | ||
2: "b", | ||
} | ||
keys := SortedKeys(m) | ||
assert.Equal(t, []int{1, 2, 3}, keys) | ||
}) | ||
t.Run("strings", func(t *testing.T) { | ||
m := map[string]int{ | ||
"c": 3, | ||
"a": 1, | ||
"b": 2, | ||
} | ||
keys := SortedKeys(m) | ||
assert.Equal(t, []string{"a", "b", "c"}, keys) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.