Skip to main content

antvet

antvet finds ant mistakes that compile but fail when the query runs, such as a condition on the wrong model.

Run it​

Install it, then pass it to go vet:

go install github.com/skudra-go/ant/tools/antvet/cmd/antvet@latest
go vet -vettool=$(which antvet) ./...
main.go:31:13: ant.Gt needs a field of a probe from ant.Fields, not of an ordinary variable
main.go:32:24: condition on User in a query of Post
main.go:33:19: OrderBy on a field of User in a query of Post
main.go:34:21: Find key is string, and User has a key of type int64

Add the vet line to CI to fail the build on a report.

What it reports​

CheckExample
A field pointer that does not come from a probeant.Gt(&age, 18)
A field pointer from another model's probedb.Posts.Where(ant.Gt(&Users.Age, 18))
A Find key of the wrong typedb.Users.Find(ctx, "1")

The second check covers a query's conditions, ordering, grouping, columns, includes and updates, and aggregates such as ant.SumOf. The key in the third check comes from the model's Configure when it sets one with Key or GenerateKey, so a model with a Code string key is judged against string, not against the field named ID.

Every field pointer passed to an ant function or builder method must come from a probe, including ant.Into, ant.EqCol, the relation functions and ant.Original. Functions that take an entity, such as ant.Add, accept any pointer.

Subqueries may name the outer model​

A subquery can refer to a column of the outer query, so a condition on another model is not always a mistake. antvet judges a query only where it runs, as in .All(ctx) or ant.SumOf(ctx, q, ...), or inside the query it belongs to. Where it cannot tell, it reports nothing.

Silence a report​

Mark code that is wrong on purpose, such as a test of an ant error, with //antvet:ignore and a reason, at the end of the line or on the line above:

_, err := db.Users.Find(ctx, "1") //antvet:ignore the test checks ErrKeyType
//antvet:ignore the test checks ErrKeyType
_, err := db.Users.Find(ctx, "1")

An end-of-line comment covers that line only.

What it does not check​

It does not look inside strings passed to ant.Raw, ant.SQL and the like. A model that has a type parameter is checked where it is instantiated. A field reached through a pointer held in a struct field, as in &holder.U.Age, is not judged.