sqlc-dev / sqlc

Generate type-safe code from SQL
https://sqlc.dev
MIT License
12.32k stars 779 forks source link

Add arbitrary fields to generated Go structs #2958

Open mylanconnolly opened 10 months ago

mylanconnolly commented 10 months ago

What do you want to change?

I wanted to try out Service Weaver with sqlc since sqlc is such a joy to use, however I ran into a slight issue. Service Weaver requires you to implement the encoding.BinaryMarshaler and encoding.BinaryUnmarshaler interfaces. There is a shortcut where you could embed weaver.AutoMarshal into the struct. I was thinking that it might be a nice enhancement to make this use-case a bit easier to handle so I won't have to write a bunch of functions for generated structs.

Basically, I was envisioning something like:

gen:
  go:
    emit_embeds:
      - weaver.AutoMarshal

Alternatively, is this something that could be accomplished with plugins?

Thanks so much!

What database engines need to be changed?

No response

What programming language backends need to be changed?

Go

andrewmbenton commented 10 months ago

Interesting idea. We'd re-use the override go_type parsing logic I think, and we'd need to add an import to the output.

I think the plugin idea is a good starting point though. We just released sqlc-gen-go which is the sqlc built-in Go codegen package extracted for use as an external plugin. It should be relatively straightforward for you to fork and modify that plugin to see if this approach works. If it's not straightforward then let us know so that we can improve our documentation.