[Asterisk-code-review] json: Add a object create with vars method and integer access macro (asterisk[18])

Kevin Harwell asteriskteam at digium.com
Thu Oct 21 12:45:52 CDT 2021


Kevin Harwell has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/16641 )


Change subject: json: Add a object create with vars method and integer access macro
......................................................................

json: Add a object create with vars method and integer access macro

Add a function that will create an ast_json object out of a list of
Asterisk variables. An excludes string can also optionally be passed
in.

Also, add a macro to make it easier to get object integers.

Change-Id: I5f34f18e102126aef3997f19a553a266d70d6226
---
M include/asterisk/json.h
M main/json.c
M tests/test_json.c
3 files changed, 84 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/41/16641/1

diff --git a/include/asterisk/json.h b/include/asterisk/json.h
index ce0baa4..e8d6bd7 100644
--- a/include/asterisk/json.h
+++ b/include/asterisk/json.h
@@ -539,6 +539,15 @@
 struct ast_json *ast_json_object_create(void);
 
 /*!
+ * \brief Create a new JSON object using the given variables
+ * \param variables A list of Asterisk variables
+ * \param excludes Variable names to exclude (optional)
+ * \return Newly allocated object.
+ * \return \c NULL on error.
+ */
+struct ast_json *ast_json_object_create_vars(const struct ast_variable *variables, const char *excludes);
+
+/*!
  * \brief Get size of JSON object.
  * \since 12.0.0
  * \param object JSON object.
@@ -573,6 +582,14 @@
 #define ast_json_object_string_get(object, key) ast_json_string_get(ast_json_object_get(object, key))
 
 /*!
+ * \brief Get an integer field from a JSON object.
+ * \param integer JSON integer.
+ * \return Value of a JSON integer.
+ * \return 0 if \a integer is not a JSON integer.
+ */
+#define ast_json_object_integer_get(object, key) ast_json_integer_get(ast_json_object_get(object, key))
+
+/*!
  * \brief Set a field in a JSON object.
  * \since 12.0.0
  *
diff --git a/main/json.c b/main/json.c
index 09101aa..abaae6d 100644
--- a/main/json.c
+++ b/main/json.c
@@ -852,3 +852,22 @@
 
 	return ret;
 }
+
+struct ast_json *ast_json_object_create_vars(const struct ast_variable *variables, const char *excludes)
+{
+	const struct ast_variable *i;
+	struct ast_json *obj;
+
+	obj = ast_json_object_create();
+	if (!obj) {
+		return NULL;
+	}
+
+	for (i = variables; i; i = i->next) {
+		if (!excludes || !strstr(excludes, i->name)) {
+			ast_json_object_set(obj, i->name, ast_json_string_create(i->value));
+		}
+	}
+
+	return obj;
+}
diff --git a/tests/test_json.c b/tests/test_json.c
index 8dbb872..edf4a9d 100644
--- a/tests/test_json.c
+++ b/tests/test_json.c
@@ -37,6 +37,7 @@
 
 #include "asterisk.h"
 
+#include "asterisk/config.h"
 #include "asterisk/json.h"
 #include "asterisk/module.h"
 #include "asterisk/test.h"
@@ -1090,6 +1091,51 @@
 	return AST_TEST_PASS;
 }
 
+AST_TEST_DEFINE(json_test_object_create_vars)
+{
+	RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
+	RAII_VAR(struct ast_variable *, vars, NULL, ast_variables_destroy);
+	const char *value;
+	struct ast_variable *new_var;
+
+	switch (cmd) {
+	case TEST_INIT:
+		info->name = "object_create_vars";
+		info->category = CATEGORY;
+		info->summary = "Testing JSON object creation initialized using Asterisk variables.";
+		info->description = "Test JSON abstraction library.";
+		return AST_TEST_NOT_RUN;
+	case TEST_EXECUTE:
+		break;
+	}
+
+	/* NULL case */
+	ast_test_validate(test, (uut = ast_json_object_create_vars(NULL, NULL)));
+	ast_test_validate(test, !(value = ast_json_object_string_get(uut, "foo")));
+
+	ast_test_validate(test, (new_var = ast_variable_new("foo", "bar", "")));
+	ast_variable_list_append(&vars, new_var);
+	ast_test_validate(test, (new_var = ast_variable_new("bar", "baz", "")));
+	ast_variable_list_append(&vars, new_var);
+
+	/* Variables case */
+	ast_json_unref(uut);
+	ast_test_validate(test, (uut = ast_json_object_create_vars(vars, NULL)));
+	ast_test_validate(test, (value = ast_json_object_string_get(uut, "foo")));
+	ast_test_validate(test, !strcmp("bar", value));
+	ast_test_validate(test, (value = ast_json_object_string_get(uut, "bar")));
+	ast_test_validate(test, !strcmp("baz", value));
+
+	/* Variables with excludes case */
+	ast_json_unref(uut);
+	ast_test_validate(test, (uut = ast_json_object_create_vars(vars, "foo")));
+	ast_test_validate(test, !(value = ast_json_object_string_get(uut, "foo")));
+	ast_test_validate(test, (value = ast_json_object_string_get(uut, "bar")));
+	ast_test_validate(test, !strcmp("baz", value));
+
+	return AST_TEST_PASS;
+}
+
 AST_TEST_DEFINE(json_test_dump_load_string)
 {
 	RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
@@ -1738,6 +1784,7 @@
 	AST_TEST_UNREGISTER(json_test_object_null);
 	AST_TEST_UNREGISTER(json_test_object_iter);
 	AST_TEST_UNREGISTER(json_test_object_iter_null);
+	AST_TEST_UNREGISTER(json_test_object_create_vars);
 	AST_TEST_UNREGISTER(json_test_dump_load_string);
 	AST_TEST_UNREGISTER(json_test_dump_load_str);
 	AST_TEST_UNREGISTER(json_test_dump_str_fail);
@@ -1794,6 +1841,7 @@
 	AST_TEST_REGISTER(json_test_object_null);
 	AST_TEST_REGISTER(json_test_object_iter);
 	AST_TEST_REGISTER(json_test_object_iter_null);
+	AST_TEST_REGISTER(json_test_object_create_vars);
 	AST_TEST_REGISTER(json_test_dump_load_string);
 	AST_TEST_REGISTER(json_test_dump_load_str);
 	AST_TEST_REGISTER(json_test_dump_str_fail);

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/16641
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: 18
Gerrit-Change-Id: I5f34f18e102126aef3997f19a553a266d70d6226
Gerrit-Change-Number: 16641
Gerrit-PatchSet: 1
Gerrit-Owner: Kevin Harwell <kharwell at digium.com>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20211021/ad416388/attachment-0001.html>


More information about the asterisk-code-review mailing list