Documentation ¶
Index ¶
- Constants
- func AsBlob(v Value) ([]byte, error)
- func AsBool(v Value) (bool, error)
- func AsFloat(v Value) (float64, error)
- func AsInt(v Value) (int64, error)
- func AsString(v Value) (string, error)
- func AsTimestamp(v Value) (time.Time, error)
- func Decode(m Map, v interface{}) error
- func Equal(v1 Value, v2 Value) bool
- func Less(v1 Value, v2 Value) bool
- func MarshalMsgpack(m Map) ([]byte, error)
- func NewIMap(m Map) map[string]interface{}
- func Summarize(val Value) string
- func ToBlob(v Value) ([]byte, error)
- func ToBool(v Value) (bool, error)
- func ToDuration(v Value) (time.Duration, error)
- func ToFloat(v Value) (float64, error)
- func ToInt(v Value) (int64, error)
- func ToString(v Value) (string, error)
- func ToTimestamp(v Value) (time.Time, error)
- type Array
- type Blob
- type Bool
- type Decoder
- type DecoderConfig
- type DecoderMetadata
- type Float
- type HashValue
- type Int
- type Map
- type Null
- type Path
- type String
- type SummarizeConfig
- type Timestamp
- type TypeID
- type Value
Constants ¶
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) )
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 ¶
AsBlob returns an array of bytes only when the type of Value is TypeBlob, otherwise it returns error.
func AsBool ¶
AsBool returns a bool value only when the type of Value is TypeBool, otherwise it returns error.
func AsFloat ¶
AsFloat returns a float value only when the type of Value is TypeFloat, otherwise it returns error.
func AsInt ¶
AsInt returns an integer value only when the type of Value is TypeInt, otherwise it returns error.
func AsString ¶
AsString returns a string only when the type of Value is TypeString, otherwise it returns error.
func AsTimestamp ¶
AsTimestamp returns a time.Time only when the type of Value is TypeTime, otherwise it returns error.
func Decode ¶ added in v0.6.0
Decode decodes a Map into a struct. The argument must be a pointer to a struct.
func Equal ¶
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 ¶
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 ¶
MarshalMsgpack returns a byte array encoded by msgpack serialization from a Map object. Returns an error when msgpack serialization failed.
func Summarize ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
AsArray returns an array of Values only when the type of Value is TypeArray, otherwise it returns error.
func NewArray ¶
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
Copy performs deep copy of an Array. The Array returned from this method can safely be modified without affecting the original.
func (*Array) UnmarshalJSON ¶
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.
type Bool ¶
type Bool bool
Bool is a boolean value. It can be assigned to Value interface.
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.
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 ¶
MarshalJSON marshals a Float to JSON. NaN and Inf will be encoded as null.
type HashValue ¶
type HashValue uint64
HashValue is a type which hold hash values of Values.
func Hash ¶
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.
type Map ¶
Map is a map of Values. It can be assigned to Value interface. Only string keys are allowed.
func AsMap ¶
AsMap returns a map of string keys and Values only when the type of Value is TypeMap, otherwise it returns error.
func NewMap ¶
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 ¶
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 ¶
Copy performs deep copy of a Map. The Map returned from this method can safely be modified without affecting the original.
func (Map) Get ¶
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 ¶
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) UnmarshalJSON ¶
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 ¶
MarshalJSON marshals Null to JSON.
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 ¶
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 ¶
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.
type SummarizeConfig ¶
type SummarizeConfig struct { }
SummarizeConfig (will) has cnofiguration parameters of Summarize function.
type Timestamp ¶
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 ¶
MarshalJSON marshals a Timestamp to JSON. A Timestamp is encoded as a string in RFC3339Nano format.
func (Timestamp) String ¶
String returns JSON representation of a Timestamp. A Timestamp is encoded as a string in RFC3339Nano format.
func (*Timestamp) UnmarshalJSON ¶
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 )
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 { ... }