recursivesort

package module
v0.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 1, 2022 License: MIT Imports: 4 Imported by: 0

README

go-recursive-sort

Build Status GitHub GoDoc Test Coverage

Recursively sort any golang interface{} for comparisons in your unit tests.

Installation
$ go get github.com/romnn/go-recursive-sort
Example (JSON)
// 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)
// 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

$ 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:

invoke pre-commit

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EqualJSON

func EqualJSON(a, b string) (bool, error)

EqualJSON checks if two JSON strings are equal, ignoring the order of values.

Strings `a` and `b` are first unmarshaled into a JSON `interface{}`. Then, they are recursively sorted and compared using `reflect.DeepEqual`

If values differ or cannot be parsed, `false` is returned and `err != nil` describes the error. Otherwise, `true` and `nil` is returned.

If you wish to compare for equality in a different way, e.g. using github.com/google/go-cmp/cmp, you can easily reimplement this method yourself.

func Sort added in v0.0.3

func Sort(v interface{})

Sort recursively sorts an interface.

Types

type RecursiveSort

type RecursiveSort struct {
	TypePriorityLookupHelper

	// MapSortKey specifies the key of maps to use as the sorting key if available
	MapSortKey interface{}

	// StructSortField specifies the field of structs to use as the sorting key if available
	StructSortField string

	// Strict forces using `StructSortField` and `MapSortKey`.
	//
	// Note: if the key or field does not exist, this will panic.
	Strict bool
}

RecursiveSort implements a recursive sort interface for arbitrary types.

func (*RecursiveSort) Sort

func (rec *RecursiveSort) Sort(v interface{})

Sort recursively sorts an interface.

type TypePriorityLookup

type TypePriorityLookup struct {
	Priorities map[reflect.Type]int
}

TypePriorityLookup maps types to integer priorities.

func (*TypePriorityLookup) CompareTypes

func (lookup *TypePriorityLookup) CompareTypes(iv, jv reflect.Value) bool

CompareTypes compars two known types based on their priorities.

If priorities are not known, it delegates to `CompareUnknownTypes`.

func (*TypePriorityLookup) CompareUnknownTypes

func (lookup *TypePriorityLookup) CompareUnknownTypes(iv, jv reflect.Value) bool

CompareUnknownTypes compares two types, of which at least one is not known, based on their string name.

func (TypePriorityLookup) FromTypes

func (lookup TypePriorityLookup) FromTypes(order ...reflect.Type) *TypePriorityLookup

FromTypes builds a `TypePriorityLookup` priority lookup table based on the order of the passed types

func (TypePriorityLookup) FromValues

func (lookup TypePriorityLookup) FromValues(order ...interface{}) *TypePriorityLookup

FromValues builds a `TypePriorityLookup` priority lookup table based on the order of the passed values

type TypePriorityLookupHelper

type TypePriorityLookupHelper interface {
	CompareTypes(iv, ij reflect.Value) bool
	CompareUnknownTypes(iv, ij reflect.Value) bool
}

TypePriorityLookupHelper is an interface to compare two types.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL