-
Notifications
You must be signed in to change notification settings - Fork 40
proposal: Implement Scanner & Valuer interfaces #216
Comments
UnmarshalText and UnmarshalBinary isn't enough? |
Unfortunately not, unless there is another way I should be doing this? package main
import (
"database/sql"
"fmt"
_ "github.com/jackc/pgx/v4/stdlib"
"inet.af/netaddr"
)
func main() {
db, err := sql.Open("pgx", "host=localhost user=postgres")
if err != nil {
panic("error connecting to database")
}
var inet netaddr.IPPrefix
if err := db.QueryRow("SELECT '192.0.2.14/26'::inet").Scan(&inet); err != nil {
panic(fmt.Sprintf("error scanning value: %v", err))
}
fmt.Println("Got value", inet.String())
} results in
|
Given that |
I'd imagine an implementation of |
I don't think it's reasonable for the types for every package to implement magic methods for the interfaces from most other packages that exist. That would lead to untenable code bloat. In contrast, I believe that packages should aim to understand a small set of common magic methods. |
create a type alias for IP/IPPrefix and implement the Valuer interface by creating a method that returns an interface{}, error where interface{} may be of e.g. type string. This can be achieved by calling MarshalText inside of the Value() method, I guess. You may also use MarshalBinary and return a byte slice as that seems to be also allowed to be returned by the Valuer interface. |
Agree with that, however with
👍 Exactly what I've done, it'd be a bit nicer if this library compatible with |
I'm using PostgreSQL to store IP address information and would like to be able to use these types with
database/sql
.Would you be open to implementing the
Scanner
andValuer
interfaces forIP
andIPPrefix
types? Alternatively, would you be open to PR which did this?The text was updated successfully, but these errors were encountered: