Skip to content

Instantly share code, notes, and snippets.

@mlaflamm
Last active September 12, 2023 20:22
Show Gist options
  • Save mlaflamm/5093c5877b401f608a1a737fab7b662c to your computer and use it in GitHub Desktop.
Save mlaflamm/5093c5877b401f608a1a737fab7b662c to your computer and use it in GitHub Desktop.
Custom types wrapper for go-jet generator

We love go-jet! We are now using it or migrating it to, in all our projects. This is simply the best go SQL builder we have tried so far. It is type safe thanks to the code generation and the query result mapping is very powerful. As far as we know, this is the only solution that fully supports mapping of LEFT OUTER JOIN out of the box.

We have used straight sql package, sqlx, squirrel with and without sqlx, and more recently sqlboiler. The latter is an ORM rather than an SQL builder but we love the type safety with code generation. Also their null types package is a breeze to work with. But over time, sqlboiler being an ORM, showed its limitations in terms of performance and flexibility. Yes this is possible to revert to raw SQL but then we lose some of the type safety.

When we discovered go-jet, we were very excited. A type safe SQL builder solution with code generation is exactly what we were looking for! Our only gripe was the usage of pointers for nullable values instead of the convenient null types. You can use different types if you provide you own custom models but we really wants to use the generated models.

After tinkering with multiple ways to generate go-jet models with null types, we ended up wrapping the jet code generator with a bash script that automatically converts the types of the go-jet models to our beloved null types.

Here is the bash wrapper in its full glory.

#!/bin/bash
# 'rm' and 'mv' are for flattening the deep directory structure
rm -rf jetdb model table
jet -source=PostgreSQL -host=localhost -port=5432 -user=jetuser -password=jetpass -dbname=jetdb -schema=dvds -path=.
mv ./jetdb/dvds/* .
rm -rf jetdb
# this is how we exclude tables
rm */schema_migrations.go
# this is the main replacement 'logic'
if [ "$(uname)" == "Darwin" ]; then
find ./model -type f -name '*.go' | xargs sed -i '' -e 's|int32|int|g'
find ./model -type f -name '*.go' | xargs sed -i '' -e 's|*int|null.Int|g'
find ./model -type f -name '*.go' | xargs sed -i '' -e 's|*float64|null.String|g'
find ./model -type f -name '*.go' | xargs sed -i '' -e 's|float64|string|g'
find ./model -type f -name '*.go' | xargs sed -i '' -e 's|*string|null.String|g'
find ./model -type f -name '*.go' | xargs sed -i '' -e 's|*time.Time|null.Time|g'
find ./model -type f -name '*.go' | xargs sed -i '' -e 's|package model|package model\'$'\nimport "github.com/volatiletech/null/v8"|g'
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
find ./model -type f -name '*.go' | xargs sed -i 's|int32|int|g'
find ./model -type f -name '*.go' | xargs sed -i 's|*int|null.Int|g'
find ./model -type f -name '*.go' | xargs sed -i 's|*float64|null.String|g'
find ./model -type f -name '*.go' | xargs sed -i 's|float64|string|g'
find ./model -type f -name '*.go' | xargs sed -i 's|*string|null.String|g'
find ./model -type f -name '*.go' | xargs sed -i 's|*time.Time|null.Time|g'
find ./model -type f -name '*.go' | xargs sed -i 's|package model|package model\n\nimport "github.com/volatiletech/null/v8"|g'
else
echo "platform $OSTYPE not supported"
exit 1
fi
# this is to remove unused import packages
find ./model -type f -name '*.go' | xargs goimports -w
@mlaflamm
Copy link
Author

Note that we are not customizing the generated code using sed anymore. We are using the built-in generator customization feature introduced in v.2.6.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment