@@ -1004,6 +1004,57 @@ func TestEncodeTypeRename(t *testing.T) {
10041004 })
10051005}
10061006
1007+ // Define custom types that are aliases of basic types but also implement fmt.Stringer
1008+ type StringerInt32 int32
1009+ type StringerFloat64 float64
1010+
1011+ // Implement the String() method for these types
1012+ func (s StringerInt32 ) String () string {
1013+ return fmt .Sprintf ("StringerInt32(%d)" , int32 (s ))
1014+ }
1015+
1016+ func (s StringerFloat64 ) String () string {
1017+ return fmt .Sprintf ("StringerFloat64(%f)" , float64 (s ))
1018+ }
1019+
1020+ // TestStringerTypes tests custom type aliases that implement the fmt.Stringer interface
1021+ func TestStringerTypes (t * testing.T ) {
1022+ t .Parallel ()
1023+
1024+ ctx , cancel := context .WithTimeout (context .Background (), 120 * time .Second )
1025+ defer cancel ()
1026+
1027+ pgxtest .RunWithQueryExecModes (ctx , t , defaultConnTestRunner , nil , func (ctx context.Context , t testing.TB , conn * pgx.Conn ) {
1028+ // Test values
1029+ inInt := StringerInt32 (42 )
1030+ var outInt StringerInt32
1031+
1032+ inFloat := StringerFloat64 (553.36 )
1033+ var outFloat StringerFloat64
1034+
1035+ // Register types with the connection
1036+ conn .TypeMap ().RegisterDefaultPgType (inInt , "int4" )
1037+ conn .TypeMap ().RegisterDefaultPgType (inFloat , "float8" )
1038+
1039+ // Test that the underlying values are properly encoded/decoded,
1040+ // not the String() representation
1041+ err := conn .QueryRow (context .Background (), "select $1::int4, $2::float8" , inInt , inFloat ).
1042+ Scan (& outInt , & outFloat )
1043+ if err != nil {
1044+ t .Fatalf ("Failed with Stringer types: %v" , err )
1045+ }
1046+
1047+ // Check that the values are correctly preserved (not converted to their String() representation)
1048+ if inInt != outInt {
1049+ t .Errorf ("StringerInt32: expected %v, got %v" , inInt , outInt )
1050+ }
1051+
1052+ if inFloat != outFloat {
1053+ t .Errorf ("StringerFloat64: expected %v, got %v" , inFloat , outFloat )
1054+ }
1055+ })
1056+ }
1057+
10071058// func TestRowDecodeBinary(t *testing.T) {
10081059// t.Parallel()
10091060
0 commit comments