Skip to content

Commit

Permalink
Merge pull request #18 from yazgazan/structs
Browse files Browse the repository at this point in the history
Supporting structs
  • Loading branch information
yazgazan authored Aug 20, 2019
2 parents da56d2b + 6f2f9ed commit 4650b6a
Show file tree
Hide file tree
Showing 8 changed files with 643 additions and 24 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
language: go
go:
- "1.10"
- 1.11
- 1.12
- "1.11"
- "1.12"

before_install:
- go get -u golang.org/x/tools/cmd/cover
Expand Down
19 changes: 9 additions & 10 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@

A JSON diff utility.

# Install
## Install

## Downloading the compiled binary
### Downloading the compiled binary

- Download the latest version of the binary: [releases](https://github.com/yazgazan/jaydiff/releases)
- extract the archive and place the `jaydiff` binary in your `$PATH`

## From source
### From source

- Have go 1.10 or greater installed: [golang.org](https://golang.org/doc/install)
- run `go get -u github.com/yazgazan/jaydiff`

# Usage
## Usage

```
```text
Usage:
jaydiff [OPTIONS] FILE_1 FILE_2
Expand All @@ -41,7 +41,7 @@ Help Options:
-h, --help Show this help message
```

## Examples
### Examples

Getting a full diff of two json files:

Expand Down Expand Up @@ -73,8 +73,8 @@ Ignoring fields:

```diff
$ jaydiff --show-types \
--ignore='.b\[\]' --ignore='.d' --ignore='.c.[ac]' \
old.json new.json
--ignore='.b\[\]' --ignore='.d' --ignore='.c.[ac]' \
old.json new.json

map[string]interface {} map[
a: float64 42
Expand Down Expand Up @@ -159,9 +159,8 @@ $ jaydiff --report --show-types --ignore-excess --ignore-values old.json new.jso
- .f: float64 42
```

# Ideas
## Ideas

- JayPatch
- Have the diff lib support more types (Structs, interfaces (?), Arrays, ...)

Sponsored by [Datumprikker.nl](https://datumprikker.nl)
32 changes: 26 additions & 6 deletions diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ func Diff(lhs, rhs interface{}, opts ...ConfigOpt) (Differ, error) {
}

func diff(c config, lhs, rhs interface{}, visited *visited) (Differ, error) {
lhsVal := reflect.ValueOf(lhs)
rhsVal := reflect.ValueOf(rhs)
lhsVal, lhs := indirectValueOf(lhs)
rhsVal, rhs := indirectValueOf(rhs)

if d, ok := nilCheck(lhs, rhs); ok {
return d, nil
Expand All @@ -56,23 +56,43 @@ func diff(c config, lhs, rhs interface{}, visited *visited) (Differ, error) {
return types{lhs, rhs}, ErrCyclic
}

if lhsVal.Type().Comparable() && rhsVal.Type().Comparable() {
if valueIsScalar(lhsVal) && valueIsScalar(rhsVal) {
return scalar{lhs, rhs}, nil
}
if lhsVal.Kind() != rhsVal.Kind() {
return types{lhs, rhs}, nil
}

if lhsVal.Kind() == reflect.Slice {
switch lhsVal.Kind() {
case reflect.Slice, reflect.Array:
return c.sliceFn(c, lhs, rhs, visited)
}
if lhsVal.Kind() == reflect.Map {
case reflect.Map:
return newMap(c, lhs, rhs, visited)
case reflect.Struct:
return newStruct(c, lhs, rhs, visited)
}

return types{lhs, rhs}, &ErrUnsupported{lhsVal.Type(), rhsVal.Type()}
}

func indirectValueOf(i interface{}) (reflect.Value, interface{}) {
v := reflect.Indirect(reflect.ValueOf(i))
if !v.IsValid() || !v.CanInterface() {
return reflect.ValueOf(i), i
}

return v, v.Interface()
}

func valueIsScalar(v reflect.Value) bool {
switch v.Kind() {
default:
return v.Type().Comparable()
case reflect.Struct, reflect.Array, reflect.Ptr, reflect.Chan:
return false
}
}

func nilCheck(lhs, rhs interface{}) (Differ, bool) {
if lhs == nil && rhs == nil {
return scalar{lhs, rhs}, true
Expand Down
Loading

0 comments on commit 4650b6a

Please # to comment.