yamlpatch

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: May 1, 2017 License: Apache-2.0 Imports: 5 Imported by: 56

README

yaml-patch

yaml-patch is a version of Evan Phoenix's json-patch, which is an implementation of JavaScript Object Notation (JSON) Patch, but for YAML.

Installing

go get github.com/krishicks/yaml-patch

API

Given the following RFC6902-ish YAML document, ops:

---
- op: add
  path: /baz/waldo
  value: fred

And the following YAML that is to be modified, src:

---
foo: bar
baz:
  quux: grault

Decode the ops file into a patch:

patch, err := yamlpatch.DecodePatch(ops)
// handle err

Then apply that patch to the document:

dst, err := patch.Apply(src)
// handle err

// do something with dst
Example
doc := []byte(`---
foo: bar
baz:
  quux: grault
`)

ops := []byte(`---
- op: add
  path: /baz/waldo
  value: fred
`)

patch, err := yamlpatch.DecodePatch(ops)
if err != nil {
  log.Fatalf("decoding patch failed: %s", err)
}

bs, err := patch.Apply(doc)
if err != nil {
  log.Fatalf("applying patch failed: %s", err)
}

fmt.Println(string(bs))
baz:
  quux: grault
  waldo: fred
foo: bar

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Container

type Container interface {
	Get(key string) (*Node, error)
	Set(key string, val *Node) error
	Add(key string, val *Node) error
	Remove(key string) error
}

Container is the interface for performing operations on Nodes

type Node

type Node struct {
	// contains filtered or unexported fields
}

Node holds a YAML document that has not yet been processed into a NodeMap or NodeSlice

func NewNode

func NewNode(raw *interface{}) *Node

NewNode returns a new Node. It expects a pointer to an interface{}

func (*Node) IsNodeSlice

func (n *Node) IsNodeSlice() bool

IsNodeSlice returns whether the contents it holds is a slice or not

func (*Node) MarshalYAML

func (n *Node) MarshalYAML() (interface{}, error)

MarshalYAML implements yaml.Marshaler, and returns the correct interface{} to be marshaled

func (*Node) NodeMap

func (n *Node) NodeMap() (*NodeMap, error)

NodeMap returns the node as a NodeMap, if the raw interface{} it holds is indeed a map[interface{}]interface{}

func (*Node) NodeSlice

func (n *Node) NodeSlice() (*NodeSlice, error)

NodeSlice returns the node as a NodeSlice, if the raw interface{} it holds is indeed a []interface{}

func (*Node) UnmarshalYAML

func (n *Node) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML implements yaml.Unmarshaler

type NodeMap

type NodeMap map[interface{}]*Node

NodeMap represents a YAML object

func (*NodeMap) Add

func (n *NodeMap) Add(key string, val *Node) error

Add the provided Node at the given key

func (*NodeMap) Get

func (n *NodeMap) Get(key string) (*Node, error)

Get the node at the given key

func (*NodeMap) Remove

func (n *NodeMap) Remove(key string) error

Remove the node at the given key

func (*NodeMap) Set

func (n *NodeMap) Set(key string, val *Node) error

Set or replace the Node at key with the provided Node

type NodeSlice

type NodeSlice []*Node

NodeSlice represents a YAML array

func (*NodeSlice) Add

func (n *NodeSlice) Add(index string, val *Node) error

Add the provided Node at the given index

func (*NodeSlice) Get

func (n *NodeSlice) Get(index string) (*Node, error)

Get the node at the given index

func (*NodeSlice) Remove

func (n *NodeSlice) Remove(index string) error

Remove the node at the given index

func (*NodeSlice) Set

func (n *NodeSlice) Set(index string, val *Node) error

Set the Node at the given index with the provided Node

type NodeType

type NodeType int

NodeType is a type alias

const (
	NodeTypeRaw NodeType = iota
	NodeTypeMap
	NodeTypeSlice
)

NodeTypes

type Op

type Op string

Op is a type alias

type OpPath

type OpPath string

OpPath is an RFC6902 'pointer'

func (*OpPath) ContainsExtendedSyntax

func (p *OpPath) ContainsExtendedSyntax() bool

ContainsExtendedSyntax returns whether the OpPath uses the "key=value" format, as in "/foo/name=bar", where /foo points at an array that contains an object with a key "name" that has a value "bar"

func (*OpPath) Decompose

func (p *OpPath) Decompose() ([]string, string, error)

Decompose returns the pointer's components: "/foo" => [], "foo" "/foo/1" => ["foo"], "1" "/foo/1/bar" => ["foo", "1"], "bar"

type Operation

type Operation struct {
	Op       Op           `yaml:"op,omitempty"`
	Path     OpPath       `yaml:"path,omitempty"`
	From     OpPath       `yaml:"from,omitempty"`
	RawValue *interface{} `yaml:"value,omitempty"`
}

Operation is an RFC6902 'Operation' https://tools.ietf.org/html/rfc6902#section-4

func (*Operation) Perform

func (o *Operation) Perform(c Container) error

Perform executes the operation on the given container

func (*Operation) Value

func (o *Operation) Value() *Node

Value returns the operation's value as a *Node

type Patch

type Patch []Operation

Patch is an ordered collection of operations.

func DecodePatch

func DecodePatch(bs []byte) (Patch, error)

DecodePatch decodes the passed YAML document as if it were an RFC 6902 patch

func (Patch) Apply

func (p Patch) Apply(doc []byte) ([]byte, error)

Apply returns a YAML document that has been mutated per the patch

Directories

Path Synopsis
cmd
yaml-patch command

Jump to

Keyboard shortcuts

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