SeaQL / SeaQL/sea-orm

Re-design `TryGetable` to unify optional value decoding

Open
#3,201 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Breaking Change
Dominant language
Rust
Stars
9.9k
Forks
734
Avg merge
6h 36m
Merged PRs (30d)
8

Description

Currently, we first try to decode the value normally. If decoding fails (because it's null), an error is created. For Option<T>, we then drop that error and return None instead. This means decoding an optional value still constructs an error string on the failure path, which adds unnecessary allocation and overhead.

Ideally, we should provide a blanket implementation for Option<T>. Then users would only need to implement the conversion from our value for their own types.

This is a breaking change because it requires users to rewrite all their implementation to add the new method.

Example:

trait TryGetable {
    fn try_get_by<I: ColIdx>(
        row: &QueryResult,
        index: I,
    ) -> Result<Self, TryGetError> {
        let value = row.value(index)?;
        Self::try_from_value(value)
    }
}

impl<T> TryGetable for Option<T> {
    fn try_from_value(value: &Value) {
        if value.is_null() {
            return Ok(None);
        }
        
        T::try_from_value(value).map(Some)
    }
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the TryGetable trait and the QueryResult value-decoding path described in the issue, then inspect existing TryGetable implementations. Update the trait shape so Option can use a blanket implementation and existing types provide the value conversion method; done means optional null decoding avoids constructing an error and all implementations compile with the breaking change.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
backend, databases
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.