jsonpatch

package module
v0.0.0-...-84fc45f Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2022 License: 0BSD Imports: 0 Imported by: 0

README

Go JSON Patch

This package implements types to reflect the json patch spec rfc6902. Additionally the patch type has helper methods to add items to the patch slice for the different operations.

Synopsis

The patch type is a slice of patch items. Calling the methods on the patch type will add items to the patch slice.

package main

import (
 "encoding/json"
 "fmt"

 "github.com/bluebrown/jsonpatch"
)

func main() {
  patch := jsonpatch.New()
  patch.Test("/a/b/c", "foo")
  patch.Remove("/a/b/c")
  patch.Add("/a/b/c", []string{"foo", "bar"})
  patch.Replace("/a/b/c", 42)
  patch.Move("/a/b/c", "/a/b/d")
  patch.Copy("/a/b/d", "/a/b/e")
  b, _ := json.MarshalIndent(patch, "", " ")
  fmt.Println(string(b))
}
Result
[
  {
    "op": "test",
    "path": "/a/b/c",
    "value": "foo"
  },
  {
    "op": "remove",
    "path": "/a/b/c"
  },
  {
    "op": "add",
    "path": "/a/b/c",
    "value": [
      "foo",
      "bar"
    ]
  },
  {
    "op": "replace",
    "path": "/a/b/c",
    "value": 42
  },
  {
    "op": "move",
    "from": "/a/b/c",
    "path": "/a/b/d"
  },
  {
    "op": "copy",
    "from": "/a/b/d",
    "path": "/a/b/e"
  }
]

Chainable methods

All methods are chainable.

jsonpatch.New().Add("/a/b/c", []string{"foo", "bar"}).Delete("/b/c/e")

PatchBuilder Interface

The patch type implements the PatcherBuilder interface.

type PatchBuilder interface {
  Test(path string, value interface{}) PatchBuilder    // Add a test operation
  Remove(path string) PatchBuilder                     // Add a remove operation
  Add(path string, value interface{}) PatchBuilder     // Add an add operation
  Replace(path string, value interface{}) PatchBuilder // Add a replace operation
  Move(from, to string) PatchBuilder                   // Add a move operation
  Copy(from, to string) PatchBuilder                   // Add a copy operation
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Item

type Item struct {
	Op    Operation   `json:"op"`
	From  string      `json:"from,omitempty"`
	Path  string      `json:"path"`
	Value interface{} `json:"value,omitempty"`
}

type Operation

type Operation string
const (
	OperationTest    Operation = "test"
	OperationRemove  Operation = "remove"
	OperationAdd     Operation = "add"
	OperationReplace Operation = "replace"
	OperationMove    Operation = "move"
	OperationCopy    Operation = "copy"
)

type Patch

type Patch []Item

func New

func New() *Patch

func (*Patch) Add

func (p *Patch) Add(path string, value interface{}) *Patch

func (*Patch) Copy

func (p *Patch) Copy(from, to string) *Patch

func (*Patch) Move

func (p *Patch) Move(from, to string) *Patch

func (*Patch) Remove

func (p *Patch) Remove(path string) *Patch

func (*Patch) Replace

func (p *Patch) Replace(path string, value interface{}) *Patch

func (*Patch) Test

func (p *Patch) Test(path string, value interface{}) *Patch

type PatchBuilder

type PatchBuilder interface {
	Test(path string, value interface{}) PatchBuilder    // Add a test operation
	Remove(path string) PatchBuilder                     // Add a remove operation
	Add(path string, value interface{}) PatchBuilder     // Add an add operation
	Replace(path string, value interface{}) PatchBuilder // Add a replace operation
	Move(from, to string) PatchBuilder                   // Add a move operation
	Copy(from, to string) PatchBuilder                   // Add a copy operation
}

Jump to

Keyboard shortcuts

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