Commit daa62fd1 authored by Kenton Varda's avatar Kenton Varda

Update sample code for new union syntax.

parent 066ce2e5
...@@ -98,7 +98,6 @@ using ::capnp::DynamicValue; ...@@ -98,7 +98,6 @@ using ::capnp::DynamicValue;
using ::capnp::DynamicStruct; using ::capnp::DynamicStruct;
using ::capnp::DynamicEnum; using ::capnp::DynamicEnum;
using ::capnp::DynamicList; using ::capnp::DynamicList;
using ::capnp::DynamicUnion;
using ::capnp::List; using ::capnp::List;
using ::capnp::Schema; using ::capnp::Schema;
using ::capnp::StructSchema; using ::capnp::StructSchema;
...@@ -133,7 +132,7 @@ void dynamicWriteAddressBook(int fd, StructSchema schema) { ...@@ -133,7 +132,7 @@ void dynamicWriteAddressBook(int fd, StructSchema schema) {
auto phone0 = alicePhones[0].as<DynamicStruct>(); auto phone0 = alicePhones[0].as<DynamicStruct>();
phone0.set("number", "555-1212"); phone0.set("number", "555-1212");
phone0.set("type", "mobile"); phone0.set("type", "mobile");
alice.get("employment").as<DynamicUnion>() alice.get("employment").as<DynamicStruct>()
.set("school", "MIT"); .set("school", "MIT");
auto bob = people[1].as<DynamicStruct>(); auto bob = people[1].as<DynamicStruct>();
...@@ -149,7 +148,7 @@ void dynamicWriteAddressBook(int fd, StructSchema schema) { ...@@ -149,7 +148,7 @@ void dynamicWriteAddressBook(int fd, StructSchema schema) {
bobPhones[0].setType(Person::PhoneNumber::Type::HOME); bobPhones[0].setType(Person::PhoneNumber::Type::HOME);
bobPhones[1].setNumber("555-7654"); bobPhones[1].setNumber("555-7654");
bobPhones[1].setType(Person::PhoneNumber::Type::WORK); bobPhones[1].setType(Person::PhoneNumber::Type::WORK);
bob.get("employment").as<DynamicUnion>() bob.get("employment").as<DynamicStruct>()
.set("unemployed", ::capnp::VOID); .set("unemployed", ::capnp::VOID);
writePackedMessageToFd(fd, message); writePackedMessageToFd(fd, message);
...@@ -208,33 +207,20 @@ void dynamicPrintValue(DynamicValue::Reader value) { ...@@ -208,33 +207,20 @@ void dynamicPrintValue(DynamicValue::Reader value) {
std::cout << "("; std::cout << "(";
auto structValue = value.as<DynamicStruct>(); auto structValue = value.as<DynamicStruct>();
bool first = true; bool first = true;
for (auto member: for (auto field: structValue.getSchema().getFields()) {
structValue.getSchema().getMembers()) { if (!structValue.has(field)) continue;
if (first) { if (first) {
first = false; first = false;
} else { } else {
std::cout << ", "; std::cout << ", ";
} }
std::cout << member.getProto().getName().cStr() std::cout << field.getProto().getName().cStr()
<< " = "; << " = ";
dynamicPrintValue(structValue.get(member)); dynamicPrintValue(structValue.get(field));
} }
std::cout << ")"; std::cout << ")";
break; break;
} }
case DynamicValue::UNION: {
auto unionValue = value.as<DynamicUnion>();
KJ_IF_MAYBE(tag, unionValue.which()) {
std::cout << tag->getProto().getName().cStr() << "(";
dynamicPrintValue(unionValue.get());
std::cout << ")";
} else {
// Unknown union member; must have come from newer
// version of the protocol.
std::cout << "?";
}
break;
}
default: default:
// There are other types, we aren't handling them. // There are other types, we aren't handling them.
std::cout << "?"; std::cout << "?";
......
...@@ -20,11 +20,11 @@ struct Person { ...@@ -20,11 +20,11 @@ struct Person {
} }
} }
employment @4 union { employment :union {
unemployed @5 :Void; unemployed @4 :Void;
employer @6 :Text; employer @5 :Text;
school @7 :Text; school @6 :Text;
selfEmployed @8 :Void; selfEmployed @7 :Void;
# We assume that a person is only one of these. # We assume that a person is only one of these.
} }
} }
......
...@@ -30,11 +30,11 @@ struct Person { ...@@ -30,11 +30,11 @@ struct Person {
} }
} }
employment @4 union { employment :union {
unemployed @5 :Void; unemployed @4 :Void;
employer @6 :Text; employer @5 :Text;
school @7 :Text; school @6 :Text;
selfEmployed @8 :Void; selfEmployed @7 :Void;
# We assume that a person is only one of these. # We assume that a person is only one of these.
} }
} }
...@@ -391,7 +391,6 @@ using ::capnp::DynamicValue; ...@@ -391,7 +391,6 @@ using ::capnp::DynamicValue;
using ::capnp::DynamicStruct; using ::capnp::DynamicStruct;
using ::capnp::DynamicEnum; using ::capnp::DynamicEnum;
using ::capnp::DynamicList; using ::capnp::DynamicList;
using ::capnp::DynamicUnion;
using ::capnp::List; using ::capnp::List;
using ::capnp::Schema; using ::capnp::Schema;
using ::capnp::StructSchema; using ::capnp::StructSchema;
...@@ -426,7 +425,7 @@ void dynamicWriteAddressBook(int fd, StructSchema schema) { ...@@ -426,7 +425,7 @@ void dynamicWriteAddressBook(int fd, StructSchema schema) {
auto phone0 = alicePhones[0].as<DynamicStruct>(); auto phone0 = alicePhones[0].as<DynamicStruct>();
phone0.set("number", "555-1212"); phone0.set("number", "555-1212");
phone0.set("type", "mobile"); phone0.set("type", "mobile");
alice.get("employment").as<DynamicUnion>() alice.get("employment").as<DynamicStruct>()
.set("school", "MIT"); .set("school", "MIT");
auto bob = people[1].as<DynamicStruct>(); auto bob = people[1].as<DynamicStruct>();
...@@ -442,7 +441,7 @@ void dynamicWriteAddressBook(int fd, StructSchema schema) { ...@@ -442,7 +441,7 @@ void dynamicWriteAddressBook(int fd, StructSchema schema) {
bobPhones[0].setType(Person::PhoneNumber::Type::HOME); bobPhones[0].setType(Person::PhoneNumber::Type::HOME);
bobPhones[1].setNumber("555-7654"); bobPhones[1].setNumber("555-7654");
bobPhones[1].setType(Person::PhoneNumber::Type::WORK); bobPhones[1].setType(Person::PhoneNumber::Type::WORK);
bob.get("employment").as<DynamicUnion>() bob.get("employment").as<DynamicStruct>()
.set("unemployed", ::capnp::VOID); .set("unemployed", ::capnp::VOID);
writePackedMessageToFd(fd, message); writePackedMessageToFd(fd, message);
...@@ -501,33 +500,20 @@ void dynamicPrintValue(DynamicValue::Reader value) { ...@@ -501,33 +500,20 @@ void dynamicPrintValue(DynamicValue::Reader value) {
std::cout << "("; std::cout << "(";
auto structValue = value.as<DynamicStruct>(); auto structValue = value.as<DynamicStruct>();
bool first = true; bool first = true;
for (auto member: for (auto field: structValue.getSchema().getFields()) {
structValue.getSchema().getMembers()) { if (!structValue.has(field)) continue;
if (first) { if (first) {
first = false; first = false;
} else { } else {
std::cout << ", "; std::cout << ", ";
} }
std::cout << member.getProto().getName().cStr() std::cout << field.getProto().getName().cStr()
<< " = "; << " = ";
dynamicPrintValue(structValue.get(member)); dynamicPrintValue(structValue.get(field));
} }
std::cout << ")"; std::cout << ")";
break; break;
} }
case DynamicValue::UNION: {
auto unionValue = value.as<DynamicUnion>();
KJ_IF_MAYBE(tag, unionValue.which()) {
std::cout << tag->getProto().getName().cStr() << "(";
dynamicPrintValue(unionValue.get());
std::cout << ")";
} else {
// Unknown union member; must have come from newer
// version of the protocol.
std::cout << "?";
}
break;
}
default: default:
// There are other types, we aren't handling them. // There are other types, we aren't handling them.
std::cout << "?"; std::cout << "?";
......
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