Author: file
Date: Fri Jul 26 16:32:44 2013
New Revision: 395573

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=395573
Log:
Add the ability to get a JSON representation of a bucket.

Modified:
    team/file/bucket/include/asterisk/bucket.h
    team/file/bucket/include/asterisk/config_options.h
    team/file/bucket/main/bucket.c
    team/file/bucket/main/config_options.c
    team/file/bucket/tests/test_bucket.c

Modified: team/file/bucket/include/asterisk/bucket.h
URL: 
http://svnview.digium.com/svn/asterisk/team/file/bucket/include/asterisk/bucket.h?view=diff&rev=395573&r1=395572&r2=395573
==============================================================================
--- team/file/bucket/include/asterisk/bucket.h (original)
+++ team/file/bucket/include/asterisk/bucket.h Fri Jul 26 16:32:44 2013
@@ -218,6 +218,18 @@
  */
 void ast_bucket_observer_remove(struct ast_sorcery_observer *callbacks);
 
+/*!
+ * \brief Get a JSON representation of a bucket
+ *
+ * \param bucket The specific bucket
+ *
+ * \retval non-NULL success
+ * \retval NULL failure
+ *
+ * \note The returned ast_json object must be unreferenced using ast_json_unref
+ */
+struct ast_json *ast_bucket_json(const struct ast_bucket *bucket);
+
 #if defined(__cplusplus) || defined(c_plusplus)
 }
 #endif

Modified: team/file/bucket/include/asterisk/config_options.h
URL: 
http://svnview.digium.com/svn/asterisk/team/file/bucket/include/asterisk/config_options.h?view=diff&rev=395573&r1=395572&r2=395573
==============================================================================
--- team/file/bucket/include/asterisk/config_options.h (original)
+++ team/file/bucket/include/asterisk/config_options.h Fri Jul 26 16:32:44 2013
@@ -53,6 +53,17 @@
        ACO_EXACT = 1,
        ACO_REGEX,
 };
+
+
+/*! \brief A callback function for handling a particular option
+ * \param opt The option being configured
+ * \param var The config variable to use to configure \a obj
+ * \param obj The object to be configured
+ *
+ * \retval 0 Parsing and recording the config value succeeded
+ * \retval non-zero Failure. Parsing should stop and no reload applied
+ */
+typedef int (*aco_option_handler)(const struct aco_option *opt, struct 
ast_variable *var, void *obj);
 
 /*! Callback functions for option parsing via aco_process_config() */
 
@@ -437,15 +448,21 @@
        OPT_UINT_T,
 };
 
-/*! \brief A callback function for handling a particular option
- * \param opt The option being configured
- * \param var The config variable to use to configure \a obj
- * \param obj The object to be configured
- *
- * \retval 0 Parsing and recording the config value succeeded
- * \retval non-zero Failure. Parsing should stop and no reload applied
- */
-typedef int (*aco_option_handler)(const struct aco_option *opt, struct 
ast_variable *var, void *obj);
+/*! \brief Configuration option structure */
+struct aco_option {
+       const char *name;
+       const char *aliased_to;
+       const char *default_val;
+       enum aco_matchtype match_type;
+       regex_t *name_regex;
+       struct aco_type **obj;
+       enum aco_option_type type;
+       aco_option_handler handler;
+       unsigned int flags;
+       unsigned char deprecated:1;
+       size_t argc;
+       intptr_t args[0];
+};
 
 /*! \brief Allocate a container to hold config options */
 struct ao2_container *aco_option_container_alloc(void);

Modified: team/file/bucket/main/bucket.c
URL: 
http://svnview.digium.com/svn/asterisk/team/file/bucket/main/bucket.c?view=diff&rev=395573&r1=395572&r2=395573
==============================================================================
--- team/file/bucket/main/bucket.c (original)
+++ team/file/bucket/main/bucket.c Fri Jul 26 16:32:44 2013
@@ -34,8 +34,10 @@
 #include "asterisk/logger.h"
 #include "asterisk/sorcery.h"
 #include "asterisk/bucket.h"
