[asterisk-commits] file: branch file/sorceryx3 r386187 - in /team/file/sorceryx3: include/asteri...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Sat Apr 20 13:20:26 CDT 2013


Author: file
Date: Sat Apr 20 13:20:22 2013
New Revision: 386187

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=386187
Log:
Add the ability to create a JSON object from a sorcery object.

Modified:
    team/file/sorceryx3/include/asterisk/sorcery.h
    team/file/sorceryx3/main/sorcery.c
    team/file/sorceryx3/res/res_sorcery_astdb.c
    team/file/sorceryx3/tests/test_sorcery.c

Modified: team/file/sorceryx3/include/asterisk/sorcery.h
URL: http://svnview.digium.com/svn/asterisk/team/file/sorceryx3/include/asterisk/sorcery.h?view=diff&rev=386187&r1=386186&r2=386187
==============================================================================
--- team/file/sorceryx3/include/asterisk/sorcery.h (original)
+++ team/file/sorceryx3/include/asterisk/sorcery.h Sat Apr 20 13:20:22 2013
@@ -479,6 +479,19 @@
 struct ast_variable *ast_sorcery_objectset_create(const struct ast_sorcery *sorcery, const void *object);
 
 /*!
+ * \brief Create an object set in JSON format for an object
+ *
+ * \param sorcery Pointer to a sorcery structure
+ * \param object Pointer to a sorcery object
+ *
+ * \retval non-NULL success
+ * \retval NULL if error occurred
+ *
+ * \note The returned ast_json object must be unreferenced using ast_json_unref
+ */
+struct ast_json *ast_sorcery_objectset_json_create(const struct ast_sorcery *sorcery, const void *object);
+
+/*!
  * \brief Apply an object set (KVP list) to an object
  *
  * \param sorcery Pointer to a sorcery structure

Modified: team/file/sorceryx3/main/sorcery.c
URL: http://svnview.digium.com/svn/asterisk/team/file/sorceryx3/main/sorcery.c?view=diff&rev=386187&r1=386186&r2=386187
==============================================================================
--- team/file/sorceryx3/main/sorcery.c (original)
+++ team/file/sorceryx3/main/sorcery.c Sat Apr 20 13:20:22 2013
@@ -40,6 +40,7 @@
 #include "asterisk/module.h"
 #include "asterisk/taskprocessor.h"
 #include "asterisk/threadpool.h"
+#include "asterisk/json.h"
 
 /* To prevent DEBUG_FD_LEAKS from interfering with things we undef open and close */
 #undef open
@@ -772,6 +773,68 @@
 	return fields;
 }
 
