data

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2017 License: MIT Imports: 17 Imported by: 126

Documentation

Index

Constants

View Source
const (
	// MaxConvFloat64 is the largest float64 that can be converted to int64.
	MaxConvFloat64 = float64(math.MaxInt64)

	// MinConvFloat64 is the smallest float64 that can be converted to int64
	MinConvFloat64 = float64(math.MinInt64)
)
View Source
const MaxInt = int(^uint(0) >> 1)

MaxInt is a maximum value of an integer on 32-bit or 64-bit environment.

Variables

This section is empty.

Functions

func AsBlob

func AsBlob(v Value) ([]byte, error)

AsBlob returns an array of bytes only when the type of Value is TypeBlob, otherwise it returns error.

func AsBool

func AsBool(v Value) (bool, error)

AsBool returns a bool value only when the type of Value is TypeBool, otherwise it returns error.

func AsFloat

func AsFloat(v Value) (float64, error)

AsFloat returns a float value only when the type of Value is TypeFloat, otherwise it returns error.

func AsInt

func AsInt(v Value) (int64, error)

AsInt returns an integer value only when the type of Value is TypeInt, otherwise it returns error.

func AsString

func AsString(v Value) (string, error)

AsString returns a string only when the type of Value is TypeString, otherwise it returns error.

func AsTimestamp

func AsTimestamp(v Value) (time.Time, error)

AsTimestamp returns a time.Time only when the type of Value is TypeTime, otherwise it returns error.

func Decode added in v0.6.0

func Decode(m Map, v interface{}) error

Decode decodes a Map into a struct. The argument must be a pointer to a struct.

func Equal

func Equal(v1 Value, v2 Value) bool

Equal tests equality of two Values. When comparing a Float and an Int, Int is implicitly converted to float so that they can be true when the Float has an integer value. For example, Equal(Float(2.0), Int(2)) is true.

If either one is NaN, Equal always returns false. Equal(Null, Null) is true although this is inconsistent with the three-valued logic.

func Less

func Less(v1 Value, v2 Value) bool

Less computes whether v1 is less than v2 in a way consistent with `Equal`. It can be used, for example, with the functions of the sort package. The rules for sorting are as follows:

  • When the types are different: Null < Bool < Int/Float < String < Blob < Timestamp < Array < Map
  • When the type is the same:
  • Null: always false
  • Bool: false < true
  • Int/Float: usual < comparison; Ints and Floats can also be compared
  • String: usual < comparison
  • Timestamp: value as returned by Time.Before()
  • Blob, Array, Map: shorter collections are less than longer collections; when length is equal hash values are compared

func MarshalMsgpack

func MarshalMsgpack(m Map) ([]byte, error)

MarshalMsgpack returns a byte array encoded by msgpack serialization from a Map object. Returns an error when msgpack serialization failed.

func NewIMap

func NewIMap(m Map) map[string]interface{}

NewIMap returns a map[string]interface{} object from Map.

func Summarize

func Summarize(val Value) string

Summarize summarizes a Value as a JSON-like string. It may truncate or filter out the content of strings and blobs. The result may not be able to parsed as a JSON.

It'll support the max depth of a map, the max number of fields of a map to be rendered, or the max number of elements in an array, and so on.

func ToBlob

func ToBlob(v Value) ([]byte, error)

ToBlob converts a given Value to []byte, if possible. The conversion rules are as follows:

  • Null: nil
  • String: []byte just copied from string
  • Blob: actual value
  • other: (error)

func ToBool

func ToBool(v Value) (bool, error)

ToBool converts a given Value to a bool, if possible. The conversion rules are similar to those in Python:

  • Null: false
  • Bool: actual boolean value
  • Int: true if non-zero
  • Float: true if non-zero and not NaN
  • String: true if non-empty
  • Blob: true if non-empty
  • Timestamp: true if IsZero() is false
  • Array: true if non-empty
  • Map: true if non-empty

func ToDuration

func ToDuration(v Value) (time.Duration, error)

