yamlpatch

package module
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2017 License: Apache-2.0 Imports: 7 Imported by: 0

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

If you want to use the CLI:

go get github.com/krishicks/yaml-patch/cmd/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) Container

func (n *Node) Container() Container

Container returns the node as a Container

func (*Node) Empty

func (n *Node) Empty() bool

Empty returns whether the raw value is nil

func (*Node) Equal

func (n *Node) Equal(other *Node) bool

Equal compares the values of the raw interfaces that the YAML was unmarshaled into

func (*Node) MarshalYAML

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

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

func (*Node) UnmarshalYAML

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

UnmarshalYAML implements yaml.Unmarshaler

func (*Node) Value

func (n *Node) Value() interface{}

Value returns the raw value of the node

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"

func (*OpPath) String

func (p *OpPath) String() string

String returns the OpPath as a string

type Operation

type Operation struct {
	Op    Op     `yaml:"op,omitempty"`
	Path  OpPath `yaml:"path,omitempty"`
	From  OpPath `yaml:"from,omitempty"`
	Value *Node  `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

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

type PathFinder

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

PathFinder can be used to find RFC6902-standard paths given non-standard (key=value) pointer syntax

func NewPathFinder

func NewPathFinder(container Container) *PathFinder

NewPathFinder takes an interface that represents a YAML document and returns a new PathFinder

func (*PathFinder) Find

func (p *PathFinder) Find(path string) []string

Find expands the given path into all matching paths, returning the canonical versions of those matching paths

type PlaceholderWrapper

type PlaceholderWrapper struct {
	LeftSide  string
	RightSide string
	// contains filtered or unexported fields
}

PlaceholderWrapper can be used to wrap placeholders that make YAML invalid in single quotes to make otherwise valid YAML

func NewPlaceholderWrapper

func NewPlaceholderWrapper(left, right string) *PlaceholderWrapper

NewPlaceholderWrapper returns a new PlaceholderWrapper which knows how to wrap and unwrap the provided left and right sides of a placeholder, e.g. {{ and }}

func (*PlaceholderWrapper) Unwrap

func (w *PlaceholderWrapper) Unwrap(input []byte) []byte

Unwrap the single quotes from the placeholder to make it invalid YAML (again)

func (*PlaceholderWrapper) Wrap

func (w *PlaceholderWrapper) Wrap(input []byte) []byte

Wrap the placeholder in single quotes to make it valid YAML

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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