1 import mir.test: should; 2 import mir.ndslice.topology: map; 3 import mir.array.allocation: array; 4 5 IonAlgebraic value; 6 7 StringMap!IonAlgebraic object; 8 9 // Default 10 assert(value.isNull); 11 assert(value.kind == IonAlgebraic.Kind.null_); 12 13 // Boolean 14 value = object["bool"] = true; 15 assert(!value.isNull); 16 assert(value == true); 17 assert(value.kind == IonAlgebraic.Kind.boolean); 18 // access 19 assert(value.boolean == true); 20 assert(value.get!bool == true); 21 assert(value.get!"boolean" == true); 22 assert(value.get!(IonAlgebraic.Kind.boolean) == true); 23 // nothrow access 24 assert(value.trustedGet!bool == true); 25 assert(value.trustedGet!"boolean" == true); 26 assert(value.trustedGet!(IonAlgebraic.Kind.boolean) == true); 27 // checks 28 assert(!value._is!string); 29 assert(value._is!bool); 30 assert(value._is!"boolean"); 31 assert(value._is!(IonAlgebraic.Kind.boolean)); 32 33 // Null 34 value = object["null"] = null; 35 assert(value.isNull); 36 assert(value == null); 37 assert(value.kind == IonAlgebraic.Kind.null_); 38 // access 39 assert(value.null_ == null); 40 assert(value.get!(typeof(null)) == null); 41 assert(value.get!(IonAlgebraic.Kind.null_) == null); 42 43 // String 44 value = object["string"] = "s"; 45 assert(value.kind == IonAlgebraic.Kind.string); 46 assert(value == "s"); 47 // access 48 // Yep, `string` here is an alias to `get!(immutable(char)[])` method 49 assert(value.string == "s"); 50 // `string` here is an alias of type `immutable(char)[]` 51 assert(value.get!string == "s"); 52 assert(value.get!"string" == "s"); 53 // finally, `string` here is an enum meber 54 assert(value.get!(IonAlgebraic.Kind.string) == "s"); 55 56 // Integer 57 value = object["integer"] = 4; 58 assert(value.kind == IonAlgebraic.Kind.integer); 59 assert(value == 4); 60 assert(value != 4.0); 61 assert(value.integer == 4); 62 63 // Float 64 value = object["float"] = 3.0; 65 assert(value.kind == IonAlgebraic.Kind.float_); 66 assert(value != 3); 67 assert(value == 3.0); 68 assert(value.float_ == 3.0); 69 70 // Array 71 IonAlgebraic[] arr = [0, 1, 2, 3, 4].map!IonAlgebraic.array; 72 73 value = object["array"] = arr; 74 assert(value.kind == IonAlgebraic.Kind.array); 75 assert(value == arr); 76 assert(value == [0, 1, 2, 3, 4].map!IonAlgebraic.array);// by value 77 assert(value.array[3] == 3); 78 79 // Object 80 assert(object.keys == ["bool", "null", "string", "integer", "float", "array"]); 81 object.values[0] = "false"; 82 assert(object["bool"] == "false"); // it is a string now 83 object.remove("bool"); // remove the member 84 85 value = object["array"] = object; 86 assert(value.kind == IonAlgebraic.Kind.object); 87 assert(value.object.keys is object.keys); 88 89 IonAlgebraic[string] aa = object.toAA; 90 object = aa.StringMap!IonAlgebraic; 91 92 IonAlgebraic fromAA = ["a" : IonAlgebraic(3), "b" : IonAlgebraic("b")]; 93 assert(fromAA.object["a"] == 3); 94 assert(fromAA.object["b"] == "b"); 95 96 // object foreach iteration 97 long sum; 98 foreach (ref key, ref val; fromAA.object) 99 if (key == "a") 100 sum += val.get!long; 101 sum.should == 3; 102 103 // annotations 104 auto annotated = Annotated!IonAlgebraic(["birthday"], Timestamp("2001-01-01")); 105 value = annotated; 106 assert(value == annotated); 107 value = annotated.IonAlgebraic; 108 assert(value == annotated);
Ion tagged algebraic alias.
The example below shows only the basic features. Advanced API to work with algebraic types can be found at $(GMREF mir-core, mir,algebraic). See also mir.string_map - ordered string-value associative array.