davsclaus commented on code in PR #29007:
URL: https://github.com/apache/camel/pull/29007#discussion_r4451472678
##########
components/camel-plc4x/src/main/java/org/apache/camel/component/plc4x/Plc4XEndpoint.java:
##########
@@ -316,16 +319,30 @@ public PlcWriteRequest buildPlcWriteRequest(Map<String,
Map<String, Object>> tag
}
PlcWriteRequest.Builder builder = current.writeRequestBuilder();
- for (Map.Entry<String, Map<String, Object>> entry : tags.entrySet()) {
+ for (Map.Entry<String, ?> entry : tags.entrySet()) {
//Tags are stored like this --> Map<Tagname,Map<Query,Value>> for
writing
String name = entry.getKey();
- String query = entry.getValue().keySet().iterator().next();
- Object value = entry.getValue().get(query);
- builder.addTagAddress(name, query, value);
+ // the body of a message is not checked for these types, so a
value may be anything
+ if (!(entry.getValue() instanceof Map<?, ?> queryAndValue) ||
queryAndValue.size() != 1
+ || !(queryAndValue.keySet().iterator().next() instanceof
String query)) {
+ // the exception ends the message with " invalid"
+ throw new PlcInvalidTagException(
+ "Tag " + name + ": the value must be a Map of one
address of the tag to the value to write,"
+ + " such as {\"%DB1:DINT\":
21}, but " + describe(entry.getValue())
+ + " is");
+ }
+ builder.addTagAddress(name, query, queryAndValue.get(query));
}
return builder.build();
}
+ private static String describe(Object value) {
+ if (value instanceof Map<?, ?> map) {
+ return map.isEmpty() ? "a Map without an address" : "a Map of " +
map.size() + " addresses";
Review Comment:
Applied: `describe()` now says `a Map of 1 address` for a size-1 Map (and
`addresses` otherwise).
`Plc4XEndpointTest.buildPlcWriteRequestFailsForAnAddressThatIsNotAString`
asserts the singular message. Commit 34ff4158b432; module suite 51 tests green.
_Claude Code on behalf of Claus Ibsen (davsclaus)_
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]