[Tarantool-patches] [PATCH 3/5] uuid: provide MP_UUID extension serializer

Vladislav Shpilevoy v.shpilevoy at tarantool.org
Tue May 12 02:45:50 MSK 2020


Msgpuck functions mp_snprint() and mp_fprint() now support
customizable MP_EXT serializer. This patch adds such for
MP_UUID. It is not activated yet, but will be later, when last
extension - MP_ERROR - also gets its serializer.

Part of #4719
---
 src/lib/uuid/mp_uuid.c | 18 ++++++++++++
 src/lib/uuid/mp_uuid.h | 27 ++++++++++++++++++
 test/unit/uuid.c       | 64 +++++++++++++++++++++++++++++++++++++++++-
 test/unit/uuid.result  | 11 +++++++-
 4 files changed, 118 insertions(+), 2 deletions(-)

diff --git a/src/lib/uuid/mp_uuid.c b/src/lib/uuid/mp_uuid.c
index 1a9daf6d1..b2341ae36 100644
--- a/src/lib/uuid/mp_uuid.c
+++ b/src/lib/uuid/mp_uuid.c
@@ -95,3 +95,21 @@ mp_encode_uuid(char *data, const struct tt_uuid *uuid)
 	data = mp_encode_extl(data, MP_UUID, UUID_PACKED_LEN);
 	return uuid_pack(data, uuid);
 }
+
+int
+mp_snprint_uuid(char *buf, int size, const char **data, uint32_t len)
+{
+	struct tt_uuid uuid;
+	if (uuid_unpack(data, len, &uuid) == NULL)
+		return -1;
+	return snprintf(buf, size, "%s", tt_uuid_str(&uuid));
+}
+
+int
+mp_fprint_uuid(FILE *file, const char **data, uint32_t len)
+{
+	struct tt_uuid uuid;
+	if (uuid_unpack(data, len, &uuid) == NULL)
+		return -1;
+	return fprintf(file, "%s", tt_uuid_str(&uuid));
+}
diff --git a/src/lib/uuid/mp_uuid.h b/src/lib/uuid/mp_uuid.h
index fdc39f7ef..3cf591848 100644
--- a/src/lib/uuid/mp_uuid.h
+++ b/src/lib/uuid/mp_uuid.h
@@ -85,6 +85,33 @@ mp_decode_uuid(const char **data, struct tt_uuid *uuid);
 char *
 mp_encode_uuid(char *data, const struct tt_uuid *uuid);
 
+/**
+ * Print uuid's string representation into a given buffer.
+ * @param buf Target buffer to write string to.
+ * @param size Buffer size.
+ * @param data MessagePack encoded uuid, without MP_EXT header.
+ * @param len Length of @a data. If not all data is used, it is
+ *        an error.
+ * @retval <0 Error. Couldn't decode uuid.
+ * @retval >=0 How many bytes were written, or would have been
+ *        written, if there was enough buffer space.
+ */
+int
+mp_snprint_uuid(char *buf, int size, const char **data, uint32_t len);
+
+/**
+ * Print uuid's string representation into a stream.
+ * @param file Target stream to write string to.
+ * @param data MessagePack encoded uuid, without MP_EXT header.
+ * @param len Length of @a data. If not all data is used, it is
+ *        an error.
+ * @retval <0 Error. Couldn't decode uuid, or couldn't write to
+ *        the stream.
+ * @retval >=0 How many bytes were written.
+ */
+int
+mp_fprint_uuid(FILE *file, const char **data, uint32_t len);
+
 #if defined(__cplusplus)
 } /* extern "C" */
 #endif /* defined(__cplusplus) */
diff --git a/test/unit/uuid.c b/test/unit/uuid.c
index b51d13cb8..fdf263092 100644
--- a/test/unit/uuid.c
+++ b/test/unit/uuid.c
@@ -2,6 +2,8 @@
 #include "uuid/tt_uuid.h"
 #include "uuid/mp_uuid.h"
 #include "core/random.h"
