Skip to content

Adds a configurable limit to the number of timestamp fractional second digits that are written to text representations.#784

Merged
tgregg merged 1 commit intomasterfrom
timestamp-tostring-limit
Apr 11, 2024
Merged

Adds a configurable limit to the number of timestamp fractional second digits that are written to text representations.#784
tgregg merged 1 commit intomasterfrom
timestamp-tostring-limit

Conversation

@tgregg
Copy link
Contributor

@tgregg tgregg commented Apr 11, 2024

Description of changes:
The Timestamp class holds fractional precision in a BigDecimal, where in many cases that precision can be compactly conveyed (e.g. 0d-1000000, which is a tuple of (0, 1000000)). In text, all those digits must be written explicitly. To avoid attempting to write an excessive number of digits, this PR proposes a configurable maximum, with the default set arbitrarily to 10,000, which is a preposterous number of digits. If the maximum is exceeded, an error is raised (rather than silently losing precision).

To avoid adding more public methods to Timestamp, I propose having all of Timestamp's toString-like methods just use the default. In the unlikely event that a user needs to raise the limit, they can use a text writer to serialize the timestamp. The tradeoff is the need for the new _Private_Trampoline utility class that allows us to call the new non-public Timestamp.toString(int) variant in IonWriterSystemText (which lies in a different Java package) without exposing it publicly in Timestamp.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Copy link
Contributor

@popematt popematt left a comment

Choose a reason for hiding this comment

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

To avoid adding more public methods to Timestamp, I propose having all of Timestamp's toString-like methods just use the default.

I agree with this.

Overall looks good, but it would be ideal to have another test case or two for the text writer.

/**
* <b>NOT FOR APPLICATION USE. This class may be removed at any time.</b>
*/
public final class _Private_Trampoline {
Copy link
Contributor

Choose a reason for hiding this comment

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

FYI, you could fairly trivially rewrite this in Kotlin and mark it as internal. You don't even need to include the class.

E.g. _Private_Trampoline.kt

package com.amazon.ion

/**
 * **NOT FOR APPLICATION USE. This method may be removed at any time.**
 * Trampoline to the non-public `Timestamp.toString(Int)` method.
 */
internal fun print(timestamp: Timestamp, maximumDigits: Int): String {
    return timestamp.toString(maximumDigits)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, thanks

super.testAnnotationNotSetToIvmOnStartOfStream();
}

@Test(expected = IllegalArgumentException.class)
Copy link
Contributor

Choose a reason for hiding this comment

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

We should probably add 1 or 2 test cases to ensure that the max length is respected when changed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

That's exactly it. I guess I missed that because I was expecting the TextWriter tests to be in TextWriterTest.java.

…d digits that are written to text representations.
@tgregg tgregg force-pushed the timestamp-tostring-limit branch from b747ee8 to 25ff035 Compare April 11, 2024 00:59
@tgregg
Copy link
Contributor Author

tgregg commented Apr 11, 2024

Revision 2:

  • Migrate _Private_Trampoline to Kotlin
  • Minor changes to code untouched by the PR to satisfy SpotBugs

@tgregg tgregg merged commit 409987b into master Apr 11, 2024
@tgregg tgregg deleted the timestamp-tostring-limit branch April 11, 2024 18:41
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.

2 participants