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

incorrect way of loading mac address. #233

Open
d0zer11st opened this issue Jun 5, 2024 · 2 comments
Open

incorrect way of loading mac address. #233

d0zer11st opened this issue Jun 5, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@d0zer11st
Copy link

Getting mac address not the same I stored before.

I think, the issue is in the Stringer.Scan() func, for macaddr the underlying type is net.HardwareAddr, simple type conversion is not the way to use it.

bob/types/marshal.go

Lines 76 to 89 in 5263402

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

how to reproduce:

  1. bob version: github.com/stephenafamo/bob v0.27.1
  2. postgres 16.2
  3. Table
create table example
(
    id bigint primary key generated always as identity,
    mac           macaddr not null,
    hostname text not null
)
  1. Code example.
	macToTest := "1b:1b:63:84:45:e6"
	hwAddr, parseMacErr := net.ParseMAC(macToTest)
	if parseMacErr != nil {
		log.Fatal(parseMacErr)
	}
	log.Printf("parsed mac: %s", hwAddr)
	hostNameString := uuid.New().String()
	exampleSetter := &models.ExampleSetter{
		Mac:      omit.From(types.Stringer[net.HardwareAddr]{Val: hwAddr}),
		Hostname: omit.From(hostNameString),
	}

	_, err := models.Examples.InsertMany(ctx, bobDB, exampleSetter)
	if err != nil {
		log.Fatal(err)
	}

	selectQueryWhere := psql.Select(
		sm.From(models.TableNames.Examples),
		sm.Where(
			models.ExampleColumns.Hostname.EQ(
				psql.Arg(hostNameString),
			),
		),
	)

	all, err := bob.All(ctx, bobDB, selectQueryWhere, scan.StructMapper[*models.Example]())
	if err != nil {
		log.Fatal(err)
	}

	hwAddrStrConv := net.HardwareAddr(macToTest)

	fmt.Printf("strconv: %s\n", hwAddrStrConv.String())  // the same way it implemented in Stringer.Scan()
	fmt.Printf("     db: %s\n", all[0].Mac.Val.String()) // loaded value
	fmt.Printf(" parsed: %s\n", hwAddr.String())         // parsed

	if !strings.EqualFold(all[0].Mac.Val.String(), hwAddr.String()) {
		log.Fatalf("%s is not the same as %s", all[0].Mac.Val.String(), hwAddr.String())
	}

Print results:

strconv: 31:62:3a:31:62:3a:36:33:3a:38:34:3a:34:35:3a:65:36
     db: 31:62:3a:31:62:3a:36:33:3a:38:34:3a:34:35:3a:65:36
 parsed: 1b:1b:63:84:45:e6

I also tried to insert Mac with just type conversion like this:

	hostNameString := uuid.New().String()

        macToTest := "1b:1b:63:84:45:e6"
	hwAddr := net.HardwareAddr(macToTest)

	exampleSetter := &models.ExampleSetter{
		Mac:      omit.From(types.Stringer[net.HardwareAddr]{Val: hwAddr}),
		Hostname: omit.From(hostNameString),
	}

	_, err := models.Examples.InsertMany(ctx, bobDB, exampleSetter)
	if err != nil {
		log.Fatal(err)
	}

that leads to error

ERROR: invalid input syntax for type macaddr: "31:62:3a:31:62:3a:36:33:3a:38:34:3a:34:35:3a:65:36" (SQLSTATE 22P02)
@stephenafamo
Copy link
Owner

Yeah, I see the issue.

I should not have assumed that it would be able to work like this, types.Stringer as a whole probably does not work. I would probably have to create a dedicated type for it.

I wish net.HardwareAddr implemented Text or Binary Marshaler.

@stephenafamo stephenafamo added the bug Something isn't working label Jun 5, 2024
@d0zer11st
Copy link
Author

yeah, as I see there is only one usage of Stringer and it is for macaddr.
the dedicated type would work better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants