]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
lib:audit_logging: Add function to add an optional boolean value to a JSON message
authorJoseph Sutton <josephsutton@catalyst.net.nz>
Mon, 15 May 2023 21:55:52 +0000 (09:55 +1200)
committerAndrew Bartlett <abartlet@samba.org>
Thu, 18 May 2023 01:03:37 +0000 (01:03 +0000)
Signed-off-by: Joseph Sutton <josephsutton@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
lib/audit_logging/audit_logging.c
lib/audit_logging/audit_logging.h

index 65d6f3915e97145a1ab669996e2561a53c1c8ca9..a7e56b7b6be6d02ff0d3d36e0fffbcd4452b7be6 100644 (file)
@@ -450,6 +450,50 @@ int json_add_bool(struct json_object *object,
        return ret;
 }
 
+/*
+ * @brief Add an optional boolean value to a JSON object.
+ *
+ * Add an optional boolean value named 'name' to the json object.
+ *
+ * @param object the JSON object to be updated.
+ * @param name the name.
+ * @param value the value.
+ *
+ * @return 0 the operation was successful
+ *        -1 the operation failed
+ *
+ */
+int json_add_optional_bool(struct json_object *object,
+                          const char *name,
+                          const bool *value)
+{
+       int ret = 0;
+
+       if (json_is_invalid(object)) {
+               DBG_ERR("Unable to add boolean [%s] value [%d], "
+                       "target object is invalid\n",
+                       name,
+                       *value);
+               return JSON_ERROR;
+       }
+
+       if (value != NULL) {
+               ret = json_object_set_new(object->root, name, json_boolean(*value));
+               if (ret != 0) {
+                       DBG_ERR("Unable to add boolean [%s] value [%d]\n", name, *value);
+                       return ret;
+               }
+       } else {
+               ret = json_object_set_new(object->root, name, json_null());
+               if (ret != 0) {
+                       DBG_ERR("Unable to add null boolean [%s]\n", name);
+                       return ret;
+               }
+       }
+
+       return ret;
+}
+
 /*
  * @brief Add a string value to a JSON object.
  *
index d3eca06b88321d290ecc58281783e33964a3ef4a..2f0935f4c5befb9e8ab6ad67fe968ae341427724 100644 (file)
@@ -62,6 +62,9 @@ _WARN_UNUSED_RESULT_ int json_add_int(struct json_object *object,
 _WARN_UNUSED_RESULT_ int json_add_bool(struct json_object *object,
                                       const char *name,
                                       const bool value);
+_WARN_UNUSED_RESULT_ int json_add_optional_bool(struct json_object *object,
+                                               const char *name,
+                                               const bool *value);
 _WARN_UNUSED_RESULT_ int json_add_string(struct json_object *object,
                                         const char *name,
                                         const char *value);