encoding/value
A dynamic value for data whose shape you do not know in advance; the shared language of every format in this group.
import encoding.value
When you read JSON whose shape you do not know in advance, it cannot be poured into a fixed struct. That is what Value is for: every value carries its own kind, and it may be an array or an object of further values.
JSON, YAML, TOML, MessagePack, CSV and XML can all be turned into a Value and built from one; the encoding.serde package uses exactly this to convert between formats.
Types
Value
struct Value| Name | Type | Description |
|---|---|---|
kind | int | The kind of the value; one of the Kind constants. |
b | bool = false | The boolean value. |
i | i64 = 0 | The whole number value. |
n | f64 = 0 | The decimal value. |
s | str = "" | The string value. |
arr | Vector | The elements of an array. |
obj | HashMap | The keys and values of an object. |
import encoding.value
func main:
mut user := value.NewValueObject()
value.ObjectSet(user, "name", value.ValueString("Narges"))
value.ObjectSet(user, "age", value.ValueInt(34))
value.ObjectSet(user, "height", value.ValueFloat(1.68))
value.ObjectSet(user, "admin", value.ValueBool(false))
value.ObjectSet(user, "phone", value.ValueNull())
mut languages := value.NewValueArray()
value.ArrayPush(languages, value.ValueString("fa"))
value.ArrayPush(languages, value.ValueString("en"))
value.ArraySet(languages, 1, value.ValueString("ar"))
value.ObjectSet(user, "langs", languages)
println value.ToDebugString(user)
println value.ObjectLen(user), value.ObjectHas(user, "age"), value.ObjectKeys(user)
println value.AsString(value.ObjectGet(user, "name")), value.AsInt(value.ObjectGet(user, "age")), value.AsFloat(value.ObjectGet(user, "height")), value.AsBool(value.ObjectGet(user, "admin"))
langs := value.ObjectGet(user, "langs")
println value.IsArray(langs), value.ArrayLen(langs), value.AsString(value.ArrayGet(langs, 1))
mut present := false
_ := value.ObjectGetOk(user, "email", present)
println present
phone := value.ObjectGet(user, "phone")
println value.IsNull(phone), value.IsBool(phone), value.IsInt(value.ValueInt(1)), value.IsFloat(value.ValueFloat(1.0)), value.IsNumber(value.ValueInt(1))
println value.IsString(value.ValueString("")), value.IsObject(user), phone.kind == value.KindNull, value.KindObject
mut copy := value.ValueClone(user)
println value.Equal(copy, user)
removed := value.ObjectDelete(copy, "phone")
println removed, value.Equal(copy, user)
mut numbers := Vector {} as Vector<value.Value>
numbers.push(value.ValueInt(1))
mut fields := HashMap {} as HashMap<str, value.Value>
fields.put("x", value.ValueArray(numbers))
println value.ToDebugString(value.ValueObject(fields))
value.ValueFree(copy)
endConstants and variables
KindNull
const KindNull: i32 = 0The example for this item is shown with the ones above: encoding-value-Value.salam
KindBool
const KindBool: i32 = 1The example for this item is shown with the ones above: encoding-value-Value.salam
KindInt
const KindInt: i32 = 2The example for this item is shown with the ones above: encoding-value-Value.salam
KindFloat
const KindFloat: i32 = 3The example for this item is shown with the ones above: encoding-value-Value.salam
KindString
const KindString: i32 = 4The example for this item is shown with the ones above: encoding-value-Value.salam
KindArray
const KindArray: i32 = 5The example for this item is shown with the ones above: encoding-value-Value.salam
KindObject
const KindObject: i32 = 6The example for this item is shown with the ones above: encoding-value-Value.salam
Functions
ToDebugString
func ToDebugString(v: Value): strobject{name: string(Ali)}, for debugging.The example for this item is shown with the ones above: encoding-value-Value.salam
ValueNull
func ValueNull(): ValueThe example for this item is shown with the ones above: encoding-value-Value.salam
ValueBool
func ValueBool(b: bool): ValueThe example for this item is shown with the ones above: encoding-value-Value.salam
ValueInt
func ValueInt(i: i64): ValueThe example for this item is shown with the ones above: encoding-value-Value.salam
ValueFloat
func ValueFloat(n: f64): ValueThe example for this item is shown with the ones above: encoding-value-Value.salam
ValueString
func ValueString(s: str): ValueThe example for this item is shown with the ones above: encoding-value-Value.salam
ValueArray
func ValueArray(items: Vector<Value>): ValueThe example for this item is shown with the ones above: encoding-value-Value.salam
ValueObject
func ValueObject(fields: HashMap<str, Value>): ValueThe example for this item is shown with the ones above: encoding-value-Value.salam
NewValueArray
func NewValueArray(): ValueThe example for this item is shown with the ones above: encoding-value-Value.salam
NewValueObject
func NewValueObject(): ValueThe example for this item is shown with the ones above: encoding-value-Value.salam
IsNull
func IsNull(v: Value): boolThe example for this item is shown with the ones above: encoding-value-Value.salam
IsBool
func IsBool(v: Value): boolThe example for this item is shown with the ones above: encoding-value-Value.salam
IsInt
func IsInt(v: Value): boolThe example for this item is shown with the ones above: encoding-value-Value.salam
IsFloat
func IsFloat(v: Value): boolThe example for this item is shown with the ones above: encoding-value-Value.salam
IsNumber
func IsNumber(v: Value): boolThe example for this item is shown with the ones above: encoding-value-Value.salam
IsString
func IsString(v: Value): boolThe example for this item is shown with the ones above: encoding-value-Value.salam
IsArray
func IsArray(v: Value): boolThe example for this item is shown with the ones above: encoding-value-Value.salam
IsObject
func IsObject(v: Value): boolThe example for this item is shown with the ones above: encoding-value-Value.salam
AsBool
func AsBool(v: Value): boolThe example for this item is shown with the ones above: encoding-value-Value.salam
AsInt
func AsInt(v: Value): i64The example for this item is shown with the ones above: encoding-value-Value.salam
AsFloat
func AsFloat(v: Value): f64The example for this item is shown with the ones above: encoding-value-Value.salam
AsString
func AsString(v: Value): strThe example for this item is shown with the ones above: encoding-value-Value.salam
ArrayLen
func ArrayLen(v: Value): intThe example for this item is shown with the ones above: encoding-value-Value.salam
ArrayGet
func ArrayGet(v: Value, i: int): Valuei; the index has to be less than ArrayLen.The example for this item is shown with the ones above: encoding-value-Value.salam
ArraySet
func ArraySet(v &: Value, i: int, item: Value)i.The example for this item is shown with the ones above: encoding-value-Value.salam
ArrayPush
func ArrayPush(v &: Value, item: Value)The example for this item is shown with the ones above: encoding-value-Value.salam
ObjectLen
func ObjectLen(v: Value): intThe example for this item is shown with the ones above: encoding-value-Value.salam
ObjectHas
func ObjectHas(v: Value, key: str): boolThe example for this item is shown with the ones above: encoding-value-Value.salam
ObjectGet
func ObjectGet(v: Value, key: str): ValueThe example for this item is shown with the ones above: encoding-value-Value.salam
ObjectGetOk
func ObjectGetOk(v: Value, key: str, ok &: bool): Valueok saying whether the key was there; for telling "absent" apart from "null".The example for this item is shown with the ones above: encoding-value-Value.salam
ObjectSet
func ObjectSet(v &: Value, key: str, item: Value)The example for this item is shown with the ones above: encoding-value-Value.salam
ObjectDelete
func ObjectDelete(v &: Value, key: str): boolThe example for this item is shown with the ones above: encoding-value-Value.salam
ObjectKeys
func ObjectKeys(v: Value): Vector<str>The example for this item is shown with the ones above: encoding-value-Value.salam
ValueClone
func ValueClone(v: Value): ValueThe example for this item is shown with the ones above: encoding-value-Value.salam
ValueFree
func ValueFree(v &: Value)The example for this item is shown with the ones above: encoding-value-Value.salam
Equal
func Equal(a: Value, b: Value): boolThe example for this item is shown with the ones above: encoding-value-Value.salam