Skip to content

Commit

Permalink
make SchemaVariant work with const
Browse files Browse the repository at this point in the history
  • Loading branch information
WebFreak001 committed Oct 4, 2019
1 parent da52d7a commit d76bb3a
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions source/mongoschema/variant.d
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,21 @@ public:
}

static foreach (Field; Fields)
mixin("Field.Type " ~ Field.name
~ "() @trusted { checkType!(Field.Type); return value.get!(Field.Type); }");
mixin("inout(Field.Type) " ~ Field.name
~ "() @trusted inout { checkType!(Field.Type); return value.get!(Field.Type); }");

void checkType(T)()
void checkType(T)() const
{
if (!isType!T)
throw new Exception("Attempted to access " ~ type ~ " field as " ~ T.stringof);
}

bool isType(T)() @trusted
bool isType(T)() @trusted const
{
return value.type == typeid(T);
}

string type()
string type() const
{
if (!value.hasValue)
return null;
Expand Down Expand Up @@ -221,4 +221,12 @@ unittest
"type": Bson("foo"),
"value": Bson(["x": Bson(3)])
]));

const x = var2;
assert(x.type == "foo");
assert(x.isType!Foo);
assert(typeof(x).toBson(x) == Bson([
"type": Bson("foo"),
"value": Bson(["x": Bson(3)])
]));
}

0 comments on commit d76bb3a

Please sign in to comment.