-
Notifications
You must be signed in to change notification settings - Fork 25
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
quick POC at non-Long numerics (encoding and decoding) #164
Conversation
|
||
val floatValue = doubleValue.toFloat() | ||
|
||
if (floatValue.isInfinite()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Json apparently doesn't have any way to express infinity and NaN, so I guess that's why KxS has this exception. Toml does have inf
and nan
though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like I actually missed that, need to support it...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ktlint found more than 10 potential problems in the proposed changes. Check the Files changed tab for more details.
POC completed - will be finalised in #171 |
no-no, let's not close it! I will merge it to add your great tests as a separate PR to make a functional PR less :) I just want to make smaller PRs, so this PR will be good to split the logic |
Ahh okay, I misunderstood 👍 |
} | ||
} | ||
|
||
testFails("${Char.MAX_VALUE.digitToInt() + 1}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
kotlin.IllegalArgumentException: Char � is not a decimal digit
@@ -26,8 +26,8 @@ public abstract class TomlAbstractDecoder : AbstractDecoder() { | |||
override fun decodeByte(): Byte = decodePrimitiveType() | |||
override fun decodeShort(): Short = decodePrimitiveType() | |||
override fun decodeInt(): Int = decodePrimitiveType() | |||
override fun decodeFloat(): Float = decodePrimitiveType() | |||
override fun decodeChar(): Char = decodePrimitiveType() | |||
override fun decodeFloat(): Float = invalidType("Float", "Double") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's split this logic into several PRs, same with Char.
Actually Char is even more discussible, because there is no syntax in Toml for char literals, only for Strings... I am afraid that allowing Char we can make A HUGE pitfall for users here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I copy and pasted Char in, but in retrospect I don't think it's clear how it should be handled. Is Char a number? Should 101
be treated as ascii and be decoded to A
? (No, probably not.) What about decoding a
to val c: Char
?
EDIT: Ah, I see you made an issue #174
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm with my changes, aSemy, thank you once again!
A quick stab at showing how non-Long numbers could be encoded and decoded.
#163
This is a very quick draft, I haven't spent a lot of time on it. I'm just sharing because I think the general approach is good.
This approach is based off the Kotlinx Serialization approach to handling JSON numbers
https://github.com/Kotlin/kotlinx.serialization/blob/dc950f5098162a3f8dd742d04b95f9a0405470e1/formats/json/commonMain/src/kotlinx/serialization/json/internal/StreamingJsonDecoder.kt#L281-L290
First the numbers are parsed as a Long or Double, as is already the case. Then ktoml will try to manually convert these numbers to the required type, but first verifying that the numbers are in range.
If you wanted to verify based on the raw string content, you could perhaps implement a function based on how KxS parses numbers: https://github.com/Kotlin/kotlinx.serialization/blob/dc950f5098162a3f8dd742d04b95f9a0405470e1/formats/json/commonMain/src/kotlinx/serialization/json/internal/lexer/AbstractJsonLexer.kt#L533-L584