ToDuration converts a Value to time.Duration, if possible. The conversion rules are as follows:

  • Null: 0
  • Int: Converted to seconds (e.g. 3 is equal to 3 seconds)
  • Float: Converted to seconds (e.g. 3.141592 equals 3s + 141ms + 592us)
  • String: time.ParseDuration will be called
  • other: (error)

func ToFloat

func ToFloat(v Value) (float64, error)

ToFloat converts a given Value to a float64, if possible. The conversion rules are as follows:

  • Null: 0.0
  • Bool: 0.0 if false, 1.0 if true
  • Int: conversion as done by float64(value)
  • Float: actual value
  • String: parsed float as per strconv.ParseFloat (values outside of valid float64 bounds will lead to an error)
  • Blob: (error)
  • Timestamp: the number of seconds (not microseconds!) elapsed since January 1, 1970 UTC, with a decimal part
  • Array: (error)
  • Map: (error)

func ToInt

func ToInt(v Value) (int64, error)

ToInt converts a given Value to an int64, if possible. The conversion rules are as follows:

  • Null: 0
  • Bool: 0 if false, 1 if true
  • Int: actual value
  • Float: conversion as done by int64(value) (values outside of valid int64 bounds will lead to an error)
  • String: parsed integer with base 0 as per strconv.ParseInt (values outside of valid int64 bounds will lead to an error)
  • Blob: (error)
  • Timestamp: the number of second elapsed since January 1, 1970 UTC.
  • Array: (error)
  • Map: (error)

func ToString

func ToString(v Value) (string, error)

ToString converts a given Value to a string. The conversion rules are as follows:

  • Null: ""
  • String: the actual string
  • Blob: base64-encoded string
  • Timestamp: ISO 8601 representation, see time.RFC3339
  • other: Go's "%#v" representation

func ToTimestamp

func ToTimestamp(v Value) (time.Time, error)

ToTimestamp converts a given Value to a time.Time struct, if possible. The conversion rules are as follows:

  • Null: zero time (this is *not* the time with Unix time 0!)
  • Int: Time with the given Unix time in seconds
  • Float: Time with the given Unix time in seconds, where the decimal part will be considered as a part of a second (values outside of valid int64 bounds will lead to an error)
  • String: Time with the given RFC3339/ISO8601 representation
  • Timestamp: actual time
  • other: (error)

Types

type Array

type Array []Value

Array is an array of Values. It can be assigned to Value interface.

func AsArray

func AsArray(v Value) (Array, error)

AsArray returns an array of Values only when the type of Value is TypeArray, otherwise it returns error.

func NewArray

func NewArray(a []interface{}) (Array, error)

NewArray returns a Array object from []interface{}. Returns an error when value type is not supported in SensorBee.

func (Array) Copy added in v0.7.0

func (a Array) Copy() Array

Copy performs deep copy of an Array. The Array returned from this method can safely be modified without affecting the original.

func (Array) String

func (a Array) String() string

String returns JSON representation of an Array.

func (Array) Type

func (a Array) Type() TypeID

Type returns TypeID of Array. It's always TypeArray.

func (*Array) UnmarshalJSON

func (a *Array) UnmarshalJSON(data []byte) error

UnmarshalJSON reconstructs an Array from JSON.

type Blob

type Blob []byte

Blob is a binary large object which may have any type of byte data. It can be assigned to Value interface.

func (Blob) String

func (b Blob) String() string

Stringreturns JSON representation of a Blob. Blob is marshaled as a string.

func (Blob) Type

func (b Blob) Type() TypeID

Type returns TypeID of Blob. It's always TypeBlob.

type Bool

type Bool bool

Bool is a boolean value. It can be assigned to Value interface.

const (
	// True is a constant having true value of Bool type.
	True Bool = true

	// False is a constant having false value of Bool type.
	False Bool = false
)

func (Bool) String

func (b Bool) String() string

String returns JSON representation of a Bool, which is true or false.

func (Bool) Type

func (b Bool) Type() TypeID

Type returns TypeID of Bool. It's always TypeBool.

type Decoder added in v0.6.0

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

Decoder decodes a Map into a struct.

