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

Remove PSQLJSONDecoder from PSQLConnection #214

Merged

Conversation

fabianfett
Copy link
Collaborator

Motivation

Currently we attach a PSQLJSONDecoder and PSQLJSONEncoder to a PostgresConnection. This is not ideal. Instead users should be able to supply an encoder and decoder on a per query basis.

Changes

  • Remove PSQLJSONDecoder from the Connection and supply it on decoding instead.

Result

  • Code that is less intertwined and easier to reason about.
  • Fewer existentials hanging around.

@codecov-commenter
Copy link

codecov-commenter commented Feb 14, 2022

Codecov Report

Merging #214 (a8717a0) into main (e231a57) will increase coverage by 4.10%.
The diff coverage is 63.63%.

@@            Coverage Diff             @@
##             main     #214      +/-   ##
==========================================
+ Coverage   40.46%   44.57%   +4.10%     
==========================================
  Files         116      116              
  Lines        7777     7778       +1     
==========================================
+ Hits         3147     3467     +320     
+ Misses       4630     4311     -319     
Flag Coverage Δ
unittests 44.57% <63.63%> (+4.10%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...resNIO/Connection/PostgresConnection+Connect.swift 0.00% <0.00%> (ø)
Sources/PostgresNIO/New/PSQLTask.swift 34.28% <ø> (-0.85%) ⬇️
Sources/PostgresNIO/New/PSQLConnection.swift 31.68% <50.00%> (+2.07%) ⬆️
Sources/PostgresNIO/New/PSQLRow.swift 63.15% <54.54%> (-0.48%) ⬇️
Sources/PostgresNIO/New/PSQLRowStream.swift 77.08% <100.00%> (+5.77%) ⬆️
...urces/PostgresNIO/New/Data/Array+PSQLCodable.swift 94.68% <0.00%> (+1.06%) ⬆️
...rces/PostgresNIO/New/Extensions/Logging+PSQL.swift 19.60% <0.00%> (+1.42%) ⬆️
...s/PostgresNIO/Message/PostgresMessageDecoder.swift 95.45% <0.00%> (+2.27%) ⬆️
Sources/PostgresNIO/New/Data/Int+PSQLCodable.swift 20.26% <0.00%> (+2.61%) ⬆️
... and 23 more

@fabianfett fabianfett requested a review from 0xTim February 15, 2022 13:19

extension PSQLRow {
func decode<T: PSQLDecodable>(column: String, as type: T.Type, file: String = #file, line: Int = #line) throws -> T {
try self.decode(column: column, as: type, jsonDecoder: JSONDecoder(), file: file, line: line)
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe worth to create a JSONDecoder once (e.g. as a static let) and reuse it here to safe a couple of allocations.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

These two methods have only been added right now, to not rewrite large parts of the tests. They will not become public API, as PSQLRow will be merged with PostgresRow in the future. Then we should def. consider keeping a longer ref to JSONDecoder and JSONEncoder around.

try self.decode(column: column, as: type, jsonDecoder: JSONDecoder(), file: file, line: line)
}

func decode<T: PSQLDecodable>(column index: Int, as type: T.Type, file: String = #file, line: Int = #line) throws -> T {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
func decode<T: PSQLDecodable>(column index: Int, as type: T.Type, file: String = #file, line: Int = #line) throws -> T {
func decodeColumn<T: PSQLDecodable>(at columnIndex: Int, as type: T.Type = T.self, file: String = #file, line: Int = #line) throws -> T {

Nit: makes the naming a bit more swiftier (is this even a word? :D)
Similar, decode where column is a String could be decodeColumn(named:as:) or just decodeColumn(_:as:)

Will this be public API?

@fabianfett fabianfett merged commit cc07811 into vapor:main Feb 17, 2022
@fabianfett fabianfett deleted the ff-remove-decoder-from-PSQLConnection branch February 17, 2022 16:48
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.

4 participants