Skip to content

Commit 003c6e1

Browse files
authored
YAML things: adjustment of messages when checking a thing element (#4761)
Signed-off-by: Laurent Garnier <[email protected]>
1 parent 0b89fbd commit 003c6e1

File tree

2 files changed

+36
-43
lines changed

2 files changed

+36
-43
lines changed

bundles/org.openhab.core.model.yaml/src/main/java/org/openhab/core/model/yaml/internal/things/YamlChannelDTO.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,41 +45,41 @@ public boolean isValid(@NonNull List<@NonNull String> errors, @NonNull List<@Non
4545
boolean ok = true;
4646
if (type != null) {
4747
if (!CHANNEL_TYPE_PATTERN.matcher(type).find()) {
48-
errors.add(
49-
"type \"%s\" is not matching the expected syntax [a-zA-Z0-9_][a-zA-Z0-9_-]*".formatted(type));
48+
errors.add("value \"%s\" for \"type\" field not matching the expected syntax %s".formatted(type,
49+
CHANNEL_TYPE_PATTERN.pattern()));
5050
ok = false;
5151
}
5252
if (kind != null) {
53-
warnings.add(
54-
"kind \"%s\" is ignored as a type is also provided; kind will be retrieved from the channel type"
55-
.formatted(kind));
53+
warnings.add("\"kind\" field ignored; channel kind will be retrieved from the channel type");
5654
}
5755
if (itemType != null) {
56+
warnings.add("\"itemType\" field ignored; item type will be retrieved from the channel type");
57+
}
58+
if (itemDimension != null) {
5859
warnings.add(
59-
"itemType \"%s\" is ignored as a type is also provided; item type will be retrieved from the channel type"
60-
.formatted(itemType));
60+
"\"itemDimension\" field ignored; item type and dimension will be retrieved from the channel type");
6161
}
6262
} else if (itemType != null) {
6363
if (!YamlElementUtils.isValidItemType(itemType)) {
64-
errors.add("itemType \"%s\" is invalid".formatted(itemType));
64+
errors.add("invalid value \"%s\" for \"itemType\" field".formatted(itemType));
6565
ok = false;
6666
} else if (YamlElementUtils.isNumberItemType(itemType)) {
6767
if (!YamlElementUtils.isValidItemDimension(itemDimension)) {
68-
errors.add("itemDimension \"%s\" is invalid".formatted(itemDimension));
68+
errors.add("invalid value \"%s\" for \"itemDimension\" field".formatted(itemDimension));
6969
ok = false;
7070
}
7171
} else if (itemDimension != null) {
72-
warnings.add("itemDimension \"%s\" is is ignored as item type is not Number".formatted(itemDimension));
72+
warnings.add("\"itemDimension\" field ignored as item type is not Number");
7373
}
7474
try {
7575
ChannelKind.parse(kind);
7676
} catch (IllegalArgumentException e) {
7777
warnings.add(
78-
"kind \"%s\" is invalid (only \"state\" and \"trigger\" whatever the case are valid); \"state\" will be considered"
78+
"invalid value \"%s\" for \"kind\" field; only \"state\" and \"trigger\" whatever the case are valid; \"state\" will be considered"
7979
.formatted(kind != null ? kind : "null"));
8080
}
8181
} else {
82-
errors.add("type or itemType is mandatory");
82+
errors.add("one of the \"type\" and \"itemType\" fields is mandatory");
8383
ok = false;
8484
}
8585
return ok;

bundles/org.openhab.core.model.yaml/src/main/java/org/openhab/core/model/yaml/internal/things/YamlThingDTO.java

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -75,44 +75,37 @@ public YamlElement cloneWithoutId() {
7575
public boolean isValid(@Nullable List<@NonNull String> errors, @Nullable List<@NonNull String> warnings) {
7676
// Check that uid is present
7777
if (uid == null || uid.isBlank()) {
78-
if (errors != null) {
79-
errors.add("thing uid is missing");
80-
}
78+
addToList(errors, "invalid thing: uid is missing while mandatory");
8179
return false;
8280
}
8381
boolean ok = true;
8482
// Check that uid has at least 3 segments and each segment respects the expected syntax
8583
String[] segments = uid.split(AbstractUID.SEPARATOR);
8684
if (segments.length < 3) {
87-
if (errors != null) {
88-
errors.add("thing %s: uid contains insufficient segments".formatted(uid));
89-
}
85+
addToList(errors, "invalid thing \"%s\": not enough segments in uid; minimum 3 is expected".formatted(uid));
9086
ok = false;
9187
}
9288
for (String segment : segments) {
9389
if (!THING_UID_SEGMENT_PATTERN.matcher(segment).matches()) {
94-
if (errors != null) {
95-
errors.add("thing %s: segment \"%s\" in uid is not matching the expected syntax %s".formatted(uid,
96-
segment, THING_UID_SEGMENT_PATTERN.pattern()));
97-
}
90+
addToList(errors, "invalid thing \"%s\": segment \"%s\" in uid not matching the expected syntax %s"
91+
.formatted(uid, segment, THING_UID_SEGMENT_PATTERN.pattern()));
9892
ok = false;
9993
}
10094
}
10195
if (bridge != null && !bridge.isBlank()) {
10296
// Check that bridge has at least 3 segments and each segment respects the expected syntax
10397
segments = bridge.split(AbstractUID.SEPARATOR);
10498
if (segments.length < 3) {
105-
if (errors != null) {
106-
errors.add("thing %s: bridge \"%s\" contains insufficient segments".formatted(uid, bridge));
107-
}
99+
addToList(errors,
100+
"invalid thing \"%s\": not enough segments in value \"%s\" for \"bridge\" field; minimum 3 is expected"
101+
.formatted(uid, bridge));
108102
ok = false;
109103
}
110104
for (String segment : segments) {
111105
if (!THING_UID_SEGMENT_PATTERN.matcher(segment).matches()) {
112-
if (errors != null) {
113-
errors.add("thing %s: segment \"%s\" in bridge is not matching the expected syntax %s"
114-
.formatted(uid, segment, THING_UID_SEGMENT_PATTERN.pattern()));
115-
}
106+
addToList(errors,
107+
"invalid thing \"%s\": segment \"%s\" in \"bridge\" field not matching the expected syntax %s"
108+
.formatted(uid, segment, THING_UID_SEGMENT_PATTERN.pattern()));
116109
ok = false;
117110
}
118111
}
@@ -121,30 +114,30 @@ public boolean isValid(@Nullable List<@NonNull String> errors, @Nullable List<@N
121114
for (Map.Entry<@NonNull String, @NonNull YamlChannelDTO> entry : channels.entrySet()) {
122115
String channelId = entry.getKey();
123116
if (!CHANNEL_ID_PATTERN.matcher(channelId).matches()) {
124-
if (errors != null) {
125-
errors.add("thing %s: channel id \"%s\" is not matching the expected syntax %s".formatted(uid,
126-
channelId, CHANNEL_ID_PATTERN.pattern()));
127-
}
117+
addToList(errors, "invalid thing \"%s\": channel id \"%s\" not matching the expected syntax %s"
118+
.formatted(uid, channelId, CHANNEL_ID_PATTERN.pattern()));
128119
ok = false;
129120
}
130121
List<String> channelErrors = new ArrayList<>();
131122
List<String> channelWarnings = new ArrayList<>();
132123
ok &= entry.getValue().isValid(channelErrors, channelWarnings);
133-
if (errors != null) {
134-
channelErrors.forEach(error -> {
135-
errors.add("thing %s channel %s: %s".formatted(uid, channelId, error));
136-
});
137-
}
138-
if (warnings != null) {
139-
channelWarnings.forEach(warning -> {
140-
warnings.add("thing %s channel %s: %s".formatted(uid, channelId, warning));
141-
});
142-
}
124+
channelErrors.forEach(error -> {
125+
addToList(errors, "invalid thing \"%s\": channel \"%s\": %s".formatted(uid, channelId, error));
126+
});
127+
channelWarnings.forEach(warning -> {
128+
addToList(warnings, "thing \"%s\": channel \"%s\": %s".formatted(uid, channelId, warning));
129+
});
143130
}
144131
}
145132
return ok;
146133
}
147134

135+
private void addToList(@Nullable List<@NonNull String> list, String value) {
136+
if (list != null) {
137+
list.add(value);
138+
}
139+
}
140+
148141
public boolean isBridge() {
149142
return isBridge == null ? false : isBridge.booleanValue();
150143
}

0 commit comments

Comments
 (0)