Tarantool development patches archive
 help / color / mirror / Atom feed
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 2/5] decimal: provide MP_DECIMAL extension serializer
Date: Tue, 12 May 2020 01:45:49 +0200	[thread overview]
Message-ID: <449685df941ca9a14e102738da413e737e559aea.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_DECIMAL. It is not activated yet, but will be later, when other
extensions also get their serializers.

Part of #4719
---
 src/lib/core/mp_decimal.c | 18 +++++++++++
 src/lib/core/mp_decimal.h | 27 +++++++++++++++++
 test/unit/decimal.c       | 63 ++++++++++++++++++++++++++++++++++++++-
 test/unit/decimal.result  | 11 ++++++-
 4 files changed, 117 insertions(+), 2 deletions(-)

diff --git a/src/lib/core/mp_decimal.c b/src/lib/core/mp_decimal.c
index 985e75291..ffc2c5773 100644
--- a/src/lib/core/mp_decimal.c
+++ b/src/lib/core/mp_decimal.c
@@ -70,3 +70,21 @@ mp_encode_decimal(char *data, const decimal_t *dec)
 	data = decimal_pack(data, dec);
 	return data;
 }
+
+int
+mp_snprint_decimal(char *buf, int size, const char **data, uint32_t len)
+{
+	decimal_t d;
+	if (decimal_unpack(data, len, &d) == NULL)
+		return -1;
+	return snprintf(buf, size, "%s", decimal_to_string(&d));
+}
+
+int
+mp_fprint_decimal(FILE *file, const char **data, uint32_t len)
+{
+	decimal_t d;
+	if (decimal_unpack(data, len, &d) == NULL)
+		return -1;
+	return fprintf(file, "%s", decimal_to_string(&d));
+}
diff --git a/src/lib/core/mp_decimal.h b/src/lib/core/mp_decimal.h
index 778529068..b8a327632 100644
--- a/src/lib/core/mp_decimal.h
+++ b/src/lib/core/mp_decimal.h
@@ -63,6 +63,33 @@ mp_decode_decimal(const char **data, decimal_t *dec);
 char *
 mp_encode_decimal(char *data, const decimal_t *dec);
 
+/**
+ * Print decimal's string representation into a given buffer.
+ * @param buf Target buffer to write string to.
+ * @param size Buffer size.
+ * @param data MessagePack encoded decimal, 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 decimal.
+ * @retval >=0 How many bytes were written, or would have been
+ *        written, if there was enough buffer space.
+ */
+int
+mp_snprint_decimal(char *buf, int size, const char **data, uint32_t len);
+
+/**
+ * Print decimal's string representation into a stream.
+ * @param file Target stream to write string to.
+ * @param data MessagePack encoded decimal, 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 decimal, or couldn't write to
+ *        the stream.
+ * @retval >=0 How many bytes were written.
+ */
+int
+mp_fprint_decimal(FILE *file, const char **data, uint32_t len);
+
 #if defined(__cplusplus)
 } /* extern "C" */
 #endif /* defined(__cplusplus) */
diff --git a/test/unit/decimal.c b/test/unit/decimal.c
index 254114b5f..179723d02 100644
--- a/test/unit/decimal.c
+++ b/test/unit/decimal.c
@@ -254,10 +254,70 @@ test_to_int(void)
 	return 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_DECIMAL)
+		return fprintf(file, "undefined");
+	return mp_fprint_decimal(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_DECIMAL)
+		return snprintf(buf, size, "undefined");
+	return mp_snprint_decimal(buf, size, data, len);
+}
+
+static void
+test_mp_print(void)
+{
+	plan(5);
+	header();
+
+	mp_snprint_ext = mp_snprint_ext_test;
+	mp_fprint_ext = mp_fprint_ext_test;
+
+	char buffer[1024];
+	char str[1024];
+	const char *expected = "1.234";
+	const int len = strlen(expected);
+	decimal_t d;
+	decimal_from_string(&d, expected);
+	mp_encode_decimal(buffer, &d);
+	int rc = mp_snprint(NULL, 0, buffer);
+	is(rc, len, "correct mp_snprint size with empty buffer");
+	rc = mp_snprint(str, sizeof(str), buffer);
+	is(rc, len, "correct mp_snprint size");
+	is(strcmp(str, expected), 0, "correct mp_snprint result");
+
+	FILE *f = tmpfile();
+	rc = mp_fprint(f, buffer);
+	is(rc, len, "correct mp_fprint size");
+	rewind(f);
+	rc = fread(str, 1, sizeof(str), f);
+	str[rc] = 0;
+	is(strcmp(str, expected), 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(281);
+	plan(282);
 
 	dectest(314, 271, uint64, uint64_t);
 	dectest(65535, 23456, uint64, uint64_t);
@@ -322,6 +382,7 @@ main(void)
 	test_pack_unpack();
 
 	test_mp_decimal();
+	test_mp_print();
 
 	return check_plan();
 }
diff --git a/test/unit/decimal.result b/test/unit/decimal.result
index e8432fb36..f11396df3 100644
--- a/test/unit/decimal.result
+++ b/test/unit/decimal.result
@@ -1,4 +1,4 @@
-1..281
+1..282
 ok 1 - decimal(314)
 ok 2 - decimal(271)
 ok 3 - decimal(314) + decimal(271)
@@ -698,3 +698,12 @@ ok 280 - subtests
     ok 197 - decimal_unpack() after mp_decode_extl() value
     ok 198 - decimal_unpack() after mp_decode_extl() len
 ok 281 - subtests
+    1..5
+	*** test_mp_print ***
+    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
+	*** test_mp_print: done ***
+ok 282 - subtests
-- 
2.21.1 (Apple Git-122.3)

  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 ` Vladislav Shpilevoy [this message]
2020-05-12 15:13   ` [Tarantool-patches] [PATCH 2/5] decimal: provide MP_DECIMAL extension serializer 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 ` [Tarantool-patches] [PATCH 3/5] uuid: provide MP_UUID " Vladislav Shpilevoy
2020-05-12 17:36   ` 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=449685df941ca9a14e102738da413e737e559aea.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 2/5] decimal: provide MP_DECIMAL 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