Commit 8ba8d95b authored by Kenton Varda's avatar Kenton Varda

Dynamic API: Allow setting an AnyPointer field from any pointer type.

parent 8a2dcdd0
......@@ -158,6 +158,37 @@ TEST(DynamicApi, AnyPointers) {
checkList<uint32_t>(root.getAnyPointerField().getAs<DynamicList>(
Schema::from<List<uint32_t>>()), {123u, 456u, 789u, 123456789u});
}
// Setting an AnyPointer to various types should work.
toDynamic(root).set("anyPointerField", capnp::Text::Reader("foo"));
EXPECT_EQ("foo", root.getAnyPointerField().getAs<Text>());
{
auto orphan = builder.getOrphanage().newOrphan<TestAllTypes>();
initTestMessage(orphan.get());
toDynamic(root).set("anyPointerField", orphan.getReader());
checkTestMessage(root.getAnyPointerField().getAs<TestAllTypes>());
toDynamic(root).adopt("anyPointerField", kj::mv(orphan));
checkTestMessage(root.getAnyPointerField().getAs<TestAllTypes>());
}
{
auto lorphan = builder.getOrphanage().newOrphan<List<uint32_t>>(3);
lorphan.get().set(0, 12);
lorphan.get().set(1, 34);
lorphan.get().set(2, 56);
toDynamic(root).set("anyPointerField", lorphan.getReader());
auto l = root.getAnyPointerField().getAs<List<uint32_t>>();
ASSERT_EQ(3, l.size());
EXPECT_EQ(12, l[0]);
EXPECT_EQ(34, l[1]);
EXPECT_EQ(56, l[2]);
}
// Just compile this one.
toDynamic(root).set("anyPointerField", Capability::Client(nullptr));
root.getAnyPointerField().getAs<Capability>();
}
TEST(DynamicApi, DynamicAnyPointers) {
......
......@@ -558,10 +558,41 @@ void DynamicStruct::Builder::set(StructSchema::Field field, const DynamicValue::
return;
}
case schema::Type::ANY_POINTER:
AnyPointer::Builder(builder.getPointerField(slot.getOffset() * POINTERS))
.set(value.as<AnyPointer>());
case schema::Type::ANY_POINTER: {
auto target = AnyPointer::Builder(builder.getPointerField(slot.getOffset() * POINTERS));
switch (value.getType()) {
case DynamicValue::Type::TEXT:
target.setAs<Text>(value.as<Text>());
return;
case DynamicValue::Type::DATA:
target.setAs<Data>(value.as<Data>());
return;
case DynamicValue::Type::LIST:
target.setAs<DynamicList>(value.as<DynamicList>());
return;
case DynamicValue::Type::STRUCT:
target.setAs<DynamicStruct>(value.as<DynamicStruct>());
return;
case DynamicValue::Type::CAPABILITY:
target.setAs<DynamicCapability>(value.as<DynamicCapability>());
return;
case DynamicValue::Type::ANY_POINTER:
target.set(value.as<AnyPointer>());
return;
case DynamicValue::Type::UNKNOWN:
case DynamicValue::Type::VOID:
case DynamicValue::Type::BOOL:
case DynamicValue::Type::INT:
case DynamicValue::Type::UINT:
case DynamicValue::Type::FLOAT:
case DynamicValue::Type::ENUM:
KJ_FAIL_ASSERT("Value type mismatch; expected AnyPointer");
}
KJ_UNREACHABLE;
}
case schema::Type::INTERFACE: {
auto interfaceType = type.asInterface();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment