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_ext

Mutable Ion value with IonNull support

This module contains a single alias definition and doesn't provide Ion serialization API.
License:
Authors:
Ilya Yaroshenko
union IonExtAlgebraicUnion;
Definition union for IonExtAlgebraic.
IonNull 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 IonExtAlgebraic = mir.algebraic.Algebraic!(TaggedType!(IonNull, "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;

IonExtAlgebraic value;

StringMap!IonExtAlgebraic object;

// Default
assert(value._is!IonNull);
assert(value.get!IonNull == IonTypeCode.null_.IonNull);
assert(value.kind == IonExtAlgebraic.Kind.null_);

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

// Null
value = object["null"] = IonTypeCode.string.IonNull;
assert(value._is!IonNull);
assert(value == IonTypeCode.string.IonNull);
assert(value.kind == IonExtAlgebraic.Kind.null_);
assert(value.get!IonNull == IonTypeCode.string.IonNull);
assert(value.get!(IonExtAlgebraic.Kind.null_) == IonTypeCode.string.IonNull);

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

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

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

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

value = object["array"] = arr;
assert(value.kind == IonExtAlgebraic.Kind.array);
assert(value == arr);
assert(value.get!(IonExtAlgebraic[])[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 == IonExtAlgebraic.Kind.object);
assert(value.get!(StringMap!IonExtAlgebraic).keys is object.keys);
assert(value.get!(StringMap!IonExtAlgebraic).values is object.values);

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

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

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