summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTor Brede Vekterli <vekterli@yahooinc.com>2023-02-22 16:30:32 +0100
committerTor Brede Vekterli <vekterli@yahooinc.com>2023-02-22 16:38:27 +0100
commit19da4dc4a9d36646d985a9a4dbd74910932a5099 (patch)
treefc5199531ffb5637cb06cc1db2bb964559a1afc2
parentfc02e9f48338cb2d0288e8297381de319f71610e (diff)
Emit type inheritance statements for concrete document types
This relates to issue #26126 where inheritance specified in the schema was not propagated to the generated meta type, causing `is-a` type checks to not give the expected results.
-rw-r--r--documentgen-test/src/test/java/com/yahoo/vespa/config/DocumentGenPluginTest.java33
-rw-r--r--vespa-documentgen-plugin/src/main/java/com/yahoo/vespa/DocumentGenMojo.java24
2 files changed, 50 insertions, 7 deletions
diff --git a/documentgen-test/src/test/java/com/yahoo/vespa/config/DocumentGenPluginTest.java b/documentgen-test/src/test/java/com/yahoo/vespa/config/DocumentGenPluginTest.java
index 680853ef687..bd2b057835c 100644
--- a/documentgen-test/src/test/java/com/yahoo/vespa/config/DocumentGenPluginTest.java
+++ b/documentgen-test/src/test/java/com/yahoo/vespa/config/DocumentGenPluginTest.java
@@ -47,8 +47,10 @@ import com.yahoo.vespa.documentgen.test.Book;
import com.yahoo.vespa.documentgen.test.Book.Ss0;
import com.yahoo.vespa.documentgen.test.Book.Ss1;
import com.yahoo.vespa.documentgen.test.Common;
+import com.yahoo.vespa.documentgen.test.Common2;
import com.yahoo.vespa.documentgen.test.ConcreteDocumentFactory;
import com.yahoo.vespa.documentgen.test.Music;
+import com.yahoo.vespa.documentgen.test.Music2;
import com.yahoo.vespa.documentgen.test.Music3;
import com.yahoo.vespa.documentgen.test.Music4;
import com.yahoo.vespa.documentgen.test.Parent;
@@ -1024,5 +1026,36 @@ public class DocumentGenPluginTest {
assertTrue(docType.hasImportedField("my_foo"));
assertFalse(docType.hasImportedField("some_field_that_does_not_exist"));
}
+
+ @Test
+ public void subtypes_are_tagged_as_inheriting_supertypes() {
+ // music -> common
+ assertTrue(Music.type.isA("common"));
+ assertTrue(Music.type.inherits(Common.type));
+ // ... but not common2
+ assertFalse(Music.type.inherits(Common2.type));
+
+ // music3 -> (music2 -> common), common2
+ assertTrue(Music3.type.isA("common"));
+ assertTrue(Music3.type.isA("common2"));
+ assertTrue(Music3.type.isA("music2"));
+ assertTrue(Music3.type.inherits(Common.type));
+ assertTrue(Music3.type.inherits(Common2.type));
+ assertTrue(Music3.type.inherits(Music2.type));
+ // ... but not parent
+ assertFalse(Music3.type.isA("parent"));
+ assertFalse(Music3.type.inherits(Parent.type));
+
+ // music4 -> music3 -> (music2 -> common), common2
+ assertTrue(Music4.type.inherits(Common.type));
+ assertTrue(Music4.type.inherits(Common2.type));
+ assertTrue(Music4.type.inherits(Music2.type));
+ assertTrue(Music4.type.inherits(Music3.type));
+ // ... but not music
+ assertFalse(Music4.type.inherits(Music.type));
+
+ // parent has no explicit inheritance
+ assertFalse(Parent.type.isA("common"));
+ }
}
diff --git a/vespa-documentgen-plugin/src/main/java/com/yahoo/vespa/DocumentGenMojo.java b/vespa-documentgen-plugin/src/main/java/com/yahoo/vespa/DocumentGenMojo.java
index 0f3531720ae..d753f3d9e73 100644
--- a/vespa-documentgen-plugin/src/main/java/com/yahoo/vespa/DocumentGenMojo.java
+++ b/vespa-documentgen-plugin/src/main/java/com/yahoo/vespa/DocumentGenMojo.java
@@ -448,7 +448,7 @@ public class DocumentGenMojo extends AbstractMojo {
out.write(ind(1)+"@Override protected boolean isGenerated() { return true; }\n\n");
Collection<Field> allUniqueFields = getAllUniqueFields(multiExtends, docType.getAllFields());
- exportExtendedStructTypeGetter(className, docType.getName(), allUniqueFields, docType.getFieldSets(),
+ exportExtendedStructTypeGetter(className, docType.getName(), docType.getInherited(), allUniqueFields, docType.getFieldSets(),
docType.getImportedFieldNames(), out, 1, "getDocumentType", "com.yahoo.document.DocumentType");
exportCopyConstructor(className, out, 1, true);
@@ -612,14 +612,17 @@ public class DocumentGenMojo extends AbstractMojo {
out.write(ind(ind) + "importedFieldNames.add(\"" + importedField + "\");\n");
}
}
- private static void exportExtendedStructTypeGetter(String className, String name, Collection<Field> fields, Set<FieldSet> fieldSets,
- Set<String> importedFieldNames, Writer out, int ind, String methodName, String retType) throws IOException {
+ private static void exportExtendedStructTypeGetter(String className, String name, Collection<NewDocumentType> parentTypes,
+ Collection<Field> fields, Set<FieldSet> fieldSets,
+ Set<String> importedFieldNames, Writer out, int ind,
+ String methodName, String retType) throws IOException {
out.write(ind(ind)+"private static "+retType+" "+methodName+"() {\n");
+ String bodyIndent = ind(ind + 1);
if (importedFieldNames != null) {
exportImportedFields(importedFieldNames, out, ind + 1);
- out.write(ind(ind+1)+retType+" ret = new "+retType+"(\"" + name + "\", importedFieldNames);\n");
+ out.write(bodyIndent+retType+" ret = new "+retType+"(\"" + name + "\", importedFieldNames);\n");
} else {
- out.write(ind(ind+1)+retType+" ret = new "+retType+"(\""+name+"\");\n");
+ out.write(bodyIndent+retType+" ret = new "+retType+"(\""+name+"\");\n");
}
for (Field f : fields) {
if (f.getDataType().equals(DataType.STRING)) {
@@ -631,8 +634,13 @@ public class DocumentGenMojo extends AbstractMojo {
if (fieldSets != null) {
exportFieldSetDefinition(fieldSets, out, ind+1);
}
+ for (NewDocumentType parentType : parentTypes) {
+ if (!parentType.getName().equals("document")) {
+ out.write("%sret.inherit(%s.type);\n".formatted(bodyIndent, className(parentType.getName())));
+ }
+ }
- out.write(ind(ind+1)+"return ret;\n");
+ out.write(bodyIndent+"return ret;\n");
out.write(ind(ind)+"}\n\n");
}
@@ -762,7 +770,9 @@ public class DocumentGenMojo extends AbstractMojo {
ind(ind+2)+"super("+structClassName+".type);\n" +
ind(ind+1)+"}\n\n");
exportCopyConstructor(structClassName, out, ind+1, false);
- exportExtendedStructTypeGetter(structClassName, structType.getName(), structType.getFields(), null, null, out, ind+1, "getStructType", "com.yahoo.document.StructDataType");
+ exportExtendedStructTypeGetter(structClassName, structType.getName(), List.of(),
+ structType.getFields(), null, null, out, ind+1, "getStructType",
+ "com.yahoo.document.StructDataType");
exportAssign(structType, structClassName, out, ind+1);
exportFieldsAndAccessors(structClassName, structType.getFields(), out, ind+1, true);