data

package
v0.0.0-...-66857fd Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2021 License: BSD-3-Clause, MPL-2.0 Imports: 13 Imported by: 8

Documentation

Overview

Package data implements an convenient object model for interacting with arbitrary rfc7951 data. The Trees, Objects, and Arrays in this library are immutable. This means that updating the structure will yield a new copy with the changes made, this is made efficient by sharing much of the structure of the new object with the old one. The library is based on the central Value type that holds arbitrary RFC7951 data this may take on Object, Array, int types, uint types, strings, float types, bools, the empty value, and nil. This may be thought of as a restricted form of the go interface{} type. The provided Tree type is a special form of Object that allows for complex operations based on instance-identifier paths.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Array

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

Array is an RFC7159 array augmented for RFC7951 behaviors. The arrays are immutable, the mutation methods return new structurally shared copies of the original array with the changes. This provides cheap copies of the array and preserves the original allowing it to be easily shared.

func ArrayFrom

func ArrayFrom(in interface{}) *Array

ArrayFrom creates an array and initializes it with the elements from the provided slice

func ArrayNew

func ArrayNew() *Array

ArrayNew creates a new array and returns its abstract representation

func ArrayWith

func ArrayWith(elements ...interface{}) *Array

ArrayWith creates an array and initializes it with the provided elements

func (*Array) Append

func (arr *Array) Append(value interface{}) *Array

Append adds a new value to the end of the array.

func (*Array) Assoc

func (arr *Array) Assoc(index int, value interface{}) *Array

Assoc associates the value with the index in the array. If the index is out of bounds the array is padded to that index and the value is associated.

func (*Array) At

func (arr *Array) At(index int) *Value

At returns the value at the index of the array, if the index is out of bounds, nil is returned.

func (*Array) Contains

func (arr *Array) Contains(index int) bool

Contains returns whether the index is in the bounds of the array.

func (*Array) Delete

func (arr *Array) Delete(index int) *Array

Delete removes an element at the supplied index from the array.

func (*Array) Equal

func (arr *Array) Equal(other interface{}) bool

Equal implements equality for arrays. An array is equal to another array if all their values at each index is equal. Equality checks are linear with respect to the number of elements.

func (*Array) Find

func (arr *Array) Find(index int) (*Value, bool)

Find returns the value at the index or nil if it doesn't exist and whether the index was in the array.

func (*Array) Length

func (arr *Array) Length() int

Length returns the number of elements in the array.

func (*Array) Range

func (arr *Array) Range(fn interface{}) *Array

Range iterates over the object's members. Range can take a set of functions matched by type. If the function returns a bool this is treated as a loop terminataion variable if false the loop will terminate.