Following types of fields are supported:

  • bool
  • int (all sizes)
  • float32, float64
  • string
  • data.Value (used like interface{})
  • map, data.Map
  • slice, data.Array
  • struct, embedded struct
  • Blob
  • time.Time, Timestamp (see ToTimestamp to know supported values)
  • time.Duration (can be decoded from following types:)
  • int: second (e.g. 5 => 5 * time.Second)
  • float: second + subsecond (e.g 2.5 => 2 * time.Second + 500 * time.Millisecond)
  • string: Go's duration format (e.g. "6s" => 6 * time.Second)
  • pointer of these types

A regular array is not supported yet. User defined time.Time-compatible types cannot be used.

By default, the name of a field is converted to snake_case. For example,

struct {
	Param1 int
	AnotherParam1 string
	AnotherParam2 float64
}

these fields are looked up by "param_1", "another_param_1", and "another_param_3", respectively. Names are case-sensitive.

A field has a custom name by specifying a tag:

struct {
	Param1 int `bql:"new_name"`
}

In this example, Param1 will be decoded from the value in a Map whose key is "new_name" rather than "param_1".

All fields are considered optional by default. To have a required field, use required option in the tag:

struct {
	Param1 int `bql:"new_name,required"`
}

Decode returns an error when it can't find a value for the field. required option can be specified without giving a custom field name as json package and other similar packages do:

struct {
	Param1 int `bql:",required"`
}

weaklytyped option is available to allow users to write values flexibly:

struct {
	Param1 int `bql:",weaklytyped"`
}

Internally, weaklytyped fields are converted by ToType function, while AsType function is used for regular fields. Following types are always considered weaklytyped: time.Time, Timestamp, time.Duration. Struct and data.Value ignore weaklytyped option. When a map or an array is weaklytyped, it means its element will be converted by ToType function.

Without weaklytyped option, a float can implicitly be converted to an int when it has an integer value. Similarly, an int can always be casted to a float implicitly.

A field may have multiple options at once.

func NewDecoder added in v0.6.0

func NewDecoder(c *DecoderConfig) *Decoder

NewDecoder creates a new Decoder with the given config.

func (*Decoder) Decode added in v0.6.0

func (d *Decoder) Decode(m Map, v interface{}) (err error)

Decode decodes a Map into a struct. The argument must be a pointer to a struct.

type DecoderConfig added in v0.6.0

type DecoderConfig struct {
	// ErrorUnused, if set to true, reports an error when a Map contains keys
	// that are not defined in a struct.
	ErrorUnused bool

	// Metadata has meta information of decode. If this is nil, meat information
	// will not be tracked.
	Metadata *DecoderMetadata

	// TagName is the name of the struct tag looked up by Decoder. The default
	// is "bql".
	TagName string
}

DecoderConfig is used to configure the behavior of Decoder.

type DecoderMetadata added in v0.6.0

type DecoderMetadata struct {
	// Keys contains keys in a Map that are processed.
	Keys []string

	// Unsed contains keys in a Map that are not defined in a struct.
	Unused []string
}

DecoderMetadata tracks field names that are used or not used for decoding.

type Float

type Float float64

Float is a 64-bit floating point number. It can be assigned to Value interface.

func (Float) MarshalJSON

func (f Float) MarshalJSON() ([]byte, error)

MarshalJSON marshals a Float to JSON. NaN and Inf will be encoded as null.

func (Float) String

func (f Float) String() string

String returns JSON representation of a Float. NaN and Inf will be encoded as null.

func (Float) Type

func (f Float) Type() TypeID

Type returns TypeID of Float. It's always TypeFloat.

type HashValue

type HashValue uint64

HashValue is a type which hold hash values of Values.

func Hash

func Hash(v Value) HashValue

Hash computes a hash value of a Value. A hash value of a Float having an integer value is computed as a Int's hash value so that Hash(Float(2.0)) equals Hash(Int(2)). The hash value of Null is always the same. Hash values of NaNs always varies. For example, Hash(Float(math.NaN())) isn't equal to Hash(Float(math.NaN())). Therefore, if an array or a map has a NaN, the hash value changes everytime calling Hash function.

type Int

type Int int64

