ard

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2020 License: Apache-2.0 Imports: 11 Imported by: 0

README

Agnostic Raw Data (ARD)

What is "agnostic raw data"?

Agnostic?

Comprising primitives (string, integer, float, boolean, null) and structures (map, list). It's agnostic because it can be trivially represented in practically any language or platform, and also because it can be transmitted in a wide variety of formats.

Note that some formats present limitations:

YAML

YAML supports a rich set of primitive types, so ARD will survive a round trip to YAML.

One difference is that YAML maps can be ordered (!!omap vs. !!map) but ARD maps have arbitrary order (always !!map) for widest compatibility. A round trip from YAML to ARD would thus lose order.

YAML allows for maps with arbitrary keys. This is non-trivial to support in Go, and so we provide special functions (MapGet, MapPut, MapDelete, MapMerge) that replace the Go native functionality with additional support for detecting and handling complex keys. This feature is provided as an independent library, yamlkeys.

JSON

JSON can be read into ARD.

However, because JSON has fewer types and more limitations than YAML (no integers, only floats; map keys can only be string), ARD will lose some type information when translated into JSON.

This could be fixed if we extend JSON with some conventions for encoding extra types. See MongoDB Extended JSON for an example. Also, maps with complex keys could be encoded as key-value pair lists.

XML

XML does not have a type system. Arbitrary XML cannot be parsed into ARD.

However, with a proper schema and custom reader this could be implemented in the future.

Raw?

The data is untreated and not validated. There's no schema.

Documentation

Index

Constants

View Source
const (
	FieldPathType = iota
	MapPathType
	ListPathType
	SequencedListPathType
)

Variables

View Source
var NoNode = &Node{nil, nil, ""}
View Source
var TypeValidators = map[string]TypeValidator{

	"!!map": IsMap,
	"!!seq": IsList,
	"!!str": IsString,

	"!!bool":  IsBool,
	"!!int":   IsInteger,
	"!!float": IsFloat,

	"!!null":      IsNull,
	"!!timestamp": IsTime,
}
View Source
var TypeZeroes = map[string]Value{
	"!!map":       make(Map),
	"!!seq":       List{},
	"!!str":       "",
	"!!bool":      false,
	"!!int":       int(0),
	"!!float":     float64(0.0),
	"!!null":      nil,
	"!!timestamp": time.Time{},
}

Functions

func FindYAMLNode

func FindYAMLNode(node *yaml.Node, path ...PathElement) *yaml.Node

func IsBool added in v0.12.0

func IsBool(value Value) bool

bool

func IsFloat added in v0.12.0

func IsFloat(value Value) bool

float64, float32

func IsInteger added in v0.12.0

func IsInteger(value Value) bool

int64, int32, int16, int8, int, uint64, uint32, uint16, uint8, uint

func IsList added in v0.12.0

func IsList(value Value) bool

List = []interface{}

func IsMap added in v0.12.0

func IsMap(value Value) bool

Map = map[interface{}]interface{}

func IsNull added in v0.14.0

func IsNull(value Value) bool

func IsString added in v0.12.0

func IsString(value Value) bool

string

func IsTime added in v0.12.0

func IsTime(value Value) bool

time.Time

func MergeMaps

func MergeMaps(target Map, source Map, mergeLists bool)

func Read

func Read(reader io.Reader, format string, locate bool) (Map, Locator, error)

func ReadFromURL

func ReadFromURL(url urlpkg.URL, locate bool) (Map, Locator, error)

func ReadJSON

func ReadJSON(reader io.Reader, locate bool) (Map, Locator, error)

func ReadYAML

func ReadYAML(reader io.Reader, locate bool) (Map, Locator, error)

func StringMapPutNested

func StringMapPutNested(map_ StringMap, key string, value string) error

TODO: use Node instead

func ToYAMLDocumentNode added in v0.12.0

func ToYAMLDocumentNode(value Value, verbose bool) *yaml.Node

func ToYAMLNode added in v0.12.0

func ToYAMLNode(value Value, verbose bool) *yaml.Node

func TypeName added in v0.12.0

func TypeName(value Value) string

Types

type List

type List = []Value

