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

Incorrect working in systems with constant msgpack types headers #870

Open
nickkkccc opened this issue Jan 27, 2025 · 0 comments
Open

Incorrect working in systems with constant msgpack types headers #870

nickkkccc opened this issue Jan 27, 2025 · 0 comments

Comments

@nickkkccc
Copy link

nickkkccc commented Jan 27, 2025

Problem description

Let's imagine that I have a system that accepts only numbers encoded in UINT16.

Using the API with automatic header selection, it is impossible to encode values from 0 to 2^8-1 in UINT16 because MessagePacker will offer the types UINT8, INT8, NEGFIXINT, POSFIXINT. This will happen until the number exceeds the ranges UINT8, INT8, NEGFIXINT, POSFIXINT.

Suggestions for API improvements for statically typed systems

I suggest introducing an API in which the client can choose the type of encoded header.
For example:

    public MessagePacker packUINT(final short b, final MessageFormat type)
        throws IOException
    {
        switch (type) {
            case UINT8:
                writeByteAndByte(UINT8, (byte) b);
                break;
            case UINT16:
                writeByteAndShort(UINT16, b);
                break;
            case UINT32:
                writeByteAndInt(UINT32, b);
                break;
            case UINT64:
                writeByteAndLong(UINT64, b);
                break;
            default: 
                throw new MessagePackException("Cannot convert '" + type + "' to UINT family types.");
        }
        return this;
    }

Here, a method should be understood as a set of overloaded methods with short, int, long, BigInteger value arguments . Conversion based on narrowing casting

A similar solution for FLOAT32/64 types, INT8,16,32,64 families.
The code has already been written, it remains to send a PR

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

No branches or pull requests

1 participant