|
| 1 | +/++ |
| 2 | +$(H1 Mutable Ion value) |
| 3 | +
|
| 4 | +This module contains a single alias definition and doesn't provide Ion serialization API. |
| 5 | +
|
| 6 | +See_also: Ion library $(MIR_PACKAGE mir-ion) |
| 7 | +
|
| 8 | +License: $(HTTP www.apache.org/licenses/LICENSE-2.0, Apache-2.0) |
| 9 | +Authors: Ilya Yaroshenko |
| 10 | +Macros: |
| 11 | ++/ |
| 12 | +module mir.algebraic_alias.ion; |
| 13 | + |
| 14 | +import mir.algebraic: TaggedVariant, This; |
| 15 | +public import mir.annotated: Annotated; |
| 16 | +public import mir.lob: Clob, Blob; |
| 17 | +public import mir.string_map: StringMap; |
| 18 | +public import mir.timestamp: Timestamp; |
| 19 | + |
| 20 | + |
| 21 | +/++ |
| 22 | +Definition union for $(LREF IonAlgebraic). |
| 23 | ++/ |
| 24 | +union IonAlgebraicUnion |
| 25 | +{ |
| 26 | + /// |
| 27 | + typeof(null) null_; |
| 28 | + /// |
| 29 | + bool boolean; |
| 30 | + /// |
| 31 | + long integer; |
| 32 | + /// |
| 33 | + double float_; |
| 34 | + /// |
| 35 | + immutable(char)[] string; |
| 36 | + /// |
| 37 | + Blob blob; |
| 38 | + /// |
| 39 | + Clob clob; |
| 40 | + /// |
| 41 | + Timestamp timestamp; |
| 42 | + /// Self alias in array. |
| 43 | + This[] array; |
| 44 | + /// Self alias in $(MREF mir,string_map). |
| 45 | + StringMap!This object; |
| 46 | + /// Self alias in $(MREF mir,annotated). |
| 47 | + Annotated!This annotations; |
| 48 | +} |
| 49 | + |
| 50 | +/++ |
| 51 | +Ion tagged algebraic alias. |
| 52 | +
|
| 53 | +The example below shows only the basic features. Advanced API to work with algebraic types can be found at $(GMREF mir-core, mir,algebraic). |
| 54 | +See also $(MREF mir,string_map) - ordered string-value associative array. |
| 55 | ++/ |
| 56 | +alias IonAlgebraic = TaggedVariant!IonAlgebraicUnion; |
| 57 | + |
| 58 | +/// |
| 59 | +unittest |
| 60 | +{ |
| 61 | + import mir.ndslice.topology: map; |
| 62 | + import mir.array.allocation: array; |
| 63 | + |
| 64 | + IonAlgebraic value; |
| 65 | + |
| 66 | + StringMap!IonAlgebraic object; |
| 67 | + |
| 68 | + // Default |
| 69 | + assert(value.isNull); |
| 70 | + assert(value.kind == IonAlgebraic.Kind.null_); |
| 71 | + |
| 72 | + // Boolean |
| 73 | + value = object["bool"] = true; |
| 74 | + assert(!value.isNull); |
| 75 | + assert(value == true); |
| 76 | + assert(value.kind == IonAlgebraic.Kind.boolean); |
| 77 | + assert(value.get!bool == true); |
| 78 | + assert(value.get!(IonAlgebraic.Kind.boolean) == true); |
| 79 | + |
| 80 | + // Null |
| 81 | + value = object["null"] = null; |
| 82 | + assert(value.isNull); |
| 83 | + assert(value == null); |
| 84 | + assert(value.kind == IonAlgebraic.Kind.null_); |
| 85 | + assert(value.get!(typeof(null)) == null); |
| 86 | + assert(value.get!(IonAlgebraic.Kind.null_) == null); |
| 87 | + |
| 88 | + // String |
| 89 | + value = object["string"] = "s"; |
| 90 | + assert(value.kind == IonAlgebraic.Kind.string); |
| 91 | + assert(value == "s"); |
| 92 | + assert(value.get!string == "s"); |
| 93 | + assert(value.get!(IonAlgebraic.Kind.string) == "s"); |
| 94 | + |
| 95 | + // Integer |
| 96 | + value = object["integer"] = 4; |
| 97 | + assert(value.kind == IonAlgebraic.Kind.integer); |
| 98 | + assert(value == 4); |
| 99 | + assert(value != 4.0); |
| 100 | + assert(value.get!long == 4); |
| 101 | + assert(value.get!(IonAlgebraic.Kind.integer) == 4); |
| 102 | + |
| 103 | + // Float |
| 104 | + value = object["float"] = 3.0; |
| 105 | + assert(value.kind == IonAlgebraic.Kind.float_); |
| 106 | + assert(value != 3); |
| 107 | + assert(value == 3.0); |
| 108 | + assert(value.get!double == 3.0); |
| 109 | + assert(value.get!(IonAlgebraic.Kind.float_) == 3.0); |
| 110 | + |
| 111 | + // Array |
| 112 | + IonAlgebraic[] arr = [0, 1, 2, 3, 4].map!IonAlgebraic.array; |
| 113 | + |
| 114 | + value = object["array"] = arr; |
| 115 | + assert(value.kind == IonAlgebraic.Kind.array); |
| 116 | + assert(value == arr); |
| 117 | + assert(value.get!(IonAlgebraic[])[3] == 3); |
| 118 | + |
| 119 | + // Object |
| 120 | + assert(object.keys == ["bool", "null", "string", "integer", "float", "array"]); |
| 121 | + object.values[0] = "false"; |
| 122 | + assert(object["bool"] == "false"); // it is a string now |
| 123 | + object.remove("bool"); // remove the member |
| 124 | + |
| 125 | + value = object["array"] = object; |
| 126 | + assert(value.kind == IonAlgebraic.Kind.object); |
| 127 | + assert(value.get!(StringMap!IonAlgebraic).keys is object.keys); |
| 128 | + assert(value.get!(StringMap!IonAlgebraic).values is object.values); |
| 129 | + |
| 130 | + IonAlgebraic[string] aa = object.toAA; |
| 131 | + object = StringMap!IonAlgebraic(aa); |
| 132 | + |
| 133 | + IonAlgebraic fromAA = ["a" : IonAlgebraic(3), "b" : IonAlgebraic("b")]; |
| 134 | + assert(fromAA.get!(StringMap!IonAlgebraic)["a"] == 3); |
| 135 | + assert(fromAA.get!(StringMap!IonAlgebraic)["b"] == "b"); |
| 136 | + |
| 137 | + auto annotated = Annotated!IonAlgebraic(["birthday"], Timestamp("2001-01-01")); |
| 138 | + value = annotated; |
| 139 | + assert(value == annotated); |
| 140 | + value = annotated.IonAlgebraic; |
| 141 | + assert(value == annotated); |
| 142 | +} |
0 commit comments