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

IntDecodingStrategy #8

Merged
merged 1 commit into from
Jun 26, 2024
Merged

IntDecodingStrategy #8

merged 1 commit into from
Jun 26, 2024

Conversation

swhitty
Copy link
Owner

@swhitty swhitty commented Jun 26, 2024

Adds IntDecodingStrategy allowing integer values to be decoded via with a FloatingPointRoundingRule

public enum IntDecodingStrategy {
    /// Decodes all number types with lossless conversion or throws error.
     case exact

    /// Decodes all floating point numbers using the provided rounding rule.
     case rounded(rule: FloatingPointRoundingRule)
}

The default is .exact to use rounding like so:

let decoder = KeyValueDecoder()
decoder.intDecodingStrategy = .rounded(rule: .toNearestOrAwayFromZero)

// [10, -21, 50]
let values = try decoder.decode([Int].self, from: [10.1, -20.9, 50.00001]),

@swhitty swhitty merged commit 2a2a882 into main Jun 26, 2024
5 checks passed
@swhitty swhitty deleted the int-decoding-strategy branch June 26, 2024 21:40
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.

1 participant