@@ -77,45 +77,6 @@ internal void DisposeWriteState(object writeState)
7777 disposable . Dispose ( ) ;
7878 }
7979
80- internal bool TryBind ( Field field , DataFormat format , out PgConverterInfo info )
81- {
82- switch ( this )
83- {
84- case { IsResolverInfo : false } :
85- // Type lies when IsBoxing is true.
86- var typeToConvert = IsBoxing ? typeof ( object ) : Type ;
87- if ( ! CachedCanConvert ( format , out var bufferRequirements ) )
88- {
89- info = default ;
90- return false ;
91- }
92- info = CreateConverterInfo ( bufferRequirements , isRead : true , Converter , typeToConvert ) ;
93- return true ;
94- case PgResolverTypeInfo resolverInfo :
95- var resolution = resolverInfo . GetResolution ( field ) ;
96- if ( ! HasCachedInfo ( resolution . Converter )
97- ? ! CachedCanConvert ( format , out bufferRequirements )
98- : ! resolution . Converter . CanConvert ( format , out bufferRequirements ) )
99- {
100- info = default ;
101- return false ;
102- }
103- info = CreateConverterInfo ( bufferRequirements , isRead : true , resolution . Converter , resolution . Converter . TypeToConvert ) ;
104- return true ;
105- default :
106- throw new NotSupportedException ( "Should not happen, please file a bug." ) ;
107- }
108- }
109-
110- // Bind for reading.
111- internal PgConverterInfo Bind ( Field field , DataFormat format )
112- {
113- if ( ! TryBind ( field , format , out var info ) )
114- ThrowHelper . ThrowInvalidOperationException ( $ "Resolved converter does not support { format } format.") ;
115-
116- return info ;
117- }
118-
11980 public PgConverterResolution GetResolution < T > ( T ? value )
12081 {
12182 // Other cases, to keep binary bloat minimal.
@@ -163,15 +124,6 @@ static PgConverterResolution ThrowNotSupported()
163124 => throw new NotSupportedException ( "Should not happen, please file a bug." ) ;
164125 }
165126
166- PgConverterInfo CreateConverterInfo ( BufferRequirements bufferRequirements , bool isRead , PgConverter converter , Type typeToConvert )
167- => new ( )
168- {
169- TypeInfo = this ,
170- Converter = converter ,
171- AsObject = Type != typeToConvert ,
172- BufferRequirement = isRead ? bufferRequirements . Read : bufferRequirements . Write
173- } ;
174-
175127 bool CachedCanConvert ( DataFormat format , out BufferRequirements bufferRequirements )
176128 {
177129 if ( format is DataFormat . Binary )
@@ -193,6 +145,44 @@ bool CachedCanConvert(DataFormat format, out BufferRequirements bufferRequiremen
193145 return success ? bufferRequirements : null ;
194146 }
195147
148+ // TryBind for reading.
149+ internal bool TryBind ( Field field , DataFormat format , out PgConverterInfo info )
150+ {
151+ switch ( this )
152+ {
153+ case { IsResolverInfo : false } :
154+ if ( ! CachedCanConvert ( format , out var bufferRequirements ) )
155+ {
156+ info = default ;
157+ return false ;
158+ }
159+ info = new ( this , Converter , bufferRequirements . Read ) ;
160+ return true ;
161+ case PgResolverTypeInfo resolverInfo :
162+ var resolution = resolverInfo . GetResolution ( field ) ;
163+ if ( ! HasCachedInfo ( resolution . Converter )
164+ ? ! CachedCanConvert ( format , out bufferRequirements )
165+ : ! resolution . Converter . CanConvert ( format , out bufferRequirements ) )
166+ {
167+ info = default ;
168+ return false ;
169+ }
170+ info = new ( this , resolution . Converter , bufferRequirements . Read ) ;
171+ return true ;
172+ default :
173+ throw new NotSupportedException ( "Should not happen, please file a bug." ) ;
174+ }
175+ }
176+
177+ // Bind for reading.
178+ internal PgConverterInfo Bind ( Field field , DataFormat format )
179+ {
180+ if ( ! TryBind ( field , format , out var info ) )
181+ ThrowHelper . ThrowInvalidOperationException ( $ "Resolved converter does not support { format } format.") ;
182+
183+ return info ;
184+ }
185+
196186 // Bind for writing.
197187 /// When result is null, the value was interpreted to be a SQL NULL.
198188 internal PgConverterInfo ? Bind < T > ( PgConverter < T > converter , T ? value , out Size size , out object ? writeState , out DataFormat format , DataFormat ? formatPreference = null )
@@ -215,13 +205,7 @@ bool CachedCanConvert(DataFormat format, out BufferRequirements bufferRequiremen
215205 if ( size is { Kind : SizeKind . Unknown } )
216206 ThrowHelper . ThrowNotSupportedException ( $ "Returning { nameof ( Size . Unknown ) } from { nameof ( PgConverter < object > . GetSize ) } is not supported yet.") ;
217207
218- return new ( )
219- {
220- TypeInfo = this ,
221- Converter = converter ,
222- AsObject = IsBoxing ,
223- BufferRequirement = bufferRequirements . Write ,
224- } ;
208+ return new ( this , converter , bufferRequirements . Write ) ;
225209 }
226210
227211 // Bind for writing.
@@ -249,13 +233,7 @@ bool CachedCanConvert(DataFormat format, out BufferRequirements bufferRequiremen
249233 if ( size is { Kind : SizeKind . Unknown } )
250234 ThrowHelper . ThrowNotSupportedException ( $ "Returning { nameof ( Size . Unknown ) } from { nameof ( PgConverter . GetSizeAsObject ) } is not supported yet.") ;
251235
252- return new ( )
253- {
254- TypeInfo = this ,
255- Converter = converter ,
256- AsObject = Type != typeof ( object ) ,
257- BufferRequirement = bufferRequirements . Write ,
258- } ;
236+ return new ( this , converter , bufferRequirements . Write ) ;
259237 }
260238
261239 // If we don't have a converter stored we must ask the retrieved one.
@@ -337,6 +315,13 @@ public PgConverterResolution(PgConverter converter, PgTypeId pgTypeId)
337315
338316readonly struct PgConverterInfo
339317{
318+ public PgConverterInfo ( PgTypeInfo pgTypeInfo , PgConverter converter , Size bufferRequirement )
319+ {
320+ TypeInfo = pgTypeInfo ;
321+ Converter = converter ;
322+ BufferRequirement = bufferRequirement ;
323+ }
324+
340325 public bool IsDefault => TypeInfo is null ;
341326
342327 public Type TypeToConvert
@@ -352,11 +337,12 @@ public Type TypeToConvert
352337 }
353338 }
354339
355- public required PgTypeInfo TypeInfo { get ; init ; }
356- public required PgConverter Converter { get ; init ; }
357- public required Size BufferRequirement { get ; init ; }
358- // Whether Converter.TypeToConvert matches the PgTypeInfo.Type, if it doesn't object apis and a downcast should be used.
359- public required bool AsObject { get ; init ; }
340+ public PgTypeInfo TypeInfo { get ; }
341+ public PgConverter Converter { get ; }
342+ public Size BufferRequirement { get ; }
343+
344+ /// Whether Converter.TypeToConvert matches PgTypeInfo.Type, if it doesn't object apis should be used.
345+ public bool IsBoxingConverter => TypeInfo . IsBoxing ;
360346
361347 public PgConverter < T > GetConverter < T > ( ) => ( PgConverter < T > ) Converter ;
362348}
0 commit comments