aboutsummaryrefslogtreecommitdiffstats
path: root/vespa-documentgen-plugin
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2019-04-05 11:17:04 +0200
committerHenning Baldersheim <balder@yahoo-inc.com>2019-04-05 11:17:57 +0200
commitf272260145f9813aa7b3365a130342dc9149745b (patch)
treeb8a2d1cadbb05fc55da22c797c1787f902d534e3 /vespa-documentgen-plugin
parent1829394ef5376e7b3d8fce9e09cb82468a508237 (diff)
Emit Position struct if necessary.
Diffstat (limited to 'vespa-documentgen-plugin')
-rw-r--r--vespa-documentgen-plugin/etc/complex/music3.sd3
-rw-r--r--vespa-documentgen-plugin/src/main/java/com/yahoo/vespa/DocumentGenMojo.java30
-rw-r--r--vespa-documentgen-plugin/src/test/java/com/yahoo/vespa/DocumentGenTest.java12
3 files changed, 37 insertions, 8 deletions
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
@@ -4,5 +4,8 @@ search music3 {
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<DataType> 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<Field> fields) {
+ for (Field f : fields) {
+ if (hasAnyPostionDataType(f.getDataType())) {
+ return true;
+ }
+ }
+ return true;
+ }
+
private Collection<Field> getAllUniqueFields(Boolean multipleInheritance, Collection<Field> allFields) {
if (multipleInheritance) {
Map<String, Field> 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<String, Search> 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<String, Search> 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<String, Search> 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/"), "");