Commit 7f083a62 authored by Kenton Varda's avatar Kenton Varda

Improve readability of schema clasess a bit by making some union members into singleton groups.

parent 2bea8005
......@@ -995,7 +995,7 @@ public:
// Set up stuff for the ValueTranslator.
ValueResolverGlue resolver(compiler->getLoader(), errorReporter);
auto type = arena.getOrphanage().newOrphan<schema::Type>();
type.get().setStruct(rootType.getProto().getId());
type.get().initStruct().setTypeId(rootType.getProto().getId());
// Set up output stream.
kj::FdOutputStream rawOutput(STDOUT_FILENO);
......
......@@ -62,16 +62,16 @@ static constexpr const char* FIELD_SIZE_NAMES[] = {
void enumerateDeps(schema::Type::Reader type, std::set<uint64_t>& deps) {
switch (type.which()) {
case schema::Type::STRUCT:
deps.insert(type.getStruct());
deps.insert(type.getStruct().getTypeId());
break;
case schema::Type::ENUM:
deps.insert(type.getEnum());
deps.insert(type.getEnum().getTypeId());
break;
case schema::Type::INTERFACE:
deps.insert(type.getInterface());
deps.insert(type.getInterface().getTypeId());
break;
case schema::Type::LIST:
enumerateDeps(type.getList(), deps);
enumerateDeps(type.getList().getElementType(), deps);
break;
default:
break;
......@@ -88,7 +88,7 @@ void enumerateDeps(schema::Node::Reader node, std::set<uint64_t>& deps) {
enumerateDeps(field.getNonGroup().getType(), deps);
break;
case schema::Field::GROUP:
deps.insert(field.getGroup());
deps.insert(field.getGroup().getTypeId());
break;
}
}
......@@ -221,14 +221,14 @@ private:
case schema::Type::DATA: return kj::strTree(" ::capnp::Data");
case schema::Type::ENUM:
return cppFullName(schemaLoader.get(type.getEnum()));
return cppFullName(schemaLoader.get(type.getEnum().getTypeId()));
case schema::Type::STRUCT:
return cppFullName(schemaLoader.get(type.getStruct()));
return cppFullName(schemaLoader.get(type.getStruct().getTypeId()));
case schema::Type::INTERFACE:
return cppFullName(schemaLoader.get(type.getInterface()));
return cppFullName(schemaLoader.get(type.getInterface().getTypeId()));
case schema::Type::LIST:
return kj::strTree(" ::capnp::List<", typeName(type.getList()), ">");
return kj::strTree(" ::capnp::List<", typeName(type.getList().getElementType()), ">");
case schema::Type::OBJECT:
// Not used.
......@@ -252,7 +252,7 @@ private:
case schema::Value::FLOAT32: return kj::strTree(value.getFloat32(), "f");
case schema::Value::FLOAT64: return kj::strTree(value.getFloat64());
case schema::Value::ENUM: {
EnumSchema schema = schemaLoader.get(type.getEnum()).asEnum();
EnumSchema schema = schemaLoader.get(type.getEnum().getTypeId()).asEnum();
if (value.getEnum() < schema.getEnumerants().size()) {
return kj::strTree(
cppFullName(schema), "::",
......@@ -437,7 +437,7 @@ private:
break;
}
case schema::Field::GROUP:
getSlots(schema.getDependency(proto.getGroup()).asStruct(), slots);
getSlots(schema.getDependency(proto.getGroup().getTypeId()).asStruct(), slots);
break;
}
}
......@@ -550,7 +550,8 @@ private:
break;
case schema::Field::GROUP: {
auto slots = getSortedSlots(schemaLoader.get(field.getProto().getGroup()).asStruct());
auto slots = getSortedSlots(schemaLoader.get(
field.getProto().getGroup().getTypeId()).asStruct());
return FieldText {
kj::strTree(
kj::mv(unionDiscrim.readerIsDecl),
......@@ -902,7 +903,7 @@ private:
bool isStructList = false;
if (kind == FieldKind::LIST) {
bool primitiveElement = false;
switch (typeBody.getList().which()) {
switch (typeBody.getList().getElementType().which()) {
case schema::Type::VOID:
case schema::Type::BOOL:
case schema::Type::INT8:
......@@ -933,7 +934,7 @@ private:
break;
}
elementReaderType = kj::str(
typeName(typeBody.getList()),
typeName(typeBody.getList().getElementType()),
primitiveElement ? "" : "::Reader");
}
......@@ -1166,7 +1167,7 @@ private:
case schema::Value::LIST: {
kj::String constType = kj::strTree(
"::capnp::_::ConstList<", typeName(type.getList()), ">").flatten();
"::capnp::_::ConstList<", typeName(type.getList().getElementType()), ">").flatten();
return ConstText {
true,
kj::strTree(linkage, "const ", constType, ' ', upperCase, ";\n"),
......@@ -1216,7 +1217,7 @@ private:
if (field.isGroup()) {
nestedTexts.add(makeNodeText(
namespace_, subScope, toTitleCase(field.getName()),
schemaLoader.get(field.getGroup())));
schemaLoader.get(field.getGroup().getTypeId())));
}
}
}
......
......@@ -173,13 +173,13 @@ private:
case schema::Type::TEXT: return kj::strTree("Text");
case schema::Type::DATA: return kj::strTree("Data");
case schema::Type::LIST:
return kj::strTree("List(", genType(type.getList(), scope), ")");
return kj::strTree("List(", genType(type.getList().getElementType(), scope), ")");
case schema::Type::ENUM:
return nodeName(scope.getDependency(type.getEnum()), scope);
return nodeName(scope.getDependency(type.getEnum().getTypeId()), scope);
case schema::Type::STRUCT:
return nodeName(scope.getDependency(type.getStruct()), scope);
return nodeName(scope.getDependency(type.getStruct().getTypeId()), scope);
case schema::Type::INTERFACE:
return nodeName(scope.getDependency(type.getInterface()), scope);
return nodeName(scope.getDependency(type.getInterface().getTypeId()), scope);
case schema::Type::OBJECT: return kj::strTree("Object");
}
return kj::strTree();
......@@ -256,12 +256,13 @@ private:
return kj::strTree(DynamicValue::Reader(value.getData()));
case schema::Value::LIST: {
KJ_REQUIRE(type.isList(), "type/value mismatch");
auto listValue = value.getList<DynamicList>(ListSchema::of(type.getList(), scope));
auto listValue = value.getList<DynamicList>(
ListSchema::of(type.getList().getElementType(), scope));
return kj::strTree(listValue);
}
case schema::Value::ENUM: {
KJ_REQUIRE(type.isEnum(), "type/value mismatch");
auto enumNode = scope.getDependency(type.getEnum()).asEnum().getProto();
auto enumNode = scope.getDependency(type.getEnum().getTypeId()).asEnum().getProto();
auto enumerants = enumNode.getEnum().getEnumerants();
KJ_REQUIRE(value.getEnum() < enumerants.size(),
"Enum value out-of-range.", value.getEnum(), enumNode.getDisplayName());
......@@ -270,7 +271,7 @@ private:
case schema::Value::STRUCT: {
KJ_REQUIRE(type.isStruct(), "type/value mismatch");
auto structValue = value.getStruct<DynamicStruct>(
scope.getDependency(type.getStruct()).asStruct());
scope.getDependency(type.getStruct().getTypeId()).asStruct());
return kj::strTree(structValue);
}
case schema::Value::INTERFACE: {
......@@ -385,7 +386,7 @@ private:
"\n");
}
case schema::Field::GROUP: {
auto group = scope.getDependency(field.getGroup()).asStruct();
auto group = scope.getDependency(field.getGroup().getTypeId()).asStruct();
return kj::strTree(
indent, field.getName(),
" :group", genAnnotations(field.getAnnotations(), scope), " {",
......
......@@ -743,16 +743,16 @@ void Compiler::Node::traverseType(const schema::Type::Reader& type, uint eagerne
uint64_t id = 0;
switch (type.which()) {
case schema::Type::STRUCT:
id = type.getStruct();
id = type.getStruct().getTypeId();
break;
case schema::Type::ENUM:
id = type.getEnum();
id = type.getEnum().getTypeId();
break;
case schema::Type::INTERFACE:
id = type.getInterface();
id = type.getInterface().getTypeId();
break;
case schema::Type::LIST:
traverseType(type.getList(), eagerness, seen);
traverseType(type.getList().getElementType(), eagerness, seen);
return;
default:
return;
......
......@@ -447,7 +447,7 @@ uint getOrdinal(StructSchema::Field field) {
KJ_ASSERT(proto.isGroup());
auto group = field.getContainingStruct().getDependency(proto.getGroup()).asStruct();
auto group = field.getContainingStruct().getDependency(proto.getGroup().getTypeId()).asStruct();
return getOrdinal(group.getFields()[0]);
}
......@@ -466,7 +466,7 @@ Orphan<DynamicValue> makeExampleValue(
case schema::Type::BOOL: return ordinal % 2 == 0;
case schema::Type::TEXT: return orphanage.newOrphanCopy(Text::Reader(kj::str(ordinal)));
case schema::Type::STRUCT: {
auto structType = scope.getDependency(type.getStruct()).asStruct();
auto structType = scope.getDependency(type.getStruct().getTypeId()).asStruct();
auto result = orphanage.newOrphan(structType);
auto builder = result.get();
......@@ -484,11 +484,11 @@ Orphan<DynamicValue> makeExampleValue(
return kj::mv(result);
}
case schema::Type::ENUM: {
auto enumerants = scope.getDependency(type.getEnum()).asEnum().getEnumerants();
auto enumerants = scope.getDependency(type.getEnum().getTypeId()).asEnum().getEnumerants();
return DynamicEnum(enumerants[ordinal %enumerants.size()]);
}
case schema::Type::LIST: {
auto elementType = type.getList();
auto elementType = type.getList().getElementType();
auto listType = ListSchema::of(elementType, scope);
auto result = orphanage.newOrphan(listType, 1);
result.get().adopt(0, makeExampleValue(
......@@ -531,7 +531,8 @@ void checkExampleValue(DynamicValue::Reader value, uint ordinal, schema::Type::R
break;
}
case schema::Type::LIST:
checkExampleValue(value.as<DynamicList>()[0], ordinal, type.getList(), sharedOrdinalCount);
checkExampleValue(value.as<DynamicList>()[0], ordinal, type.getList().getElementType(),
sharedOrdinalCount);
break;
default:
KJ_FAIL_ASSERT("You added a new possible field type!");
......@@ -550,7 +551,7 @@ void setExampleField(DynamicStruct::Builder builder, StructSchema::Field field,
case schema::Field::GROUP:
builder.adopt(field, makeExampleStruct(
Orphanage::getForMessageContaining(builder),
field.getContainingStruct().getDependency(fieldProto.getGroup()).asStruct(),
field.getContainingStruct().getDependency(fieldProto.getGroup().getTypeId()).asStruct(),
sharedOrdinalCount));
break;
}
......@@ -656,7 +657,7 @@ static void loadStructAndGroups(const SchemaLoader& src, SchemaLoader& dst, uint
for (auto field: proto.getStruct().getFields()) {
if (field.isGroup()) {
loadStructAndGroups(src, dst, field.getGroup());
loadStructAndGroups(src, dst, field.getGroup().getTypeId());
}
}
}
......
......@@ -1090,7 +1090,7 @@ private:
uint64_t groupId = generateGroupId(parent->node.getId(), index);
node.setId(groupId);
node.setScopeId(parent->node.getId());
getSchema().setGroup(groupId);
getSchema().initGroup().setTypeId(groupId);
}
}
};
......@@ -1313,9 +1313,9 @@ bool NodeTranslator::compileType(TypeExpression::Reader source, schema::Type::Bu
bool handledParams = false;
switch (base->kind) {
case Declaration::ENUM: target.setEnum(base->id); break;
case Declaration::STRUCT: target.setStruct(base->id); break;
case Declaration::INTERFACE: target.setInterface(base->id); break;
case Declaration::ENUM: target.initEnum().setTypeId(base->id); break;
case Declaration::STRUCT: target.initStruct().setTypeId(base->id); break;
case Declaration::INTERFACE: target.initInterface().setTypeId(base->id); break;
case Declaration::BUILTIN_LIST: {
auto params = source.getParams();
......@@ -1324,7 +1324,7 @@ bool NodeTranslator::compileType(TypeExpression::Reader source, schema::Type::Bu
return false;
}
auto elementType = target.initList();
auto elementType = target.initList().initElementType();
if (!compileType(params[0], elementType)) {
return false;
}
......@@ -1568,7 +1568,7 @@ kj::Maybe<Orphan<DynamicValue>> ValueTranslator::compileValue(
case DynamicValue::LIST:
if (type.isList()) {
KJ_IF_MAYBE(schema, makeListSchemaOf(type.getList())) {
KJ_IF_MAYBE(schema, makeListSchemaOf(type.getList().getElementType())) {
if (result.getReader().as<DynamicList>().getSchema() == *schema) {
return kj::mv(result);
}
......@@ -1580,7 +1580,7 @@ kj::Maybe<Orphan<DynamicValue>> ValueTranslator::compileValue(
case DynamicValue::ENUM:
if (type.isEnum()) {
KJ_IF_MAYBE(schema, resolver.resolveType(type.getEnum())) {
KJ_IF_MAYBE(schema, resolver.resolveType(type.getEnum().getTypeId())) {
if (result.getReader().as<DynamicEnum>().getSchema() == *schema) {
return kj::mv(result);
}
......@@ -1592,7 +1592,7 @@ kj::Maybe<Orphan<DynamicValue>> ValueTranslator::compileValue(
case DynamicValue::STRUCT:
if (type.isStruct()) {
KJ_IF_MAYBE(schema, resolver.resolveType(type.getStruct())) {
KJ_IF_MAYBE(schema, resolver.resolveType(type.getStruct().getTypeId())) {
if (result.getReader().as<DynamicStruct>().getSchema() == *schema) {
return kj::mv(result);
}
......@@ -1625,7 +1625,7 @@ Orphan<DynamicValue> ValueTranslator::compileValueInner(
kj::StringPtr id = name.getBase().getRelativeName().getValue();
if (type.isEnum()) {
KJ_IF_MAYBE(enumSchema, resolver.resolveType(type.getEnum())) {
KJ_IF_MAYBE(enumSchema, resolver.resolveType(type.getEnum().getTypeId())) {
KJ_IF_MAYBE(enumerant, enumSchema->asEnum().findEnumerantByName(id)) {
return DynamicEnum(*enumerant);
}
......@@ -1689,7 +1689,7 @@ Orphan<DynamicValue> ValueTranslator::compileValueInner(
errorReporter.addErrorOn(src, "Type mismatch.");
return nullptr;
}
auto elementType = type.getList();
auto elementType = type.getList().getElementType();
KJ_IF_MAYBE(listSchema, makeListSchemaOf(elementType)) {
auto srcList = src.getList();
Orphan<DynamicList> result = orphanage.newOrphan(*listSchema, srcList.size());
......@@ -1710,7 +1710,7 @@ Orphan<DynamicValue> ValueTranslator::compileValueInner(
errorReporter.addErrorOn(src, "Type mismatch.");
return nullptr;
}
KJ_IF_MAYBE(schema, resolver.resolveType(type.getStruct())) {
KJ_IF_MAYBE(schema, resolver.resolveType(type.getStruct().getTypeId())) {
auto structSchema = schema->asStruct();
Orphan<DynamicStruct> result = orphanage.newOrphan(structSchema);
fillStructValue(result.get(), src.getStruct());
......@@ -1783,10 +1783,11 @@ kj::String ValueTranslator::makeTypeName(schema::Type::Reader type) {
case schema::Type::FLOAT64: return kj::str("Float64");
case schema::Type::TEXT: return kj::str("Text");
case schema::Type::DATA: return kj::str("Data");
case schema::Type::LIST: return kj::str("List(", makeTypeName(type.getList()), ")");
case schema::Type::ENUM: return makeNodeName(type.getEnum());
case schema::Type::STRUCT: return makeNodeName(type.getStruct());
case schema::Type::INTERFACE: return makeNodeName(type.getInterface());
case schema::Type::LIST:
return kj::str("List(", makeTypeName(type.getList().getElementType()), ")");
case schema::Type::ENUM: return makeNodeName(type.getEnum().getTypeId());
case schema::Type::STRUCT: return makeNodeName(type.getStruct().getTypeId());
case schema::Type::INTERFACE: return makeNodeName(type.getInterface().getTypeId());
case schema::Type::OBJECT: return kj::str("Object");
}
KJ_UNREACHABLE;
......@@ -1819,7 +1820,8 @@ kj::Maybe<DynamicValue::Reader> NodeTranslator::readConstant(
auto constType = constReader.getType();
switch (constType.which()) {
case schema::Type::STRUCT:
KJ_IF_MAYBE(structSchema, resolver.resolveBootstrapSchema(constType.getStruct())) {
KJ_IF_MAYBE(structSchema, resolver.resolveBootstrapSchema(
constType.getStruct().getTypeId())) {
constValue = objValue.as(structSchema->asStruct());
} else {
// The struct's schema is broken for reasons already reported.
......@@ -1827,7 +1829,7 @@ kj::Maybe<DynamicValue::Reader> NodeTranslator::readConstant(
}
break;
case schema::Type::LIST:
KJ_IF_MAYBE(listSchema, makeListSchemaOf(constType.getList())) {
KJ_IF_MAYBE(listSchema, makeListSchemaOf(constType.getList().getElementType())) {
constValue = objValue.as(*listSchema);
} else {
// The list's schema is broken for reasons already reported.
......@@ -1881,25 +1883,26 @@ static kj::Maybe<ListSchema> makeListSchemaImpl(schema::Type::Reader elementType
const ResolveTypeFunc& resolveType) {
switch (elementType.which()) {
case schema::Type::ENUM:
KJ_IF_MAYBE(enumSchema, resolveType(elementType.getEnum())) {
KJ_IF_MAYBE(enumSchema, resolveType(elementType.getEnum().getTypeId())) {
return ListSchema::of(enumSchema->asEnum());
} else {
return nullptr;
}
case schema::Type::STRUCT:
KJ_IF_MAYBE(structSchema, resolveType(elementType.getStruct())) {
KJ_IF_MAYBE(structSchema, resolveType(elementType.getStruct().getTypeId())) {
return ListSchema::of(structSchema->asStruct());
} else {
return nullptr;
}
case schema::Type::INTERFACE:
KJ_IF_MAYBE(interfaceSchema, resolveType(elementType.getInterface())) {
KJ_IF_MAYBE(interfaceSchema, resolveType(elementType.getInterface().getTypeId())) {
return ListSchema::of(interfaceSchema->asInterface());
} else {
return nullptr;
}
case schema::Type::LIST:
KJ_IF_MAYBE(listSchema, makeListSchemaImpl(elementType.getList(), resolveType)) {
KJ_IF_MAYBE(listSchema, makeListSchemaImpl(
elementType.getList().getElementType(), resolveType)) {
return ListSchema::of(*listSchema);
} else {
return nullptr;
......
......@@ -232,7 +232,7 @@ DynamicValue::Reader DynamicStruct::Reader::get(StructSchema::Field field) const
uint16_t typedDval;
typedDval = dval.getEnum();
return DynamicEnum(
field.getContainingStruct().getDependency(type.getEnum()).asEnum(),
field.getContainingStruct().getDependency(type.getEnum().getTypeId()).asEnum(),
reader.getDataField<uint16_t>(nonGroup.getOffset() * ELEMENTS, typedDval));
}
......@@ -249,7 +249,7 @@ DynamicValue::Reader DynamicStruct::Reader::get(StructSchema::Field field) const
}
case schema::Type::LIST: {
auto elementType = type.getList();
auto elementType = type.getList().getElementType();
return DynamicList::Reader(
ListSchema::of(elementType, field.getContainingStruct()),
reader.getListField(nonGroup.getOffset() * POINTERS,
......@@ -259,7 +259,7 @@ DynamicValue::Reader DynamicStruct::Reader::get(StructSchema::Field field) const
case schema::Type::STRUCT: {
return DynamicStruct::Reader(
field.getContainingStruct().getDependency(type.getStruct()).asStruct(),
field.getContainingStruct().getDependency(type.getStruct().getTypeId()).asStruct(),
reader.getStructField(nonGroup.getOffset() * POINTERS,
dval.getStruct<_::UncheckedMessage>()));
}
......@@ -279,7 +279,8 @@ DynamicValue::Reader DynamicStruct::Reader::get(StructSchema::Field field) const
}
case schema::Field::GROUP:
return DynamicStruct::Reader(schema.getDependency(proto.getGroup()).asStruct(), reader);
return DynamicStruct::Reader(
schema.getDependency(proto.getGroup().getTypeId()).asStruct(), reader);
}
KJ_UNREACHABLE;
......@@ -324,7 +325,7 @@ DynamicValue::Builder DynamicStruct::Builder::get(StructSchema::Field field) {
uint16_t typedDval;
typedDval = dval.getEnum();
return DynamicEnum(
field.getContainingStruct().getDependency(type.getEnum()).asEnum(),
field.getContainingStruct().getDependency(type.getEnum().getTypeId()).asEnum(),
builder.getDataField<uint16_t>(nonGroup.getOffset() * ELEMENTS, typedDval));
}
......@@ -341,7 +342,8 @@ DynamicValue::Builder DynamicStruct::Builder::get(StructSchema::Field field) {
}
case schema::Type::LIST: {
ListSchema listType = ListSchema::of(type.getList(), field.getContainingStruct());
ListSchema listType = ListSchema::of(type.getList().getElementType(),
field.getContainingStruct());
if (listType.whichElementType() == schema::Type::STRUCT) {
return DynamicList::Builder(listType,
builder.getStructListField(nonGroup.getOffset() * POINTERS,
......@@ -357,7 +359,7 @@ DynamicValue::Builder DynamicStruct::Builder::get(StructSchema::Field field) {
case schema::Type::STRUCT: {
auto structSchema =
field.getContainingStruct().getDependency(type.getStruct()).asStruct();
field.getContainingStruct().getDependency(type.getStruct().getTypeId()).asStruct();
return DynamicStruct::Builder(structSchema,
builder.getStructField(
nonGroup.getOffset() * POINTERS,
......@@ -381,7 +383,8 @@ DynamicValue::Builder DynamicStruct::Builder::get(StructSchema::Field field) {
}
case schema::Field::GROUP:
return DynamicStruct::Builder(schema.getDependency(proto.getGroup()).asStruct(), builder);
return DynamicStruct::Builder(
schema.getDependency(proto.getGroup().getTypeId()).asStruct(), builder);
}
KJ_UNREACHABLE;
......@@ -532,7 +535,8 @@ void DynamicStruct::Builder::set(StructSchema::Field field, const DynamicValue::
case schema::Type::ENUM: {
uint16_t rawValue;
auto enumSchema = field.getContainingStruct().getDependency(type.getEnum()).asEnum();
auto enumSchema = field.getContainingStruct()
.getDependency(type.getEnum().getTypeId()).asEnum();
if (value.getType() == DynamicValue::TEXT) {
// Convert from text.
rawValue = enumSchema.getEnumerantByName(value.as<Text>()).getOrdinal();
......@@ -611,7 +615,7 @@ DynamicValue::Builder DynamicStruct::Builder::init(StructSchema::Field field) {
auto nonGroup = proto.getNonGroup();
auto type = nonGroup.getType();
KJ_REQUIRE(type.isStruct(), "init() without a size is only valid for struct fields.");
auto subSchema = schema.getDependency(type.getStruct()).asStruct();
auto subSchema = schema.getDependency(type.getStruct().getTypeId()).asStruct();
return DynamicStruct::Builder(subSchema,
builder.initStructField(nonGroup.getOffset() * POINTERS,
structSizeFromSchema(subSchema)));
......@@ -619,7 +623,8 @@ DynamicValue::Builder DynamicStruct::Builder::init(StructSchema::Field field) {
case schema::Field::GROUP: {
clear(field);
return DynamicStruct::Builder(schema.getDependency(proto.getGroup()).asStruct(), builder);
return DynamicStruct::Builder(
schema.getDependency(proto.getGroup().getTypeId()).asStruct(), builder);
}
}
......@@ -638,7 +643,7 @@ DynamicValue::Builder DynamicStruct::Builder::init(StructSchema::Field field, ui
auto type = nonGroup.getType();
switch (type.which()) {
case schema::Type::LIST: {
auto listType = ListSchema::of(type.getList(), schema);
auto listType = ListSchema::of(type.getList().getElementType(), schema);
if (listType.whichElementType() == schema::Type::STRUCT) {
return DynamicList::Builder(listType,
builder.initStructListField(
......@@ -707,7 +712,8 @@ void DynamicStruct::Builder::adopt(StructSchema::Field field, Orphan<DynamicValu
break;
case schema::Type::LIST: {
ListSchema listType = ListSchema::of(type.getList(), field.getContainingStruct());
ListSchema listType = ListSchema::of(type.getList().getElementType(),
field.getContainingStruct());
KJ_REQUIRE(orphan.getType() == DynamicValue::LIST && orphan.listSchema == listType,
"Value type mismatch.");
break;
......@@ -715,7 +721,7 @@ void DynamicStruct::Builder::adopt(StructSchema::Field field, Orphan<DynamicValu
case schema::Type::STRUCT: {
auto structType =
field.getContainingStruct().getDependency(type.getStruct()).asStruct();
field.getContainingStruct().getDependency(type.getStruct().getTypeId()).asStruct();
KJ_REQUIRE(orphan.getType() == DynamicValue::STRUCT && orphan.structSchema == structType,
"Value type mismatch.");
break;
......@@ -883,7 +889,8 @@ void DynamicStruct::Builder::clear(StructSchema::Field field) {
}
case schema::Field::GROUP: {
DynamicStruct::Builder group(schema.getDependency(proto.getGroup()).asStruct(), builder);
DynamicStruct::Builder group(
schema.getDependency(proto.getGroup().getTypeId()).asStruct(), builder);
// We clear the union field with discriminant 0 rather than the one that is set because
// we want the union to end up with its default field active.
......@@ -1440,16 +1447,18 @@ DynamicValue::Reader::Reader(ConstSchema constant) {
case schema::Type::DATA: *this = value.getData(); break;
case schema::Type::ENUM:
*this = DynamicEnum(constant.getDependency(typeSchema.getEnum()).asEnum(), value.getEnum());
*this = DynamicEnum(constant.getDependency(
typeSchema.getEnum().getTypeId()).asEnum(), value.getEnum());
break;
case schema::Type::STRUCT:
*this = value.getStruct<DynamicStruct>(
constant.getDependency(typeSchema.getStruct()).asStruct());
constant.getDependency(typeSchema.getStruct().getTypeId()).asStruct());
break;
case schema::Type::LIST:
*this = value.getList<DynamicList>(ListSchema::of(typeSchema.getList(), constant));
*this = value.getList<DynamicList>(
ListSchema::of(typeSchema.getList().getElementType(), constant));
break;
case schema::Type::OBJECT:
......
......@@ -64,10 +64,12 @@ TEST(SchemaLoader, LoadLateUnion) {
loader.load(Schema::from<test::TestLateUnion::TheUnion>().getProto()).asStruct();
loader.load(Schema::from<test::TestLateUnion::AnotherUnion>().getProto()).asStruct();
EXPECT_EQ(6, schema.getDependency(schema.getFieldByName("theUnion").getProto().getGroup())
.asStruct().getFieldByName("grault").getProto().getOrdinal().getExplicit());
EXPECT_EQ(9, schema.getDependency(schema.getFieldByName("anotherUnion").getProto().getGroup())
.asStruct().getFieldByName("corge").getProto().getOrdinal().getExplicit());
EXPECT_EQ(6,
schema.getDependency(schema.getFieldByName("theUnion").getProto().getGroup().getTypeId())
.asStruct().getFieldByName("grault").getProto().getOrdinal().getExplicit());
EXPECT_EQ(9,
schema.getDependency(schema.getFieldByName("anotherUnion").getProto().getGroup().getTypeId())
.asStruct().getFieldByName("corge").getProto().getOrdinal().getExplicit());
EXPECT_TRUE(schema.findFieldByName("corge") == nullptr);
EXPECT_TRUE(schema.findFieldByName("grault") == nullptr);
}
......@@ -185,8 +187,8 @@ Schema loadUnderAlternateTypeId(SchemaLoader& loader, uint64_t id) {
for (auto field: fields) {
if (field.isNonGroup()) {
auto type = field.getNonGroup().getType();
if (type.isStruct() && type.getStruct() == typeId<T>()) {
type.setStruct(id);
if (type.isStruct() && type.getStruct().getTypeId() == typeId<T>()) {
type.getStruct().setTypeId(id);
}
}
}
......
......@@ -326,7 +326,7 @@ private:
case schema::Field::GROUP:
// Require that the group is a struct node.
validateTypeId(field.getGroup(), schema::Node::STRUCT);
validateTypeId(field.getGroup().getTypeId(), schema::Node::STRUCT);
break;
}
......@@ -467,17 +467,17 @@ private:
break;
case schema::Type::STRUCT:
validateTypeId(type.getStruct(), schema::Node::STRUCT);
validateTypeId(type.getStruct().getTypeId(), schema::Node::STRUCT);
break;
case schema::Type::ENUM:
validateTypeId(type.getEnum(), schema::Node::ENUM);
validateTypeId(type.getEnum().getTypeId(), schema::Node::ENUM);
break;
case schema::Type::INTERFACE:
validateTypeId(type.getInterface(), schema::Node::INTERFACE);
validateTypeId(type.getInterface().getTypeId(), schema::Node::INTERFACE);
break;
case schema::Type::LIST:
validate(type.getList());
validate(type.getList().getElementType());
break;
}
......@@ -714,7 +714,8 @@ private:
break;
}
case schema::Field::GROUP:
checkUpgradeToStruct(nonGroup.getType(), replacement.getGroup(), existingNode, field);
checkUpgradeToStruct(nonGroup.getType(), replacement.getGroup().getTypeId(),
existingNode, field);
break;
}
......@@ -724,11 +725,12 @@ private:
case schema::Field::GROUP:
switch (replacement.which()) {
case schema::Field::NON_GROUP:
checkUpgradeToStruct(replacement.getNonGroup().getType(), field.getGroup(),
checkUpgradeToStruct(replacement.getNonGroup().getType(), field.getGroup().getTypeId(),
replacementNode, replacement);
break;
case schema::Field::GROUP:
VALIDATE_SCHEMA(field.getGroup() == replacement.getGroup(), "group id changed");
VALIDATE_SCHEMA(field.getGroup().getTypeId() == replacement.getGroup().getTypeId(),
"group id changed");
break;
}
break;
......@@ -840,10 +842,10 @@ private:
if (upgradeToStructMode == ALLOW_UPGRADE_TO_STRUCT) {
if (type.isStruct()) {
checkUpgradeToStruct(replacement, type.getStruct());
checkUpgradeToStruct(replacement, type.getStruct().getTypeId());
return;
} else if (replacement.isStruct()) {
checkUpgradeToStruct(type, replacement.getStruct());
checkUpgradeToStruct(type, replacement.getStruct().getTypeId());
return;
}
}
......@@ -870,11 +872,13 @@ private:
return;
case schema::Type::LIST:
checkCompatibility(type.getList(), replacement.getList(), ALLOW_UPGRADE_TO_STRUCT);
checkCompatibility(type.getList().getElementType(), replacement.getList().getElementType(),
ALLOW_UPGRADE_TO_STRUCT);
return;
case schema::Type::ENUM:
VALIDATE_SCHEMA(replacement.getEnum() == type.getEnum(), "type changed enum type");
VALIDATE_SCHEMA(replacement.getEnum().getTypeId() == type.getEnum().getTypeId(),
"type changed enum type");
return;
case schema::Type::STRUCT:
......@@ -885,12 +889,12 @@ private:
// be compatible. However, that has another problem, which is that it could be that the
// whole reason the type was replaced was to fork that type, and so an incompatibility
// could be very much expected. This could be a rat hole...
VALIDATE_SCHEMA(replacement.getStruct() == type.getStruct(),
VALIDATE_SCHEMA(replacement.getStruct().getTypeId() == type.getStruct().getTypeId(),
"type changed to incompatible struct type");
return;
case schema::Type::INTERFACE:
VALIDATE_SCHEMA(replacement.getInterface() == type.getInterface(),
VALIDATE_SCHEMA(replacement.getInterface().getTypeId() == type.getInterface().getTypeId(),
"type changed to incompatible interface type");
return;
}
......@@ -1027,7 +1031,7 @@ private:
if (type.isText()) {
return true;
} else if (type.isList()) {
switch (type.getList().which()) {
switch (type.getList().getElementType().which()) {
case schema::Type::INT8:
case schema::Type::UINT8:
return true;
......
......@@ -54,7 +54,7 @@ private:
static uint64_t getFieldTypeFileId(StructSchema::Field field) {
return field.getContainingStruct()
.getDependency(field.getProto().getNonGroup().getType().getStruct())
.getDependency(field.getProto().getNonGroup().getType().getStruct().getTypeId())
.getProto().getScopeId();
}
......
......@@ -100,7 +100,7 @@ TEST(Schema, Unions) {
EXPECT_TRUE(schema.findFieldByName("u1f0s8") == nullptr);
auto union1 = schema.getFieldByName("union1");
auto union1g = schema.getDependency(union1.getProto().getGroup()).asStruct();
auto union1g = schema.getDependency(union1.getProto().getGroup().getTypeId()).asStruct();
EXPECT_EQ(schema, union1g.getDependency(union1g.getProto().getScopeId()));
EXPECT_TRUE(union1g.findFieldByName("bin0") == nullptr);
......@@ -230,7 +230,7 @@ TEST(Schema, Lists) {
auto context = Schema::from<TestAllTypes>();
auto type = context.getFieldByName("enumList").getProto().getNonGroup().getType();
ListSchema schema = ListSchema::of(type.getList(), context);
ListSchema schema = ListSchema::of(type.getList().getElementType(), context);
EXPECT_EQ(schema::Type::ENUM, schema.whichElementType());
EXPECT_TRUE(schema.getEnumElementType() == Schema::from<TestEnum>());
EXPECT_ANY_THROW(schema.getStructElementType());
......
......@@ -293,16 +293,16 @@ ListSchema ListSchema::of(schema::Type::Reader elementType, Schema context) {
return of(elementType.which());
case schema::Type::STRUCT:
return of(context.getDependency(elementType.getStruct()).asStruct());
return of(context.getDependency(elementType.getStruct().getTypeId()).asStruct());
case schema::Type::ENUM:
return of(context.getDependency(elementType.getEnum()).asEnum());
return of(context.getDependency(elementType.getEnum().getTypeId()).asEnum());
case schema::Type::INTERFACE:
return of(context.getDependency(elementType.getInterface()).asInterface());
return of(context.getDependency(elementType.getInterface().getTypeId()).asInterface());
case schema::Type::LIST:
return of(of(elementType.getList(), context));
return of(of(elementType.getList().getElementType(), context));
case schema::Type::OBJECT:
KJ_FAIL_REQUIRE("List(Object) not supported.");
......
......@@ -190,8 +190,12 @@ struct Field {
defaultValue @6 :Value;
}
group @7 :Id;
# A group. This is the ID of the group's node.
group :group {
# A group.
typeId @7 :Id;
# The ID of the group's node.
}
}
ordinal :union {
......@@ -264,11 +268,19 @@ struct Type {
text @12 :Void;
data @13 :Void;
list @14 :Type; # Value = the element type.
list :group {
elementType @14 :Type;
}
enum @15 :Id;
struct @16 :Id;
interface @17 :Id;
enum :group {
typeId @15 :Id;
}
struct :group {
typeId @16 :Id;
}
interface :group {
typeId @17 :Id;
}
object @18 :Void;
}
......
This diff is collapsed.
This diff is collapsed.
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