Documentation ¶
Index ¶
- Variables
- func Add(i1, i2 interface{}) (res interface{}, ok bool)
- func Bool(i interface{}) (res bool, ok bool)
- func Cmp(arg1 interface{}, arg2 interface{}) int
- func Copy(copyfrom interface{}) (interface{}, error)
- func Divide(i1, i2 interface{}) (res float64, ok bool)
- func Eq(arg1 interface{}, arg2 interface{}) (res bool, ok bool)
- func Equal(arg1 interface{}, arg2 interface{}) (res bool, ok bool)
- func Float(i interface{}) (res float64, ok bool)
- func Get(i interface{}, elem ...interface{}) (val interface{}, ok bool)
- func Gt(arg1 interface{}, arg2 interface{}) (res bool, ok bool)
- func Gte(arg1 interface{}, arg2 interface{}) (res bool, ok bool)
- func Int(i interface{}) (res int64, ok bool)
- func JSONString(i interface{}) string
- func Keys(i interface{}) (keys []string, ok bool)
- func Length(i interface{}) (l int, ok bool)
- func Lt(arg1 interface{}, arg2 interface{}) (res bool, ok bool)
- func Lte(arg1 interface{}, arg2 interface{}) (res bool, ok bool)
- func Mod(i1, i2 interface{}) (res float64, ok bool)
- func Multiply(i1, i2 interface{}) (res float64, ok bool)
- func Set(i interface{}, setto interface{}, elem ...interface{}) (ok bool)
- func String(i interface{}) (res string, ok bool)
- func Subtract(i1, i2 interface{}) (res float64, ok bool)
Constants ¶
This section is empty.
Variables ¶
var ( LessThan = -1 GreaterThan = 1 Equals = 0 CantCompare = -2 )
Variables used for Cmp
Functions ¶
func Add ¶
func Add(i1, i2 interface{}) (res interface{}, ok bool)
Add attempts to add two interfaces. It works similarly to expectations everywhere, with the extra 'extreme string ducktyping' twist.
"hello" + "world" = "helloworld" true + 1 = 2 3.14 + 1 = 4.14 "23"+"42" = 65
func Bool ¶
Bool attempts to convert the given interface into a boolean. There can be a loss of information in this one.
"hi" -> !ok true -> true "true" -> true "false" -> false "0.0" -> false "3.34" -> true 1 -> true 1337 -> true 1.434 -> true -1 -> false nil -> false 0.0 -> false 0 -> false "" -> false
func Cmp ¶
func Cmp(arg1 interface{}, arg2 interface{}) int
Cmp performs a comparison between the two values, and returns the result of the comparison (LessThan, GreaterThan, Equals, CantCompare), which are defined as ints
func Copy ¶
func Copy(copyfrom interface{}) (interface{}, error)
Copy performs a deep copy of the given interface. The copy makes sure that no pointers/maps are shared
between the original data and new points.
WARNING: This is not a real deep copy. It totally cheats at the moment. Do not use when you need
to preserve structs in output.
WARNING 2: Converts int to float. Ultimately, this function is not yet good for normal use. It is just
barely useful for our own internal usage. Feel free to fix it.
func Equal ¶
Equal attempts to check equality between two interfaces. If the values are not directly comparable thru DeepEqual, tries to do a "duck" comparison.
true true -> true "true" true -> true "1" true -> true 1.0 1 -> true 1.345 "1.345" -> true 50.0 true -> true 0.0 false -> true
func Float ¶
Float attempts to convert the given interface into a float
1 -> 1.0 "1.34" -> 1.34 1.34 -> 1.34 false -> 0.0 true -> 1.0 "false" -> 0.0 " 2.3 " -> !ok nil -> 0.0
func Get ¶
func Get(i interface{}, elem ...interface{}) (val interface{}, ok bool)
Get takes an object, and the element name/index to extract, and returns the element, as well as an ok boolean specifying if the element was found. Remember that only exported fields are available from structs One note: only `map[string]` is supported right now. maps of another type will cause get to panic! This is an active weakness in the current implementation. It should be enough to get data from arbitrary marshalled json.
Get also supports multiple args for multilevel data:
//returns deeplyNestedStruct->foo->bar duck.Get(deeplyNestedStruct, "foo","bar")
func Int ¶
Int attempts to convert the given interface into an integer
"1" -> 1 1 -> 1 1.0 -> 1 "1.0" -> 1 false -> 0 true -> 1 "1.3" -> !ok nil -> 0
func JSONString ¶
func JSONString(i interface{}) string
JSONString attempts to convert to a string... but if that fails it marshals the given data into json, and returns the json string. If it can't marshal, it just returns an empty string
func Keys ¶
Keys gets the field names by which the object can be accessed. Note that arrays do not return any keys
func Set ¶
func Set(i interface{}, setto interface{}, elem ...interface{}) (ok bool)
Set attempts to set a subelement of the passed-in object. In the same way that Get gets the value, Set allows to set it. The only possibly confusing thing about Set is that the "to" value and the element names are reversed to allow for multiple arguments (as in Get)
v := map[string]string{"hi":"world"} duck.Set(&v,45,"hi") //now v["hi"] == "45" v2 := map[string]interface{}{"foo":map[string]interface{}{"bar": 23}} duck.Set(&v2,"hello!","foo","bar") //v2["foo"]["bar"] == "hello!"
NOTE: Currently setting structs (ie, if setto is a struct) is non-functional if the receiving end is not interface{}. Also, only map[string]interface{} is supported of maps to receive values. You are risking panic if another map type is used
Types ¶
This section is empty.