Giter VIP home page Giter VIP logo

libra's Introduction

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.

libra's People

Contributors

haritsfahreza avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

isgasho

libra's Issues

[NEW] Support Ignore Field

Is your feature request related to a problem? Please describe.
As a developer, I want to ignore some fields that I do not want to compare using tagging.

Describe the solution you'd like

  • Add ability to define the ignore tagging of the struct
  • Ignore the field comparison that has ignore tagging
  • Enhance the process to read the tagging

Describe alternatives you've considered
None

Additional context
None

[BUG] Better support of struct with private field

Describe the bug
Unable to compare the struct with a private field, even it's not filled properly.

Expected behavior
It should compare the public field only and might ignore the private field.

Snippet Codes

type Example struct {
    Exposed string //should be compared
    hidden string // should NOT be compared
}

[NEW] Compare nested struct

Is your feature request related to a problem? Please describe.
As a user, I want to compare my struct and its field that also a struct (nested struct)

Describe the solution you'd like
Add capabilities to cover nested struct

Describe alternatives you've considered
None

Additional context
None

[NEW] Support Object Unique ID

Is your feature request related to a problem? Please describe.
As a developer, I want to know which object from my struct is changed based on the unique ID that I defined before.

Describe the solution you'd like

  • Add ability to define the unique ID tagging of the struct
  • Add field Object ID to keep the Object Unique ID information in Diff
  • Enhance the process to read the tagging
  • Only one field that tagged as unique ID per struct permitted

Describe alternatives you've considered
None

Additional context
None

[NEW] Implement fuzzing for better data type coverage

Is your feature request related to a problem? Please describe.
The .Compare function is receiving interface. Since it's a generic data type, we need to find a way to cover all possible data types on Go better.

Describe the solution you'd like
We can start by upgrading it to 1.18 and implement Go fuzzing as one of the tests.

Describe alternatives you've considered
Another alternative is to do trial and error for every Go data type, but it could be spending more time and effort.

[NEW] Support basic types

Is your feature request related to a problem? Please describe.
As a developer, I want to compare all of the basic types like bool, int, float, string, etc.

Describe the solution you'd like
Return a Diff result instead of an error Unsupported comparable values

Describe alternatives you've considered
None

Additional context
None

[NEW] Compare pointer

This code below will show error message "panic: Unsupported comparable values". I think it will be very usefull if this library can support comparing struct with pointer, since reflect.DeepEqual doesn't support it very well.

oldPerson := &person{
		Name:           "Gopher",
		Age:            10,
		Weight:         50.0,
		IsMarried:      false,
		Hobbies:        []string{"Coding"},
		Numbers:        []int{0, 1, 2},
		AdditionalInfo: "I love Golang",
	}

	newPerson := &person{
		Name:           "Gopher",
		Age:            10,
		Weight:         60.0,
		IsMarried:      false,
		Hobbies:        []string{"Hacking"},
		Numbers:        []int{1, 2, 3},
		AdditionalInfo: "I love Golang so much",
	}

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.