+#include "asterisk/config_options.h"
 #include "asterisk/astobj2.h"
 #include "asterisk/strings.h"
+#include "asterisk/json.h"
 
 /*! \brief Default scheme for when one is not specified */
 #define DEFAULT_UNSPECIFIED_SCHEME "local"
@@ -339,6 +341,50 @@
        return ast_sorcery_delete(bucket_sorcery, bucket);
 }
 
+struct ast_json *ast_bucket_json(const struct ast_bucket *bucket)
+{
+       RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
+       struct ast_json *files, *buckets;
+       struct ao2_iterator i;
+       char *uri;
+
+       json = ast_sorcery_objectset_json_create(bucket_sorcery, bucket);
+       if (!json) {
+               return NULL;
+       }
+
+       buckets = ast_json_array_create();
+       if (!buckets) {
+               return NULL;
+       }
+
+       /* TODO: Below needs better error checking */
+
+       i = ao2_iterator_init(bucket->buckets, 0);
+       for (; (uri = ao2_iterator_next(&i)); ao2_ref(uri, -1)) {
+               ast_json_array_append(buckets, ast_json_string_create(uri));
+       }
+       ao2_iterator_destroy(&i);
+
+       ast_json_object_set(json, "buckets", buckets);
+
+       files = ast_json_array_create();
+       if (!files) {
+               return NULL;
+       }
+
+       i = ao2_iterator_init(bucket->files, 0);
+       for (; (uri = ao2_iterator_next(&i)); ao2_ref(uri, -1)) {
+               ast_json_array_append(files, ast_json_string_create(uri));
+       }
+       ao2_iterator_destroy(&i);
+
+       ast_json_object_set(json, "files", files);
+
+       ast_json_ref(json);
+       return json;
+}
+
 /*! \brief Destructor for bucket files */
 static void bucket_file_destroy(void *obj)
 {
@@ -388,6 +434,20 @@
        ast_sorcery_wizard_unregister(&bucket_file_wizard);
 
        ao2_cleanup(schemes);
+}
+
+/*! \brief Custom handler for translating from a string timeval to actual 
structure */
+static int timeval_str2struct(const struct aco_option *opt, struct 
ast_variable *var, void *obj)
+{
+       struct timeval *field = (struct timeval *)(obj + opt->args[0]);
+       return ast_get_timeval(var->value, field, ast_tv(0, 0), NULL);
+}
+
+/*! \brief Custom handler for translating from an actual structure timeval to 
string */
+static int timeval_struct2str(const void *obj, const intptr_t *args, char 
**buf)
+{
+       struct timeval *field = (struct timeval *)(obj + args[0]);
+       return (ast_asprintf(buf, "%lu", field->tv_sec) < 0) ? -1 : 0;
 }
 
 /*! \brief Initialize bucket support */
@@ -425,6 +485,11 @@
                goto failure;
        }
 
