sqlc icon indicating copy to clipboard operation
sqlc copied to clipboard

config overrides - support additional imports for go generics

Open krhubert opened this issue 3 years ago • 6 comments

What do you want to change?

Hi,

it would be great to allow add more imports when overriding db types. Let's say we have a Null package like below.

package null

type Null[T any] struct {
  V     T
  Valid bool
}

Now I want to set a custom nullable ts type in config as follows:

  - db_type: "pg_catalog.timestamp"
    nullable: true
    go_type:
      import: "github.com/myproject/null"
      type: "Null[time.Time]"

But this won't import "time" pkg.

Solution

Allow to specify imports instead of import. With this change config will look like:

  - db_type: "pg_catalog.timestamp"
    nullable: true
    go_type:
      imports:
      - "time" 
      - "github.com/myproject/null"
      type: "null.Null[time.Time]"

What database engines need to be changed?

N/A

What programming language backends need to be changed?

Go

krhubert avatar Jun 06 '22 15:06 krhubert

I think this is a great idea. When the 1.18 beta was released, I asked for ideas on how sqlc should use them. Allowing people to experiment with nullable wrappers seems like a good first step.

I'm not sold on the config syntax, but I think it's a good starting point.

kyleconroy avatar Jun 12 '22 18:06 kyleconroy

  - db_type: "pg_catalog.timestamp"
    nullable: true
    go_type:
      import: "time"\n"github.com/myproject/null"
      type: "null.Null[time.Time]"

There's a great workaround, that worked for me. I only hope it will work in the next versions.

krhubert avatar Jul 28 '22 10:07 krhubert

@krhubert Wow, that's one... inventive workaround. It's like SQL injection but for Go code generation.

I think imports vs import works, but need to make sure the code generated constructs the generic type correctly. It sounds like that may already be the case.

kyleconroy avatar Aug 29 '22 02:08 kyleconroy

Having an imports keys would be awesome. I mentioned a possible API here.

go_type:
  imports: 
    - package: "utils"
      alias: "utils1"
    - package: "data"
      alias: "data1"
  type: "utils1.JSONB[data2.Type]"

sourcec0de avatar Sep 25 '22 18:09 sourcec0de