-
Notifications
You must be signed in to change notification settings - Fork 201
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
Allow writing the char representation when creating enums #448
Comments
See #434 for my proposal, which overlaps this one. |
#434 proposes much more radical changes. This one is doable staying in current paradigm: enums stay as lists of integer-based constants, the only thing changes is that we could introduce an extra way to represent integers by doing string-to-integer conversion (akin to I want to emphasize that these are not the strings. No integer-to-string conversion (with encoding and stuff) and a string comparison should take place here. Typically, languages like C/C++ and even Java allow syntax like
I.e. although even this does not work right now, it should be something like |
I just look a quick look at the YAML spec, it already allows putting escape sequences into double-quoted strings, you can write literals such as: So, I think my proposal is to allow someone to write this: enums:
messages:
"R": AuthenticationRequest
10: BackendKeyData
0x05: ParameterStatus
"\x06": Bind Where Kaitai attempts to convert each key into a byte array and throws an error if all keys do not result in byte arrays with the same width. |
This rule gets a bit tricky if you allow literals such as enums:
messages:
"R": AuthenticationRequest
300: BackendKeyData Because the first key says "this enum is for a byte array with width 1", while the second key requires at least a 2-byte enum. |
Is there a reason why having a consistent size matters? (So long as all of the values can be coerced to an integer type, I would think that "shorter" ones could simply be padded or promoted.) I think all that really matters is whether the keys can be represented as an enum-type (which is an integer of some limited size (like 32 bits)). So, single character strings can be rendered as their ASCII code value or their Unicode code-point; integers work as they are; and, anything else has to be a string containing a number in a convertible format (with an optional radix specifier, etc.). |
I suppose you could generalize things to accept non-numeric strings of an appropriately short length (shorter ones would be padded with nulls) which could be converted to a numeric enum value by converting each character to its ASCII code (etc.) and then shifting and summing them. |
@webbnh There are two severe problems that I see here:
|
The postgres protocol (usually) begins every message with a byte which determines what the rest of the message will be. This is a great use-case for a enum. What I'd like to be able to do is this:
However, this fails:
As a workaround I'm just using a type:
But it'd be nice to not need to.
The text was updated successfully, but these errors were encountered: