Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

decimal to string serialization #10

Closed
wants to merge 3 commits into from

Conversation

parihaaraka
Copy link

No description provided.

Copy link

@sergepetrenko sergepetrenko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the patch!
Please see some of my comments below.

msgpuck.c Outdated Show resolved Hide resolved
msgpuck.c Outdated Show resolved Hide resolved
msgpuck.c Outdated Show resolved Hide resolved
Copy link

@sergepetrenko sergepetrenko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test and check out my 2 comments below.

Regarding the test encode some simple decimal here
https://github.com/tarantool/msgpuck/blob/master/test/msgpuck.c#L887
You may say char decimal[] = "\xd6\x01\x02\x03\x45\x6d", this is -34.56 encoded,
and then print it with mp_snprint

Otherwise LGTM

msgpuck.c Outdated Show resolved Hide resolved
msgpuck.c Outdated Show resolved Hide resolved
Copy link

@sergepetrenko sergepetrenko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the fixes.
Please consider one last nit below. Otherwise LGTM
I will forward this to @Totktonada to take a look and merge.

test/msgpuck.c Outdated Show resolved Hide resolved
Copy link

@sergepetrenko sergepetrenko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks. @Totktonada, take a look, please.

parihaaraka pushed a commit to parihaaraka/cpp2tnt that referenced this pull request Nov 5, 2019
Copy link
Member

@Totktonada Totktonada left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@parihaaraka Thank you for your work on that, but I think it should not be done in this way (see more below). Maybe it would be better to file an issue to tarantool/tarantool or this repository and describe what exactly you want to achieve.

msgpuck.c Outdated
@@ -232,6 +232,94 @@ mp_format(char *data, size_t data_size, const char *format, ...)
return res;
}

size_t
print_decimal(char **buf, size_t buf_size, const char *val, uint32_t val_bytes)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should not be part of msgpuck library, but our mp_decimal implementation.

We should however provide an ability to set custom handlers for MP_EXT subtypes for mp_[v]format and mp_{f,sn}print.

I propose to provide subroutines and (maybe) mp_format percent-placeholders for extension types via defines that should exist before including of msgpuck.h.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should not be part of mp_decimal, gmp or any other library. We convert messagepack to string here. Any decimal library internals are out of scope. This repository is a tarantool's one, so we may relay on ext subtypes being used by tarantool. But callbacks to encode/decode external data (e.g. mp_decimal) to/from decimal are expected.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is the general purpose library, so it would be good to handle all operations with decimals from mp_decimal. Of course, if this will not block usage of this library by, say, tarantool connectors developers. That is why I keep asking you about your case.

Maybe we should provide mp_decimal.h with tarantool-dev/devel packages. Or ship extensions with msgpuck and allow to enable / disable them at build time.

test/msgpuck.c Outdated
assert(d <= msgpack + sizeof(msgpack));

const char *expected =
"[-5, 42, \"kill bill\", [], "
"["
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unneeded change.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need it to match the mess with the array size. Unneeded reproval.

msgpuck.c Outdated
scale = mp_decode_int(&val);

if (scale_head == val || !val_bytes)
return 0; /* "undefined" (is ext mp type 1 misused?) */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't got why the comment is indented with 9 whitespaces. We usually avoid comments on the same line with code.

The same for comments below.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just like aligned comments. You guys love different things. Just prepare it by yourself.

int int_status = 0; /* 1 - signed, 2 - unsigned */

#define __STDC_CONSTANT_MACROS 1 /* make С++ to be happy */

and tons below

msgpuck.c Outdated
if (ext_type != 1) { \
PRINTF("undefined"); \
} else { \
char static_buf[128]; \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use PRINTF inside print_decimal and don't create static (automatic really) / dynamic buffers explicitly in this part of code.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You the boss (the second one)

test/msgpuck.c Outdated
int esize = strlen(expected);

char result[256];
char result[512];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side note: the branch contains commits from rtsisyk/master, they should be removed. Other commits should be squashed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?!
256 bytes is not enougth

@parihaaraka
Copy link
Author

@parihaaraka Thank you for your work on that, but I think it should not be done in this way (see more below). Maybe it would be better to file an issue to tarantool/tarantool or this repository and describe what exactly you want to achieve.

I want to print all the mp types supported by tarantool.

@Totktonada
Copy link
Member

tarantool/tarantool#4719

@Totktonada
Copy link
Member

Usage example: parihaaraka/cpp2tnt@3ead452

@Totktonada
Copy link
Member

I added @alyapunov to make final decisions and proceed somehow (or decline).

@sergepetrenko
Copy link

sergepetrenko commented May 20, 2022

@Totktonada
We already have decimal printer in tarantool's libcore:
https://github.com/tarantool/tarantool/blob/a74c549a16f1168b21644bd9aa85bceba99ea6a1/src/lib/core/mp_decimal.c#L74-L81

Same stands for each other ext type used in tarantool.
See, for example, mp_snprint_datetime, mp_snprint_uuid and so on.\

And msgpack has mp_snprint_ext stub, which is replaced by tarantool's msgpack_snprint_ext.
msgpack_snprint_ext in turn chooses the appropriate handler (e.g. mp_snprint_decimal)

@parihaaraka
Copy link
Author

@Totktonada We already have decimal printer in tarantool's libcore: https://github.com/tarantool/tarantool/blob/a74c549a16f1168b21644bd9aa85bceba99ea6a1/src/lib/core/mp_decimal.c#L74-L81

Same stands for each other ext type used in tarantool. See, for example, mp_snprint_datetime, mp_snprint_uuid and so on.\

And msgpack has mp_snprint_ext stub, which is replaced by tarantool's msgpack_snprint_ext. msgpack_snprint_ext in turn chooses the appropriate handler (e.g. mp_snprint_decimal)

External application doesn't deal with internal tarantool's structures. It needs to print msgpack (and convert to mpdecimal or another corresponding stuff). BTW string to tnt ext types conversion would be great too.

@sergos
Copy link

sergos commented Nov 18, 2022

@alyapunov please proceed with review

@alyapunov
Copy link
Collaborator

Hi, thanks for effort!
We decided to leave msgpuck as independent library that provides external configuration for ext types (see mp_fprint_ext etc). Tarantool sets those handlers, introducing tarantool-specific exts. There's no more need to patch msgpuck.
If you need to use tarantool extensions outside of tarantool - just grab msgpack_init etc.

@alyapunov alyapunov closed this Nov 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants