Skip to content

Commit

Permalink
Merge pull request #299 from hayeah/master
Browse files Browse the repository at this point in the history
Fixes #298
  • Loading branch information
mcuadros authored Mar 20, 2019
2 parents 0d84b50 + fda9fd6 commit a7b3dba
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,22 @@ If that is not the case you can set the following environment variables:
- `DBUSER`: database user
- `DBPASS`: database user password

#### Docker PostgreSQL

If you have docker, you may run an instance of postgres in a container:

```
docker run -it --rm --name kallax \
-e POSTGRES_PASSWORD=testing \
-e POSTGRES_USER=testing \
-e POSTGRES_DB=testing \
-v `pwd`/.pgdata:/var/lib/postgresql/data \
-p 127.0.0.1:5432:5432 \
postgres:11
```

Remove `.pgdata` after you are done.

License
-------

Expand Down
15 changes: 14 additions & 1 deletion types/slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,20 @@ type ByteArray []uint8
func (a *ByteArray) Scan(src interface{}) error {
switch src := src.(type) {
case []byte:
*(*[]byte)(a) = src
dst := *(*[]byte)(a)

if a == nil || cap(dst) < len(src) {
// nil, or shorter destination than source. Create a new slice.
dst = make([]byte, len(src))
} else {
// Resize slice, but retain capacity
dst = dst[0:len(src)]
}

// Copy, do not retain reference to reference types.
copy(dst, src)

*(*[]byte)(a) = dst
return nil
case string:
*(*[]byte)(a) = []byte(src)
Expand Down
17 changes: 17 additions & 0 deletions types/slices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,23 @@ func TestSlice_Integration(t *testing.T) {
}
}

func TestByteArray_ScannerNoBufferReuse(t *testing.T) {
require := require.New(t)

var sharedbuf [32]byte

var ba ByteArray

err := ba.Scan(sharedbuf[:])
require.NoError(err)

// Modify the "driver" buffer src
sharedbuf[0] = 1

require.Equal(uint8(0), ba[0], "ByteBuffer should not share reference with scanned src")

}

func envOrDefault(key string, def string) string {
v := os.Getenv(key)
if v == "" {
Expand Down

0 comments on commit a7b3dba

Please sign in to comment.