pgx icon indicating copy to clipboard operation
pgx copied to clipboard

Migrate from ERB to Go Templates

Open alexandear opened this issue 1 year ago • 1 comments

I propose to get rid of ERB in favor of native Go templates. Go has powerful generation tools, and it's better to use them.

Motivation

In a Go project, it is generally better to use Go tools for template generation to maintain consistency and leverage the native capabilities of the language. Using Go templates instead of ERB (Embedded Ruby) will:

  • Improve maintainability by using a single language for both code and templates.
  • Enhance readability for Go developers who may not be familiar with Ruby.
  • Simplify the build process by reducing dependencies on external tools.

Tasks

  1. Identify all ERB templates currently used in the project.
  2. Convert each ERB template to an equivalent Go template.
  3. Use Makefile (Taskfile, simple script) instead of Rakefile.

Example of converting one file

int_test.go.erb to int_test.go.tmpl

Current ERB Template

<% [2, 4, 8].each do |pg_byte_size| %>
<% pg_bit_size = pg_byte_size * 8 %>
func TestInt<%= pg_byte_size %>Codec(t *testing.T) {
    // ...
}
<% end %>

Equivalent Go Template:

{{ range $pg_byte_size := .ByteSizes }}
{{ $pg_bit_size := mul $pg_byte_size 8 }}
func TestInt{{ $pg_byte_size }}Codec(t *testing.T) {
    // ...
}
{{ end }}

Links

alexandear avatar Dec 18 '24 00:12 alexandear

While in theory I would prefer to use Go rather than another language and avoid the Ruby dependency, for templates I am not convinced. The erb templates are self-contained. But text/template would need some sort of setup to register the mul function and to pass in .ByteSizes.

Also, in my option, text/template syntax is rather awkward. e.g. pg_byte_size * 8 vs. mul $pg_byte_size 8.

jackc avatar Dec 21 '24 14:12 jackc