samber / mo

🦄 Monads and popular FP abstractions, powered by Go 1.18+ Generics (Option, Result, Either...)
https://pkg.go.dev/github.com/samber/mo
MIT License
2.61k stars 85 forks source link

Failed scanning row for mysql with Option #24

Closed yflau closed 2 months ago

yflau commented 1 year ago

For example

CREATE TABLEt(optionalVARCHAR(64));

rows, err := db.Query("SELECT optional FROM t  LIMIT 1?")
for rows.Next() {
  var o mo.Option[string]
  if err := rows.Scan(&o); err != nil {
      t.Errorf("Failed scanning row: %s", err.Error())
      continue
  }
}

then will got Failed scanning row: sql: Scan error on column index 1, name "optional": failed to scan Option[T], and the reason is that type of arg src of Scan is []uint8, and failed here when assert type is string, refer to sql.NullString, we can use the function sql.convertAssign to replace the driver.DefaultParameterConverter.ConvertValue to resolve it, but the convertAssign is private, and maybe won't be public(refer to https://github.com/golang/go/issues/24258), so we can copy out the convertAssign and make some modification or write a new convert function? what do you think? Or have some other solution?

samber commented 1 year ago

Can you test with v1.9.0 please ?

gsiffert commented 8 months ago

@samber I just tried with both the v1.9.0 and the v1.11.0, both of them have the same issue.

graineri commented 5 months ago

Similar issue here as well. We are about to fork this repository and make use of the sql.convertAssign function.

samber commented 2 months ago

-> v1.13.0