+#include "msgpuck/msgpuck.h"
+#include "mp_extension_types.h"
 #include <string.h>
 
 static void
@@ -48,10 +50,69 @@ mp_uuid_test()
         check_plan();
 }
 
+static int
+mp_fprint_ext_test(FILE *file, const char **data, int depth)
+{
+        (void)depth;
+        int8_t type;
+        uint32_t len = mp_decode_extl(data, &type);
+        if (type != MP_UUID)
+                return fprintf(file, "undefined");
+        return mp_fprint_uuid(file, data, len);
+}
+
+static int
+mp_snprint_ext_test(char *buf, int size, const char **data, int depth)
+{
+        (void)depth;
+        int8_t type;
+        uint32_t len = mp_decode_extl(data, &type);
+        if (type != MP_UUID)
+                return snprintf(buf, size, "undefined");
+        return mp_snprint_uuid(buf, size, data, len);
+}
+
+static void
+mp_print_test(void)
+{
+        plan(5);
+        header();
+
+        mp_snprint_ext = mp_snprint_ext_test;
+        mp_fprint_ext = mp_fprint_ext_test;
+
+        char buffer[1024];
+        char str[1024];
+        struct tt_uuid uuid;
+        tt_uuid_create(&uuid);
+
+        mp_encode_uuid(buffer, &uuid);
+        int rc = mp_snprint(NULL, 0, buffer);
+        is(rc, UUID_STR_LEN, "correct mp_snprint size with empty buffer");
+        rc = mp_snprint(str, sizeof(str), buffer);
+        is(rc, UUID_STR_LEN, "correct mp_snprint size");
+        is(strcmp(str, tt_uuid_str(&uuid)), 0, "correct mp_snprint result");
+
+        FILE *f = tmpfile();
+        rc = mp_fprint(f, buffer);
+        is(rc, UUID_STR_LEN, "correct mp_fprint size");
+        rewind(f);
+        rc = fread(str, 1, sizeof(str), f);
+        str[rc] = 0;
+        is(strcmp(str, tt_uuid_str(&uuid)), 0, "correct mp_fprint result");
+        fclose(f);
+
+        mp_snprint_ext = mp_snprint_ext_default;
+        mp_fprint_ext = mp_fprint_ext_default;
+
+        footer();
+        check_plan();
+}
+
 int
 main(void)
 {
-        plan(3);
+        plan(4);
 
         uuid_test(
                 (struct tt_uuid){.time_low = 1712399963,
@@ -85,6 +146,7 @@ main(void)
                 -1);
 
         mp_uuid_test();
+        mp_print_test();
 
         return check_plan();
 }
diff --git a/test/unit/uuid.result b/test/unit/uuid.result
index 50a1140c5..4899ae4a2 100644
--- a/test/unit/uuid.result
+++ b/test/unit/uuid.result
@@ -1,4 +1,4 @@
-1..3
+1..4
 ok 1 - 6611265b-8852-4832-af8b-4164d52c62eb > 186ebbf7-cf97-4e2e-8b1b-76154f6f3949
 ok 2 - 075b4148-8fb0-2e7f-af50-4164d52c62eb < 1fbc929f-5da8-28c5-8b36-76154f6f3949
     1..4
@@ -7,3 +7,12 @@ ok 2 - 075b4148-8fb0-2e7f-af50-4164d52c62eb < 1fbc929f-5da8-28c5-8b36-76154f6f39
     ok 3 - mp_sizeof_uuid() == decoded length
     ok 4 - mp_decode_uuid(mp_encode_uuid(uu)) == uu
 ok 3 - subtests
+    1..5
+	*** mp_print_test ***
+    ok 1 - correct mp_snprint size with empty buffer
+    ok 2 - correct mp_snprint size
+    ok 3 - correct mp_snprint result
+    ok 4 - correct mp_fprint size
+    ok 5 - correct mp_fprint result
+	*** mp_print_test: done ***
+ok 4 - subtests
-- 
2.21.1 (Apple Git-122.3)



More information about the Tarantool-patches mailing list