Report a bug
If you spot a problem with this page, click here to create a GitHub issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

mir.algebraic_alias.ion

Mutable Ion value

This module contains a single alias definition and doesn't provide Ion serialization API.
See Also:
Ion library mir-ion
License:
Authors:
Ilya Yaroshenko
union IonAlgebraicUnion;
Definition union for IonAlgebraic.
typeof(null) null_;
bool boolean;
long integer;
double float_;
string string;
Blob blob;
Clob clob;
Timestamp timestamp;
This[] array;
Self alias in array.
StringMap!This object;
Self alias in mir.string_map.
Annotated!This annotations;
Self alias in mir.annotated.
alias IonAlgebraic = mir.algebraic.Algebraic!(TaggedType!(typeof(null), "null_"), TaggedType!(bool, "boolean"), TaggedType!(StringMap!(This, uint), "object"), TaggedType!(double, "float_"), TaggedType!(long, "integer"), TaggedType!(This[], "array"), TaggedType!(string, "string"), TaggedType!(Blob, "blob"), TaggedType!(Clob, "clob"), TaggedType!(Annotated!(This), "annotations"), TaggedType!(Timestamp, "timestamp")).Algebraic;
Ion tagged algebraic alias.
The example below shows only the basic features. Advanced API to work with algebraic types can be found at mir.algebraic. See also mir.string_map - ordered string-value associative array.
Examples:
import mir.ndslice.topology: map;
import mir.array.allocation: array;

IonAlgebraic value;

StringMap!IonAlgebraic object;

// Default
assert(value.isNull);
assert(value.kind == IonAlgebraic.Kind.null_);

// Boolean
value = object["bool"] = true;
assert(!value.isNull);
assert(value == true);
assert(value.kind == IonAlgebraic.Kind.boolean);
assert(value.get!bool == true);
assert(value.get!(IonAlgebraic.Kind.boolean) == true);

// Null
value = object["null"] = null;
assert(value.isNull);
assert(value == null);
assert(value.kind == IonAlgebraic.Kind.null_);
assert(value.get!(typeof(null)) == null);
assert(value.get!(IonAlgebraic.Kind.null_) == null);

// String
value = object["string"] = "s";
assert(value.kind == IonAlgebraic.Kind.string);
assert(value == "s");
assert(value.get!string == "s");
assert(value.get!(IonAlgebraic.Kind.string) == "s");

// Integer
value = object["integer"] = 4;
assert(value.kind == IonAlgebraic.Kind.integer);
assert(value == 4);
assert(value != 4.0);
assert(value.get!long == 4);
assert(value.get!(IonAlgebraic.Kind.integer) == 4);

// Float
value = object["float"] = 3.0;
assert(value.kind == IonAlgebraic.Kind.float_);
assert(value != 3);
assert(value == 3.0);
assert(value.get!double == 3.0);
assert(value.get!(IonAlgebraic.Kind.float_) == 3.0);

// Array
IonAlgebraic[] arr = [0, 1, 2, 3, 4].map!IonAlgebraic.array;

value = object["array"] = arr;
assert(value.kind == IonAlgebraic.Kind.array);
assert(value == arr);
assert(value.get!(IonAlgebraic[])[3] == 3);

// Object
assert(object.keys == ["bool", "null", "string", "integer", "float", "array"]);
object.values[0] = "false";
assert(object["bool"] == "false"); // it is a string now
object.remove("bool"); // remove the member

value = object["array"] = object;
assert(value.kind == IonAlgebraic.Kind.object);
assert(value.get!(StringMap!IonAlgebraic).keys is object.keys);
assert(value.get!(StringMap!IonAlgebraic).values is object.values);

IonAlgebraic[string] aa = object.toAA;
object = StringMap!IonAlgebraic(aa);

IonAlgebraic fromAA = ["a" : IonAlgebraic(3), "b" : IonAlgebraic("b")];
assert(fromAA.get!(StringMap!IonAlgebraic)["a"] == 3);
assert(fromAA.get!(StringMap!IonAlgebraic)["b"] == "b");

auto annotated = Annotated!IonAlgebraic(["birthday"], Timestamp("2001-01-01"));
value = annotated;
assert(value == annotated);
value = annotated.IonAlgebraic;
assert(value == annotated);