Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[modbus] Discard data if transformation failed #17457

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,19 @@ public ModbusTransformation(@Nullable List<String> transformationList) {
}
}

/**
* Transform the given value using the configured transformations.
*
* @param value the value to transform
* @return the transformed value. If the transformation failed, return a blank string.
* This could happen in one of these situations:
* - The transformation service is not available.
* - An error occurred when performing transformations.
* - The transformation service intentionally returned null.
*/
public String transform(String value) {
if (transformation != null) {
// return input if transformation failed
return Objects.requireNonNull(transformation.apply(value).orElse(value));
return Objects.requireNonNull(transformation.apply(value).orElse(""));
}

return Objects.requireNonNullElse(constantOutput, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,11 @@ public void testTransformationConstant() {
assertFalse(transformation.isIdentityTransform());
assertEquals("constant", transformation.transform("xx"));
}

@Test
public void testTransformationFailed() {
ModbusTransformation transformation = new ModbusTransformation(List.of("NONEXISTENT(test)"));
assertFalse(transformation.isIdentityTransform());
assertEquals("", transformation.transform("xx"));
}
}