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 ¶
- type Array
- func (arr *Array) Append(value interface{}) *Array
- func (arr *Array) Assoc(index int, value interface{}) *Array
- func (arr *Array) At(index int) *Value
- func (arr *Array) Contains(index int) bool
- func (arr *Array) Delete(index int) *Array
- func (arr *Array) Equal(other interface{}) bool
- func (arr *Array) Find(index int) (*Value, bool)
- func (arr *Array) Length() int
- func (arr *Array) Range(fn interface{}) *Array
- func (arr *Array) Sort(options ...SortOption) *Array
- func (arr *Array) String() string
- func (arr *Array) Transform(fn func(*TArray)) *Array
- type EditAction
- type EditEntry
- type EditEntryOption
- type EditOperation
- type InstanceID
- func (i *InstanceID) Equal(other interface{}) bool
- func (i *InstanceID) Find(value *Value) (*Value, bool)
- func (i *InstanceID) MarshalRFC7951() ([]byte, error)
- func (i *InstanceID) MatchAgainst(value *Value) *Value
- func (i *InstanceID) RFC7951String() string
- func (i *InstanceID) String() string
- func (i *InstanceID) UnmarshalRFC7951(msg []byte) (err error)
- type Object
- func (obj *Object) Assoc(key string, value interface{}) *Object
- func (obj *Object) At(key string) *Value
- func (obj *Object) Contains(key string) bool
- func (obj *Object) Delete(key string) *Object
- func (obj *Object) Equal(other interface{}) bool
- func (obj *Object) Find(key string) (*Value, bool)
- func (obj *Object) Length() int
- func (obj *Object) Range(fn interface{}) *Object
- func (obj *Object) String() string
- func (obj *Object) Transform(fn func(*TObject)) *Object
- type Pair
- type SortOption
- type String
- type TArray
- func (arr *TArray) Append(value interface{}) *TArray
- func (arr *TArray) Assoc(i int, v interface{}) *TArray
- func (arr *TArray) At(index int) *Value
- func (arr *TArray) Contains(index int) bool
- func (arr *TArray) Delete(index int) *TArray
- func (arr *TArray) Find(index int) (*Value, bool)
- func (arr *TArray) Length() int
- func (arr *TArray) Range(fn interface{})
- func (arr *TArray) Sort(options ...SortOption) *TArray
- func (arr *TArray) String() string
- type TObject
- func (obj *TObject) Assoc(key string, value interface{}) *TObject
- func (obj *TObject) At(key string) *Value
- func (obj *TObject) Contains(key string) bool
- func (obj *TObject) Delete(key string) *TObject
- func (obj *TObject) Equal(other interface{}) bool
- func (obj *TObject) Find(key string) (*Value, bool)
- func (obj *TObject) Length() int
- func (obj *TObject) Range(fn interface{})
- func (obj *TObject) String() string
- type Tree
- func (t *Tree) Assoc(instanceID string, value interface{}) *Tree
- func (t *Tree) At(instanceID string) *Value
- func (t *Tree) Contains(instanceID string) bool
- func (t *Tree) Delete(instanceID string) *Tree
- func (t *Tree) Diff(other *Tree) *EditOperation
- func (t *Tree) Edit(edit *EditOperation) *Tree
- func (t *Tree) Equal(other interface{}) bool
- func (t *Tree) Find(instanceID string) (*Value, bool)
- func (t *Tree) Length() int
- func (t *Tree) MarshalRFC7951() ([]byte, error)
- func (t *Tree) Merge(new *Tree) *Tree
- func (t *Tree) Range(fn interface{}) *Tree
- func (t *Tree) Root() *Value
- func (t *Tree) String() string
- func (t *Tree) UnmarshalRFC7951(msg []byte) error
- type Value
- func (val *Value) AsArray() *Array
- func (val *Value) AsBoolean() bool
- func (val *Value) AsFloat() float64
- func (val *Value) AsInstanceID() *InstanceID
- func (val *Value) AsInt32() int32
- func (val *Value) AsInt64() int64
- func (val *Value) AsObject() *Object
- func (val *Value) AsString() string
- func (val *Value) AsUint32() uint32
- func (val *Value) AsUint64() uint64
- func (val *Value) Compare(other interface{}) int
- func (val *Value) Equal(other interface{}) bool
- func (val *Value) IsArray() bool
- func (val *Value) IsBoolean() bool
- func (val *Value) IsEmpty() bool
- func (val *Value) IsFloat() bool
- func (val *Value) IsInstanceID() bool
- func (val *Value) IsInt32() bool
- func (val *Value) IsInt64() bool
- func (val *Value) IsNull() bool
- func (val *Value) IsObject() bool
- func (val *Value) IsString() bool
- func (val *Value) IsUint32() bool
- func (val *Value) IsUint64() bool
- func (val *Value) MarshalRFC7951() ([]byte, error)
- func (val *Value) Merge(new *Value) *Value
- func (val *Value) Perform(fns ...interface{}) interface{}
- func (val *Value) RFC7951String() string
- func (val *Value) String() string
- func (val *Value) ToArray(defaultVal ...*Array) *Array
- func (val *Value) ToBoolean(defaultVal ...bool) bool
- func (val *Value) ToData() interface{}
- func (val *Value) ToFloat(defaultVal ...float64) float64
- func (val *Value) ToInstanceID(defaultVal ...*InstanceID) *InstanceID
- func (val *Value) ToInt32(defaultVal ...int32) int32
- func (val *Value) ToInt64(defaultVal ...int64) int64
- func (val *Value) ToInterface() interface{}
- func (val *Value) ToNative() interface{}
- func (val *Value) ToObject(defaultVal ...*Object) *Object
- func (val *Value) ToString(defaultVal ...string) string
- func (val *Value) ToTree() *Tree
- func (val *Value) ToUint32(defaultVal ...uint32) uint32
- func (val *Value) ToUint64(defaultVal ...uint64) uint64
- func (val *Value) UnmarshalRFC7951(msg []byte) error
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) Assoc ¶
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 ¶
At returns the value at the index of the array, if the index is out of bounds, nil is returned.
func (*Array) Equal ¶
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 ¶
Find returns the value at the index or nil if it doesn't exist and whether the index was in the array.
func (*Array) Range ¶
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.
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 ¶
ObjectFrom creates a new object and then populates it with the data from the supplied map
func ObjectWith ¶
ObjectWith creates a new object and then populates it with the supplied pairs
func (*Object) Assoc ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
Find returns the value at the key or nil if it doesn't exist and whether the key was in the object.
func (*Object) Range ¶
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
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.
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) Assoc ¶
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 ¶
At returns the value at the index of the array, if the index is out of bounds, nil is returned.
func (*TArray) Find ¶
Find returns the value at the index or nil if it doesn't exist and whether the index was 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.
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
Find returns the value at the key or nil if it doesn't exist and whether the key was 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
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 ¶
TreeFromObject creates a tree rooted at the supplied object.
func TreeFromValue ¶
TreeFromValue creates a tree with a single member, 'rfc7951:data', in its root pointing to the supplied value.
func (*Tree) Assoc ¶
Assoc associates the value provided at the location pointed to by the instance-identifier.
func (*Tree) Contains ¶
Contains returns whether the instance-identifer points to a node in 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) Find ¶
Find returns the Value at the instance-identifier or nil if none, and whether the value is in the tree.
func (*Tree) MarshalRFC7951 ¶
MarshalRFC7951 returns the Tree encoded as RFC7951 data.
func (*Tree) Range ¶
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) UnmarshalRFC7951 ¶
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 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) AsBoolean ¶
AsBoolean returns a bool if the value is a bool or if the value is Empty it returns true.
func (*Value) AsFloat ¶
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 ¶
AsInt32 returns an int32 if the type is convertable to int32 and panics otherwise.
func (*Value) AsInt64 ¶
AsInt64 returns an int64 if the type is convertable to int64 and panics otherwise.
func (*Value) AsObject ¶
AsObject returns an *Object if the value is an Object and panics otherwise.
func (*Value) AsUint32 ¶
AsUint32 returns an uint32 if the type is convertable to uint32 and panics otherwise.
func (*Value) AsUint64 ¶
AsUint64 returns an uint64 if the type is convertable to uint64 and panics otherwise.
func (*Value) IsInstanceID ¶
IsInstanceID returns whether the value is an instance-identifier.
func (*Value) MarshalRFC7951 ¶
MarshalRFC7951 returns the value encoded in an RFC7951 compatible way.
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 ¶
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) ToArray ¶
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 ¶
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 ¶
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 ¶
ToInt32 returns an int32 if the type is convertable to int32 and returns the user supplied default or 0 otherwise.
func (*Value) ToInt64 ¶
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 ¶
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 ¶
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) ToUint32 ¶
ToUint32 returns an uint32 if the type is convertable to uint32 and returns the user supplied default or 0 otherwise.
func (*Value) ToUint64 ¶
ToUint64 returns an uint64 if the type is convertable to uint64 and returns the user supplied default or 0 otherwise.
func (*Value) UnmarshalRFC7951 ¶
UnmarshalRFC7951 extracts a value from an rfc7951 encoded value.