Commit 0e7f7116 authored by Jason Paryani's avatar Jason Paryani

Allow DynamicStruct::Reader/Builder.as<AnyStruct>

Also fix AnyStruct::Reader/Builder as methods
parent 223d5363
......@@ -114,6 +114,12 @@ TEST(Any, AnyStruct) {
checkTestMessage(root.getAnyPointerField().getAs<TestAllTypes>());
checkTestMessage(root.asReader().getAnyPointerField().getAs<TestAllTypes>());
auto all_types = root.getAnyPointerField().getAs<AnyStruct>().as<TestAllTypes>();
auto all_types_reader = root.getAnyPointerField().getAs<AnyStruct>().asReader().as<TestAllTypes>();
all_types.setInt32Field(100);
EXPECT_EQ(100, all_types.getInt32Field());
EXPECT_EQ(100, all_types_reader.getInt32Field());
EXPECT_EQ(48, root.getAnyPointerField().getAs<AnyStruct>().getDataSection().size());
EXPECT_EQ(20, root.getAnyPointerField().getAs<AnyStruct>().getPointerSection().size());
......
......@@ -436,7 +436,9 @@ public:
}
template <typename T>
ReaderFor<T> as();
ReaderFor<T> as() const {
return typename T::Reader(_reader);
}
// T must be a struct type.
private:
_::StructReader _reader;
......@@ -465,6 +467,12 @@ public:
inline operator Reader() const { return Reader(_builder.asReader()); }
inline Reader asReader() const { return Reader(_builder.asReader()); }
template <typename T>
BuilderFor<T> as() {
return typename T::Builder(_builder);
}
// T must be a struct type.
private:
_::StructBuilder _builder;
friend class Orphanage;
......
......@@ -222,6 +222,15 @@ TEST(DynamicApi, DynamicAnyPointers) {
}
}
TEST(DynamicApi, DynamicAnyStructs) {
MallocMessageBuilder builder;
auto root = builder.initRoot<DynamicStruct>(Schema::from<TestAllTypes>());
root.as<AnyStruct>().as<TestAllTypes>().setInt8Field(123);
EXPECT_EQ(root.get("int8Field").as<int8_t>(), 123);
EXPECT_EQ(root.asReader().as<AnyStruct>().as<TestAllTypes>().getInt8Field(), 123);
}
#define EXPECT_MAYBE_EQ(name, exp, expected, actual) \
KJ_IF_MAYBE(name, exp) { \
EXPECT_EQ(expected, actual); \
......
......@@ -1435,6 +1435,7 @@ typename T::Reader DynamicStruct::Reader::as() const {
schema.requireUsableAs<T>();
return typename T::Reader(reader);
}
template <typename T>
typename T::Builder DynamicStruct::Builder::as() {
static_assert(kind<T>() == Kind::STRUCT,
......@@ -1456,6 +1457,16 @@ inline DynamicStruct::Reader DynamicStruct::Builder::asReader() const {
return DynamicStruct::Reader(schema, builder.asReader());
}
template <>
inline AnyStruct::Reader DynamicStruct::Reader::as<AnyStruct>() const {
return AnyStruct::Reader(reader);
}
template <>
inline AnyStruct::Builder DynamicStruct::Builder::as<AnyStruct>() {
return AnyStruct::Builder(builder);
}
template <typename T>
typename T::Pipeline DynamicStruct::Pipeline::releaseAs() {
static_assert(kind<T>() == Kind::STRUCT,
......
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