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

[WIP] Performant decoding/encoding with updated PostgresNIO APIs #253

Closed
wants to merge 5 commits into from

Conversation

MahdiBM
Copy link
Contributor

@MahdiBM MahdiBM commented Oct 29, 2023

This PR has been opened so we can discuss the direction of the change while i'm working on it. Nothing is final.

Adds a macro name PostgresRecord for decoding/encoding of Swift types with newer more-performant PostgresNIO APIs.

Example

@PostgresRecord
struct MyTable {
    let int: Int
    let string: String
}

func decode(from row: PostgresRow) -> MyTable {
    row.decode() /// The macro will handle the decoding from here.
}

The macro-generated code:

extension MyTable: PostgresRecord {
    init(
        _from row: PostgresRow,
        context: PostgresDecodingContext<some PostgresJSONDecoder>,
        file: String,
        line: Int
    ) throws {
        let decoded = try row.decode(
            (Int, String).self,
            context: context,
            file: file,
            line: line
        )
        self.int = decoded.0
        self.string = decoded.1
    }
}

Checklist

  • Handle encoding too.
    • Support snake-case key strategy.
  • Properly document APIs as soon as we're settled on the APIs.
  • Tests for usage of the new APIs.
  • Conditional Swift 5.9 support if we don't want to move to Swift 5.9+ just yet.

@gwynne
Copy link
Member

gwynne commented Nov 6, 2023

I'm confused, did you for some reason miss the fact that I updated PostgresKit to the newer APIs a long time ago?

@gwynne gwynne closed this Nov 6, 2023
@MahdiBM
Copy link
Contributor Author

MahdiBM commented Nov 6, 2023

Nope, not really. This is just an inherent limitation of the newer PostgresNIO APIs, if you want them to be as performant as they should be (e.g. not use PostgresRandomAccessRow). You can't decode any arbitrary row with the newer APIs to an object/struct. You need to manually write out the tuple, and then one by one initialize your struct members.
This PR's primary purpose is to solve that.

@MahdiBM MahdiBM reopened this Nov 6, 2023
@MahdiBM MahdiBM closed this Mar 24, 2024
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