diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2019-04-05 11:17:04 +0200 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2019-04-05 11:17:57 +0200 |
commit | f272260145f9813aa7b3365a130342dc9149745b (patch) | |
tree | b8a2d1cadbb05fc55da22c797c1787f902d534e3 | |
parent | 1829394ef5376e7b3d8fce9e09cb82468a508237 (diff) |
Emit Position struct if necessary.
6 files changed, 48 insertions, 14 deletions
diff --git a/document/src/main/java/com/yahoo/document/CollectionDataType.java b/document/src/main/java/com/yahoo/document/CollectionDataType.java index a73588a710c..c6420b5e71f 100644 --- a/document/src/main/java/com/yahoo/document/CollectionDataType.java +++ b/document/src/main/java/com/yahoo/document/CollectionDataType.java @@ -32,7 +32,6 @@ public abstract class CollectionDataType extends DataType { return type; } - @SuppressWarnings("deprecation") public DataType getNestedType() { return nestedType; } @@ -58,11 +57,7 @@ public abstract class CollectionDataType extends DataType { return false; } CollectionFieldValue cfv = (CollectionFieldValue) value; - if (equals(cfv.getDataType())) { - //the field value if of this type: - return true; - } - return false; + return equals(cfv.getDataType()); } @Override diff --git a/documentgen-test/etc/complex/music4.sd b/documentgen-test/etc/complex/music4.sd index c8100ba7de2..eab0018360d 100644 --- a/documentgen-test/etc/complex/music4.sd +++ b/documentgen-test/etc/complex/music4.sd @@ -4,5 +4,8 @@ search music4 { field mu4 type string { } + field pos type position { + + } } } 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 be1ee48df6b..b6a0f165ca6 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 @@ -974,5 +974,12 @@ public class DocumentGenPluginTest { book.setVector(Tensor.from("{{x:0}:1.0, {x:1}:2.0, {x:2}:3.0}")); assertEquals("tensor(x{}):{{x:0}:1.0,{x:1}:2.0,{x:2}:3.0}", book.getVector().toString()); } + + @Test + public void testPositionType() { + Music4 book = new Music4(new DocumentId("doc:music4:0")); + book.setPos(new Music4.Position().setX(7).setY(8)); + assertEquals(new Music4.Position().setX(7).setY(8), book.getPos()); + } } 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/"), ""); |