Code

proto: Add support for marshaling all data types.
authorSebastian Harl <sh@tokkee.org>
Tue, 23 Dec 2014 17:47:21 +0000 (18:47 +0100)
committerSebastian Harl <sh@tokkee.org>
Tue, 23 Dec 2014 17:47:21 +0000 (18:47 +0100)
src/utils/proto.c
t/unit/utils/proto_test.c

index 56ef745ff0d5f6b06336174f55538b40fd042013..51c2ed8c45361f4625c4e7dc6d956594d7a96a7e 100644 (file)
@@ -202,8 +202,26 @@ sdb_proto_marshal_data(char *buf, size_t buf_len, sdb_data_t *datum)
                        char **v = datum->data.array.values;
                        n = marshal_string(buf, buf_len, v[i]);
                }
+               else if (type == SDB_TYPE_DATETIME) {
+                       sdb_time_t *v = datum->data.array.values;
+                       n = marshal_datetime(buf, buf_len, v[i]);
+               }
+               else if (type == SDB_TYPE_BINARY) {
+                       struct {
+                               size_t length;
+                               unsigned char *datum;
+                       } *v = datum->data.array.values;
+                       n = marshal_binary(buf, buf_len, v[i].length, v[i].datum);
+               }
+               else if (type == SDB_TYPE_REGEX) {
+                       struct {
+                               char *raw;
+                               regex_t regex;
+                       } *v = datum->data.array.values;
+                       n = marshal_string(buf, buf_len, v[i].raw);
+               }
                else {
-                       errno = ENOTSUP;
+                       errno = EINVAL;
                        return -1;
                }
 
index 52ae936bc4bf6a69bf3ab77f81e67093524e26d3..9f24c81e43cf1b054be3e45c1c391cbca6636900 100644 (file)
@@ -53,6 +53,20 @@ START_TEST(test_marshal_data)
        int64_t int_values[] = { 47, 11, 23 };
        double dec_values[] = { 47.11, .5 };
        char *string_values[] = { "foo", "abcd" };
+       sdb_time_t datetime_values[] = { 4711, 1234567890123456789L };
+       struct {
+               size_t length;
+               unsigned char *datum;
+       } binary_values[] = {
+               { 3, (unsigned char *)"\x1\x2\x3" },
+               { 4, (unsigned char *)"\x42\x0\xa\x1b" },
+       };
+       struct {
+               char *raw;
+               regex_t regex;
+       } regex_values[] = {
+               { "dummy regex", dummy_re },
+       };
 
        struct {
                sdb_data_t datum;
@@ -107,6 +121,23 @@ START_TEST(test_marshal_data)
                        25, STRING_ARRAY "\0\0\0\x2" "\0\0\0\x4" "foo\0"
                                "\0\0\0\x5" "abcd\0"
                },
+               {
+                       { SDB_TYPE_DATETIME | SDB_TYPE_ARRAY, { .array = {
+                               2, datetime_values } } },
+                       24, DATETIME_ARRAY "\0\0\0\x2" "\0\0\0\0\0\0\x12\x67"
+                               "\x11\x22\x10\xf4\x7d\xe9\x81\x15"
+               },
+               {
+                       { SDB_TYPE_BINARY | SDB_TYPE_ARRAY, { .array = {
+                               2, binary_values } } },
+                       23, BINARY_ARRAY "\0\0\0\x2" "\0\0\0\x3" "\x1\x2\x3"
+                               "\0\0\0\4" "\x42\x0\xa\x1b"
+               },
+               {
+                       { SDB_TYPE_REGEX | SDB_TYPE_ARRAY, { .array = {
+                               1, regex_values } } },
+                       24, REGEX_ARRAY "\0\0\0\1" "\0\0\0\xc" "dummy regex\0"
+               },
        };
 
        size_t i;