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

Discussion about -go-type #31

Open
aight8 opened this issue Apr 23, 2021 · 3 comments
Open

Discussion about -go-type #31

aight8 opened this issue Apr 23, 2021 · 3 comments

Comments

@aight8
Copy link

aight8 commented Apr 23, 2021

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
Copy link
Author

aight8 commented Apr 23, 2021

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:

  • destination type is the same than the transcoder type (just assign it - the type must implement the full ValueTranscoder interface)
  • destination type can be used as destination parameter in AssignTo for the used transcoder type
  • ? investigate: pointer destination type? can AssignTo handle values and also pointers...?

@jschaf
Copy link
Owner

jschaf commented Apr 26, 2021

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

I think this implemented in 0056cfb. 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
Copy link
Author

aight8 commented Jun 2, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants