Giter VIP home page Giter VIP logo

go-diff's Introduction

go-diff

GoDoc License Travis

go-diff is a Go package that aims to provide functions to calculate differences between two Go structures. The result is presented as a generic map containing old and new values. See the documentation for more details and examples.

Installation

go get -u github.com/e-XpertSolutions/go-diff/diff

Usage

package main

import (
  "fmt"
  "log"

  "github.com/e-XpertSolutions/go-diff/diff"
)

type StructA struct {
  SomeInt int
  B *StructB
}

type StructB struct {
  SomeVal string
  SomeFloat float32
}

func main() {
  a1 := StructA{
    SomeInt: 42,
    B: &StructB{ SomeVal: "Foo", SomeFloat: 123.456 },
  }
  a2 := StructA{
    SomeInt: 24,
    B: &StructB{ SomeVal: "Bar", SomeFloat: 123.4567 },
  }

  d, err :=  diff.Compute(a1, a2)
  if err != nil {
    log.Fatal(err)
  }

  fmt.Println(string(d.PrettyJSON()))

  // Output:
  // {
  //   "B": {
  //     "SomeVal": {
  //       "old_value": "Foo",
  //       "new_value": "Bar",
  //       "type": "MOD"
  //     }
  //   },
  //   "SomeInt": {
  //     "old_value": 42,
  //     "new_value": 24,
  //     "type": "MOD"
  //   }
  // }
}

Diff calculation

  • Basic types (int, float, bool and string): Basic types are directly compared using language defined operators (== and !=).
  • Pointers: The values pointed are compared, not the addresses.
  • Structures: Structures are compared recursively. If they do not contain any exported fields, the structures are compared as strings.
  • Slices and arrays: Slices and arrays are compared row by row.
  • Maps: Maps are not yet supported.

TODO

  • Add support for map types
  • Add support for complex numbers
  • Allow the user to override comparisons through an interface
  • Add an option to limit the depth of the comparisons
  • Better support for slices/arrays comparison
  • More tests and benchmarks
  • Add XML serialization

Contributing

We appreciate any form of contribution (feature request, bug report, pull request, ...). We have no special requirements for Pull Request, just follow the standard GitHub way.

License

The sources are release under a BSD 3-Clause License. The full terms of that license can be found in LICENSE file of this repository.

go-diff's People

Contributors

bcuisinier avatar gilliek avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

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.