Skip to content

fix: escape backticks in generated Go struct tags - #1131

Closed
Kjubikstronk wants to merge 1 commit into
supabase:masterfrom
Kjubikstronk:go-struct-tag-backtick
Closed

fix: escape backticks in generated Go struct tags#1131
Kjubikstronk wants to merge 1 commit into
supabase:masterfrom
Kjubikstronk:go-struct-tag-backtick

Conversation

@Kjubikstronk

Copy link
Copy Markdown

Closes #1125.

The Go generator writes struct tags as a raw string literal, ```go
json:"NAME"

and Go raw literals cannot contain a backtick at all. A column named `bad`tag` therefore closes the literal early and the generated file is not parseable Go, so `gofmt` reports "string literal not terminated".

Go allows a struct tag to be an interpreted literal too, so this switches to that form only when the tag contains a backtick, escaping backslashes and double quotes. Names without a backtick keep the exact raw-literal output they had before, which is what the issue asks for.

There were two identical call sites, one for tables and views and one for composite types. Both now go through the same helper.

Three tests added to the existing `test/server/templates/go.test.ts`, which is a pure unit test file needing no database: a backtick name produces the escaped interpreted literal, a name with a double quote and a backtick escapes both, and an ordinary name still produces the unchanged raw literal. The first two fail without the change and the third passes either way, so the previous output is pinned as well.

`prettier --check` and `tsc -p tsconfig.json --noEmit` are clean.

@Kjubikstronk
Kjubikstronk requested review from a team, avallete and soedirgo as code owners August 31, 2026 08:07
@spydon

spydon commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

Thank you for the contribution! postgres-meta's type generation is moving to the shared @supabase/postgrest-typegen package in supabase/sdk (see #1084), so open template fixes are being re-landed there. The struct tag escaping issue this PR addresses is fixed in supabase/sdk#122, which also escapes quotes, backslashes and control characters inside the tag value so reflect.StructTag.Get round-trips the exact column name.

@Kjubikstronk

Copy link
Copy Markdown
Author

Makes sense, and sdk#122 is the better fix, since escaping quotes, backslashes and control characters means reflect.StructTag.Get round-trips anything rather than just surviving a backtick. Closing this one. I had not spotted #1084, so thanks for the pointer.

spydon added a commit to supabase/sdk that referenced this pull request Sep 1, 2026
… names

Go struct tags were interpolated into raw string literals verbatim, so a
column name containing a backtick terminated the literal early and the
generated source failed to parse. Names containing double quotes or
backslashes compiled but produced tags that reflect.StructTag could not
round-trip.

Column and composite attribute names are now quoted with JSON.stringify,
whose escape sequences are a subset of Go's, so reflect.StructTag.Get
recovers the exact name. Ordinary names keep their previous raw literal
representation; only names containing a backtick fall back to an
interpreted literal, since Go raw literals cannot contain one.

Validated against the Go toolchain: generated structs for names with
backticks, quotes, backslashes and control characters parse under gofmt
and round-trip through reflect.StructTag.Get.

Ported from supabase/postgres-meta#1127 and supabase/postgres-meta#1131.
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

Successfully merging this pull request may close these issues.

Go type generation emits invalid struct tags for backtick column names

2 participants