Skip to content

Commit

Permalink
Require unit provider only when really required
Browse files Browse the repository at this point in the history
Also factorize the log of conversion failing.

Signed-off-by: Laurent Garnier <[email protected]>
  • Loading branch information
lolodomo committed Oct 8, 2024
1 parent b308cd7 commit 3d921d6
Showing 1 changed file with 21 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,22 @@ public static Map<String, Object> map(ActionType actionType, Map<String, Object>
*/
public static @Nullable Object map(ActionType actionType, Input input, @Nullable Object argument,
@Nullable UnitProvider unitProvider) {
boolean failed = false;
if (argument == null) {
return null;
}
Matcher matcher = ActionInputsToConfigDescriptionParameters.QUANTITY_TYPE_PATTERN.matcher(input.getType());
if (argument instanceof Double valueDouble) {
// When an integer value is provided as input value, the value type in the Map is Double.
// We have to convert Double type into the target type.
if (matcher.matches() && unitProvider != null) {
if (matcher.matches()) {
try {
return new QuantityType<>(valueDouble, ActionInputsToConfigDescriptionParameters
.getDefaultUnit(matcher.group("dimension"), unitProvider));
if (unitProvider != null) {
return new QuantityType<>(valueDouble, ActionInputsToConfigDescriptionParameters
.getDefaultUnit(matcher.group("dimension"), unitProvider));
}
} catch (IllegalArgumentException e) {
LOGGER.warn(
"Action {} input parameter '{}': converting value {} into type {} failed! Input parameter is ignored.",
actionType.getUID(), input.getName(), argument, input.getType());
return null;
failed = true;
}
} else {
try {
Expand All @@ -110,29 +110,25 @@ public static Map<String, Object> map(ActionType actionType, Map<String, Object>
default -> argument;
};
} catch (NumberFormatException e) {
LOGGER.warn(
"Action {} input parameter '{}': converting value {} into type {} failed! Input parameter is ignored.",
actionType.getUID(), input.getName(), argument, input.getType());
return null;
failed = true;
}
}
} else if (argument instanceof String valueString) {
// String value is accepted to instantiate few target types
if (matcher.matches() && unitProvider != null) {
if (matcher.matches()) {
try {
// The string can contain either a simple decimal value without unit or a decimal value with unit
try {
BigDecimal bigDecimal = new BigDecimal(valueString);
return new QuantityType<>(bigDecimal, ActionInputsToConfigDescriptionParameters
.getDefaultUnit(matcher.group("dimension"), unitProvider));
if (unitProvider != null) {
return new QuantityType<>(bigDecimal, ActionInputsToConfigDescriptionParameters
.getDefaultUnit(matcher.group("dimension"), unitProvider));
}
} catch (NumberFormatException e) {
return new QuantityType<>(valueString);
}
} catch (IllegalArgumentException e) {
LOGGER.warn(
"Action {} input parameter '{}': converting value '{}' into type {} failed! Input parameter is ignored.",
actionType.getUID(), input.getName(), argument, input.getType());
return null;
failed = true;
}
} else {
try {
Expand Down Expand Up @@ -169,13 +165,15 @@ public static Map<String, Object> map(ActionType actionType, Map<String, Object>
default -> argument;
};
} catch (NumberFormatException | DateTimeParseException | ParseException e) {
LOGGER.warn(
"Action {} input parameter '{}': converting value '{}' into type {} failed! Input parameter is ignored.",
actionType.getUID(), input.getName(), argument, input.getType());
return null;
failed = true;
}
}
}
return argument;
if (failed) {
LOGGER.warn(
"Action {} input parameter '{}': converting value '{}' into type {} failed! Input parameter is ignored.",
actionType.getUID(), input.getName(), argument, input.getType());
}
return failed ? null : argument;
}
}

0 comments on commit 3d921d6

Please sign in to comment.