summaryrefslogtreecommitdiffstats
path: root/docproc
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2019-06-28 12:39:19 +0200
committerHenning Baldersheim <balder@yahoo-inc.com>2019-06-28 12:39:19 +0200
commitb809a87e9d0d94d2ca238f8ab2f4d687130ddbf2 (patch)
tree95387c50a2e50e4d38355c8038729a645e94cdd6 /docproc
parente8d94725ce3698a764b4710f486f4358e1360df7 (diff)
Use a utility method for upgrading to concrete types.
Diffstat (limited to 'docproc')
-rw-r--r--docproc/abi-spec.json3
-rw-r--r--docproc/src/main/java/com/yahoo/docproc/AbstractConcreteDocumentFactory.java27
2 files changed, 25 insertions, 5 deletions
diff --git a/docproc/abi-spec.json b/docproc/abi-spec.json
index 65ca886efaf..dee2d2172e4 100644
--- a/docproc/abi-spec.json
+++ b/docproc/abi-spec.json
@@ -11,7 +11,8 @@
"public abstract java.util.Map documentTypes()",
"public abstract java.util.Map structTypes()",
"public abstract java.util.Map annotationTypes()",
- "public abstract com.yahoo.document.Document getDocumentCopy(java.lang.String, com.yahoo.document.datatypes.StructuredFieldValue, com.yahoo.document.DocumentId)"
+ "public abstract com.yahoo.document.Document getDocumentCopy(java.lang.String, com.yahoo.document.datatypes.StructuredFieldValue, com.yahoo.document.DocumentId)",
+ "public com.yahoo.document.datatypes.FieldValue optionallyUpgrade(com.yahoo.document.Field, com.yahoo.document.datatypes.FieldValue)"
],
"fields": []
},
diff --git a/docproc/src/main/java/com/yahoo/docproc/AbstractConcreteDocumentFactory.java b/docproc/src/main/java/com/yahoo/docproc/AbstractConcreteDocumentFactory.java
index 8f771418959..d31573c76a4 100644
--- a/docproc/src/main/java/com/yahoo/docproc/AbstractConcreteDocumentFactory.java
+++ b/docproc/src/main/java/com/yahoo/docproc/AbstractConcreteDocumentFactory.java
@@ -1,15 +1,15 @@
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.docproc;
-import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import com.yahoo.document.Document;
import com.yahoo.document.DocumentId;
+import com.yahoo.document.Field;
import com.yahoo.document.annotation.Annotation;
+import com.yahoo.document.datatypes.FieldValue;
import com.yahoo.document.datatypes.Struct;
import com.yahoo.document.datatypes.StructuredFieldValue;
-import com.yahoo.yolean.Exceptions;
+
/**
* Subtyped by factory classes for concrete document types. The factory classes are auto-generated
@@ -28,5 +28,24 @@ public abstract class AbstractConcreteDocumentFactory extends com.yahoo.componen
*
* @return A concrete document instance
*/
- public abstract com.yahoo.document.Document getDocumentCopy(java.lang.String type, com.yahoo.document.datatypes.StructuredFieldValue src, com.yahoo.document.DocumentId id);
+ public abstract Document getDocumentCopy(java.lang.String type, StructuredFieldValue src, DocumentId id);
+
+ /**
+ * If the FieldValue is a StructuredFieldValue it will upgrade to the concrete type
+ * @param field
+ * @param fv
+ * @return fv or upgraded fv
+ */
+ public FieldValue optionallyUpgrade(Field field, FieldValue fv) {
+ if (fv instanceof StructuredFieldValue) {
+ try {
+ return structTypes().get(field.getDataType().getName())
+ .getConstructor(StructuredFieldValue.class)
+ .newInstance(fv);
+ } catch (java.lang.Exception ex) {
+ throw new RuntimeException(ex);
+ }
+ }
+ return fv;
+ }
}