Git Product home page Git Product logo

structdiff's Introduction

Description

Comparing the two structures, the output differs according to the syntax definition format of gjson

Example

package modeldiff

import (
    "encoding/json"
    "log"
    "testing"
)

type Student struct {
    Name      string
    Subjects1 []*Subject `comparedby:"Name"`
    Subjects2 []*Subject

    Contacts1 Contacts `comparedby:",direct"`
    Contacts2 Contacts
}

type Subject struct {
    Name  string
    Score float64
}

type Contacts struct {
    Phone string
}

func TestCompare(t *testing.T) {
    diffdetails := &DiffDetails{}

    oldStudent := &Student{
        Name:      "LiLei",
        Subjects1: []*Subject{{Name: "Math", Score: 80}, {Name: "Geography", Score: 80}},
        Subjects2: []*Subject{{Name: "Math", Score: 80}, {Name: "Geography", Score: 80}},
        Contacts1: Contacts{Phone: "12345678"},
        Contacts2: Contacts{Phone: "12345678"},
    }

    newStudent := &Student{
        Name:      "LiLei",
        Subjects1: []*Subject{{Name: "Math", Score: 88}, {Name: "English", Score: 88}},
        Subjects2: []*Subject{{Name: "Math", Score: 88}, {Name: "English", Score: 88}},
        Contacts1: Contacts{Phone: "123456"},
        Contacts2: Contacts{Phone: "123456"},
    }

    diffdetails, err := Diff(newStudent, oldStudent)
    if err != nil {
        panic(err)
    }
    body, _ := json.Marshal(diffdetails)
    log.Println(string(body))
}
 {
    "Add": {
        "Subjects1.#(Name=="English")": null
    },
    "Del": {
        "Subjects1.#(Name=="Geography")": {
            "Score": 80,
            "Name": "Geography"
        }
    },
    "Change": {
        "Subjects2": [
            {
                "Score": 80,
                "Name": "Math"
            },
            {
                "Score": 80,
                "Name": "Geography"
            }
        ],
        "Subjects1.#(Name=="Math").Score": 80,
        "Contacts2.Phone": "12345678",
        "Contacts1": {
            "Phone": "12345678"
        }
    }
}

structdiff's People

Contributors

lqu3j avatar

Stargazers

 avatar

Watchers

 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.