Skip to content

haritsfahreza/libra

Repository files navigation

Go Reference CircleCI codecov Go Report Card License

libra

Libra is a Go library to compare the interface (struct, maps, etc) and spot the differences between two of them.

Installing

$ go get -u github.com/haritsfahreza/libra

Usage

//Prepare the objects that you want to compare
oldPerson := Person{
	Name:      "Sudirman",
	Age:       22,
	Weight:    float64(80),
	IsMarried: true,
}

newPerson := Person{
	Name:      "Sudirman",
	Age:       23,
	Weight:    float64(85),
	IsMarried: true,
}

diffs, err := libra.Compare(context.Background(), oldPerson, newPerson)
if err != nil {
	panic(err)
}

//Use the diffs array with your own purpose e.g. printout
for i, diff := range diffs {
	fmt.Printf("#%d : ChangeType=%s Field=%s ObjectType=%s Old='%v' New='%v'\n", i, diff.ChangeType, diff.Field, diff.ObjectType, diff.Old, diff.New)
}

Please see examples for the other usage references

Comparing struct with private fields

Currently, we need to have String function to get the value of the struct with private fields since reflect library would not be able to compare them.

type HiddenPerson struct {
	name string
	age int
}

func (h HiddenPerson) String() string {
	return fmt.Sprintf("%s, %d", h.name, h.age)
}

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

  • Harits Fahreza Christyonotoputra - Initial work - haritsfahreza

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Acknowledgments

  • With libra, you can speed up your Go software development, especially when you build the object auditing and data versioning system.
  • This project is inspired by Javers.