jschaf / pggen

Generate type-safe Go for any Postgres query. If Postgres can run the query, pggen can generate code for it.
MIT License
282 stars 26 forks source link

Discussion about -go-type #31

Open aight8 opened 3 years ago

aight8 commented 3 years ago

I noticed that the --pg-type maps a pg type to a destination go type. The go type have to implement the ValueTranscoder interface, which are quiet a lot functions. Furthermore, a transcoder type cannot be reused.

Often (including pggen itself) has following mechanics: pg type <-> transcoder type <-> destination go type

Screenshot_20210423_133235 Screenshot_20210423_133308 note: this is a view of the pgx.ConnInfo state with it's default data set + 2 registerd Composit Types note: the light green "yes" is the default one - the dark green "yes" is implemented but not the default

What do you think to optionally define separate pgx type / destination types?

-go-type=pgtype=valuetype=assigntotype
aight8 commented 3 years ago

When I handcraft the destination type I can also set the optional fields manually as pointer. The only requirement is that the transcoder type fullfill one of the things:

jschaf commented 3 years ago

What do you think to optionally define separate pgx type / destination types?

I think this implemented in https://github.com/jschaf/pggen/commit/0056cfbec4a2258e1e0d14a9464613861a06e551. I still need to test the type override logic but it should work.

You can now do:

    q := NewQuerierConfig(conn, QuerierConfig{DataTypes: []pgtype.DataType{
        {
            Value: MyTranscoder,
            Name:  "a_postgres_type",
        },
    }})

Then you can tweak MyTranscoder.assignTo to set fields however you want on structs. The next step is to a support customizing the output type. We don't need it for anything and we can support any type.

aight8 commented 3 years ago

I don't get it. Why this is a runtime option? The problem is when I assign a domain pg type (which is literally only a basic pg type with predefined options and validation) to a go type. Pggen try to initialize the "int" as scan destination, but it's not implement the whole pgx Value interface.