From f272260145f9813aa7b3365a130342dc9149745b Mon Sep 17 00:00:00 2001 From: Henning Baldersheim Date: Fri, 5 Apr 2019 11:17:04 +0200 Subject: Emit Position struct if necessary. --- vespa-documentgen-plugin/etc/complex/music3.sd | 3 +++ .../main/java/com/yahoo/vespa/DocumentGenMojo.java | 30 ++++++++++++++++++++-- .../test/java/com/yahoo/vespa/DocumentGenTest.java | 12 ++++----- 3 files changed, 37 insertions(+), 8 deletions(-) (limited to 'vespa-documentgen-plugin') diff --git a/vespa-documentgen-plugin/etc/complex/music3.sd b/vespa-documentgen-plugin/etc/complex/music3.sd index 65f37029d04..45ce11fd581 100644 --- a/vespa-documentgen-plugin/etc/complex/music3.sd +++ b/vespa-documentgen-plugin/etc/complex/music3.sd @@ -3,6 +3,9 @@ search music3 { document music3 inherits music2, common2 { field mu3 type string { + } + field pos type position { + } } } 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 7e73d6b5915..bc34a4ac3df 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 @@ -3,11 +3,14 @@ package com.yahoo.vespa; import com.yahoo.collections.Pair; import com.yahoo.document.ArrayDataType; +import com.yahoo.document.CollectionDataType; import com.yahoo.document.DataType; import com.yahoo.document.Field; import com.yahoo.document.MapDataType; +import com.yahoo.document.PositionDataType; import com.yahoo.document.ReferenceDataType; import com.yahoo.document.StructDataType; +import com.yahoo.document.StructuredDataType; import com.yahoo.document.TensorDataType; import com.yahoo.document.WeightedSetDataType; import com.yahoo.document.annotation.AnnotationReferenceDataType; @@ -18,7 +21,6 @@ import com.yahoo.searchdefinition.Search; import com.yahoo.searchdefinition.SearchBuilder; import com.yahoo.searchdefinition.parser.ParseException; import org.apache.maven.plugin.AbstractMojo; -import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.Component; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; @@ -31,6 +33,7 @@ import java.io.FilenameFilter; import java.io.IOException; import java.io.Writer; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Date; import java.util.HashMap; @@ -468,6 +471,9 @@ public class DocumentGenMojo extends AbstractMojo { exportHashCode(allUniqueFields, out, 1, "(getDataType() != null ? getDataType().hashCode() : 0) + getId().hashCode()"); exportEquals(className, allUniqueFields, out, 1); Set exportedStructs = exportStructTypes(docType.getTypes(), out, 1, null); + if (hasAnyPositionField(allUniqueFields)) { + exportedStructs = exportStructTypes(Arrays.asList(PositionDataType.INSTANCE), out, 1, exportedStructs); + } docTypes.put(docType.getName(), packageName+"."+className); for (DataType exportedStruct : exportedStructs) { structTypes.put(exportedStruct.getName(), packageName+"."+className+"."+className(exportedStruct.getName())); @@ -475,6 +481,25 @@ public class DocumentGenMojo extends AbstractMojo { out.write("}\n"); } + private static boolean hasAnyPostionDataType(DataType dt) { + if (dt instanceof CollectionDataType) { + return hasAnyPostionDataType(((CollectionDataType)dt).getNestedType()); + } else if (dt instanceof StructuredDataType) { + return hasAnyPositionField(((StructuredDataType)dt).getFields()); + } else { + return PositionDataType.INSTANCE.equals(dt); + } + } + + private static boolean hasAnyPositionField(Collection fields) { + for (Field f : fields) { + if (hasAnyPostionDataType(f.getDataType())) { + return true; + } + } + return true; + } + private Collection getAllUniqueFields(Boolean multipleInheritance, Collection allFields) { if (multipleInheritance) { Map seen = new HashMap<>(); @@ -732,7 +757,8 @@ public class DocumentGenMojo extends AbstractMojo { ind(ind)+" * Input struct type: "+structType.getName()+"\n" + ind(ind)+" * Date: "+new Date()+"\n" + ind(ind)+" */\n" + - ind(ind)+"@com.yahoo.document.Generated public static class "+structClassName+" extends com.yahoo.document.datatypes.Struct {\n\n" + + ind(ind)+"@com.yahoo.document.Generated\n" + + ind(ind) + "public static class "+structClassName+" extends com.yahoo.document.datatypes.Struct {\n\n" + ind(ind+1)+"/** The type of this.*/\n" + ind(ind+1)+"public static final com.yahoo.document.StructDataType type = getStructType();\n\n"); out.write(ind(ind+1)+"public "+structClassName+"() {\n" + diff --git a/vespa-documentgen-plugin/src/test/java/com/yahoo/vespa/DocumentGenTest.java b/vespa-documentgen-plugin/src/test/java/com/yahoo/vespa/DocumentGenTest.java index b21f38c586a..c195e116bf0 100644 --- a/vespa-documentgen-plugin/src/test/java/com/yahoo/vespa/DocumentGenTest.java +++ b/vespa-documentgen-plugin/src/test/java/com/yahoo/vespa/DocumentGenTest.java @@ -5,8 +5,6 @@ import com.yahoo.document.DataType; import com.yahoo.document.StructDataType; import com.yahoo.document.WeightedSetDataType; import com.yahoo.searchdefinition.Search; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; import org.junit.Test; import java.io.File; @@ -19,7 +17,7 @@ import static org.junit.Assert.fail; public class DocumentGenTest { @Test - public void testMusic() throws MojoExecutionException, MojoFailureException { + public void testMusic() { DocumentGenMojo mojo = new DocumentGenMojo(); mojo.execute(new File("etc/music/"), new File("target/generated-test-sources/vespa-documentgen-plugin/"), "com.yahoo.vespa.document"); Map searches = mojo.getSearches(); @@ -28,19 +26,21 @@ public class DocumentGenTest { } @Test - public void testComplex() throws MojoFailureException { + public void testComplex() { DocumentGenMojo mojo = new DocumentGenMojo(); mojo.execute(new File("etc/complex/"), new File("target/generated-test-sources/vespa-documentgen-plugin/"), "com.yahoo.vespa.document"); Map searches = mojo.getSearches(); assertEquals(searches.get("video").getDocument("video").getField("weight").getDataType(), DataType.FLOAT); assertEquals(searches.get("book").getDocument("book").getField("sw1").getDataType(), DataType.FLOAT); + assertTrue(searches.get("music3").getDocument("music3").getField("pos").getDataType() instanceof StructDataType); + assertEquals(searches.get("music3").getDocument("music3").getField("pos").getDataType().getName(), "position"); assertTrue(searches.get("book").getDocument("book").getField("mystruct").getDataType() instanceof StructDataType); assertTrue(searches.get("book").getDocument("book").getField("mywsfloat").getDataType() instanceof WeightedSetDataType); assertTrue(((WeightedSetDataType)(searches.get("book").getDocument("book").getField("mywsfloat").getDataType())).getNestedType() == DataType.FLOAT); } @Test - public void testLocalApp() throws MojoFailureException { + public void testLocalApp() { DocumentGenMojo mojo = new DocumentGenMojo(); mojo.execute(new File("etc/localapp/"), new File("target/generated-test-sources/vespa-documentgen-plugin/"), "com.yahoo.vespa.document"); Map searches = mojo.getSearches(); @@ -51,7 +51,7 @@ public class DocumentGenTest { } @Test - public void testEmptyPkgNameForbidden() throws MojoFailureException { + public void testEmptyPkgNameForbidden() { DocumentGenMojo mojo = new DocumentGenMojo(); try { mojo.execute(new File("etc/localapp/"), new File("target/generated-test-sources/vespa-documentgen-plugin/"), ""); -- cgit v1.2.3