mirror of https://github.com/apache/lucene.git
SOLR-1547 TemplateTransformer should copy array/list more intelligently
git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@881766 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
6e8521565c
commit
cb500c0534
|
@ -22,6 +22,7 @@ New Features
|
|||
----------------------
|
||||
|
||||
* SOLR-1525 allow DIH to refer to core properties (noble)
|
||||
* SOLR-1547 TemplateTransformer copy objects more intelligently when there when the template is a single variable (noble)
|
||||
|
||||
Optimizations
|
||||
----------------------
|
||||
|
|
|
@ -49,6 +49,7 @@ import org.slf4j.LoggerFactory;
|
|||
public class TemplateTransformer extends Transformer {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(TemplateTransformer.class);
|
||||
private Map<String ,List<String>> templateVsVars = new HashMap<String, List<String>>();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Object transformRow(Map<String, Object> row, Context context) {
|
||||
|
@ -67,7 +68,7 @@ public class TemplateTransformer extends Transformer {
|
|||
|
||||
// Verify if all variables can be resolved or not
|
||||
boolean resolvable = true;
|
||||
List<String> variables = TemplateString.getVariables(expr);
|
||||
List<String> variables = getVars(expr);
|
||||
for (String v : variables) {
|
||||
if (resolver.resolve(v) == null) {
|
||||
LOG.warn("Unable to resolve variable: " + v
|
||||
|
@ -78,13 +79,26 @@ public class TemplateTransformer extends Transformer {
|
|||
|
||||
if (!resolvable)
|
||||
continue;
|
||||
if(variables.size() == 1 && expr.startsWith("${") && expr.endsWith("}")){
|
||||
row.put(column, resolver.resolve(variables.get(0)));
|
||||
} else {
|
||||
row.put(column, resolver.replaceTokens(expr));
|
||||
}
|
||||
|
||||
row.put(column, resolver.replaceTokens(expr));
|
||||
}
|
||||
|
||||
|
||||
return row;
|
||||
}
|
||||
|
||||
private List<String> getVars(String expr) {
|
||||
List<String> result = this.templateVsVars.get(expr);
|
||||
if(result == null){
|
||||
result = TemplateString.getVariables(expr);
|
||||
this.templateVsVars.put(expr, result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static final String TEMPLATE = "template";
|
||||
}
|
||||
|
|
|
@ -22,6 +22,7 @@ import org.junit.Test;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
|
@ -43,14 +44,20 @@ public class TestTemplateTransformer {
|
|||
fields.add(AbstractDataImportHandlerTest.createMap("column", "name",
|
||||
TemplateTransformer.TEMPLATE,
|
||||
"${e.lastName}, ${e.firstName} ${e.middleName}"));
|
||||
fields.add(AbstractDataImportHandlerTest.createMap("column", "emails",
|
||||
TemplateTransformer.TEMPLATE,
|
||||
"${e.mail}"));
|
||||
|
||||
// test reuse of template output in another template
|
||||
fields.add(AbstractDataImportHandlerTest.createMap("column", "mrname",
|
||||
TemplateTransformer.TEMPLATE,"Mr ${e.name}"));
|
||||
|
||||
|
||||
List<String> mails = Arrays.asList(new String[]{"a@b.com", "c@d.com"});
|
||||
Map row = AbstractDataImportHandlerTest.createMap(
|
||||
"firstName", "Shalin",
|
||||
"middleName", "Shekhar",
|
||||
"lastName", "Mangar");
|
||||
"lastName", "Mangar",
|
||||
"mail", mails);
|
||||
|
||||
VariableResolverImpl resolver = new VariableResolverImpl();
|
||||
resolver.addNamespace("e", row);
|
||||
|
@ -62,6 +69,7 @@ public class TestTemplateTransformer {
|
|||
new TemplateTransformer().transformRow(row, context);
|
||||
Assert.assertEquals("Mangar, Shalin Shekhar", row.get("name"));
|
||||
Assert.assertEquals("Mr Mangar, Shalin Shekhar", row.get("mrname"));
|
||||
Assert.assertEquals(mails,row.get("emails"));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue