YAML things: adjustment of messages when checking a thing element (#4761)

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
This commit is contained in:
lolodomo
2025-04-30 06:42:08 +02:00
committed by GitHub
parent 0b89fbd2cd
commit 003c6e18e1
2 changed files with 36 additions and 43 deletions
@@ -45,41 +45,41 @@ public class YamlChannelDTO {
boolean ok = true;
if (type != null) {
if (!CHANNEL_TYPE_PATTERN.matcher(type).find()) {
errors.add(
"type \"%s\" is not matching the expected syntax [a-zA-Z0-9_][a-zA-Z0-9_-]*".formatted(type));
errors.add("value \"%s\" for \"type\" field not matching the expected syntax %s".formatted(type,
CHANNEL_TYPE_PATTERN.pattern()));
ok = false;
}
if (kind != null) {
warnings.add(
"kind \"%s\" is ignored as a type is also provided; kind will be retrieved from the channel type"
.formatted(kind));
warnings.add("\"kind\" field ignored; channel kind will be retrieved from the channel type");
}
if (itemType != null) {
warnings.add("\"itemType\" field ignored; item type will be retrieved from the channel type");
}
if (itemDimension != null) {
warnings.add(
"itemType \"%s\" is ignored as a type is also provided; item type will be retrieved from the channel type"
.formatted(itemType));
"\"itemDimension\" field ignored; item type and dimension will be retrieved from the channel type");
}
} else if (itemType != null) {
if (!YamlElementUtils.isValidItemType(itemType)) {
errors.add("itemType \"%s\" is invalid".formatted(itemType));
errors.add("invalid value \"%s\" for \"itemType\" field".formatted(itemType));
ok = false;
} else if (YamlElementUtils.isNumberItemType(itemType)) {
if (!YamlElementUtils.isValidItemDimension(itemDimension)) {
errors.add("itemDimension \"%s\" is invalid".formatted(itemDimension));
errors.add("invalid value \"%s\" for \"itemDimension\" field".formatted(itemDimension));
ok = false;
}
} else if (itemDimension != null) {
warnings.add("itemDimension \"%s\" is is ignored as item type is not Number".formatted(itemDimension));
warnings.add("\"itemDimension\" field ignored as item type is not Number");
}
try {
ChannelKind.parse(kind);
} catch (IllegalArgumentException e) {
warnings.add(
"kind \"%s\" is invalid (only \"state\" and \"trigger\" whatever the case are valid); \"state\" will be considered"
"invalid value \"%s\" for \"kind\" field; only \"state\" and \"trigger\" whatever the case are valid; \"state\" will be considered"
.formatted(kind != null ? kind : "null"));
}
} else {
errors.add("type or itemType is mandatory");
errors.add("one of the \"type\" and \"itemType\" fields is mandatory");
ok = false;
}
return ok;
@@ -75,26 +75,20 @@ public class YamlThingDTO implements YamlElement, Cloneable {
public boolean isValid(@Nullable List<@NonNull String> errors, @Nullable List<@NonNull String> warnings) {
// Check that uid is present
if (uid == null || uid.isBlank()) {
if (errors != null) {
errors.add("thing uid is missing");
}
addToList(errors, "invalid thing: uid is missing while mandatory");
return false;
}
boolean ok = true;
// Check that uid has at least 3 segments and each segment respects the expected syntax
String[] segments = uid.split(AbstractUID.SEPARATOR);
if (segments.length < 3) {
if (errors != null) {
errors.add("thing %s: uid contains insufficient segments".formatted(uid));
}
addToList(errors, "invalid thing \"%s\": not enough segments in uid; minimum 3 is expected".formatted(uid));
ok = false;
}
for (String segment : segments) {
if (!THING_UID_SEGMENT_PATTERN.matcher(segment).matches()) {
if (errors != null) {
errors.add("thing %s: segment \"%s\" in uid is not matching the expected syntax %s".formatted(uid,
segment, THING_UID_SEGMENT_PATTERN.pattern()));
}
addToList(errors, "invalid thing \"%s\": segment \"%s\" in uid not matching the expected syntax %s"
.formatted(uid, segment, THING_UID_SEGMENT_PATTERN.pattern()));
ok = false;
}
}
@@ -102,17 +96,16 @@ public class YamlThingDTO implements YamlElement, Cloneable {
// Check that bridge has at least 3 segments and each segment respects the expected syntax
segments = bridge.split(AbstractUID.SEPARATOR);
if (segments.length < 3) {
if (errors != null) {
errors.add("thing %s: bridge \"%s\" contains insufficient segments".formatted(uid, bridge));
}
addToList(errors,
"invalid thing \"%s\": not enough segments in value \"%s\" for \"bridge\" field; minimum 3 is expected"
.formatted(uid, bridge));
ok = false;
}
for (String segment : segments) {
if (!THING_UID_SEGMENT_PATTERN.matcher(segment).matches()) {
if (errors != null) {
errors.add("thing %s: segment \"%s\" in bridge is not matching the expected syntax %s"
.formatted(uid, segment, THING_UID_SEGMENT_PATTERN.pattern()));
}
addToList(errors,
"invalid thing \"%s\": segment \"%s\" in \"bridge\" field not matching the expected syntax %s"
.formatted(uid, segment, THING_UID_SEGMENT_PATTERN.pattern()));
ok = false;
}
}
@@ -121,30 +114,30 @@ public class YamlThingDTO implements YamlElement, Cloneable {
for (Map.Entry<@NonNull String, @NonNull YamlChannelDTO> entry : channels.entrySet()) {
String channelId = entry.getKey();
if (!CHANNEL_ID_PATTERN.matcher(channelId).matches()) {
if (errors != null) {
errors.add("thing %s: channel id \"%s\" is not matching the expected syntax %s".formatted(uid,
channelId, CHANNEL_ID_PATTERN.pattern()));
}
addToList(errors, "invalid thing \"%s\": channel id \"%s\" not matching the expected syntax %s"
.formatted(uid, channelId, CHANNEL_ID_PATTERN.pattern()));
ok = false;
}
List<String> channelErrors = new ArrayList<>();
List<String> channelWarnings = new ArrayList<>();
ok &= entry.getValue().isValid(channelErrors, channelWarnings);
if (errors != null) {
channelErrors.forEach(error -> {
errors.add("thing %s channel %s: %s".formatted(uid, channelId, error));
});
}
if (warnings != null) {
channelWarnings.forEach(warning -> {
warnings.add("thing %s channel %s: %s".formatted(uid, channelId, warning));
});
}
channelErrors.forEach(error -> {
addToList(errors, "invalid thing \"%s\": channel \"%s\": %s".formatted(uid, channelId, error));
});
channelWarnings.forEach(warning -> {
addToList(warnings, "thing \"%s\": channel \"%s\": %s".formatted(uid, channelId, warning));
});
}
}
return ok;
}
private void addToList(@Nullable List<@NonNull String> list, String value) {
if (list != null) {
list.add(value);
}
}
public boolean isBridge() {
return isBridge == null ? false : isBridge.booleanValue();
}