Skip to content

fix(postgrest-typegen): escape Go struct tags for pathological column names - #122

Open
spydon wants to merge 2 commits into
mainfrom
fix/typegen-go-fixes
Open

fix(postgrest-typegen): escape Go struct tags for pathological column names#122
spydon wants to merge 2 commits into
mainfrom
fix/typegen-go-fixes

Conversation

@spydon

@spydon spydon commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

Summary

Ports the Go struct tag escaping fix from postgres-meta's open template PRs into this package (the templates are being deleted in favor of this package in supabase/postgres-meta#1084, so open fixes there are triaged and re-landed here).

A backtick in a column name terminated the raw struct tag literal and produced unparseable Go (supabase/postgres-meta#1125). Tag values are now built with JSON.stringify (JSON escape sequences are a strict subset of Go's, so reflect.StructTag.Get recovers the exact name via strconv.Unquote), and tags containing a backtick fall back to an interpreted string literal. Ordinary names keep the exact raw-literal form, so normal output is byte-identical and the parity golden is unchanged.

Triage of origin PRs

postgres-meta PR Verdict Reasoning
#1127 Ported The stronger duplicate: escapes quotes, backslashes and control characters too, matching this package's existing JSON.stringify escaping precedent.
#1131 Skipped Duplicate of #1127; only switches literal forms and leaves quotes unescaped, so a"b produced a tag that reflect.StructTag.Get misparses (its own test pins the broken output).

Known limitation, documented in the code: names that encoding/json itself rejects as tag names (commas, quotes, backticks) still compile and round-trip through reflect.StructTag, but the marshaler falls back to the Go field name at runtime; that is a limitation of the struct tag convention, not the generated source. Output for the pathological cases was validated against real Go (gofmt parse + reflect.StructTag.Get round-trip).

Validation

  • Seven new unit tests (ordinary pinned byte-identical, backtick, quote, backslash, control character, combined, composite attribute).
  • check-types, format-and-lint, knip, build, test (100 pass, including Docker-backed parity) all green.
  • Parity golden unchanged. Note: the nightly parity job against real postgres-meta will only show drift for schemas with pathological column names, which the fixture does not contain.

… 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.
@spydon
spydon requested a review from a team as a code owner August 31, 2026 13:13
@coderabbitai

coderabbitai Bot commented Aug 31, 2026

Copy link
Copy Markdown

Important

  • 🔍 Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: c3cf9694-9203-4640-9000-fbad62d66ab0


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

…g-shaped names

encoding/json does not fall back to the Go field name for every name it
cannot represent: a comma splits the name from tag options, so only the
part before it is used. Reword the formatForGoStructTag docstring to
match the verified runtime behavior and add regression tests for a
column name shaped like a full json tag and for a control character
escape inside the interpreted-literal fallback.
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.

2 participants