https://github.com/romnn/go-recursive-sort
recursively sort any golang interface for comparisons in your unit tests.
https://github.com/romnn/go-recursive-sort
golang interfaces-go json recursive reflection sort sorting testing unittest
Last synced: 8 months ago
JSON representation
recursively sort any golang interface for comparisons in your unit tests.
- Host: GitHub
- URL: https://github.com/romnn/go-recursive-sort
- Owner: romnn
- License: mit
- Created: 2020-12-26T16:56:58.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2022-11-01T21:30:11.000Z (about 3 years ago)
- Last Synced: 2025-03-27T16:02:48.329Z (10 months ago)
- Topics: golang, interfaces-go, json, recursive, reflection, sort, sorting, testing, unittest
- Language: Go
- Homepage:
- Size: 27.3 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## go-recursive-sort
[](https://github.com/romnn/go-recursive-sort/actions)
[](https://github.com/romnn/go-recursive-sort)
[](https://godoc.org/github.com/romnn/go-recursive-sort)
[](https://codecov.io/gh/romnn/go-recursive-sort)
Recursively sort any golang `interface{}` for comparisons in your unit tests.
#### Installation
```bash
$ go get github.com/romnn/go-recursive-sort
```
#### Example (JSON)
```go
// examples/json/json.go
package main
import (
"log"
recursivesort "github.com/romnn/go-recursive-sort"
)
func main() {
a := `{"test": ["a", "c", "b"]}`
b := `{"test": ["c", "a", "b"]}`
equal, err := recursivesort.EqualJSON(a, b)
if err != nil {
log.Fatalf("failed to compare JSON values: %v", err)
}
if !equal {
log.Fatalf("expected %s == %s", a, b)
}
}
```
#### Example (Struct)
```go
// examples/struct/struct.go
package main
import (
"log"
"reflect"
recursivesort "github.com/romnn/go-recursive-sort"
)
// Common fields must be exported to be sorted
type Common struct {
Values map[string][]string
willNotBeSorted []string
}
// Struct fields must be exported to be sorted
type Struct struct {
A string
B string
C []string
Common Common
}
func main() {
a := Struct{
A: "a",
B: "b",
C: []string{"a", "b", "c"},
Common: Common{
Values: map[string][]string{
"a": []string{"a", "b", "c"},
"b": []string{"a", "b", "c"},
},
willNotBeSorted: []string{"a", "b"},
},
}
b := Struct{
A: "a",
B: "b",
C: []string{"c", "b", "a"},
Common: Common{
Values: map[string][]string{
"b": []string{"c", "b", "a"},
"a": []string{"c", "b", "a"},
},
willNotBeSorted: []string{"a", "b"},
},
}
sort := recursivesort.RecursiveSort{}
sort.Sort(&a)
sort.Sort(&b)
if equal := reflect.DeepEqual(a, b); !equal {
log.Fatalf("expected %v == %v", a, b)
}
}
```
For more examples, see `examples/`.
#### Development
###### Prerequisites
Before you get started, make sure you have installed the following tools
```bash
$ python3 -m pip install pre-commit bump2version invoke
$ go install golang.org/x/tools/cmd/goimports@latest
$ go install golang.org/x/lint/golint@latest
$ go install github.com/fzipp/gocyclo/cmd/gocyclo@latest
```
Please always make sure all code checks pass:
```bash
invoke pre-commit
```