pgxoption: nested module for native pgx v5 (pgtype) integration - #54
Open
moznion wants to merge 1 commit into
Open
pgxoption: nested module for native pgx v5 (pgtype) integration#54moznion wants to merge 1 commit into
moznion wants to merge 1 commit into
Conversation
Registers Option[T] wrapper plans into pgx's pgtype.Map so Option values encode and scan with the element type's own codec: None <=> SQL NULL, Some(v) behaves exactly like v, in both text and binary formats. This bypasses the generic sql.Scanner/driver.Valuer fallback, which detours through driver.Value (lossy for types like numeric and slower). A nested module (github.com/moznion/go-optional/pgxoption) keeps the core package dependency-free; tag releases as pgxoption/vX.Y.Z. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01P7UbMMpmzxDECU9gqxAAZ3
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #54 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 2 2
Lines 211 168 -43
=========================================
- Hits 211 168 -43 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds
github.com/moznion/go-optional/pgxoptionas a nested Go module (owngo.mod, so the core package stays dependency-free; release by taggingpgxoption/vX.Y.Z).pgxoption.Register(m *pgtype.Map)installsTryWrapScanPlanFunc/TryWrapEncodePlanFuncwrappers that plugOption[T]directly into pgx v5's plan machinery:**T, so pgx's own NULL→nil-pointer handling applies;nilmaps toNone, a value maps toSome(a previousSomeis overwritten by a NULL scan).None→ SQLNULL;Some(v)delegates toT's registered codec.Why
Without registration, pgx handles
Option[T]via the genericsql.Scanner/driver.Valuerfallback, which detours throughdriver.Value— extra conversions, text-ish and lossy for types likenumeric, andOption[T]'s underlying[]Tshape sits uncomfortably close to pgx's slice→array reflection. Native plans give full type fidelity in both text and binary formats for every element type the connection'spgtype.Mapsupports.Detection is by defined type (
PkgPath+Option[name prefix), so plain slices keep flowing through pgx's array machinery untouched (pinned by a test).Notes
go 1.23.0/ pgxv5.7.6— the oldest pgx line that doesn't force a newer Go directive on consumers (MVS lets users float pgx upward freely). CI gets a separatepgxoptionjob (1.23.x/1.24.x), since the root matrix still includes 1.22.pgtype.NewMap(): text/binary scans, NULL→None, None→NULL encoding, a timestamptz binary round-trip, and the plain-slice non-interference pin.Context: sqletch is adopting
Option[T]across its generated code (moznion/go-sqletch#21). Its generated scan path deliberately does not require this module — pgxoption is the opt-in native-fidelity path for direct pgx users.🤖 Generated with Claude Code
https://claude.ai/code/session_01P7UbMMpmzxDECU9gqxAAZ3