sqlc
                                
                                 sqlc copied to clipboard
                                
                                    sqlc copied to clipboard
                            
                            
                            
                        config overrides - support additional imports for go generics
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
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.
  - 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 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.
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]"