Search in sources :

Example 1 with Variable

use of com.nextdoor.bender.operation.substitution.Variable in project bender by Nextdoor.

the class FormattedSubstitution method doSubstitution.

@Override
protected void doSubstitution(InternalEvent ievent, DeserializedEvent devent, Map<String, Object> nested) {
    Object[] values = new Object[this.variables.size()];
    List<String> keyToRemove = new ArrayList<String>();
    for (int i = 0; i < this.variables.size(); i++) {
        Variable<?> variable = this.variables.get(i);
        /*
       * Get values
       */
        if (variable instanceof Variable.FieldVariable) {
            Pair<String, Object> kv = null;
            try {
                kv = getFieldAndSource(devent, ((Variable.FieldVariable) variable).getSrcFields(), true);
                if (((Variable.FieldVariable) variable).getRemoveSrcField()) {
                    keyToRemove.add(kv.getKey());
                }
            } catch (FieldNotFoundException e) {
                if (((Variable.FieldVariable) variable).getFailSrcNotFound()) {
                    throw new OperationException(e);
                }
            }
            if (kv != null) {
                values[i] = kv.getValue();
            } else {
                /*
           * This handles the case of when fail src not found is false
           */
                values[i] = null;
            }
        } else if (variable instanceof Variable.StaticVariable) {
            values[i] = ((Variable.StaticVariable) variable).getValue();
        }
    }
    /*
     * Format string with values
     */
    String formatted = format.format(values);
    /*
     * Perform substitution
     */
    if (nested != null) {
        nested.put(this.key, formatted);
        keyToRemove.forEach(fieldName -> {
            if (fieldName.equals(this.key)) {
                return;
            }
            try {
                devent.removeField(fieldName);
            } catch (FieldNotFoundException e) {
            }
        });
        return;
    }
    try {
        devent.setField(this.key, formatted);
    } catch (FieldNotFoundException e) {
        if (this.failDstNotFound) {
            throw new OperationException(e);
        }
    }
    /*
     * Remove source fields
     */
    keyToRemove.forEach(fieldName -> {
        if (fieldName.equals(this.key)) {
            return;
        }
        try {
            devent.removeField(fieldName);
        } catch (FieldNotFoundException e) {
        }
    });
}
Also used : Variable(com.nextdoor.bender.operation.substitution.Variable) ArrayList(java.util.ArrayList) FieldNotFoundException(com.nextdoor.bender.deserializer.FieldNotFoundException) OperationException(com.nextdoor.bender.operation.OperationException)

Aggregations

FieldNotFoundException (com.nextdoor.bender.deserializer.FieldNotFoundException)1 OperationException (com.nextdoor.bender.operation.OperationException)1 Variable (com.nextdoor.bender.operation.substitution.Variable)1 ArrayList (java.util.ArrayList)1