Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nesting Struct Support for Supported Types #957

Open
nf-brentsaner opened this issue Dec 17, 2024 · 0 comments
Open

Nesting Struct Support for Supported Types #957

nf-brentsaner opened this issue Dec 17, 2024 · 0 comments

Comments

@nf-brentsaner
Copy link

nf-brentsaner commented Dec 17, 2024

Certain types, many stdlib (e.g. a net/netip.Prefix) implement a:

These interfaces, notably encoding.TextUnmarshaler/encoding.TextMarshaler, should most likely be used where the DB field type supports it.

For example:

package main

import (
    `net/netip`
)

type MyStruct struct {
    ID uint `db:"id"`
    Network netip.Prefix `db:"net"`
}

Preparing a named statement for this passes just fine (if nmStmt, err = db.PrepareNamed("INSERT INTO someTbl (id, net) VALUES (:id, :net);"); err != nil {...).

However, upon actually trying to execute it (if res, err = nmStmt.Exec(&MyStruct{ID: 3, Network: netip.MustParsePrefix("192.168.1.0/24")}); err != nil {...), the execution will fail with an unsupported type netip.Prefix, a struct error.

Even if the target column data type is a SQLite TEXT.

Now, because this is stdlib, I'd have to coerce type it, which leads to a lot of what is probably unnecessary code because this is a stdlib type that indicates that it can both marshal and unmarshal itself to/from both text and bytes.

Does sqlx do any sort of on-the-fly marshaling/unmarshaling like that, or is this likely instead an issue with the driver (in this case, the recommended github.com/mattn/go-sqlite3 per here)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant