diff options
author | Joseph Sutton <josephsutton@catalyst.net.nz> | 2023-05-16 09:55:52 +1200 |
---|---|---|
committer | Andrew Bartlett <abartlet@samba.org> | 2023-05-18 01:03:37 +0000 |
commit | 0080148483c2972393d33bf1f2c7dbb248bbb9c0 (patch) | |
tree | d83d60629d4bb48d7ea9896323b42e0141d99f0b | |
parent | 4440f1db54b7ad54b7a4920ac67236d1d8605353 (diff) | |
download | samba-0080148483c2972393d33bf1f2c7dbb248bbb9c0.tar.gz |
lib:audit_logging: Add function to add an optional boolean value to a JSON message
Signed-off-by: Joseph Sutton <josephsutton@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
-rw-r--r-- | lib/audit_logging/audit_logging.c | 44 | ||||
-rw-r--r-- | lib/audit_logging/audit_logging.h | 3 |
2 files changed, 47 insertions, 0 deletions
diff --git a/lib/audit_logging/audit_logging.c b/lib/audit_logging/audit_logging.c index 65d6f3915e9..a7e56b7b6be 100644 --- a/lib/audit_logging/audit_logging.c +++ b/lib/audit_logging/audit_logging.c @@ -451,6 +451,50 @@ int json_add_bool(struct json_object *object, } /* + * @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. * * Add a string value named 'name' to the json object. diff --git a/lib/audit_logging/audit_logging.h b/lib/audit_logging/audit_logging.h index d3eca06b883..2f0935f4c5b 100644 --- a/lib/audit_logging/audit_logging.h +++ b/lib/audit_logging/audit_logging.h @@ -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); |