Skip to content

Commit

Permalink
support sql nullable types
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancoLiberali committed Aug 7, 2023
1 parent 921c8c5 commit f13b79b
Show file tree
Hide file tree
Showing 28 changed files with 220 additions and 82 deletions.
29 changes: 17 additions & 12 deletions cmd/gen/conditions/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,25 @@ func (condition *Condition) generateForNamedType(objectType Type, field Field) {
_, err := field.Type.BadaasModelStruct()

if err == nil {
// field is a badaas model
condition.generateForBadaasModel(objectType, field)
} else if field.Type.IsSQLNullableType() {
// field is a sql nullable type (sql.NullBool, sql.NullInt, etc.)
condition.param.SQLToBasicType(field.Type)
condition.generateWhere(
objectType,
field,
)
} else if field.Type.IsGormCustomType() || field.TypeString() == "time.Time" {
// field is a Gorm Custom type (implements Scanner and Valuer interfaces)
// or a named type supported by gorm (time.Time)
condition.param.ToCustomType(condition.destPkg, field.Type)
condition.generateWhere(
objectType,
field,
)
} else {
// field is not a Badaas Model
if field.Type.IsGormCustomType() || field.TypeString() == "time.Time" {
// field is a Gorm Custom type (implements Scanner and Valuer interfaces)
// or a named type supported by gorm (time.Time, gorm.DeletedAt)
condition.param.ToCustomType(condition.destPkg, field.Type)
condition.generateWhere(
objectType,
field,
)
} else {
log.Logger.Debugf("struct field type not handled: %s", field.TypeString())
}
log.Logger.Debugf("struct field type not handled: %s", field.TypeString())
}
}

Expand Down
24 changes: 24 additions & 0 deletions cmd/gen/conditions/jenParam.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,27 @@ func (param JenParam) ToCustomType(destPkg string, typeV Type) {
typeV.Name(),
)
}

func (param JenParam) SQLToBasicType(typeV Type) {
switch typeV.String() {
case nullString:
param.internalType.String()
case nullInt64:
param.internalType.Int64()
case nullInt32:
param.internalType.Int32()
case nullInt16:
param.internalType.Int16()
case nullByte:
param.internalType.Int8()
case nullFloat64:
param.internalType.Float64()
case nullBool:
param.internalType.Bool()
case nullTime, deletedAt:
param.internalType.Qual(
"time",
"Time",
)
}
}
6 changes: 6 additions & 0 deletions cmd/gen/conditions/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ func TestCustomType(t *testing.T) {
})
}

func TestNullableTypes(t *testing.T) {
doTest(t, "./tests/nullabletypes", []Comparison{
{Have: "nullable_types_conditions.go", Expected: "./tests/results/nullabletypes.go"},
})
}

func TestBelongsTo(t *testing.T) {
doTest(t, "./tests/belongsto", []Comparison{
{Have: "owner_conditions.go", Expected: "./tests/results/belongsto_owner.go"},
Expand Down
20 changes: 20 additions & 0 deletions cmd/gen/conditions/tests/nullabletypes/nullabletypes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package nullabletypes

import (
"database/sql"

"github.com/ditrit/badaas/orm"
)

type NullableTypes struct {
orm.UUIDModel

String sql.NullString
Int64 sql.NullInt64
Int32 sql.NullInt32
Int16 sql.NullInt16
Byte sql.NullByte
Float64 sql.NullFloat64
Bool sql.NullBool
Time sql.NullTime
}
5 changes: 2 additions & 3 deletions cmd/gen/conditions/tests/results/basicpointers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions cmd/gen/conditions/tests/results/basicslices.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions cmd/gen/conditions/tests/results/basicslicespointer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions cmd/gen/conditions/tests/results/basictypes.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions cmd/gen/conditions/tests/results/belongsto_owned.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions cmd/gen/conditions/tests/results/belongsto_owner.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions cmd/gen/conditions/tests/results/columndefinition.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions cmd/gen/conditions/tests/results/customtype.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions cmd/gen/conditions/tests/results/goembedded.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions cmd/gen/conditions/tests/results/gormembedded.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions cmd/gen/conditions/tests/results/hasmany_company.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions cmd/gen/conditions/tests/results/hasmany_seller.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions cmd/gen/conditions/tests/results/hasone_city.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions cmd/gen/conditions/tests/results/hasone_country.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions cmd/gen/conditions/tests/results/multiplepackage_package1.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f13b79b

Please sign in to comment.