Int is an integer. It can be assigned to Value interface. A value more than 2^53 - 1 cannot exactly be marshaled to JSON because some languages like JavaScript or Lua only has float as a numeric type.

func (Int) String

func (i Int) String() string

String returns JSON representation of an Int.

func (Int) Type

func (i Int) Type() TypeID

Type returns TypeID of Int. It's always TypeInt.

type Map

type Map map[string]Value

Map is a map of Values. It can be assigned to Value interface. Only string keys are allowed.

func AsMap

func AsMap(v Value) (Map, error)

AsMap returns a map of string keys and Values only when the type of Value is TypeMap, otherwise it returns error.

func NewMap

func NewMap(m map[string]interface{}) (Map, error)

NewMap returns a Map object from map[string]interface{}. Returns an error when value type is not supported in SensorBee.

Example: The following sample interface{} will be converted to mapSample Map.

 var sample = map[string]interface{}{
    "bool":   true,
    "int":    int64(1),
    "float":  float64(0.1),
    "string": "homhom",
    "time":   time.Date(2015, time.May, 1, 14, 27, 0, 0, time.UTC),
    "array": []interface{}{true, 10, "inarray",
        map[string]interface{}{
            "mapinarray": "arraymap",
        }},
    "map": map[string]interface{}{
        "map_a": "a",
        "map_b": 2,
    },
    "byte": []byte("test byte"),
    "null": nil,
}
var mapSample = Map{
    "bool":   Bool(true),
    "int":    Int(1),
    "float":  Float(0.1),
    "string": String("homhom"),
    "time":   Timestamp(time.Date(2015, time.May, 1, 14, 27, 0, 0, time.UTC)),
    "array": Array([]Value{Bool(true), Int(10), String("inarray"),
        Map{
            "mapinarray": String("arraymap"),
        }}),
    "map": Map{
        "map_a": String("a"),
        "map_b": Int(2),
    },
    "byte": Blob([]byte("test byte")),
    "null": Null{},
}

func UnmarshalMsgpack

func UnmarshalMsgpack(b []byte) (Map, error)

UnmarshalMsgpack returns a Map object from a byte array encoded by msgpack serialization. The byte is expected to decode key-value style map. Returns an error when value type is not supported in SensorBee.

func (Map) Copy

func (m Map) Copy() Map

Copy performs deep copy of a Map. The Map returned from this method can safely be modified without affecting the original.

func (Map) Get

func (m Map) Get(path Path) (Value, error)

Get returns value(s) from a structured Map as addressed by the given path expression. Returns an error when the path expression is invalid or the path is not found in the Map.

Type conversion can be done for each type using the Value interface's methods.

Example:

p, err := CompilePath("path")
if err != nil { ... }
v, err := map.Get(p)
if err != nil { ... }
s, err := v.asString() // cast to String

Path Expression Example: Given the following Map structure

Map{
	"store": Map{
		"name": String("store name"),
		"book": Array([]Value{
			Map{
				"title": String("book name"),
			},
		}),
	},
}

To get values, access the following path expressions

