jackc / jackc/pgx

Not working when using a pointer to a custom type

Open
#1,229 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Go
Stars
14.3k
Forks
1.1k
Avg merge
6d 9h
Merged PRs (30d)
11

Description

I have a custom ISBN type which is defined `type ISBN [13]byte`

I am testing that I can use the Scan method with it, both as a value type and a pointer type. The latter gives me the error `can't scan into dest[1]: unable to assign to *[13]uint8`

I here is a snippit of my test code

```
type testcase struct {
ID int
Isbn *ISBN // nullable
Title *[13]byte // nullable
}

assert.NilError(t, db.QueryRow(context.Background(), `SELECT id, isbn, title FROM foo WHERE id = 1`).Scan(&tc.ID, &tc.Isbn, &tc.Title))
```

For some reason the Title one works however I can't get my custom type to work.

Here is what my `Scan` method looks like:

```
func (isbn *ISBN) Scan(v any) (err error) {
switch u := v.(type) {
case []byte: // sqlite3
*isbn, err = ParseBytes(u)
case string:
*isbn, err = Parse(u)
default:
return invalidTypeError{reflect.TypeOf(v)}
}
return err
}
```

It doesn't even seem to get called so I am unsure what else I need in order to get it to work

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No repository file or test is named. Start by reproducing the Scan call with the custom ISBN and pointer destinations, then trace the relevant pgx scanning path to determine why ISBN.Scan is not reached; done means the pointer case has a verified, documented outcome or a regression test covering the expected behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, postgresql
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.