paulmach / orb

Types and utilities for working with 2d geometry in Golang
MIT License
893 stars 104 forks source link

Add NULL support to wkb #21

Closed icco closed 5 years ago

icco commented 5 years ago

The package encoding/wkb seems to have issues if a null point is passed into a scanner.

Example:

package main

import (
    "database/sql"
    "log"

    _ "github.com/lib/pq"
    "github.com/paulmach/orb"
    "github.com/paulmach/orb/encoding/wkb"
)

func main() {
    db, _ := sql.Open("postgres", "postgres://localhost/writing?sslmode=disable")
    if err := db.Ping(); err != nil {
        log.Panic(err.Error())
    }

    row := db.QueryRow("SELECT ST_AsBinary(NULL::geometry)")
    var p orb.Point
    err := row.Scan(wkb.Scanner(&p))
    if err != nil {
        log.Panic(err.Error())
    }
}

Output:

$ go run main.go
2019/02/03 17:12:28 sql: Scan error on column index 0, name "st_asbinary": wkb: scan value must be []byte
panic: sql: Scan error on column index 0, name "st_asbinary": wkb: scan value must be []byte

goroutine 1 [running]:
log.Panic(0xc0000e9f58, 0x1, 0x1)
    /usr/local/Cellar/go/1.11.5/libexec/src/log/log.go:326 +0xc0
main.main()
    /Users/nat/tmp/example/main.go:22 +0x1f2
exit status 2

This is talking to a postgres 9.6 server.

icco commented 5 years ago

The use case for this is I have a data structure which has optional lat/long points attached to it.

paulmach commented 5 years ago

fix is here https://github.com/paulmach/orb/pull/22 let me know what you think.

paulmach commented 5 years ago

https://github.com/paulmach/orb/pull/22 has been merged and I tagged v0.1.2