sqlcgetters

sqlcgetters command line tool and go module that generates getters for stucts that were generated by sqlc.
Usage: sqlcgetters <path>
sqlcgetters generates getters for structs generated by sqlc.
Arguments:
<path> Path to sqlc output directory.
Flags:
-h, --help Show context-sensitive help.
--version Output the version and exit.
Outputs to stdout. Example usage:
sqlcgetters ./sqlqueries > ./sqlqueries/getters.go
Why do I need this?
sqlc generates structs with exported fields, so why would you want getters? It's all about interfaces. Take these
queries for example:
-- name: BooksByTitleYear :many
SELECT book_id, title, authors.name AS author_name, isbn
FROM books
LEFT JOIN authors ON books.author_id = authors.author_id
WHERE title = $1 AND year = $2;
-- name: BooksByTags :many
SELECT book_id, title, authors.name AS author_name, isbn, tags
FROM books
LEFT JOIN authors ON books.author_id = authors.author_id
WHERE tags && $1::varchar[];
sqlc will generate two queries with these structs as their results:
type BooksByTitleYearRow struct {
BookID int32
Title string
AuthorName sql.NullString
Isbn string
}
type BooksByTagsRow struct {
BookID int32
Title string
AuthorName sql.NullString
Isbn string
Tags []string
}
You have two different structs with the same fields. Writing a function that handles either of them is difficult.
You need to accept interface{} and the type switch to handle the different structs. But if you had a Get*()
function for each field, your function can accept an interface with the fields you need.
Install
go get
go get -u github.com/willabides/sqlcgetters/cmd/sqlcgetters
bindown
Add a bindown dependency:
$ bindown template-source add sqlcgetters https://raw.githubusercontent.com/WillAbides/sqlcgetters/main/bindown.yml
$ bindown dependency add sqlcgetters sqlcgetters#sqlcgetters
Please enter a value for required variable "version": <latest version>