func(int, *Value) iterates over indicies and values.
func(int, *Value) bool
func(int) iterates over only the indicies
func(int) bool
func(*Value) iterates over only the values
func(*Value bool

func (*Array) Sort

func (arr *Array) Sort(options ...SortOption) *Array

Sort sorts an array returning a new array that is sorted. by default sort will use dyn.Compare as the comparison operator this may be overridden using the Compare option.

func (*Array) String

func (arr *Array) String() string

String returns a string representation of the Array.

func (*Array) Transform

func (arr *Array) Transform(fn func(*TArray)) *Array

Transform executes the provided function against a mutable transient array to provide a faster, less memory intensive, array editing mechanism.

type EditAction

type EditAction string

EditAction is an action that can be performed by the edit engine.

const (
	// EditAssoc is the edit action association with the Assoc operation.
	EditAssoc EditAction = "assoc"
	// EditDelete is the edit action association with the Delete operation.
	EditDelete EditAction = "delete"
	// EditMerge is the edit action association with the Merge operation.
	EditMerge EditAction = "merge"
)

func (EditAction) MarshalRFC7951

func (e EditAction) MarshalRFC7951() ([]byte, error)

MarshalRFC7951 returns the EditAction as RFC7951 encoded data.

func (EditAction) String

func (e EditAction) String() string

String returns the EditAction as a string.

func (*EditAction) UnmarshalRFC7951

func (e *EditAction) UnmarshalRFC7951(msg []byte) error

UnmarshalRFC7951 unmarshals the RFC7951 encoded message into the EditAction.

type EditEntry

type EditEntry struct {
	Action EditAction  `rfc7951:"action"`
	Path   *InstanceID `rfc7951:"path"`
	Value  *Value      `rfc7951:"value,omitempty"`
}

EditEntry contains the actions to perform as well as the instance-id to perform it at and the value if any to be used.

func EditEntryNew

func EditEntryNew(action EditAction, path string, options ...EditEntryOption) EditEntry

EditEntryNew constructs a new EditEntry from the provided parameters. The last option in wins if they write the same option.

type EditEntryOption

type EditEntryOption func(*editEntryOptions)

EditEntryOption is a constructor for the optional parts of an EditEntry.

func EditEntryValue

func EditEntryValue(val interface{}) EditEntryOption

EditEntryValue produce an EditEntryOption that populates the value field of an EditEntry.

type EditOperation

type EditOperation struct {
	Actions []EditEntry `rfc7951:"actions,omitempty"`
}

EditOperation holds edit actions and allow them to be encoded as RFC7951 data.

Example (Marshal)
edit := EditOperation{
	Actions: []EditEntry{
		{
			Action: EditAssoc,
			Path:   InstanceIDNew("/module-v1:foo/bar"),
			Value: ValueNew(ObjectWith(
				PairNew("bar", "quuz"))),
		},
	},
}
enc := rfc7951.NewEncoder(os.Stdout)
err := enc.Encode(&edit)
if err != nil {
	fmt.Fprintln(os.Stderr, err)
}
Output:

{"actions":[{"action":"assoc","path":"/module-v1:foo/bar","value":{"bar":"quuz"}}]}
Example (String)
edit := EditOperation{
	Actions: []EditEntry{
		{
			Action: EditAssoc,
			Path:   InstanceIDNew("/module-v1:foo/bar"),
			Value: ValueNew(ObjectWith(
				PairNew("bar", "quuz"))),
		},
	},
}
fmt.Println(edit.String())
Output:

{"actions":[{"action":"assoc","path":"/module-v1:foo/bar","value":{"bar":"quuz"}}]}
Example (Unmarshal)
var edit EditOperation
s := `{
		"actions":[
			{
				"action":"assoc",
				"path":"/module-v1:foo/bar",
				"value":{"bar":"quuz"}
			},
			{
				"action":"delete",
				"path":"/module-v1:foo/bar"
			},
			{
				"action":"merge",
				"path":"/module-v1:foo/bar",
				"value":{"bar":"quux"}
			}
		]
	}`
dec := rfc7951.NewDecoder(strings.NewReader(s))
err := dec.Decode(&edit)
if err != nil {
	fmt.Fprintln(os.Stderr, err)
}
enc := rfc7951.NewEncoder(os.Stdout)
err = enc.Encode(&edit)
if err != nil {
	fmt.Fprintln(os.Stderr, err)
}
Output:

{"actions":[{"action":"assoc","path":"/module-v1:foo/bar","value":{"bar":"quuz"}},{"action":"delete","path":"/module-v1:foo/bar"},{"action":"merge","path":"/module-v1:foo/bar","value":{"bar":"quux"}}]}

func EditOperationNew

func EditOperationNew(entries ...EditEntry) *EditOperation

EditOperationNew produces a new EditOperation from the provided entries. This allows one to declaratively build an EditOperation.

func (*EditOperation) String

func (e *EditOperation) String() string

String returns a string representation of the EditOperation.

type InstanceID

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

InstanceID is an RFC7951 instance-identifier type. It is defined here https://tools.ietf.org/html/rfc7951#section-6.11

RFC7951 instance identifiers match the following grammar:

instance-identifier = 1*("/" (node-identifier *predicate))
predicate           = "[" *WSP (predicate-expr / pos) *WSP "]"
predicate-expr      = (node-identifier / ".") *WSP "=" *WSP
                      ((DQUOTE string DQUOTE) /
                       (SQUOTE string SQUOTE))
pos                 = non-negative-integer-value
node-identifier     = [prefix ":"] identifier
identifier          = (ALPHA / "_")
                      *(ALPHA / DIGIT / "_" / "-" / ".")
prefix              = identifier
non-negative-integer-value = "0" / positive-integer-value
positive-integer-value = (non-zero-digit *DIGIT)
string              = < an unquoted string as returned by the scanner >
non-zero-digit      = %x31-39
DIGIT               = %x30-39
                      ; 0-9
ALPHA               = %x41-5A / %x61-7A
                      ; A-Z / a-z
WSP                 = SP / HTAB
                      ; whitespace
DQUOTE              = %x22
                      ; " (Double Quote)
SQUOTE              = %x27
                      ; ' (Single Quote)

func InstanceIDNew

func InstanceIDNew(instance string) *InstanceID

InstanceIDNew parses an instance identifier string into an InstanceID object

func (*InstanceID) Equal

func (i *InstanceID) Equal(other interface{}) bool

Equal determines if two instance-identifiers are the same. It implements a common equality interface so other must be interface{}.

func (*InstanceID) Find

func (i *InstanceID) Find(value *Value) (*Value, bool)

Find will traverse the tree to find the Value to which the instance-identifier refers.

func (*InstanceID) MarshalRFC7951

func (i *InstanceID) MarshalRFC7951() ([]byte, error)

MarshalRFC7951 will serialize the instance-identifier into an RFC7951 compatible format.

func (*InstanceID) MatchAgainst

func (i *InstanceID) MatchAgainst(value *Value) *Value

MatchAgainst returns the value at the location represented by the instance-identifier. If none, it returns nil.

func (*InstanceID) RFC7951String

func (i *InstanceID) RFC7951String() string

RFC7951String implements string conversion as expected by the value type. RFC7951String is different than stringer in case the types need to encode differently for the encoding format.

func (*InstanceID) String

func (i *InstanceID) String() string

String will format an instance-identifier as a string. This instance-identifier is normalized to the RFC7951 spec.

func (*InstanceID) UnmarshalRFC7951

func (i *InstanceID) UnmarshalRFC7951(msg []byte) (err error)

UnmarshalRFC7951 will parse an instance-identifier that was received via an RFC7951 encoded message.

type Object

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

Object is an RFC7159 (JSON) object augmented for RFC7951 behaviors. These objects are immutable, the mutation methods return a structurally shared copy of the object with the required changes. This provides cheap copies of the object and preserves the original allowing it to be easily shared. Objects store the module:key as the full key, but one may access operations using only the key if the module is the same as the parent object.

func ObjectFrom

func ObjectFrom(in map[string]interface{}) *Object

ObjectFrom creates a new object and then populates it with the data from the supplied map

func ObjectNew

func ObjectNew() *Object

ObjectNew creates a new object.

func ObjectWith

func ObjectWith(pairs ...Pair) *Object

ObjectWith creates a new object and then populates it with the supplied pairs

func (*Object) Assoc

func (obj *Object) Assoc(key string, value interface{}) *Object

Assoc associates a new value with the key. The key may be either 'module:key' or just key if the module is the same as the containing object's module.

func (*Object) At

func (obj *Object) At(key string) *Value

At returns the Value at the key's location or nil if it doesn't exist. The key may be either 'module:key' or just key if the module is the same as the containing object's module.

func (*Object) Contains

func (obj *Object) Contains(key string) bool

Contains returns true if the key exists in the object. The key may be either 'module:key' or just key if the module is the same as the containing object's module.

func (*Object) Delete

func (obj *Object) Delete(key string) *Object

Delete removes a key from the object. The key may be either 'module:key' or just key if the module is the same as the containing object's module.

func (*Object) Equal

func (obj *Object) Equal(other interface{}) bool

Equal implements equality for objects. An object is equal to another object if all their keys contains equal values. Equality checks are linear with respect to the number of keys.

func (*Object) Find

func (obj *Object) Find(key string) (*Value, bool)

Find returns the value at the key or nil if it doesn't exist and whether the key was in the object.

func (*Object) Length

func (obj *Object) Length() int

Length returns the number of elements in the object.

func (*Object) Range

func (obj *Object) Range(fn interface{}) *Object

Range iterates over the object's members. Range can take a set of functions matched by type. If the function returns a bool this is treated as a loop terminataion variable if false the loop will terminate.

func(Pair) iterates over Pairs
func(Pair) bool, called with a Pair, terminates the loop on false.
func(string, *Value) iterates over keys and values.
func(string, *Value) bool
func(string) iterates over only the keys
func(string) bool
func(*Value) iterates over only the values
func(*Value bool

func (*Object) String

func (obj *Object) String() string

String returns a string representation of the Object.

func (*Object) Transform

func (obj *Object) Transform(fn func(*TObject)) *Object

Transform executes the provided function against a mutable transient object to provide a faster, less memory intensive, object editing mechanism.

type Pair

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

Pair is a key/value pair. These are representations of the members of Objects per RFC7159.

func PairNew

func PairNew(key string, value interface{}) Pair

PairNew creates a new pair

func (Pair) Equal

func (p Pair) Equal(other interface{}) bool

Equal implements equality between Pairs.

func (Pair) Key

func (p Pair) Key() string

Key returns the key.

func (Pair) String

func (p Pair) String() string

String returns a string representation of the Pair.

func (Pair) Value

func (p Pair) Value() *Value

Value returns the value.

type SortOption

type SortOption func(*sortOpts)

SortOption is an option to the Array.Sort function

func Compare

func Compare(fn func(a, b *Value) int) SortOption

Compare takes a comparison function and returns a sort option A compare function takes two values and returns a trinary state as an integer. Less than zero indicates the first was less than the last, zero indicates the two values were equal, and greater than zero indicates that the first was greater than the last.

type String

type String string

String is a type that allows differentiation of functions that require a go string or an RFC7951 compatible string. It can be used with perform to unpack the value correctly depending on the desired semantics.

type TArray

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

TArray is a transient array that may be used to perform transformations on an array in a fast mutable fashion. This can only be accessed via the (*Array).Transform method. Care should be taken not to share this among threads as its values are mutable.

func (*TArray) Append

func (arr *TArray) Append(value interface{}) *TArray

Append adds a new value to the end of the array.

func (*TArray) Assoc

func (arr *TArray) Assoc(i int, v interface{}) *TArray

Assoc associates the value with the index in the array. If the index is out of bounds the array is padded to that index and the value is associated.

func (*TArray) At

func (arr *TArray) At(index int) *Value

At returns the value at the index of the array, if the index is out of bounds, nil is returned.

func (*TArray) Contains

func (arr *TArray) Contains(index int) bool

Contains returns whether the index is in the bounds of the array.

func (*TArray) Delete

func (arr *TArray) Delete(index int) *TArray

Delete removes an element at the supplied index from the array.

func (*TArray) Find

func (arr *TArray) Find(index int) (*Value, bool)

Find returns the value at the index or nil if it doesn't exist and whether the index was in the array.

func (*TArray) Length

func (arr *TArray) Length() int

Length returns the number of elements in the array.

func (*TArray) Range

func (arr *TArray) Range(fn interface{})

Range iterates over the object's members. Range can take a set of functions matched by type. If the function returns a bool this is treated as a loop terminataion variable if false the loop will terminate.

func(int, *Value) iterates over indicies and values.
func(int, *Value) bool
func(int) iterates over only the indicies
func(int) bool
func(*Value) iterates over only the values
func(*Value bool

func (*TArray) Sort

func (arr *TArray) Sort(options ...SortOption) *TArray

Sort sorts an array returning a new array that is sorted. by default sort will use dyn.Compare as the comparison operator this may be overridden using the Compare option.

func (*TArray) String

func (arr *TArray) String() string

String returns a string representation of the Array.

type TObject

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

TObject is a transient object that may be used to perform transformations on an object in a fast mutable fashion. This can only be accessed via the (*Object).Transform method. Care should be taken not to share this among threads as its values are mutable.

func (*TObject) Assoc

func (obj *TObject) Assoc(key string, value interface{}) *TObject

Assoc associates a new value with the key. The key may be either 'module:key' or just key if the module is the same as the containing object's module.

func (*TObject) At

func (obj *TObject) At(key string) *Value

At returns the Value at the key's location or nil if it doesn't exist. The key may be either 'module:key' or just key if the module is the same as the containing object's module.

func (*TObject) Contains

func (obj *TObject) Contains(key string) bool

Contains returns true if the key exists in the object. The key may be either 'module:key' or just key if the module is the same as the containing object's module.

func (*TObject) Delete

func (obj *TObject) Delete(key string) *TObject

Delete removes a key from the object. The key may be either 'module:key' or just key if the module is the same as the containing object's module.

func (*TObject) Equal

func (obj *TObject) Equal(other interface{}) bool

Equal implements equality for objects. An object is equal to another object if all their keys contains equal values. Equality checks are linear with respect to the number of keys.

func (*TObject) Find

func (obj *TObject) Find(key string) (*Value, bool)

Find returns the value at the key or nil if it doesn't exist and whether the key was in the object.

func (*TObject) Length

func (obj *TObject) Length() int

Length returns the number of elements in the object.

func (*TObject) Range

func (obj *TObject) Range(fn interface{})

Range iterates over the object's members. Range can take a set of functions matched by type. If the function returns a bool this is treated as a loop terminataion variable if false the loop will terminate.

func(Pair) iterates over Pairs
func(Pair) bool, called with a Pair, terminates the loop on false.
func(string, *Value) iterates over keys and values.
func(string, *Value) bool
func(string) iterates over only the keys
func(string) bool
func(*Value) iterates over only the values
func(*Value bool

func (*TObject) String

func (obj *TObject) String() string

String returns a string representation of the Object.

type Tree

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

Tree represents an RFC7951 tree, it is rooted at an object and provides additional functionallity on top of the object functionallity. Trees are indexed using instance-identifiers instead of single keys. Trees are immutable and any mutation operation will return a new structurally shared copy of the tree with the changes made. This allows for cheap copies of the tree and for it to be shared easily.

func TreeFromObject

func TreeFromObject(obj *Object) *Tree

TreeFromObject creates a tree rooted at the supplied object.

func TreeFromValue

func TreeFromValue(v *Value) *Tree

TreeFromValue creates a tree with a single member, 'rfc7951:data', in its root pointing to the supplied value.

func TreeNew

func TreeNew() *Tree

TreeNew creates a new empty tree

func (*Tree) Assoc

func (t *Tree) Assoc(instanceID string, value interface{}) *Tree

Assoc associates the value provided at the location pointed to by the instance-identifier.

func (*Tree) At

func (t *Tree) At(instanceID string) *Value

At returns the Value at the instance-idenfitifer provided.

func (*Tree) Contains

func (t *Tree) Contains(instanceID string) bool

Contains returns whether the instance-identifer points to a node in the tree.

func (*Tree) Delete

func (t *Tree) Delete(instanceID string) *Tree

Delete removes the instance-identifier from the tree.

func (*Tree) Diff

func (t *Tree) Diff(other *Tree) *EditOperation

Diff compares two trees and returns the operations required to edit the original to produce the other one.

func (*Tree) Edit

func (t *Tree) Edit(edit *EditOperation) *Tree

Edit applies an EditOperation to the tree. This allows for capturing large change sets as a piece of data than can be evaluated as tree operations and applied to the tree.

func (*Tree) Equal

func (t *Tree) Equal(other interface{}) bool

Equal implements equality for the tree. It compares the roots for equality.

func (*Tree) Find

func (t *Tree) Find(instanceID string) (*Value, bool)

Find returns the Value at the instance-identifier or nil if none, and whether the value is in the tree.

func (*Tree) Length

func (t *Tree) Length() int

Length returns the number of elements in the tree.

func (*Tree) MarshalRFC7951

func (t *Tree) MarshalRFC7951() ([]byte, error)

MarshalRFC7951 returns the Tree encoded as RFC7951 data.

func (*Tree) Merge

func (t *Tree) Merge(new *Tree) *Tree

Merge merges two trees together by recursively calling Merge on the roots.

func (*Tree) Range

func (t *Tree) Range(fn interface{}) *Tree

Range iterates over the Trees's paths. Range can take a set of functions matched by type. If the function returns a bool this is treated as a loop terminataion variable if false the loop will terminate.

func(*InstanceID, *Value) iterates over paths as an instance-identifier
                          and values.
func(*InstanceID, *Value) bool
func(string, *Value) iterates over paths and values.
func(string, *Value) bool
func(*InstanceID) iterates over only the paths as an instance-identifier
func(*InstanceID) bool
func(string) iterates over only the paths
func(string) bool
func(*Value) iterates over only the values
func(*Value) bool

func (*Tree) Root

func (t *Tree) Root() *Value

Root returns the tree's root Object as a Value.

func (*Tree) String

func (t *Tree) String() string

String returns a string representation of the tree.

func (*Tree) UnmarshalRFC7951

func (t *Tree) UnmarshalRFC7951(msg []byte) error

UnmarshalRFC7951 fills out the Tree from the RFC7951 encoded message. This can't be fully immutable, the caller has to ensure the array isn't used until unmarshal is finished.

type Value

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

Value is an RFC7951 value. Values may be *Object, *Array, *InstanceID, int32, int64, uint32, uint64, float64, string, bool, Empty or nil. All (u)integer types less than 32 are up-converted to a 32bit type when creating a value.

func Empty

func Empty() *Value

Empty returns the constant empty value

func ValueNew

func ValueNew(data interface{}) *Value

ValueNew turns a native go value into an RFC7951 Value as long as the type can be represented in RFC7951 encoding. ValueNew will panic if the value is not an RFC7951 compatible type.

func (*Value) AsArray

func (val *Value) AsArray() *Array

AsArray returns an *Array if the value is an Array and panics otherwise.

func (*Value) AsBoolean

func (val *Value) AsBoolean() bool

AsBoolean returns a bool if the value is a bool or if the value is Empty it returns true.

func (*Value) AsFloat

func (val *Value) AsFloat() float64

AsFloat returns an float64 if the type is convertable to float64 and panics otherwise.

func (*Value) AsInstanceID

func (val *Value) AsInstanceID() *InstanceID

AsInstanceID returns an instance-identifier if the type is string an attempt to parse the instance-identifier will be made.

func (*Value) AsInt32

func (val *Value) AsInt32() int32

AsInt32 returns an int32 if the type is convertable to int32 and panics otherwise.

func (*Value) AsInt64

func (val *Value) AsInt64() int64

AsInt64 returns an int64 if the type is convertable to int64 and panics otherwise.

func (*Value) AsObject

func (val *Value) AsObject() *Object

AsObject returns an *Object if the value is an Object and panics otherwise.

func (*Value) AsString

func (val *Value) AsString() string

AsString returns an string if the value is an String and panics otherwise.

func (*Value) AsUint32

func (val *Value) AsUint32() uint32

AsUint32 returns an uint32 if the type is convertable to uint32 and panics otherwise.

func (*Value) AsUint64

func (val *Value) AsUint64() uint64

AsUint64 returns an uint64 if the type is convertable to uint64 and panics otherwise.

func (*Value) Compare

func (val *Value) Compare(other interface{}) int

Compare provides an implementation of Comparison for Value types.

func (*Value) Equal

func (val *Value) Equal(other interface{}) bool

Equal provides an implementation of Equality for Value types.

func (*Value) IsArray

func (val *Value) IsArray() bool

IsArray returns if the data stored in the value is an Array.

func (*Value) IsBoolean

func (val *Value) IsBoolean() bool

IsBoolean returns if the value is an bool

func (*Value) IsEmpty

func (val *Value) IsEmpty() bool

IsEmpty returns whether a node is the Empty node or not.

func (*Value) IsFloat

func (val *Value) IsFloat() bool

IsFloat returns if the value is an float

func (*Value) IsInstanceID

func (val *Value) IsInstanceID() bool

IsInstanceID returns whether the value is an instance-identifier.

func (*Value) IsInt32

func (val *Value) IsInt32() bool

IsInt32 returns if the value is an int32

func (*Value) IsInt64

func (val *Value) IsInt64() bool

IsInt64 returns if the value is an int64

func (*Value) IsNull

func (val *Value) IsNull() bool

IsNull returns whether the values data is nil.

func (*Value) IsObject

func (val *Value) IsObject() bool

IsObject returns if the data stored in the value is an Object.

func (*Value) IsString

func (val *Value) IsString() bool

IsString returns if the data stored in the value is an String.

func (*Value) IsUint32

func (val *Value) IsUint32() bool

IsUint32 returns if the value is an uint32

func (*Value) IsUint64

func (val *Value) IsUint64() bool

IsUint64 returns if the value is an uint64

func (*Value) MarshalRFC7951

func (val *Value) MarshalRFC7951() ([]byte, error)

MarshalRFC7951 returns the value encoded in an RFC7951 compatible way.

func (*Value) Merge

func (val *Value) Merge(new *Value) *Value

Merge will combine the old value with the new value and return the result.

func (*Value) Perform

func (val *Value) Perform(fns ...interface{}) interface{}

Perform allows one to match the type of the Value with a behavior to perform on that type without resulting to the assertion operations. Think of this as the switch v.(type) { ... } analogue for RFC7951 types. It takes a list of func(v vT) oT functions and applies the first match to the value.

If vT above is *Value, String, or interface{} it matches all value types. If it is String then RFC7951String is called on the value first. If the value is a numeric type and the numeric type is convertable to vT then that is considered a match and the conversion is applied first, this is not go's standard ConvertibleTo however, only uint32 <-> int32 and uint64 <-> int64 are supported and only if the values fit.

func (*Value) RFC7951String

func (val *Value) RFC7951String() string

RFC7951String converts the object to a string that can be encoded in RFC7951 format. This may be different than what String returns so interface { RFC7951String() string } may be implemented to override the behavior here.

func (*Value) String

func (val *Value) String() string

String returns a go string representation of the Value.

func (*Value) ToArray

func (val *Value) ToArray(defaultVal ...*Array) *Array

ToArray returns an *Array and allows the user to define a default. The value (*Array)(nil) is returned if no default is defined and the value is not an *Array.

func (*Value) ToBoolean

func (val *Value) ToBoolean(defaultVal ...bool) bool

ToBoolean returns an bool if the type is convertable to bool and returns the user supplied default or false otherwise.

func (*Value) ToData

func (val *Value) ToData() interface{}

ToData returns the held data as a value that can easily be used with standard library packages such as text/template.

Example
tree := TreeFromObject(TESTOBJ)
const test = `
{{- range (.At "/module-v1:nested/list").ToData -}}
{{with .ToObject -}}
{{.At "key"}} {{.At "objleaf"}}
{{end -}}
{{end -}}
{{range (.At "/module-v1:nested/leaf-list").ToData -}}
{{.}}
{{end -}}
`
testTmpl := template.Must(template.New("test").Parse(test))
testTmpl.Execute(os.Stdout, tree)
Output:

foo bar
bar baz
baz quux
quux quuz
1
2
3
4
5
6
7

func (*Value) ToFloat

func (val *Value) ToFloat(defaultVal ...float64) float64

ToFloat returns an float64 if the type is convertable to float64 and returns the user supplied default or 0 otherwise.

func (*Value) ToInstanceID

func (val *Value) ToInstanceID(defaultVal ...*InstanceID) *InstanceID

ToInstanceID returns an *InstanceID and allows the user to define a default. The value (*InstanceID)(nil) is returned if no default is defined and the value is not an *InstanceID.

func (*Value) ToInt32

func (val *Value) ToInt32(defaultVal ...int32) int32

ToInt32 returns an int32 if the type is convertable to int32 and returns the user supplied default or 0 otherwise.

func (*Value) ToInt64

func (val *Value) ToInt64(defaultVal ...int64) int64

ToInt64 returns an int64 if the type is convertable to int64 and returns the user supplied default or 0 otherwise.

func (*Value) ToInterface

func (val *Value) ToInterface() interface{}

ToInterface returns the held data directly as a native interface. Caution should be used as the integer types may not be the same as the type that was passed into the value due to the way they are stored internally. For instance all positive integer values are stored as uint32 value and the same goes for int64 and uint64.

func (*Value) ToNative

func (val *Value) ToNative() interface{}

ToNative converts a value to a go native type. It is not recommended that this is used as the integer types may not be what you expect we store integers in a specific way to ensure the marshaller works consistently and it may be different than the type that was inserted.

func (*Value) ToObject

func (val *Value) ToObject(defaultVal ...*Object) *Object

ToObject returns an *Object and allows the user to define a default. The value (*Object)(nil) is returned if no default is defined and the value is not an *Object.

func (*Value) ToString

func (val *Value) ToString(defaultVal ...string) string

ToString returns an string and allows the user to define a default. The value "" is returned if no default is defined and the value is not an string.

func (*Value) ToTree

func (val *Value) ToTree() *Tree

ToTree returns a *Tree if the value is an Object and panics otherwise.

func (*Value) ToUint32

func (val *Value) ToUint32(defaultVal ...uint32) uint32

ToUint32 returns an uint32 if the type is convertable to uint32 and returns the user supplied default or 0 otherwise.

func (*Value) ToUint64

func (val *Value) ToUint64(defaultVal ...uint64) uint64

ToUint64 returns an uint64 if the type is convertable to uint64 and returns the user supplied default or 0 otherwise.

func (*Value) UnmarshalRFC7951

func (val *Value) UnmarshalRFC7951(msg []byte) error

UnmarshalRFC7951 extracts a value from an rfc7951 encoded value.

Jump to

Keyboard shortcuts

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