crawshaw / sqlite

Go SQLite3 driver
ISC License
571 stars 68 forks source link

Blob struct should not have io interfaces as fields #147

Open infogulch opened 4 months ago

infogulch commented 4 months ago

https://github.com/crawshaw/sqlite/blob/6c1d4ad4ed9d0acc0ca4b99dd67c5c14d488633c/blob.go#L81-L85

This code defines a struct with fields named ReadWriteSeeker, ReaderAt, WriterAt, and Closer which are never used. Notably, Blob itself implements the functions required by these interfaces. If Blob was an interface this syntax would mean that whatever implements Blob must have the methods in the listed interfaces, but written as a struct this syntax defines four separate fields in the struct itself. This appears to be a mistake.

Here's a playground example that shows this: https://go.dev/play/p/Hr0SlKqYhV9

To require that a struct Blob implements the required interfaces, one can use the "interface guards" pattern like this:

var _ io.ReadWriteSeeker = &Blob{}
var _ io.ReaderAt = &Blob{}
var _ io.WriterAt = &Blob{}
var _ io.Closer = &Blob{}
anacrolix commented 4 months ago

https://github.com/go-llsqlite/crawshaw/commit/236e19baec475346141ad362c2cad59796c5129c