stephenafamo / bob

SQL query builder and ORM/Factory generator for Go with support for PostgreSQL, MySQL and SQLite
https://bob.stephenafamo.com
MIT License
760 stars 39 forks source link

mac address insert error: SQLSTATE 22021 #224

Closed d0zer11st closed 3 months ago

d0zer11st commented 4 months ago

postgres returns: ERROR: invalid byte sequence for encoding "UTF8": (SQLSTATE 22021) On trying to insert record containing mac address.

how to reproduce:

  1. bob version: github.com/stephenafamo/bob v0.26.1
  2. postgres 16.2
  3. Table
    create table example
    (
    id bigint primary key generated always as identity,
    mac           macaddr not null
    )
  4. Trying to insert

    db, openErr := sql.Open("pgx", connectionString)
    if openErr != nil {
        log.Fatal(openErr)
    }
    defer db.Close()
    
    bobDB := bob.NewDB(db)
    
    macToTest := "1b:1b:63:84:45:e6"
    hwAddr, parseMacErr := net.ParseMAC(macToTest)
    if parseMacErr != nil {
        log.Fatal(parseMacErr)
    }
    log.Printf("parsed mac: %s", hwAddr)
    omitMacValue := omit.From(
        types.Stringer[net.HardwareAddr]{
            Val: hwAddr,
        },
    )
    exampleSetter := &models.ExampleSetter{
        Mac: omitMacValue,
    }
    _, err := models.Examples.InsertMany(ctx, bobDB, exampleSetter)
    if err != nil {
        log.Fatal(err)
    }

Generated models.ExampleSetter

// ExampleSetter is used for insert/upsert/update operations
// All values are optional, and do not have to be set
// Generated columns are not included
type ExampleSetter struct {
    Mac omit.Val[types.Stringer[net.HardwareAddr]] `db:"mac"`
}

My assumption is something wrong with types.Stringer returns []byte https://github.com/stephenafamo/bob/blob/c4cf5c19989a7fe2a2ee20883eeb4e5deb1c69de/types/marshal.go#L72-L74

As well as it loads values. https://github.com/stephenafamo/bob/blob/c4cf5c19989a7fe2a2ee20883eeb4e5deb1c69de/types/marshal.go#L76-L89

stephenafamo commented 4 months ago

I'll take a look