[ovs-dev] [PATCH 03/14] ovsdb-client: Introduce new data formatting style as the default.

Ben Pfaff blp at nicira.com
Fri Feb 12 11:37:23 PST 2010


The new data formatting style is generally shorter and easier to read than
the JSON representation (which is still available using "-d json").
---
 ovsdb/ovsdb-client.1.in |   13 ++++++++++++-
 ovsdb/ovsdb-client.c    |   46 ++++++++++++++++++++++++++++++++++++++++++++--
 tests/ovsdb-monitor.at  |    2 +-
 3 files changed, 57 insertions(+), 4 deletions(-)

diff --git a/ovsdb/ovsdb-client.1.in b/ovsdb/ovsdb-client.1.in
index 119add3..b2d767f 100644
--- a/ovsdb/ovsdb-client.1.in
+++ b/ovsdb/ovsdb-client.1.in
@@ -100,7 +100,7 @@ The following options controlling output formatting:
 .
 .IP "\fB-f \fIformat\fR"
 .IQ "\fB--format=\fIformat\fR"
-Sets the basic type of output formatting.  The following types of
+Sets the type of table formatting.  The following types of
 \fIformat\fR are available:
 .RS
 .IP "\fBtable\fR (default)"
@@ -111,6 +111,17 @@ HTML tables.
 Comma-separated values as defined in RFC 4180.
 .RE
 .
+.IP "\fB-d \fIformat\fR"
+.IP "\fB--data=\fIformat\fR"
+Sets the formatting for cells within output tables.  The following
+types of \fIformat\fR are available:
+.RS
+.IP "\fBstring\fR (default)"
+The simple format described in \fBovs-vsctl\fR(8).
+.IP "\fBjson\fR"
+JSON.
+.RE
+.
 .IP "\fB--no-heading\fR"
 This option suppresses the heading row that otherwise appears in the
 first row of table output.
diff --git a/ovsdb/ovsdb-client.c b/ovsdb/ovsdb-client.c
index 0900a67..45bb54c 100644
--- a/ovsdb/ovsdb-client.c
+++ b/ovsdb/ovsdb-client.c
@@ -33,6 +33,7 @@
 #include "json.h"
 #include "jsonrpc.h"
 #include "ovsdb.h"
+#include "ovsdb-data.h"
 #include "ovsdb-error.h"
 #include "stream.h"
 #include "stream-ssl.h"
@@ -56,6 +57,12 @@ static int output_headings = true;
 /* --pretty: Flags to pass to json_to_string(). */
 static int json_flags = JSSF_SORT;
 
+/* --data: Format of data in output tables. */
+static enum {
+    DF_STRING,                  /* String format. */
+    DF_JSON,                    /* JSON. */
+} data_format;
+
 static const struct command all_commands[];
 
 static void usage(void) NO_RETURN;
@@ -82,7 +89,8 @@ parse_options(int argc, char *argv[])
     };
     static struct option long_options[] = {
         {"format", required_argument, 0, 'f'},
-	    {"no-headings", no_argument, &output_headings, 0},
+        {"data", required_argument, 0, 'd'},
+        {"no-headings", no_argument, &output_headings, 0},
         {"pretty", no_argument, &json_flags, JSSF_PRETTY | JSSF_SORT},
         {"verbose", optional_argument, 0, 'v'},
         {"help", no_argument, 0, 'h'},
@@ -117,6 +125,16 @@ parse_options(int argc, char *argv[])
             }
             break;
 
+        case 'd':
+            if (!strcmp(optarg, "string")) {
+                data_format = DF_STRING;
+            } else if (!strcmp(optarg, "json")) {
+                data_format = DF_JSON;
+            } else {
+                ovs_fatal(0, "unknown data format \"%s\"", optarg);
+            }
+            break;
+
         case 'h':
             usage();
 
@@ -697,6 +715,30 @@ do_transact(int argc OVS_UNUSED, char *argv[])
     jsonrpc_close(rpc);
 }
 
+static char *
+format_data(const struct json *json, const struct ovsdb_type *type)
+{
+    if (data_format == DF_JSON) {
+        return json_to_string(json, JSSF_SORT);
+    } else if (data_format == DF_STRING) {
+        struct ovsdb_datum datum;
+        struct ovsdb_error *error;
+        struct ds s;
+
+        error = ovsdb_datum_from_json(&datum, type, json, NULL);
+        if (error) {
+            return json_to_string(json, JSSF_SORT);
+        }
+
+        ds_init(&s);
+        ovsdb_datum_to_string(&datum, type, &s);
+        ovsdb_datum_destroy(&datum, type);
+        return ds_steal_cstr(&s);
+    } else {
+        NOT_REACHED();
+    }
+}
+
 static void
 monitor_print_row(struct json *row, const char *type, const char *uuid,
                   const struct ovsdb_column_set *columns, struct table *t)
@@ -718,7 +760,7 @@ monitor_print_row(struct json *row, const char *type, const char *uuid,
         const struct ovsdb_column *column = columns->columns[i];
         struct json *value = shash_find_data(json_object(row), column->name);
         if (value) {
-            table_add_cell_nocopy(t, json_to_string(value, JSSF_SORT));
+            table_add_cell_nocopy(t, format_data(value, &column->type));
         } else {
             table_add_cell(t, "");
         }
diff --git a/tests/ovsdb-monitor.at b/tests/ovsdb-monitor.at
index 87afa1e..ad68740 100644
--- a/tests/ovsdb-monitor.at
+++ b/tests/ovsdb-monitor.at
@@ -24,7 +24,7 @@ m4_define([OVSDB_CHECK_MONITOR],
    m4_foreach([txn], [$3],
      [AT_CHECK([ovsdb-tool transact db 'txn'], [0], [ignore], [ignore])])
    AT_CHECK([ovsdb-server --detach --pidfile=$PWD/server-pid --remote=punix:socket --unixctl=$PWD/unixctl db], [0], [ignore], [ignore])
-   AT_CHECK([ovsdb-client --detach --pidfile=$PWD/client-pid monitor --format=csv unix:socket ordinals $4 > output], 
+   AT_CHECK([ovsdb-client --detach --pidfile=$PWD/client-pid -d json monitor --format=csv unix:socket ordinals $4 > output], 
             [0], [ignore], [ignore], [kill `cat server-pid`])
    m4_foreach([txn], [$5],
      [AT_CHECK([ovsdb-client transact unix:socket 'txn'], [0],
-- 
1.6.6.1





More information about the dev mailing list