pgx icon indicating copy to clipboard operation
pgx copied to clipboard

Scan not only anonymous structs

Open defany opened this issue 1 year ago • 4 comments

Why in pgx we don't scan non anonymous structs? For ex:

type Currency struct {
	Code         string `json:"code"`
	IsHidden     bool   `json:"is_hidden"`
	ImageURL     string `json:"image_url"`
	FriendlyName string `json:"friendly_name"`
}

type DetailedBalance struct {
	UserID   int64    `json:"user_id,omitempty"`
	Currency Currency `json:"currency"`
	Balance  float64  `json:"balance"`
}

But it words with:

type DetailedBalance struct {
	UserID   int64    `json:"user_id,omitempty"`
	Currency struct {
	  Code         string `json:"code"`
	  IsHidden     bool   `json:"is_hidden"`
	  ImageURL     string `json:"image_url"`
	  FriendlyName string `json:"friendly_name"`
        }
	Balance  float64  `json:"balance"`
}

Is there any specific issues with this?

defany avatar Jun 03 '24 20:06 defany

I believe that should work. But you would need a runnable example for anyone else to see exactly what it happening.

jackc avatar Jun 03 '24 22:06 jackc

I will give tomorrow runnable example, forgot about it, ty for so fast response

defany avatar Jun 03 '24 23:06 defany

@jackc

There are some code: https://go.dev/play/p/x-s1Iwg5ro2

IDK, but right now no one of variants are not working, maybe Im doing something wrong (its ~12pm right now)

But I was testing and anonymous structs were working and first variant was not working

Thanks for your attention

defany avatar Jun 04 '24 19:06 defany

You also need to register any composite types. See TestCompositeCodecTranscodeStruct in the pgx source for an example.

jackc avatar Jun 04 '24 20:06 jackc