Git Product home page Git Product logo

patchwerk's Introduction

Patchwerk

Patchwerk logo

Go JSON patch

Build Status

Use Patchwerk to create RFC6902 JSON patches.

At the moment of writing this is the only working Go library for creating JSON patches. If you wish to apply the patches I recommend using evanphx/json-patch (it only allows for applying patches, not generating them).

The project was originally cloned from mattbaird/jsonpatch.

Installation

go get github.com/herkyl/patchwerk

Usage

package main

import (
	"fmt"
	"github.com/herkyl/patchwerk"
)

func main() {
	a := `{"a":100, "b":200}`
	b := `{"a":100, "b":200, "c":300}`
	patch, err := patchwerk.DiffBytes([]byte(a), []byte(b))
	if err != nil {
		fmt.Printf("Error creating JSON patch: %v", err)
		return
	}
	fmt.Println(string(patch)) // [{"op": "add", "path": "/c", "value": 300}]
}

patchwerk's People

Contributors

herkyl avatar

Stargazers

Lucas Towers avatar Stephen avatar Konstantinos Katsantonis avatar isvicy avatar georgreen avatar bonedaddy avatar Tomisin Jenrola avatar

Watchers

James Cloos avatar georgreen avatar  avatar

patchwerk's Issues

Bug when rearranging

Let us consider this code

import (
	"fmt"

	"github.com/herkyl/patchwerk"
)

func main() {
	a := `{"L1": ["A", "B","C"]}`
	b := `{"L1": ["C", "A","B"]}`
	patch, err := patchwerk.DiffBytes([]byte(a), []byte(b))
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(string(patch))
}

Output:
[{"op":"add","path":"/L1/0","value":"C"}]'

But the actual output of the patch should be
[{remove /L1/2 } {add /L1/0 C}]
Where C at index 2 is removed and C is added at index 0

Panics with list of objects

I was able to reproduce with following minimal code:

package main

import (
	"encoding/json"

	"github.com/herkyl/patchwerk"
)

type Foo struct {
	Name string
}

func main() {
	from, _ := json.Marshal([]Foo{
		Foo{
			Name: "a",
		},
		Foo{
			Name: "b",
		},
	})
	to, _ := json.Marshal([]Foo{
		Foo{
			Name: "c",
		},
	})
	_, err := patchwerk.Diff(
		[]byte(from),
		[]byte(to),
	)
	if err != nil {
		panic(err)
	}
}

It panics with: panic: runtime error: index out of range [1] with length 1

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.