narendly commented on a change in pull request #1499:
URL: https://github.com/apache/helix/pull/1499#discussion_r514895873
##########
File path:
zookeeper-api/src/main/java/org/apache/helix/zookeeper/util/ZNRecordUtil.java
##########
@@ -62,4 +66,31 @@ public static int getSerializerWriteSizeLimit() {
return writeSizeLimit;
}
+
+ /**
+ * Trims leading/trailing spaces for all strings in Simple/List/Map fields
of a {@link ZNRecord}.
+ *
+ * @param record the {@link ZNRecord} to be trimmed.
+ * @return a {@link ZNRecord} that has been trimmed: strings in its fields
are trimmed.
+ */
+ public static ZNRecord trimFields(ZNRecord record) {
+ Map<String, String> simpleFields =
record.getSimpleFields().entrySet().stream()
+ .collect(Collectors.toMap(e -> e.getKey().trim(), e ->
e.getValue().trim()));
+
+ Map<String, List<String>> listFields =
record.getListFields().entrySet().stream()
+ .collect(Collectors.toMap(e -> e.getKey().trim(),
+ e ->
e.getValue().stream().map(String::trim).collect(Collectors.toList())));
+
+ Map<String, Map<String, String>> mapFields =
record.getMapFields().entrySet().stream()
+ .collect(Collectors.toMap(e1 -> e1.getKey().trim(),
+ e1 -> e1.getValue().entrySet().stream()
+ .collect(Collectors.toMap(e2 -> e2.getKey().trim(), e2 ->
e2.getValue().trim()))));
+
+ ZNRecord trimmedRecord = new ZNRecord(record);
+ trimmedRecord.setSimpleFields(simpleFields);
+ trimmedRecord.setListFields(listFields);
+ trimmedRecord.setMapFields(mapFields);
+
+ return trimmedRecord;
+ }
Review comment:
Two comments:
1. technically spaces are allowed in names I believe? sure it's ugly, but we
haven't enforced the "no-leading/trailing spaces" rule, so these are valid
strings as names.
2. this is probably more minor, but doing trimming this way right here might
not be so ideal because this might increase gc pressure / heap usage.
all in all, I am also of the opinion that we enforce this at the UI level.
this is users' responsibility to make sure their input is correct after all.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]