vapor / postgres-nio

🐘 Non-blocking, event-driven Swift client for PostgreSQL.
https://api.vapor.codes/postgresnio/documentation/postgresnio/
MIT License
304 stars 70 forks source link

Fix `reverseChunked(by:)` Method Implementation #465

Closed jiahan-wu closed 4 months ago

jiahan-wu commented 4 months ago

Summary

This pull request is being made to resolve an inconsistency found in the reverseChunked(by:) method located within the Collection extension. The intended functionality of the method is to divide a collection into chunks of a given size, where, if the collection is not evenly divisible, the first chunk will be smaller. Instead of this expected behavior, the current implementation produces incorrect results, with the first chunk not being the smallest when required and subsequent chunks varying incorrectly in size.

Example

Here are concrete examples that demonstrate the problem with the reverseChunked(by:) method's current implementation:

Example 1:

let collection = "1234567898765"
let chunks = collection.reverseChunked(by: 4)

// Expected Chunks: ["1", "2345", "6789", "8765"]
// Actual Chunks:   ["1", "2345", "67898", "765"]

Example 2:

let collection = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]
let chunks = collection.reverseChunked(by: 4)

// Expected Chunks: [[1], [2, 3, 4, 5], [6, 7, 8, 9], [10, 11, 12, 13], [14, 15, 16, 17]]
// Actual Chunks:   [[1], [2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15, 16, 17], []]

Impact of Changes

The changes introduced by this pull request will resolve the crashing issue in the PostgresNumeric(decimalString:) initializer. The crash is currently caused by the improper handling of string to numeric conversion when the string is split into chunks using the reverseChunked(by:) method.

Screenshot 2024-03-08 at 10 10 48 PM
codecov[bot] commented 4 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 61.76%. Comparing base (43929b0) to head (6eb5201).

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #465 +/- ## ========================================== - Coverage 61.78% 61.76% -0.03% ========================================== Files 125 125 Lines 10030 10024 -6 ========================================== - Hits 6197 6191 -6 Misses 3833 3833 ``` | [Files](https://app.codecov.io/gh/vapor/postgres-nio/pull/465?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vapor) | Coverage Δ | | |---|---|---| | [...ources/PostgresNIO/Data/PostgresData+Numeric.swift](https://app.codecov.io/gh/vapor/postgres-nio/pull/465?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vapor#diff-U291cmNlcy9Qb3N0Z3Jlc05JTy9EYXRhL1Bvc3RncmVzRGF0YStOdW1lcmljLnN3aWZ0) | `70.50% <100.00%> (-0.86%)` | :arrow_down: |
fabianfett commented 4 months ago

@jiahan-wu Thanks for your contribution! Great fix!