Skip to content

Add API to NpgsqlDataSource to resolve type mappings#6084

Open
vonzshik wants to merge 1 commit intomainfrom
data-source-support-type-mapping-resolve
Open

Add API to NpgsqlDataSource to resolve type mappings#6084
vonzshik wants to merge 1 commit intomainfrom
data-source-support-type-mapping-resolve

Conversation

@vonzshik
Copy link
Contributor

No description provided.

@vonzshik vonzshik added this to the 10.0.0 milestone Mar 31, 2025
@vonzshik vonzshik self-assigned this Mar 31, 2025
@vonzshik vonzshik marked this pull request as ready for review March 31, 2025 12:47
@vonzshik vonzshik requested a review from roji as a code owner March 31, 2025 12:47
/// <summary>
/// Attempts to return a mapping for a specific type.
/// </summary>
public PgTypeInfo? TryGetMapping(Type? type = null, string? dataTypeName = null)
Copy link
Member

Choose a reason for hiding this comment

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

Note that PgTypeInfo is [Experimental], and also in the Internal namespace - I think that up to now the idea was that this was only for converter writers; we'd be exposing this to users for the first time. This also shows in the public API surface of this type - most of the stuff there probably shouldn't be shown to users.

I think that if we want to add an introspection API like this, we need another type. We do have the PostgresType hierarchy, but that really models the PG type only (no CLR type there, for example).

(just noting that there's no real urgent need for this API from my side)

/cc @NinoFloris

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that if we want to add an introspection API like this, we need another type. We do have the PostgresType hierarchy, but that really models the PG type only (no CLR type there, for example).

Makes sense. We just have to agree on what exactly that type has to have. Probably, CLR type, the name of postgres's type + whether it supports writing?

Copy link
Member

Choose a reason for hiding this comment

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

NpgsqlMappingInfo or something? Maybe @NinoFloris has a good idea here.

It could actually reference the PostgresType rather than just a string name (for full introspection capabilities). Besides that, just having the CLR type seems sufficient to me as a start, writability seems like an extra thing we don't necessarily need to do...

@roji roji removed the enhancement label Jun 14, 2025
@vonzshik vonzshik force-pushed the data-source-support-type-mapping-resolve branch from c6130c4 to d5314a8 Compare September 10, 2025 11:43
/// <summary>
/// Attempts to return a mapping for a specific type.
/// </summary>
public async ValueTask<PgTypeInfo?> TryGetMappingAsync(Type? type = null, string? dataTypeName = null, CancellationToken cancellationToken = default)
Copy link
Member

Choose a reason for hiding this comment

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

Yeah, ideally we'd be able to return mapping info to the user without ever connecting to the database, purely based on how the data source was configured - in which case we wouldn't need an async API here. This is a bit related to us returning a separate type (not PgTypeInfo), which would presumably return restricted info that's purely client-side mapping config.

Or am I missing something and this mapping information API really doesn't make sense without first connecting to the database and getting actual type OIDs etc.?

/// <summary>
/// Attempts to return a mapping for a specific type.
/// </summary>
public ValueTask<PgTypeInfo?> TryGetMappingAsync<T>(string? dataTypeName = null, CancellationToken cancellationToken = default)
Copy link
Member

Choose a reason for hiding this comment

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

I think that for cases where we don't actually need a generic type parameter, i.e. a Type parameter is enough (and will likely always be enough), there's no reason to introduce the additional generic API.

Copy link
Member

@roji roji left a comment

Choose a reason for hiding this comment

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

@vonzshik there's probably quite a bit to discuss here, and it's quite non-urgent, so let's postpone this to the future and not hurry for 10, ok?

/// <summary>
/// Attempts to return a mapping for a specific type.
/// </summary>
public PgTypeInfo? TryGetMapping(Type? type = null, string? dataTypeName = null)
Copy link
Member

Choose a reason for hiding this comment

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

API design note: rather than a single method with two nullable parameters - which throws when both are null - we can have two methods, so that the annotations prevent the call at compile-time (internally they'd both just immediately call into a single method).

@vonzshik
Copy link
Contributor Author

vonzshik commented Oct 5, 2025

@vonzshik there's probably quite a bit to discuss here, and it's quite non-urgent, so let's postpone this to the future and not hurry for 10, ok?

Yep, I'm fine with that. After all, you're the one who requested this feature (and probably the only one who's actually going to use it).

@roji
Copy link
Member

roji commented Oct 5, 2025

and probably the only one who's actually going to use it

FWIW I do think there are other usages for it - I've seen some cases over the years where dynamic layers/users want to know what Npgsql will map as; that's why the "existing introspection API" exists on NpgsqlParameter. But these are indeed quite rare/advanced indeed.

@roji roji modified the milestones: 10.0.0, 11.0.0 Nov 2, 2025
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