`store`              -> get store's Map
`store.name`         -> get "store name"
`store.book[0].title -> get "book name"

or

`["store"]`                     -> get store's Map
`["store"]["name"]`             -> get "store name"
`["store"]["book"][0]["title"]` -> get "book name"

func (Map) Set

func (m Map) Set(path Path, val Value) error

Set sets a value in a structured Map as addressed by the given path expression. It is possible to set items even if the parent levels do not exist (`mkdir -p` behavior), i.e., you can set an item at "stores[17].owner.name" also when there is no "stores" key in the top-level Map. If a list index is higher than the current length, intermediate items will be created as Null items. Set returns an error when the path expression is invalid or when one of the intermediate components already exists but is not a map/list.

func (Map) String

func (m Map) String() string

String returns JSON representation of a Map.

func (Map) Type

func (m Map) Type() TypeID

Type returns TypeID of Map. It's always TypeMap.

func (*Map) UnmarshalJSON

func (m *Map) UnmarshalJSON(data []byte) error

UnmarshalJSON reconstructs a Map from JSON.

type Null

type Null struct{}

Null corresponds to null in JSON. It can be assigned to Value interface. Null is provided for Null Object pattern and it should always be used instead of nil.

func (Null) MarshalJSON

func (n Null) MarshalJSON() ([]byte, error)

MarshalJSON marshals Null to JSON.

func (Null) String

func (n Null) String() string

String returns JSON representation of a Null.

func (Null) Type

func (n Null) Type() TypeID

Type returns TypeID of Null. It's always TypeNull.

type Path

type Path interface {
	// contains filtered or unexported methods
}

Path is an entity that can be evaluated with a Map to return the value stored at the location specified by the Path.

func CompilePath

func CompilePath(s string) (p Path, err error)

CompilePath takes a JSON Path as a string and returns an instance of Path representing that JSON Path, or an error if the parameter is not a valid JSON Path.

func MustCompilePath

func MustCompilePath(s string) Path

MustCompilePath takes a JSON Path as a string and returns an instance of Path representing that JSON Path, or panics if the parameter is not a valid JSON Path.

type String

type String string

String is a string. It can be assigned to Value interface.

func (String) String

func (s String) String() string

String returns JSON representation of a String. A string "a" will be marshaled as `"a"` (double quotes are included), not `a`. To obtain a plain string without double quotes, use ToString function.

func (String) Type

func (s String) Type() TypeID

Type returns TypeID of String. It's always TypeString.

type SummarizeConfig

type SummarizeConfig struct {
}

SummarizeConfig (will) has cnofiguration parameters of Summarize function.

type Timestamp

type Timestamp time.Time

Timestamp is a date time information having microsecond-precision. It can be assigned to Value. It may have nanosecond values but they're usually ignored.

func (Timestamp) MarshalJSON

func (t Timestamp) MarshalJSON() ([]byte, error)

MarshalJSON marshals a Timestamp to JSON. A Timestamp is encoded as a string in RFC3339Nano format.

func (Timestamp) String

func (t Timestamp) String() string

String returns JSON representation of a Timestamp. A Timestamp is encoded as a string in RFC3339Nano format.

func (Timestamp) Type

func (t Timestamp) Type() TypeID

Type returns TypeID of Timestamp. It's always TypeTimestamp.

func (*Timestamp) UnmarshalJSON

func (t *Timestamp) UnmarshalJSON(data []byte) error

UnmarshalJSON reconstructs a Timestamp from JSON. It first tries to parse a string in RFC3339Nano format. When it fails, then it tries again with RFC3339 format.

type TypeID

type TypeID int

TypeID is an ID of a type. A unique value is assigned to each type.

const (

	// TypeNull is a TypeID of Null.
	TypeNull TypeID
	// TypeBool is a TypeID of Bool.
	TypeBool
	// TypeInt is a TypeID of Int.
	TypeInt
	// TypeFloat is a TypeID of Float.
	TypeFloat
	// TypeString is a TypeID of String.
	TypeString
	// TypeBlob is a TypeID of Blob.
	TypeBlob
	// TypeTimestamp is a TypeID of Timestamp.
	TypeTimestamp
	// TypeArray is a TypeID of Array.
	TypeArray
	// TypeMap is a TypeID of Map.
	TypeMap
)

func (TypeID) String

func (t TypeID) String() string

type Value

type Value interface {
	// Type returns the actual type of a Value. (Note that this is
	// faster than a type switch.) If a Value has `Type() == Type{X}`,
	// then it can be assumed that the `As{X}()` conversion will
	// not fail.
	Type() TypeID

	String() string
	// contains filtered or unexported methods
}

Value is the generic interface for all data that can be stored inside a Tuple. Since we assume the data not to conform to any schema, data can have any shape and it can also change within a stream from one Tuple to the next. Therefore we need to be careful with respect to type conversions. A Value obtained, e.g., by Map.Get should always be converted using the appropriate method and error checking must be done.

Example:

i, err := val.asInt()
if err != nil { ... }

func NewValue

func NewValue(v interface{}) (Value, error)

NewValue returns a Value object from interface{}. Returns an error when value type is not supported in SensorBee.

Jump to

Keyboard shortcuts

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