Note: This is just a convenient alias, *not* a type. An extra type would ensure more strictness but would make life more complicated than it needs to be. That said, if we *do* want to make this into a type, we need to make sure not to add any methods to the type, otherwise the goja JavaScript engine will treat it as a host object instead of a regular JavaScript dict object.

type Locator

type Locator interface {
	Locate(path ...PathElement) (int, int, bool)
}

type Map

type Map = map[Value]Value

Note: This is just a convenient alias, *not* a type. An extra type would ensure more strictness but would make life more complicated than it needs to be. That said, if we *do* want to make this into a type, we need to make sure not to add any methods to the type, otherwise the goja JavaScript engine will treat it as a host object instead of a regular JavaScript dict object.

func EnsureMaps

func EnsureMaps(map_ Value) Map

func ToMap

func ToMap(stringMap StringMap) Map

type Node added in v0.14.0

type Node struct {
	Data interface{}
	// contains filtered or unexported fields
}

func NewNode added in v0.14.0

func NewNode(data interface{}) *Node

func (*Node) Append added in v0.14.0

func (self *Node) Append(value interface{}) bool

func (*Node) Boolean added in v0.14.0

func (self *Node) Boolean(allowNil bool) (bool, bool)

func (*Node) Float added in v0.14.0

func (self *Node) Float(allowNil bool) (float64, bool)

func (*Node) Get added in v0.14.0

func (self *Node) Get(key string) *Node

func (*Node) Integer added in v0.14.0

func (self *Node) Integer(allowNil bool) (int64, bool)

func (*Node) List added in v0.14.0

func (self *Node) List(allowNil bool) (List, bool)

func (*Node) Map added in v0.14.0

func (self *Node) Map(allowNil bool) (Map, bool)

func (*Node) Put added in v0.14.0

func (self *Node) Put(key string, value interface{}) bool

func (*Node) String added in v0.14.0

func (self *Node) String(allowNil bool) (string, bool)

func (*Node) StringMap added in v0.14.0

func (self *Node) StringMap(allowNil bool) (StringMap, bool)

func (*Node) UnsignedInteger added in v0.14.0

func (self *Node) UnsignedInteger(allowNil bool) (uint64, bool)

type Path

type Path []PathElement

func (Path) Append added in v0.13.0

func (self Path) Append(element PathElement) Path

func (Path) AppendField added in v0.13.0

func (self Path) AppendField(name string) Path

func (Path) AppendList added in v0.13.0

func (self Path) AppendList(index int) Path

func (Path) AppendMap added in v0.13.0

func (self Path) AppendMap(name string) Path

func (Path) AppendSequencedList added in v0.14.0

func (self Path) AppendSequencedList(index int) Path

func (Path) String

func (self Path) String() string

fmt.Stringer interface

type PathElement

type PathElement struct {
	Type  PathElementType
	Value interface{} // string for FieldPathType and MapPathType, int for ListPathType and SequencedListPathType
}

func NewFieldPathElement

func NewFieldPathElement(name string) PathElement

func NewListPathElement

func NewListPathElement(index int) PathElement

func NewMapPathElement

func NewMapPathElement(name string) PathElement

func NewSequencedListPathElement added in v0.14.0

func NewSequencedListPathElement(index int) PathElement

type PathElementType added in v0.14.0

type PathElementType uint8

type StringMap

type StringMap = map[string]Value

func EnsureStringMaps

func EnsureStringMaps(map_ Value) StringMap

func ToStringMap

func ToStringMap(map_ Map) StringMap

type TypeValidator added in v0.12.0

type TypeValidator = func(Value) bool

type Value added in v0.14.0

type Value = interface{}

func Copy

func Copy(value Value) Value

func ToMaps

func ToMaps(value Value) (Value, bool)

func ToStringMaps

func ToStringMaps(value Value) (Value, bool)

type YAMLLocator

type YAMLLocator struct {
	RootNode *yaml.Node
}

func NewYAMLLocator

func NewYAMLLocator(rootNode *yaml.Node) *YAMLLocator

func (*YAMLLocator) Locate

func (self *YAMLLocator) Locate(path ...PathElement) (int, int, bool)

Locator interface

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