stephenafamo / bob

SQL query builder and ORM/Factory generator for Go with support for PostgreSQL, MySQL and SQLite

Home Page:https://bob.stephenafamo.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

mac address insert error: SQLSTATE 22021

d0zer11st opened this issue · comments

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
)
  1. 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

bob/types/marshal.go

Lines 72 to 74 in c4cf5c1

func (s Stringer[T]) Value() (driver.Value, error) {
return []byte(s.Val), nil
}

As well as it loads values.

bob/types/marshal.go

Lines 76 to 89 in c4cf5c1

func (s *Stringer[T]) Scan(value any) error {
switch x := value.(type) {
case string:
s.Val = T(x)
return nil
case []byte:
s.Val = T(x)
return nil
case nil:
return nil
default:
return fmt.Errorf("cannot scan type %T: %v", value, value)
}
}

I'll take a look