patchwerk

package module
v0.0.0-...-f0ea770 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 29, 2019 License: Apache-2.0 Imports: 5 Imported by: 2

README

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}]
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DiffBytes

func DiffBytes(a, b []byte) ([]byte, error)

DiffBytes creates a patch as specified in http://jsonpatch.com/

'a' is original, 'b' is the modified document. Both are to be given as json encoded content. The function will return JSON as a byte array

Types

type ByPath

type ByPath []*JSONPatchOperation

func (ByPath) Len

func (a ByPath) Len() int

func (ByPath) Less

func (a ByPath) Less(i, j int) bool

func (ByPath) Swap

func (a ByPath) Swap(i, j int)

type JSONPatchOperation

type JSONPatchOperation struct {
	Operation string      `json:"op"`
	Path      string      `json:"path"`
	Value     interface{} `json:"value,omitempty"`
}

func Diff

func Diff(a, b []byte) ([]*JSONPatchOperation, error)

Diff creates a patch as specified in http://jsonpatch.com/

'a' is original, 'b' is the modified document. Both are to be given as json encoded content. The function will return an array of JSONPatchOperations

func NewPatch

func NewPatch(operation, path string, value interface{}) *JSONPatchOperation

func (*JSONPatchOperation) JSON

func (j *JSONPatchOperation) JSON() string

func (*JSONPatchOperation) MarshalJSON

func (j *JSONPatchOperation) MarshalJSON() ([]byte, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL