value

package
v0.0.0-...-d88c8b5 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2021 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package value defines types for an in-memory representation of yaml or json objects, organized for convenient comparison with a schema (as defined by the sibling schema package). Functions for reading and writing the objects are also provided.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Boolean

type Boolean bool

func (Boolean) Compare

func (b Boolean) Compare(rhs Boolean) int

Compare compares booleans. The result will be 0 if b==rhs, -1 if b < rhs, and +1 if b > rhs.

type Field

type Field struct {
	Name  string
	Value Value
}

Field is an individual key-value pair.

type Float

type Float float64

func (Float) Compare

func (f Float) Compare(rhs Float) int

Compare compares floats. The result will be 0 if f==rhs, -1 if f < rhs, and +1 if f > rhs.

type Int

type Int int64

func (Int) Compare

func (i Int) Compare(rhs Int) int

Compare compares integers. The result will be 0 if i==rhs, -1 if i < rhs, and +1 if i > rhs.

type List

type List struct {
	Items []Value
}

List is a list of items.

func (*List) Compare

func (l *List) Compare(rhs *List) int

Compare compares two lists lexically. The result will be 0 if l==rhs, -1 if l < rhs, and +1 if l > rhs.

func (*List) Equals

func (l *List) Equals(rhs *List) bool

Equals compares two lists lexically.

func (*List) Less

func (l *List) Less(rhs *List) bool

Less compares two lists lexically.

type Map

type Map struct {
	Items []Field
	// contains filtered or unexported fields
}

Map is a map of key-value pairs. It represents both structs and maps. We use a list and a go-language map to preserve order.

Set and Get helpers are provided.

func (*Map) Compare

func (m *Map) Compare(rhs *Map) int

Compare compares two maps lexically.

func (*Map) Delete

func (m *Map) Delete(key string)

Delete removes the key from the set.

func (*Map) Equals

func (m *Map) Equals(rhs *Map) bool

Equals compares two maps lexically.

func (*Map) Get

func (m *Map) Get(key string) (*Field, bool)

Get returns the (Field, true) or (nil, false) if it is not present

func (*Map) Less

func (m *Map) Less(rhs *Map) bool

Less compares two maps lexically.

func (*Map) Set

func (m *Map) Set(key string, value Value)

Set inserts or updates the given item.

type String

type String string

type Value

type Value struct {
	// Exactly one of the below must be set.
	FloatValue   *Float
	IntValue     *Int
	StringValue  *String
	BooleanValue *Boolean
	ListValue    *List
	MapValue     *Map
	Null         bool // represents an explicit `"foo" = null`
}

A Value is an object; it corresponds to an 'atom' in the schema.

func BooleanValue

func BooleanValue(b bool) Value

BooleanValue returns b as a scalar boolean Value.

func FloatValue

func FloatValue(f float64) Value

FloatValue returns f as a scalar numeric (float) Value.

func FromJSON

func FromJSON(input []byte) (Value, error)

FromJSON is a helper function for reading a JSON document

func FromJSONFast

func FromJSONFast(input []byte) (Value, error)

FromJSONFast is a helper function for reading a JSON document

func FromUnstructured

func FromUnstructured(in interface{}) (Value, error)

FromUnstructured will convert a go interface to a Value. It's most commonly expected to be used with map[string]interface{} as the input. `in` must not have any structures with cycles in them. yaml.MapSlice may be used for order-preservation.

func FromYAML

func FromYAML(input []byte) (Value, error)

FromYAML is a helper function for reading a YAML document; it attempts to preserve order of keys within maps/structs. This is as a convenience to humans keeping YAML documents, not because there is a behavior difference.

Known bug: objects with top-level arrays don't parse correctly.

func IntValue

func IntValue(i int) Value

IntValue returns i as a scalar numeric (integer) Value.

func ReadJSONIter

func ReadJSONIter(iter *jsoniter.Iterator) (Value, error)

func StringValue

func StringValue(s string) Value

StringValue returns s as a scalar string Value.

func (Value) Compare

func (v Value) Compare(rhs Value) int

Compare provides a total ordering for Value (so that they can be sorted, even if they are of different types). The result will be 0 if v==rhs, -1 if v < rhs, and +1 if v > rhs.

func (Value) Equals

func (v Value) Equals(rhs Value) bool

Equals returns true iff the two values are equal.

func (Value) Less

func (v Value) Less(rhs Value) bool

Less provides a total ordering for Value (so that they can be sorted, even if they are of different types).

func (Value) String

func (v Value) String() string

String returns a human-readable representation of the value.

func (*Value) ToJSON

func (v *Value) ToJSON() ([]byte, error)

ToJSON is a helper function for producing a JSon document.

func (*Value) ToJSONFast

func (v *Value) ToJSONFast() ([]byte, error)

ToJSONFast is a helper function for producing a JSon document.

func (*Value) ToUnstructured

func (v *Value) ToUnstructured(preserveOrder bool) interface{}

ToUnstructured will convert the Value into a go-typed object. If preserveOrder is true, then maps will be converted to the yaml.MapSlice type. Otherwise, map[string]interface{} must be used-- this destroys ordering information and is not recommended if the result of this will be serialized. Other types: * list -> []interface{} * others -> corresponding go type, wrapped in an interface{}

Of note, floats and ints will always come out as float64 and int64, respectively.

func (*Value) ToYAML

func (v *Value) ToYAML() ([]byte, error)

ToYAML is a helper function for producing a YAML document; it attempts to preserve order of keys within maps/structs. This is as a convenience to humans keeping YAML documents, not because there is a behavior difference.

func (*Value) WriteJSONStream

func (v *Value) WriteJSONStream(stream *jsoniter.Stream)

Jump to

Keyboard shortcuts

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