control:utils: Modifier `less_than` default value

Support an optional `default_value` to be provided on `less_than`
modifiers where that value, if given, is used as the value returned when
no entry is found instead of returning the default for the data type of
the parameter value.

Change-Id: Ide067a06896ac73ff3a0df6f646d8eca57b097fe
Signed-off-by: Matthew Barth <msbarth@us.ibm.com>
diff --git a/control/json/utils/modifier.cpp b/control/json/utils/modifier.cpp
index 6a9d278..2374960 100644
--- a/control/json/utils/modifier.cpp
+++ b/control/json/utils/modifier.cpp
@@ -50,8 +50,16 @@
  * @brief Return a default value to use when the argument passed
  *        to LessThanOperator is out of range.
  */
-PropertyVariantType getDefaultValue(const PropertyVariantType& val)
+PropertyVariantType
+    getDefaultValue(const PropertyVariantType& val,
+                    const std::optional<PropertyVariantType>& defaultValue)
 {
+    // When a default value is given, return that
+    if (defaultValue)
+    {
+        return *defaultValue;
+    }
+
     if (std::holds_alternative<bool>(val))
     {
         return false;
@@ -85,8 +93,8 @@
  */
 struct MinusOperator : public Modifier::BaseOperator
 {
-    MinusOperator(const json& valueObj) :
-        arg(ConfigBase::getJsonValue(valueObj))
+    MinusOperator(const json& jsonObj) :
+        arg(ConfigBase::getJsonValue(jsonObj["value"]))
     {}
 
     PropertyVariantType operator()(double val) override
@@ -128,13 +136,14 @@
 };
 
 /**
- * @brief Implements an operator to return a value specified in
- *        the JSON that is chosen based on if the value passed
- *        into the operator is less than the lowest arg_value it
- *        is true for.
+ * @brief Implements an operator to return a value specified in the JSON that is
+ * chosen based on if the value passed into the operator is less than the lowest
+ * arg_value it is true for or the given `default_value` if not found to be less
+ * than any entries.
  *
  * "modifier": {
  *  "operator": "less_than",
+ *  "default_value": 1000, // OPTIONAL
  *  "value": [
  *    {
  *      "arg_value": 30, // if value is less than 30
@@ -147,14 +156,15 @@
  *   ]
  *  }
  *
- * If the value passed in is higher than the highest arg_value,
- * it returns a default value this is chosen based on the
- * data type of parameter_value.
+ * If the value passed in is higher than the highest arg_value, it returns a
+ * default value this is the `default_value` given or based on the data type of
+ * parameter_value.
  */
 struct LessThanOperator : public Modifier::BaseOperator
 {
-    LessThanOperator(const json& valueArray)
+    LessThanOperator(const json& jsonObj)
     {
+        const auto& valueArray = jsonObj["value"];
         if (!valueArray.is_array())
         {
             log<level::ERR>(
@@ -204,6 +214,11 @@
                                 .c_str());
             throw std::invalid_argument("Invalid modifier JSON");
         }
+
+        if (jsonObj.contains("default_value"))
+        {
+            defaultValue = ConfigBase::getJsonValue(jsonObj["default_value"]);
+        }
     }
 
     PropertyVariantType operator()(double val) override
@@ -216,7 +231,7 @@
             }
         }
         // Return a default value based on last entry type
-        return getDefaultValue(rangeValues.back().second);
+        return getDefaultValue(rangeValues.back().second, defaultValue);
     }
 
     PropertyVariantType operator()(int32_t val) override
@@ -228,7 +243,7 @@
                 return rangeValue.second;
             }
         }
-        return getDefaultValue(rangeValues.back().second);
+        return getDefaultValue(rangeValues.back().second, defaultValue);
     }
 
     PropertyVariantType operator()(int64_t val) override
@@ -240,7 +255,7 @@
                 return rangeValue.second;
             }
         }
-        return getDefaultValue(rangeValues.back().second);
+        return getDefaultValue(rangeValues.back().second, defaultValue);
     }
 
     PropertyVariantType operator()(const std::string& val) override
@@ -253,7 +268,7 @@
                 return rangeValue.second;
             }
         }
-        return getDefaultValue(rangeValues.back().second);
+        return getDefaultValue(rangeValues.back().second, defaultValue);
     }
 
     PropertyVariantType operator()(bool val) override
@@ -264,6 +279,7 @@
 
     std::vector<std::pair<PropertyVariantType, PropertyVariantType>>
         rangeValues;
+    std::optional<PropertyVariantType> defaultValue;
 };
 
 Modifier::Modifier(const json& jsonObj)
@@ -287,11 +303,11 @@
 
     if (op == "minus")
     {
-        _operator = std::make_unique<MinusOperator>(jsonObj["value"]);
+        _operator = std::make_unique<MinusOperator>(jsonObj);
     }
     else if (op == "less_than")
     {
-        _operator = std::make_unique<LessThanOperator>(jsonObj["value"]);
+        _operator = std::make_unique<LessThanOperator>(jsonObj);
     }
     else
     {