hn.today

Field pointers as column names: compile-checked Go queries without codegen

skudra.net3 points0 comments
Screenshot of Field pointers as column names: compile-checked Go queries without codegen

The Go ORM ant uses pointers to fields on a model “probe” as column identifiers, aiming to combine readable, chainable queries with compile-time checks and no code generation. For example, a query can filter with `ant.Gt(&Users.Age, 18)` and sort with `OrderBy(&Users.Name)`: misspelled fields and values of the wrong type fail during compilation, as do misspelled relations. Go 1.27’s generic methods make this chainable API possible; earlier Go versions required nested package-level functions or weaker type checks. Unlike schema-driven tools such as ent and sqlc, ant derives its metadata from ordinary Go structs.

At runtime, ant identifies a field pointer by comparing its address with cached probe objects, then matching the field’s offset and type. This takes about 21 nanoseconds with no allocations in the reported test, but relies on Go’s current non-moving garbage collector and uses an unsafe-pointer registry. Compile-time checking is incomplete: pointers from ordinary values, fields from the wrong model, and some column/relation mix-ups fail only at query execution, though antvet catches several cases. Ant also snapshots loaded entities so `SaveChanges` writes changed fields even when set to zero values, reducing accidental overwrites. In benchmarks, tracking made 1,000-row reads about 1.7 times slower and used three times the memory; without tracking, reads were close to plain SQL. Single-row operations were broadly comparable to other libraries, though ant had relatively high allocation counts.

Read on skudra.net0 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Programming

Ggml v1.0.0

Ggml v1.0.0

ggml 1.0.0 removes deprecated public APIs and legacy DIAG_MASK operations, while adding configurable YaRN truncation and FP8 support. It also improves backend performance with quantized matrix multiplication, fused MoE operations, and new attention and scheduling paths. (github.com)

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.