libra

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2023 License: MIT Imports: 4 Imported by: 0

README

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.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compare

func Compare(ctx context.Context, old, new interface{}) ([]diff.Diff, error)

Compare is used to compare two different values and spot the differences from them

Example (Struct)
package main

import (
	"context"
	"fmt"

	"github.com/haritsfahreza/libra"
)

type person struct {
	ID        int `libra:"id"`
	Name      string
	Age       int
	Weight    float64
	IsMarried bool
	Hobbies   []string
	Numbers   []int
	Ignore    string `libra:"ignore"`
	Interface interface{}
}

func main() {
	oldPerson := person{
		Name:      "Gopher",
		Age:       10,
		Weight:    50.0,
		IsMarried: false,
		Hobbies:   []string{"Coding"},
		Numbers:   []int{0, 1, 2},
	}

	newPerson := person{
		Name:      "Gopher",
		Age:       10,
		Weight:    60.0,
		IsMarried: false,
		Hobbies:   []string{"Hacking"},
		Numbers:   []int{1, 2, 3},
	}

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

	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)
	}
}
Output:

#0 : ChangeType=changed Field=Weight ObjectType=libra_test.person Old='50' New='60'
#1 : ChangeType=changed Field=Hobbies ObjectType=libra_test.person Old='Coding' New='Hacking'
#2 : ChangeType=changed Field=Numbers ObjectType=libra_test.person Old='0,1,2' New='1,2,3'

Types

This section is empty.

Directories

Path Synopsis
pkg

Jump to

Keyboard shortcuts

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