jackc / pgx

PostgreSQL driver and toolkit for Go
MIT License
10.81k stars 841 forks source link

Scan not only anonymous structs #2037

Open defany opened 5 months ago

defany commented 5 months ago

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?

jackc commented 5 months ago

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

defany commented 5 months ago

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

defany commented 5 months ago

@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

jackc commented 5 months ago

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