dom

package
v1.0.30 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// CompactFn is WalkFn that you can use to compact document tree by removing empty containers.
	CompactFn = func(path string, parent ContainerBuilder, node Node) bool {
		if node.IsContainer() {
			if len(node.(ContainerBuilder).Children()) == 0 {
				parent.Remove(path)
			}
		}
		return true
	}
)

Functions

func DefaultJsonDecoder

func DefaultJsonDecoder(r io.Reader, v interface{}) error

func DefaultJsonEncoder

func DefaultJsonEncoder(w io.Writer, v interface{}) error

func DefaultNodeMappingFn

func DefaultNodeMappingFn(n Container) interface{}

func DefaultYamlDecoder

func DefaultYamlDecoder(r io.Reader, v interface{}) error

func DefaultYamlEncoder

func DefaultYamlEncoder(w io.Writer, v interface{}) error

Types

type Container

type Container interface {
	Node
	// Children returns mapping between child name and its corresponding Node
	Children() map[string]Node
	// Child returns single child Node by its name
	Child(name string) Node
	// Lookup attempts to find child Node at given path
	Lookup(path string) Node
	// Flatten flattens this Container into list of leaves
	Flatten() map[string]Leaf
	// Search finds all paths where Node's value is equal to given value according to provided SearchValueFunc.
	// If no match is found, nil is returned.
	Search(fn SearchValueFunc) []string
}

Container is element that has zero or more child Nodes

type ContainerBuilder

type ContainerBuilder interface {
	Container
	Serializable
	// AddValue adds Node value into this Container
	AddValue(name string, value Node)
	// AddValueAt adds Leaf value into this Container at given path.
	// Child nodes are creates as needed.
	AddValueAt(path string, value Node)
	// AddContainer adds child Container into this Container
	AddContainer(name string) ContainerBuilder
	// AddList adds child List into this Container
	AddList(name string) ListBuilder
	// Remove removes direct child Node.
	Remove(name string)
	// RemoveAt removes child Node at given path.
	RemoveAt(path string)
	// Walk walks whole document tree, visiting every node
	Walk(fn WalkFn)
}

ContainerBuilder is mutable extension of Container

type ContainerFactory

type ContainerFactory interface {
	// Container creates empty ContainerBuilder
	Container() ContainerBuilder
	// FromReader creates ContainerBuilder pre-populated with data from provided io.Reader and DecoderFunc
	FromReader(r io.Reader, fn DecoderFunc) (ContainerBuilder, error)
	// FromMap creates ContainerBuilder pre-populated with data from provided map
	FromMap(in map[string]interface{}) ContainerBuilder
	// FromAny creates ContainerBuilder from any object. Any error encountered in process will result in panic.
	// This method uses YAML codec internally to perform translation between raw interface and map
	FromAny(v interface{}) ContainerBuilder
}

func Builder

func Builder() ContainerFactory

type Coordinate added in v1.0.10

type Coordinate interface {
	// Layer returns name of layer within OverlayDocument
	Layer() string
	// Path returns path to Node within layer
	Path() string
}

Coordinate is address of Node within OverlayDocument

type Coordinates added in v1.0.30

type Coordinates []Coordinate

Coordinates is collection of Coordinate

func (Coordinates) String added in v1.0.30

func (cs Coordinates) String() string

type DecoderFunc

type DecoderFunc func(r io.Reader, v interface{}) error

DecoderFunc decodes byte stream into raw value using provided io.Reader

type EncoderFunc

type EncoderFunc func(w io.Writer, v interface{}) error

EncoderFunc encodes raw value into stream using provided io.Writer

type Leaf

type Leaf interface {
	Node
	Value() interface{}
}

Leaf represent Node of scalar value

func LeafNode

func LeafNode(val interface{}) Leaf

type List added in v1.0.12

type List interface {
	Node
	// Size returns current count of items in this list
	Size() int
	// Items returns copy of slice of all nodes in this list
	Items() []Node
}

List is collection of Nodes

type ListBuilder added in v1.0.12

type ListBuilder interface {
	List
	// Clear sets items to empty slice
	Clear()
	// Set sets item at given index. Items are allocated and set to nil Leaf as necessary.
	Set(uint, Node)
	// MustSet sets item at given index. Panics if index is out of bounds.
	MustSet(uint, Node)
	// Append adds new item at the end of slice
	Append(Node)
}

func ListNode added in v1.0.13

func ListNode(items ...Node) ListBuilder

type MergeOption added in v1.0.24

type MergeOption func(*merger)

MergeOption is function used to customize merger behavior

func ListsMergeAppend added in v1.0.24

func ListsMergeAppend() MergeOption

func ListsMergeFunc added in v1.0.24

func ListsMergeFunc(fn func(_, _ List) List) MergeOption

type Node

type Node interface {
	// IsContainer returns true if this node is Container
	IsContainer() bool
	// IsList returns true if this node is List
	IsList() bool
	// IsLeaf returns true if this node is Leaf
	IsLeaf() bool
	// SameAs returns true if this node is of same type like other node.
	// The other Node can be nil, in which case return value is false.
	SameAs(Node) bool
	// Equals return true is value of this node is equal to value of provided Node.
	Equals(Node) bool
	// Clone returns deep copy of this Node.
	Clone() Node
}

Node is elemental unit of document. At runtime, it could be either Leaf or Container.

type NodeList added in v1.0.25

type NodeList []Node

NodeList is sequence of zero or more Nodes

type NodeMappingFunc

type NodeMappingFunc func(Container) interface{}

NodeMappingFunc maps internal Container value into external data representation

type OverlayDocument

type OverlayDocument interface {
	Serializable
	// Lookup lookups data in given overlay and path
	// if no node is present at any level, nil is returned
	Lookup(overlay, path string) Node
	// LookupAny lookups data in all overlays (in creation order) and path.
	// if no node is present at any level, nil is returned
	LookupAny(path string) Node
	// Search finds all occurrences of given value in all layers using custom SearchValueFunc
	Search(fn SearchValueFunc) Coordinates
	// Populate puts dictionary into overlay at given path
	Populate(overlay, path string, data *map[string]interface{})
	// Put puts Node value into overlay at given path
	Put(overlay, path string, value Node)
	// Merged returns read-only, merged view of all overlays
	Merged(option ...MergeOption) Container
	// Layers return copy of list with all layer names
	Layers() []string
}

OverlayDocument represents multiple documents layered over each other. It allows lookup across all layers while respecting precedence

func NewOverlayDocument

func NewOverlayDocument() OverlayDocument

type SearchValueFunc added in v1.0.11

type SearchValueFunc func(val interface{}) bool

SearchValueFunc is used to search for value within document

func SearchEqual added in v1.0.11

func SearchEqual(in interface{}) SearchValueFunc

SearchEqual is SearchValueFunc that search for equivalent value

type Serializable

type Serializable interface {
	// Serialize writes content into given writer, while encoding using provided EncoderFunc
	Serialize(writer io.Writer, mappingFunc NodeMappingFunc, encFn EncoderFunc) error
}

Serializable interface allows to persist data into provided io.Writer

type WalkFn added in v1.0.20

type WalkFn func(path string, parent ContainerBuilder, node Node) bool

Jump to

Keyboard shortcuts

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