+       ast_sorcery_object_field_register(bucket_sorcery, "bucket", "uri", "", 
OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_bucket, uri));
+       ast_sorcery_object_field_register(bucket_sorcery, "bucket", "scheme", 
"", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_bucket, scheme));
+       ast_sorcery_object_field_register_custom(bucket_sorcery, "bucket", 
"created", "", timeval_str2struct, timeval_struct2str, 0, FLDSET(struct 
ast_bucket, created));
+       ast_sorcery_object_field_register_custom(bucket_sorcery, "bucket", 
"modified", "", timeval_str2struct, timeval_struct2str, 0, FLDSET(struct 
ast_bucket, modified));
+
        if (ast_sorcery_apply_default(bucket_sorcery, "file", "bucket_file", 
NULL)) {
                ast_log(LOG_ERROR, "Failed to apply intermediary for 'file' 
object type in Bucket sorcery\n");
                goto failure;

Modified: team/file/bucket/main/config_options.c
URL: 
http://svnview.digium.com/svn/asterisk/team/file/bucket/main/config_options.c?view=diff&rev=395573&r1=395572&r2=395573
==============================================================================
--- team/file/bucket/main/config_options.c (original)
+++ team/file/bucket/main/config_options.c Fri Jul 26 16:32:44 2013
@@ -57,21 +57,6 @@
 struct aco_type_internal {
        regex_t *regex;
        struct ao2_container *opts; /*!< The container of options registered to 
the aco_info */
-};
-
-struct aco_option {
-       const char *name;
-       const char *aliased_to;
-       const char *default_val;
-       enum aco_matchtype match_type;
-       regex_t *name_regex;
-       struct aco_type **obj;
-       enum aco_option_type type;
-       aco_option_handler handler;
-       unsigned int flags;
-       unsigned char deprecated:1;
-       size_t argc;
-       intptr_t args[0];
 };
 
 #ifdef AST_XML_DOCS

Modified: team/file/bucket/tests/test_bucket.c
URL: 
http://svnview.digium.com/svn/asterisk/team/file/bucket/tests/test_bucket.c?view=diff&rev=395573&r1=395572&r2=395573
==============================================================================
--- team/file/bucket/tests/test_bucket.c (original)
+++ team/file/bucket/tests/test_bucket.c Fri Jul 26 16:32:44 2013
@@ -289,6 +289,42 @@
        return AST_TEST_PASS;
 }
 
+AST_TEST_DEFINE(bucket_json)
+{
+       RAII_VAR(struct ast_bucket *, bucket, NULL, ao2_cleanup);
+       RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
+
+       switch (cmd) {
+       case TEST_INIT:
+               info->name = "bucket_json";
+               info->category = "/main/bucket/";
+               info->summary = "bucket json unit test";
+               info->description =
+                       "Test creation of JSON for a bucket";
+               return AST_TEST_NOT_RUN;
+       case TEST_EXECUTE:
+               break;
+       }
+
+       if (!(bucket = ast_bucket_alloc("test:///tmp/bob"))) {
+               ast_test_status_update(test, "Failed to allocate bucket\n");
+               return AST_TEST_FAIL;
+       }
+
+       ast_str_container_add(bucket->buckets, "test:///tmp/bob/joe");
+       ast_str_container_add(bucket->files, "test:///tmp/bob/recording.wav");
+
+       json = ast_bucket_json(bucket);
+       if (!json) {
+               ast_test_status_update(test, "Could not produce JSON for a 
valid bucket\n");
+               return AST_TEST_FAIL;
+       }
+
+       /* TODO: Check contents of JSON against expected */
+
+       return AST_TEST_PASS;
+}
+
 AST_TEST_DEFINE(bucket_retrieve)
 {
        RAII_VAR(struct ast_bucket *, bucket, NULL, ao2_cleanup);
@@ -321,6 +357,7 @@
        AST_TEST_UNREGISTER(bucket_create);
        AST_TEST_UNREGISTER(bucket_delete);
        AST_TEST_UNREGISTER(bucket_retrieve);
+       AST_TEST_UNREGISTER(bucket_json);
        return 0;
 }
 
@@ -331,6 +368,7 @@
        AST_TEST_REGISTER(bucket_create);
        AST_TEST_REGISTER(bucket_delete);
        AST_TEST_REGISTER(bucket_retrieve);
+       AST_TEST_REGISTER(bucket_json);
        return AST_MODULE_LOAD_SUCCESS;
 }
 


--
_____________________________________________________________________
-- Bandwidth and Colocation Provided by http://www.api-digital.com --

svn-commits mailing list
To UNSUBSCRIBE or update options visit:
   http://lists.digium.com/mailman/listinfo/svn-commits

Reply via email to