2020import io .streamthoughts .kafka .connect .filepulse .data .TypedStruct ;
2121import io .streamthoughts .kafka .connect .filepulse .expression .Expression ;
2222import io .streamthoughts .kafka .connect .filepulse .expression .StandardEvaluationContext ;
23+ import io .streamthoughts .kafka .connect .filepulse .expression .ValueExpression ;
2324import io .streamthoughts .kafka .connect .filepulse .expression .parser .regex .RegexExpressionParser ;
2425import io .streamthoughts .kafka .connect .filepulse .reader .RecordsIterable ;
2526import org .apache .kafka .common .config .ConfigDef ;
@@ -36,10 +37,12 @@ public class AppendFilter extends AbstractMergeRecordFilter<AppendFilter> {
3637 private AppendFilterConfig config ;
3738
3839 private List <Expression > values ;
39- private Expression target ;
40+ private Expression fieldExpression ;
4041
4142 private RegexExpressionParser parser ;
4243
44+ private boolean mustEvaluateWriteExpression = true ;
45+
4346 /**
4447 * {@inheritDoc}
4548 */
@@ -50,8 +53,15 @@ public void configure(final Map<String, ?> props) {
5053
5154 parser = new RegexExpressionParser ();
5255 // currently, multiple expressions is not supported
53- values = Collections .singletonList (parser .parseExpression (config .value (), DEFAULT_ROOT_OBJECT , true ));
54- target = parser .parseExpression (config .field ());
56+ values = Collections .singletonList (parser .parseExpression (config .value (), DEFAULT_ROOT_OBJECT ));
57+
58+ fieldExpression = parser .parseExpression (config .field (), DEFAULT_ROOT_OBJECT );
59+
60+ // Check whether the field expression can be pre-evaluated
61+ if (fieldExpression instanceof ValueExpression ) {
62+ fieldExpression = evaluateWriteExpression (new StandardEvaluationContext (new Object ()));
63+ mustEvaluateWriteExpression = false ;
64+ }
5565 }
5666
5767 /**
@@ -70,34 +80,42 @@ protected RecordsIterable<TypedStruct> apply(final FilterContext context,
7080 final TypedStruct record ) throws FilterException {
7181
7282 InternalFilterContext internalContext = (InternalFilterContext ) context ;
83+ internalContext .setValue (record );
7384
7485 StandardEvaluationContext readEvaluationContext = new StandardEvaluationContext (
7586 internalContext ,
7687 internalContext .variables ()
7788 );
7889
79- final String evaluatedTarget = target .readValue (readEvaluationContext , String .class );
80- final Expression targetExpression = parser .parseExpression (evaluatedTarget , DEFAULT_ROOT_OBJECT , false );
90+ final Expression writeExpression = evaluateWriteExpression (readEvaluationContext );
8191
8292 final TypedStruct target = TypedStruct .struct ();
8393 for (final Expression expression : values ) {
8494
8595 internalContext .setValue (record );
86- final Object typed = expression .readValue (readEvaluationContext );
96+ final Object value = expression .readValue (readEvaluationContext );
8797
88- if (typed != null ) {
98+ if (value != null ) {
8999 internalContext .setValue (target );
90100
91101 final StandardEvaluationContext writeEvaluationContext = new StandardEvaluationContext (
92102 internalContext ,
93103 internalContext .variables ()
94104 );
95- targetExpression .writeValue (typed , writeEvaluationContext );
105+ writeExpression .writeValue (value , writeEvaluationContext );
96106 }
97107 }
98108 return RecordsIterable .of (target );
99109 }
100110
111+ private Expression evaluateWriteExpression (final StandardEvaluationContext evaluationContext ) {
112+ if (mustEvaluateWriteExpression ) {
113+ final String evaluated = fieldExpression .readValue (evaluationContext , String .class );
114+ return parser .parseExpression (evaluated , DEFAULT_ROOT_OBJECT , false );
115+ }
116+ return fieldExpression ;
117+ }
118+
101119 /**
102120 * {@inheritDoc}
103121 */
0 commit comments