Skip to content

Commit d76bb3a

Browse files
committed
make SchemaVariant work with const
1 parent da52d7a commit d76bb3a

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

source/mongoschema/variant.d

+13-5
Original file line numberDiff line numberDiff line change
@@ -103,21 +103,21 @@ public:
103103
}
104104

105105
static foreach (Field; Fields)
106-
mixin("Field.Type " ~ Field.name
107-
~ "() @trusted { checkType!(Field.Type); return value.get!(Field.Type); }");
106+
mixin("inout(Field.Type) " ~ Field.name
107+
~ "() @trusted inout { checkType!(Field.Type); return value.get!(Field.Type); }");
108108

109-
void checkType(T)()
109+
void checkType(T)() const
110110
{
111111
if (!isType!T)
112112
throw new Exception("Attempted to access " ~ type ~ " field as " ~ T.stringof);
113113
}
114114

115-
bool isType(T)() @trusted
115+
bool isType(T)() @trusted const
116116
{
117117
return value.type == typeid(T);
118118
}
119119

120-
string type()
120+
string type() const
121121
{
122122
if (!value.hasValue)
123123
return null;
@@ -221,4 +221,12 @@ unittest
221221
"type": Bson("foo"),
222222
"value": Bson(["x": Bson(3)])
223223
]));
224+
225+
const x = var2;
226+
assert(x.type == "foo");
227+
assert(x.isType!Foo);
228+
assert(typeof(x).toBson(x) == Bson([
229+
"type": Bson("foo"),
230+
"value": Bson(["x": Bson(3)])
231+
]));
224232
}

0 commit comments

Comments
 (0)