Add the ability to use custom generic types with non std inner types

- fix #442

- Since handling all edge cases for generic type parsing is not trivial, users will specify the fields manually.
- To support more than one import per type, add an additional field for extra imports per type.

- Add examples for defining those types in the current tests that show how to define custom types.
This commit is contained in:
markvai 2025-03-27 17:42:27 +02:00
parent 33c1d9e663
commit 0947de0628
3 changed files with 42 additions and 26 deletions

View file

@ -3,9 +3,6 @@ package postgres
import (
"database/sql"
"fmt"
"path/filepath"
"testing"
"github.com/go-jet/jet/v2/generator/metadata"
"github.com/go-jet/jet/v2/generator/postgres"
"github.com/go-jet/jet/v2/generator/template"
@ -16,6 +13,8 @@ import (
"github.com/go-jet/jet/v2/tests/dbconfig"
file2 "github.com/go-jet/jet/v2/tests/internal/utils/file"
"github.com/stretchr/testify/require"
"path/filepath"
"testing"
)
const tempTestDir = "./.tempTestDir"
@ -432,6 +431,12 @@ func TestGeneratorTemplate_Model_ChangeFieldTypes(t *testing.T) {
defaultTableModelField.Type = template.NewType(sql.NullFloat64{})
case "*time.Time":
defaultTableModelField.Type = template.NewType(sql.NullTime{})
case "time.Time":
defaultTableModelField.Type = template.Type{
ImportPath: "database/sql",
AdditionalImportPaths: []string{"github.com/google/uuid"},
Name: "sql.Null[uuid.UUID]",
}
}
return defaultTableModelField
})
@ -443,10 +448,12 @@ func TestGeneratorTemplate_Model_ChangeFieldTypes(t *testing.T) {
require.Nil(t, err)
data := file2.Exists(t, defaultModelPath, "film.go")
require.Contains(t, data, "\"database/sql\"")
require.Contains(t, data, `"database/sql"`)
require.Contains(t, data, `"github.com/google/uuid"`)
require.Contains(t, data, "Description sql.NullString")
require.Contains(t, data, "ReleaseYear sql.NullInt32")
require.Contains(t, data, "SpecialFeatures sql.NullString")
require.Contains(t, data, "LastUpdate sql.Null[uuid.UUID]")
}
func TestGeneratorTemplate_SQLBuilder_ChangeColumnTypes(t *testing.T) {