[svn-commits] dlee: branch dlee/stasis-res r385141 - in /team/dlee/stasis-res: include/aste...
SVN commits to the Digium repositories
svn-commits at lists.digium.com
Tue Apr 9 14:45:45 CDT 2013
Author: dlee
Date: Tue Apr 9 14:45:41 2013
New Revision: 385141
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=385141
Log:
Addressed review feedback.
Got another complaint about ast_channel_snapshot_to_json, so I decided
to finally make it better. Instead of chaining together a bunch of
ast_json_object_set functions, it's now just a single json_pack.
While it may be more difficult to keep the formatting string straight,
the unit test should catch any problems we might have.
Modified:
team/dlee/stasis-res/include/asterisk/json.h
team/dlee/stasis-res/main/json.c
team/dlee/stasis-res/main/stasis_channels.c
team/dlee/stasis-res/tests/test_json.c
team/dlee/stasis-res/tests/test_stasis_channels.c
Modified: team/dlee/stasis-res/include/asterisk/json.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-res/include/asterisk/json.h?view=diff&rev=385141&r1=385140&r2=385141
==============================================================================
--- team/dlee/stasis-res/include/asterisk/json.h (original)
+++ team/dlee/stasis-res/include/asterisk/json.h Tue Apr 9 14:45:41 2013
@@ -791,7 +791,7 @@
* \return JSON string with ISO 8601 formatted date/time.
* \return \c NULL on error.
*/
-struct ast_json *ast_json_timeval(const struct timeval *tv, const char *zone);
+struct ast_json *ast_json_timeval(const struct timeval tv, const char *zone);
/*!
* \brief Construct a context/exten/priority as JSON.
Modified: team/dlee/stasis-res/main/json.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-res/main/json.c?view=diff&rev=385141&r1=385140&r2=385141
==============================================================================
--- team/dlee/stasis-res/main/json.c (original)
+++ team/dlee/stasis-res/main/json.c Tue Apr 9 14:45:41 2013
@@ -519,16 +519,12 @@
"priority", priority != -1 ? ast_json_integer_create(priority) : ast_json_null());
}
-struct ast_json *ast_json_timeval(const struct timeval *tv, const char *zone)
+struct ast_json *ast_json_timeval(const struct timeval tv, const char *zone)
{
char buf[AST_ISO8601_LEN];
struct ast_tm tm = {};
- if (tv == NULL) {
- return NULL;
- }
-
- ast_localtime(tv, &tm, zone);
+ ast_localtime(&tv, &tm, zone);
ast_strftime(buf, sizeof(buf),AST_ISO8601_FORMAT, &tm);
Modified: team/dlee/stasis-res/main/stasis_channels.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-res/main/stasis_channels.c?view=diff&rev=385141&r1=385140&r2=385141
==============================================================================
--- team/dlee/stasis-res/main/stasis_channels.c (original)
+++ team/dlee/stasis-res/main/stasis_channels.c Tue Apr 9 14:45:41 2013
@@ -458,45 +458,30 @@
struct ast_json *ast_channel_snapshot_to_json(const struct ast_channel_snapshot *snapshot)
{
RAII_VAR(struct ast_json *, json_chan, NULL, ast_json_unref);
- int r = 0;
if (snapshot == NULL) {
return NULL;
}
- json_chan = ast_json_object_create();
- if (!json_chan) { ast_log(LOG_ERROR, "Error creating channel json object\n"); return NULL; }
-
- r = ast_json_object_set(json_chan, "name", ast_json_string_create(snapshot->name));
- if (r) { ast_log(LOG_ERROR, "Error adding attrib to channel json object\n"); return NULL; }
- r = ast_json_object_set(json_chan, "state", ast_json_string_create(ast_state2str(snapshot->state)));
- if (r) { ast_log(LOG_ERROR, "Error adding attrib to channel json object\n"); return NULL; }
- r = ast_json_object_set(json_chan, "accountcode", ast_json_string_create(snapshot->accountcode));
- if (r) { ast_log(LOG_ERROR, "Error adding attrib to channel json object\n"); return NULL; }
- r = ast_json_object_set(json_chan, "peeraccount", ast_json_string_create(snapshot->peeraccount));
- if (r) { ast_log(LOG_ERROR, "Error adding attrib to channel json object\n"); return NULL; }
- r = ast_json_object_set(json_chan, "userfield", ast_json_string_create(snapshot->userfield));
- if (r) { ast_log(LOG_ERROR, "Error adding attrib to channel json object\n"); return NULL; }
- r = ast_json_object_set(json_chan, "uniqueid", ast_json_string_create(snapshot->uniqueid));
- if (r) { ast_log(LOG_ERROR, "Error adding attrib to channel json object\n"); return NULL; }
- r = ast_json_object_set(json_chan, "linkedid", ast_json_string_create(snapshot->linkedid));
- if (r) { ast_log(LOG_ERROR, "Error adding attrib to channel json object\n"); return NULL; }
- r = ast_json_object_set(json_chan, "parkinglot", ast_json_string_create(snapshot->parkinglot));
- if (r) { ast_log(LOG_ERROR, "Error adding attrib to channel json object\n"); return NULL; }
- r = ast_json_object_set(json_chan, "hangupsource", ast_json_string_create(snapshot->hangupsource));
- if (r) { ast_log(LOG_ERROR, "Error adding attrib to channel json object\n"); return NULL; }
- r = ast_json_object_set(json_chan, "appl", ast_json_string_create(snapshot->appl));
- if (r) { ast_log(LOG_ERROR, "Error adding attrib to channel json object\n"); return NULL; }
- r = ast_json_object_set(json_chan, "data", ast_json_string_create(snapshot->data));
- if (r) { ast_log(LOG_ERROR, "Error adding attrib to channel json object\n"); return NULL; }
- r = ast_json_object_set(json_chan, "dialplan", ast_json_dialplan_cep(snapshot->context, snapshot->exten, snapshot->priority));
- if (r) { ast_log(LOG_ERROR, "Error adding attrib to channel json object\n"); return NULL; }
- r = ast_json_object_set(json_chan, "caller", ast_json_name_number(snapshot->caller_name, snapshot->caller_number));
- if (r) { ast_log(LOG_ERROR, "Error adding attrib to channel json object\n"); return NULL; }
- r = ast_json_object_set(json_chan, "connected", ast_json_name_number(snapshot->connected_name, snapshot->connected_number));
- if (r) { ast_log(LOG_ERROR, "Error adding attrib to channel json object\n"); return NULL; }
- r = ast_json_object_set(json_chan, "creationtime", ast_json_timeval(&snapshot->creationtime, NULL));
- if (r) { ast_log(LOG_ERROR, "Error adding attrib to channel json object\n"); return NULL; }
+ json_chan = ast_json_pack("{ s: s, s: s, s: s, s: s, s: s, s: s, s: s,"
+ " s: s, s: s, s: s, s: s, s: o, s: o, s: o,"
+ " s: o"
+ "}",
+ "name", snapshot->name,
+ "state", ast_state2str(snapshot->state),
+ "accountcode", snapshot->accountcode,
+ "peeraccount", snapshot->peeraccount,
+ "userfield", snapshot->userfield,
+ "uniqueid", snapshot->uniqueid,
+ "linkedid", snapshot->linkedid,
+ "parkinglot", snapshot->parkinglot,
+ "hangupsource", snapshot->hangupsource,
+ "appl", snapshot->appl,
+ "data", snapshot->data,
+ "dialplan", ast_json_dialplan_cep(snapshot->context, snapshot->exten, snapshot->priority),
+ "caller", ast_json_name_number(snapshot->caller_name, snapshot->caller_number),
+ "connected", ast_json_name_number(snapshot->connected_name, snapshot->connected_number),
+ "creationtime", ast_json_timeval(snapshot->creationtime, NULL));
return ast_json_ref(json_chan);
}
Modified: team/dlee/stasis-res/tests/test_json.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-res/tests/test_json.c?view=diff&rev=385141&r1=385140&r2=385141
==============================================================================
--- team/dlee/stasis-res/tests/test_json.c (original)
+++ team/dlee/stasis-res/tests/test_json.c Tue Apr 9 14:45:41 2013
@@ -1659,12 +1659,11 @@
break;
}
- ast_test_validate(test, NULL == ast_json_timeval(NULL, NULL));
expected = ast_json_string_create("2013-02-07T09:32:34.314-0600");
tv.tv_sec = 1360251154;
tv.tv_usec = 314159;
- uut = ast_json_timeval(&tv, "America/Chicago");
+ uut = ast_json_timeval(tv, "America/Chicago");
ast_test_validate(test, ast_json_equal(expected, uut));
Modified: team/dlee/stasis-res/tests/test_stasis_channels.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-res/tests/test_stasis_channels.c?view=diff&rev=385141&r1=385140&r2=385141
==============================================================================
--- team/dlee/stasis-res/tests/test_stasis_channels.c (original)
+++ team/dlee/stasis-res/tests/test_stasis_channels.c Tue Apr 9 14:45:41 2013
@@ -199,11 +199,76 @@
return AST_TEST_PASS;
}
+AST_TEST_DEFINE(channel_snapshot_json)
+{
+ RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
+ RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
+ RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
+ RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
+ RAII_VAR(struct ast_json *, actual, NULL, ast_json_unref);
+
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = __func__;
+ info->category = test_category;
+ info->summary = "Test creation of ast_channel_blob objects";
+ info->description = "Test creation of ast_channel_blob objects";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ ast_test_validate(test, NULL == ast_channel_snapshot_to_json(NULL));
+
+ chan = ast_channel_alloc(0, AST_STATE_DOWN, "cid_num", "cid_name", "acctcode", "exten", "context", NULL, 0, "TEST/name");
+ ast_test_validate(test, NULL != chan);
+ snapshot = ast_channel_snapshot_create(chan);
+ ast_test_validate(test, NULL != snapshot);
+
+ actual = ast_channel_snapshot_to_json(snapshot);
+ expected = ast_json_pack("{ s: s, s: s, s: s, s: s, s: s, s: s, s: s,"
+ " s: s, s: s, s: s, s: s,"
+ " s: { s: s, s: s, s: i },"
+ " s: { s: s, s: s },"
+ " s: { s: s, s: s },"
+ " s: o"
+ "}",
+ "name", "TEST/name",
+ "state", "Down",
+ "accountcode", "acctcode",
+ "peeraccount", "",
+ "userfield", "",
+ "uniqueid", ast_channel_uniqueid(chan),
+ "linkedid", ast_channel_uniqueid(chan),
+ "parkinglot", "",
+ "hangupsource", "",
+ "appl", "",
+ "data", "",
+ "dialplan",
+ "context", "context",
+ "exten", "exten",
+ "priority", 1,
+ "caller",
+ "name", "cid_name",
+ "number", "cid_num",
+ "connected",
+ "name", "",
+ "number", "",
+ "creationtime",
+ ast_json_timeval(
+ ast_channel_creationtime(chan), NULL));
+
+ ast_test_validate(test, ast_json_equal(expected, actual));
+
+ return AST_TEST_PASS;
+}
+
static int unload_module(void)
{
AST_TEST_UNREGISTER(channel_blob_create);
AST_TEST_UNREGISTER(multi_channel_blob_create);
AST_TEST_UNREGISTER(multi_channel_blob_snapshots);
+ AST_TEST_UNREGISTER(channel_snapshot_json);
return 0;
}
@@ -213,6 +278,7 @@
AST_TEST_REGISTER(channel_blob_create);
AST_TEST_REGISTER(multi_channel_blob_create);
AST_TEST_REGISTER(multi_channel_blob_snapshots);
+ AST_TEST_REGISTER(channel_snapshot_json);
return AST_MODULE_LOAD_SUCCESS;
}
More information about the svn-commits
mailing list