+struct ast_json *ast_sorcery_objectset_json_create(const struct ast_sorcery *sorcery, const void *object)
+{
+	const struct ast_sorcery_object_details *details = object;
+	RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->type, OBJ_KEY), ao2_cleanup);
+	struct ao2_iterator i;
+	struct ast_sorcery_object_field *object_field;
+	struct ast_json *json = ast_json_object_create();
+	int res = 0;
+
+	if (!object_type || !json) {
+		return NULL;
+	}
+
+	i = ao2_iterator_init(object_type->fields, 0);
+
+	for (; (object_field = ao2_iterator_next(&i)) && !res; ao2_ref(object_field, -1)) {
+		if (object_field->multiple_handler) {
+			struct ast_variable *tmp = NULL;
+			struct ast_variable *field;
+
+			if ((res = object_field->multiple_handler(object, &tmp))) {
+				break;
+			}
+
+			for (field = tmp; field; field = field->next) {
+				struct ast_json *value = ast_json_string_create(field->value);
+
+				if (value && ast_json_object_set(json, field->name, value)) {
+					ast_json_unref(value);
+					res = -1;
+				}
+			}
+
+			ast_variables_destroy(tmp);
+		} else if (object_field->handler) {
+			char *buf = NULL;
+			struct ast_json *value = NULL;
+
+			if ((res = object_field->handler(object, object_field->args, &buf)) ||
+				!(value = ast_json_string_create(buf)) ||
+				ast_json_object_set(json, object_field->name, value)) {
+				ast_json_unref(value);
+				res = -1;
+			}
+
+			ast_free(buf);
+		} else {
+			continue;
+		}
+	}
+
+	ao2_iterator_destroy(&i);
+
+	/* If any error occurs we destroy the JSON object so a partial objectset is not returned */
+	if (res) {
+		ast_json_unref(json);
+		json = NULL;
+	}
+
+	return json;
+}
+
 int ast_sorcery_objectset_apply(const struct ast_sorcery *sorcery, void *object, struct ast_variable *objectset)
 {
 	const struct ast_sorcery_object_details *details = object;

Modified: team/file/sorceryx3/res/res_sorcery_astdb.c
URL: http://svnview.digium.com/svn/asterisk/team/file/sorceryx3/res/res_sorcery_astdb.c?view=diff&rev=386187&r1=386186&r2=386187
==============================================================================
--- team/file/sorceryx3/res/res_sorcery_astdb.c (original)
+++ team/file/sorceryx3/res/res_sorcery_astdb.c Sat Apr 20 13:20:22 2013
@@ -125,13 +125,12 @@
 
 static int sorcery_astdb_create(const struct ast_sorcery *sorcery, void *data, void *object)
 {
-	RAII_VAR(struct ast_variable *, objset, ast_sorcery_objectset_create(sorcery, object), ast_variables_destroy);
-	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
+	RAII_VAR(struct ast_json *, objset, ast_sorcery_objectset_json_create(sorcery, object), ast_json_unref);
 	RAII_VAR(char *, value, NULL, ast_free_ptr);
 	const char *prefix = data;
 	char family[strlen(prefix) + strlen(ast_sorcery_object_get_type(object)) + 2];
 
-	if (!objset || !(json = sorcery_objectset_to_json(objset)) || !(value = ast_json_dump_string(json))) {
+	if (!objset || !(value = ast_json_dump_string(objset))) {
 		return -1;
 	}
 

Modified: team/file/sorceryx3/tests/test_sorcery.c
URL: http://svnview.digium.com/svn/asterisk/team/file/sorceryx3/tests/test_sorcery.c?view=diff&rev=386187&r1=386186&r2=386187
==============================================================================
--- team/file/sorceryx3/tests/test_sorcery.c (original)
+++ team/file/sorceryx3/tests/test_sorcery.c Sat Apr 20 13:20:22 2013
@@ -37,6 +37,7 @@
 #include "asterisk/module.h"
 #include "asterisk/sorcery.h"
 #include "asterisk/logger.h"
+#include "asterisk/json.h"
 
 /*! \brief Dummy sorcery object */
 struct test_sorcery_object {
@@ -910,6 +911,63 @@
 			}
 		} else {
 			ast_test_status_update(test, "Object set created field '%s' which is unknown\n", field->name);
+			res = AST_TEST_FAIL;
+		}
+	}
+
+	return res;
+}
+
+AST_TEST_DEFINE(objectset_json_create)
+{
+	int res = AST_TEST_PASS;
+	RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
+	RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
+	RAII_VAR(struct ast_json *, objset, NULL, ast_json_unref);
+	struct ast_json_iter *field;
+
+	switch (cmd) {
+	case TEST_INIT:
+		info->name = "objectset_json_create";
+		info->category = "/main/sorcery/";
+		info->summary = "sorcery json object set creation unit test";
+		info->description =
+			"Test object set creation (for JSON format) in sorcery";
+		return AST_TEST_NOT_RUN;
+	case TEST_EXECUTE:
+		break;
+	}
+
+	if (!(sorcery = alloc_and_initialize_sorcery())) {
+		ast_test_status_update(test, "Failed to open sorcery structure\n");
+		return AST_TEST_FAIL;
+	}
+
+	if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
+		ast_test_status_update(test, "Failed to allocate a known object type\n");
+		return AST_TEST_FAIL;
+	}
+
+	if (!(objset = ast_sorcery_objectset_json_create(sorcery, obj))) {
+		ast_test_status_update(test, "Failed to create an object set for a known sane object\n");
+		return AST_TEST_FAIL;
+	}
+
+	for (field = ast_json_object_iter(objset); field; field = ast_json_object_iter_next(objset, field)) {
+		struct ast_json *value = ast_json_object_iter_value(field);
+
+		if (!strcmp(ast_json_object_iter_key(field), "bob")) {
+			if (strcmp(ast_json_string_get(value), "5")) {
+				ast_test_status_update(test, "Object set failed to create proper value for 'bob'\n");
+				res = AST_TEST_FAIL;
+			}
+		} else if (!strcmp(ast_json_object_iter_key(field), "joe")) {
+			if (strcmp(ast_json_string_get(value), "10")) {
+				ast_test_status_update(test, "Object set failed to create proper value for 'joe'\n");
+				res = AST_TEST_FAIL;
+			}
+		} else {
+			ast_test_status_update(test, "Object set created field '%s' which is unknown\n", ast_json_object_iter_key(field));
 			res = AST_TEST_FAIL;
 		}
 	}
@@ -2507,6 +2565,7 @@
 	AST_TEST_UNREGISTER(object_diff);
 	AST_TEST_UNREGISTER(object_diff_native);
 	AST_TEST_UNREGISTER(objectset_create);
+	AST_TEST_UNREGISTER(objectset_json_create);
 	AST_TEST_UNREGISTER(objectset_create_regex);
 	AST_TEST_UNREGISTER(objectset_apply);
 	AST_TEST_UNREGISTER(objectset_apply_handler);
@@ -2553,6 +2612,7 @@
 	AST_TEST_REGISTER(object_diff);
 	AST_TEST_REGISTER(object_diff_native);
 	AST_TEST_REGISTER(objectset_create);
+	AST_TEST_REGISTER(objectset_json_create);
 	AST_TEST_REGISTER(objectset_create_regex);
 	AST_TEST_REGISTER(objectset_apply);
 	AST_TEST_REGISTER(objectset_apply_handler);




More information about the asterisk-commits mailing list