From: Vladislav Shpilevoy <v.shpilevoy@tarantool.org> To: tarantool-patches@dev.tarantool.org, gorcunov@gmail.com, sergepetrenko@tarantool.org, korablev@tarantool.org Subject: [Tarantool-patches] [PATCH 3/5] uuid: provide MP_UUID extension serializer Date: Tue, 12 May 2020 01:45:50 +0200 [thread overview] Message-ID: <1584dd24e386d29c899689418571517d10c18a90.1589240704.git.v.shpilevoy@tarantool.org> (raw) In-Reply-To: <cover.1589240704.git.v.shpilevoy@tarantool.org> 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)
next prev parent reply other threads:[~2020-05-11 23:45 UTC|newest] Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top 2020-05-11 23:45 [Tarantool-patches] [PATCH 0/5] mp_snprint() and mp_fprint() for decimal, uuid, error Vladislav Shpilevoy 2020-05-11 23:45 ` [Tarantool-patches] [PATCH 1/5] msgpuck: bump version to enable extension printer Vladislav Shpilevoy 2020-05-12 17:34 ` Cyrill Gorcunov 2020-05-11 23:45 ` [Tarantool-patches] [PATCH 2/5] decimal: provide MP_DECIMAL extension serializer Vladislav Shpilevoy 2020-05-12 15:13 ` Cyrill Gorcunov 2020-05-12 20:30 ` Vladislav Shpilevoy 2020-05-12 20:56 ` Cyrill Gorcunov 2020-05-12 17:35 ` Cyrill Gorcunov 2020-05-11 23:45 ` Vladislav Shpilevoy [this message] 2020-05-12 17:36 ` [Tarantool-patches] [PATCH 3/5] uuid: provide MP_UUID " Cyrill Gorcunov 2020-05-11 23:45 ` [Tarantool-patches] [PATCH 4/5] error: provide MP_ERROR " Vladislav Shpilevoy 2020-05-12 17:52 ` Cyrill Gorcunov 2020-05-12 20:38 ` Vladislav Shpilevoy 2020-05-12 21:27 ` Cyrill Gorcunov 2020-05-18 15:24 ` Serge Petrenko 2020-05-13 12:31 ` Nikita Pettik 2020-05-13 22:10 ` Vladislav Shpilevoy 2020-05-14 2:32 ` Nikita Pettik 2020-05-14 21:28 ` Vladislav Shpilevoy 2020-05-19 13:21 ` Nikita Pettik 2020-05-20 21:57 ` Vladislav Shpilevoy 2020-05-19 11:51 ` Alexander Turenko 2020-05-19 20:48 ` Vladislav Shpilevoy 2020-05-11 23:45 ` [Tarantool-patches] [PATCH 5/5] msgpuck: activate MP_EXT custom serializers Vladislav Shpilevoy 2020-05-12 17:52 ` Cyrill Gorcunov 2020-05-13 21:06 ` Nikita Pettik 2020-05-13 21:48 ` Vladislav Shpilevoy 2020-05-14 2:24 ` Nikita Pettik 2020-05-14 21:27 ` Vladislav Shpilevoy 2020-05-19 12:11 ` Alexander Turenko 2020-05-19 20:48 ` Vladislav Shpilevoy 2020-05-19 13:23 ` Nikita Pettik 2020-05-18 15:25 ` [Tarantool-patches] [PATCH 0/5] mp_snprint() and mp_fprint() for decimal, uuid, error Serge Petrenko 2020-05-21 18:25 ` Alexander Turenko
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=1584dd24e386d29c899689418571517d10c18a90.1589240704.git.v.shpilevoy@tarantool.org \ --to=v.shpilevoy@tarantool.org \ --cc=gorcunov@gmail.com \ --cc=korablev@tarantool.org \ --cc=sergepetrenko@tarantool.org \ --cc=tarantool-patches@dev.tarantool.org \ --subject='Re: [Tarantool-patches] [PATCH 3/5] uuid: provide MP_UUID extension serializer' \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox