Skip to content

Nesting Struct Support for Supported Types #957

Open

Description

@nf-brentsaner

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)?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions