From f13b79b1498abe8e8fc5c06342bd00589724572c Mon Sep 17 00:00:00 2001 From: Franco Liberali Date: Wed, 2 Aug 2023 12:02:54 +0200 Subject: [PATCH] support sql nullable types --- cmd/gen/conditions/condition.go | 29 ++++--- cmd/gen/conditions/jenParam.go | 24 ++++++ cmd/gen/conditions/main_test.go | 6 ++ .../tests/nullabletypes/nullabletypes.go | 20 +++++ .../conditions/tests/results/basicpointers.go | 5 +- .../conditions/tests/results/basicslices.go | 5 +- .../tests/results/basicslicespointer.go | 5 +- .../conditions/tests/results/basictypes.go | 5 +- .../tests/results/belongsto_owned.go | 5 +- .../tests/results/belongsto_owner.go | 5 +- .../tests/results/columndefinition.go | 5 +- .../conditions/tests/results/customtype.go | 5 +- .../conditions/tests/results/goembedded.go | 5 +- .../conditions/tests/results/gormembedded.go | 5 +- .../tests/results/hasmany_company.go | 5 +- .../tests/results/hasmany_seller.go | 5 +- .../conditions/tests/results/hasone_city.go | 5 +- .../tests/results/hasone_country.go | 5 +- .../tests/results/multiplepackage_package1.go | 5 +- .../conditions/tests/results/nullabletypes.go | 81 +++++++++++++++++++ .../tests/results/overrideforeignkey.go | 5 +- .../results/overrideforeignkeyinverse.go | 5 +- .../tests/results/overridereferences.go | 5 +- .../results/overridereferencesinverse.go | 5 +- .../tests/results/selfreferential.go | 5 +- cmd/gen/conditions/tests/results/uintmodel.go | 5 +- cmd/gen/conditions/tests/results/uuidmodel.go | 5 +- cmd/gen/conditions/type.go | 32 +++++++- 28 files changed, 220 insertions(+), 82 deletions(-) create mode 100644 cmd/gen/conditions/tests/nullabletypes/nullabletypes.go create mode 100644 cmd/gen/conditions/tests/results/nullabletypes.go diff --git a/cmd/gen/conditions/condition.go b/cmd/gen/conditions/condition.go index b901411..29b0c9e 100644 --- a/cmd/gen/conditions/condition.go +++ b/cmd/gen/conditions/condition.go @@ -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()) } } diff --git a/cmd/gen/conditions/jenParam.go b/cmd/gen/conditions/jenParam.go index 486a866..4657c00 100644 --- a/cmd/gen/conditions/jenParam.go +++ b/cmd/gen/conditions/jenParam.go @@ -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", + ) + } +} diff --git a/cmd/gen/conditions/main_test.go b/cmd/gen/conditions/main_test.go index b3f9823..c223aba 100644 --- a/cmd/gen/conditions/main_test.go +++ b/cmd/gen/conditions/main_test.go @@ -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"}, diff --git a/cmd/gen/conditions/tests/nullabletypes/nullabletypes.go b/cmd/gen/conditions/tests/nullabletypes/nullabletypes.go new file mode 100644 index 0000000..5c26271 --- /dev/null +++ b/cmd/gen/conditions/tests/nullabletypes/nullabletypes.go @@ -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 +} diff --git a/cmd/gen/conditions/tests/results/basicpointers.go b/cmd/gen/conditions/tests/results/basicpointers.go index 9ae6ee4..fc1dc3d 100644 --- a/cmd/gen/conditions/tests/results/basicpointers.go +++ b/cmd/gen/conditions/tests/results/basicpointers.go @@ -4,7 +4,6 @@ package conditions import ( basicpointers "github.com/ditrit/badaas-cli/cmd/gen/conditions/tests/basicpointers" orm "github.com/ditrit/badaas/orm" - gorm "gorm.io/gorm" "time" ) @@ -26,8 +25,8 @@ func BasicPointersUpdatedAt(operator orm.Operator[time.Time]) orm.WhereCondition Operator: operator, } } -func BasicPointersDeletedAt(operator orm.Operator[gorm.DeletedAt]) orm.WhereCondition[basicpointers.BasicPointers] { - return orm.FieldCondition[basicpointers.BasicPointers, gorm.DeletedAt]{ +func BasicPointersDeletedAt(operator orm.Operator[time.Time]) orm.WhereCondition[basicpointers.BasicPointers] { + return orm.FieldCondition[basicpointers.BasicPointers, time.Time]{ Field: "DeletedAt", Operator: operator, } diff --git a/cmd/gen/conditions/tests/results/basicslices.go b/cmd/gen/conditions/tests/results/basicslices.go index 863d0ec..a6e56c3 100644 --- a/cmd/gen/conditions/tests/results/basicslices.go +++ b/cmd/gen/conditions/tests/results/basicslices.go @@ -4,7 +4,6 @@ package conditions import ( basicslices "github.com/ditrit/badaas-cli/cmd/gen/conditions/tests/basicslices" orm "github.com/ditrit/badaas/orm" - gorm "gorm.io/gorm" "time" ) @@ -26,8 +25,8 @@ func BasicSlicesUpdatedAt(operator orm.Operator[time.Time]) orm.WhereCondition[b Operator: operator, } } -func BasicSlicesDeletedAt(operator orm.Operator[gorm.DeletedAt]) orm.WhereCondition[basicslices.BasicSlices] { - return orm.FieldCondition[basicslices.BasicSlices, gorm.DeletedAt]{ +func BasicSlicesDeletedAt(operator orm.Operator[time.Time]) orm.WhereCondition[basicslices.BasicSlices] { + return orm.FieldCondition[basicslices.BasicSlices, time.Time]{ Field: "DeletedAt", Operator: operator, } diff --git a/cmd/gen/conditions/tests/results/basicslicespointer.go b/cmd/gen/conditions/tests/results/basicslicespointer.go index 2eabe29..90b84f5 100644 --- a/cmd/gen/conditions/tests/results/basicslicespointer.go +++ b/cmd/gen/conditions/tests/results/basicslicespointer.go @@ -4,7 +4,6 @@ package conditions import ( basicslicespointer "github.com/ditrit/badaas-cli/cmd/gen/conditions/tests/basicslicespointer" orm "github.com/ditrit/badaas/orm" - gorm "gorm.io/gorm" "time" ) @@ -26,8 +25,8 @@ func BasicSlicesPointerUpdatedAt(operator orm.Operator[time.Time]) orm.WhereCond Operator: operator, } } -func BasicSlicesPointerDeletedAt(operator orm.Operator[gorm.DeletedAt]) orm.WhereCondition[basicslicespointer.BasicSlicesPointer] { - return orm.FieldCondition[basicslicespointer.BasicSlicesPointer, gorm.DeletedAt]{ +func BasicSlicesPointerDeletedAt(operator orm.Operator[time.Time]) orm.WhereCondition[basicslicespointer.BasicSlicesPointer] { + return orm.FieldCondition[basicslicespointer.BasicSlicesPointer, time.Time]{ Field: "DeletedAt", Operator: operator, } diff --git a/cmd/gen/conditions/tests/results/basictypes.go b/cmd/gen/conditions/tests/results/basictypes.go index 9182e1c..beee4cf 100644 --- a/cmd/gen/conditions/tests/results/basictypes.go +++ b/cmd/gen/conditions/tests/results/basictypes.go @@ -4,7 +4,6 @@ package conditions import ( basictypes "github.com/ditrit/badaas-cli/cmd/gen/conditions/tests/basictypes" orm "github.com/ditrit/badaas/orm" - gorm "gorm.io/gorm" "time" ) @@ -26,8 +25,8 @@ func BasicTypesUpdatedAt(operator orm.Operator[time.Time]) orm.WhereCondition[ba Operator: operator, } } -func BasicTypesDeletedAt(operator orm.Operator[gorm.DeletedAt]) orm.WhereCondition[basictypes.BasicTypes] { - return orm.FieldCondition[basictypes.BasicTypes, gorm.DeletedAt]{ +func BasicTypesDeletedAt(operator orm.Operator[time.Time]) orm.WhereCondition[basictypes.BasicTypes] { + return orm.FieldCondition[basictypes.BasicTypes, time.Time]{ Field: "DeletedAt", Operator: operator, } diff --git a/cmd/gen/conditions/tests/results/belongsto_owned.go b/cmd/gen/conditions/tests/results/belongsto_owned.go index a1fdecb..dca65d7 100644 --- a/cmd/gen/conditions/tests/results/belongsto_owned.go +++ b/cmd/gen/conditions/tests/results/belongsto_owned.go @@ -4,7 +4,6 @@ package conditions import ( belongsto "github.com/ditrit/badaas-cli/cmd/gen/conditions/tests/belongsto" orm "github.com/ditrit/badaas/orm" - gorm "gorm.io/gorm" "time" ) @@ -26,8 +25,8 @@ func OwnedUpdatedAt(operator orm.Operator[time.Time]) orm.WhereCondition[belongs Operator: operator, } } -func OwnedDeletedAt(operator orm.Operator[gorm.DeletedAt]) orm.WhereCondition[belongsto.Owned] { - return orm.FieldCondition[belongsto.Owned, gorm.DeletedAt]{ +func OwnedDeletedAt(operator orm.Operator[time.Time]) orm.WhereCondition[belongsto.Owned] { + return orm.FieldCondition[belongsto.Owned, time.Time]{ Field: "DeletedAt", Operator: operator, } diff --git a/cmd/gen/conditions/tests/results/belongsto_owner.go b/cmd/gen/conditions/tests/results/belongsto_owner.go index c43e798..9fb6104 100644 --- a/cmd/gen/conditions/tests/results/belongsto_owner.go +++ b/cmd/gen/conditions/tests/results/belongsto_owner.go @@ -4,7 +4,6 @@ package conditions import ( belongsto "github.com/ditrit/badaas-cli/cmd/gen/conditions/tests/belongsto" orm "github.com/ditrit/badaas/orm" - gorm "gorm.io/gorm" "time" ) @@ -26,8 +25,8 @@ func OwnerUpdatedAt(operator orm.Operator[time.Time]) orm.WhereCondition[belongs Operator: operator, } } -func OwnerDeletedAt(operator orm.Operator[gorm.DeletedAt]) orm.WhereCondition[belongsto.Owner] { - return orm.FieldCondition[belongsto.Owner, gorm.DeletedAt]{ +func OwnerDeletedAt(operator orm.Operator[time.Time]) orm.WhereCondition[belongsto.Owner] { + return orm.FieldCondition[belongsto.Owner, time.Time]{ Field: "DeletedAt", Operator: operator, } diff --git a/cmd/gen/conditions/tests/results/columndefinition.go b/cmd/gen/conditions/tests/results/columndefinition.go index a547977..0f21596 100644 --- a/cmd/gen/conditions/tests/results/columndefinition.go +++ b/cmd/gen/conditions/tests/results/columndefinition.go @@ -4,7 +4,6 @@ package conditions import ( columndefinition "github.com/ditrit/badaas-cli/cmd/gen/conditions/tests/columndefinition" orm "github.com/ditrit/badaas/orm" - gorm "gorm.io/gorm" "time" ) @@ -26,8 +25,8 @@ func ColumnDefinitionUpdatedAt(operator orm.Operator[time.Time]) orm.WhereCondit Operator: operator, } } -func ColumnDefinitionDeletedAt(operator orm.Operator[gorm.DeletedAt]) orm.WhereCondition[columndefinition.ColumnDefinition] { - return orm.FieldCondition[columndefinition.ColumnDefinition, gorm.DeletedAt]{ +func ColumnDefinitionDeletedAt(operator orm.Operator[time.Time]) orm.WhereCondition[columndefinition.ColumnDefinition] { + return orm.FieldCondition[columndefinition.ColumnDefinition, time.Time]{ Field: "DeletedAt", Operator: operator, } diff --git a/cmd/gen/conditions/tests/results/customtype.go b/cmd/gen/conditions/tests/results/customtype.go index d0bde63..ba55b61 100644 --- a/cmd/gen/conditions/tests/results/customtype.go +++ b/cmd/gen/conditions/tests/results/customtype.go @@ -4,7 +4,6 @@ package conditions import ( customtype "github.com/ditrit/badaas-cli/cmd/gen/conditions/tests/customtype" orm "github.com/ditrit/badaas/orm" - gorm "gorm.io/gorm" "time" ) @@ -26,8 +25,8 @@ func CustomTypeUpdatedAt(operator orm.Operator[time.Time]) orm.WhereCondition[cu Operator: operator, } } -func CustomTypeDeletedAt(operator orm.Operator[gorm.DeletedAt]) orm.WhereCondition[customtype.CustomType] { - return orm.FieldCondition[customtype.CustomType, gorm.DeletedAt]{ +func CustomTypeDeletedAt(operator orm.Operator[time.Time]) orm.WhereCondition[customtype.CustomType] { + return orm.FieldCondition[customtype.CustomType, time.Time]{ Field: "DeletedAt", Operator: operator, } diff --git a/cmd/gen/conditions/tests/results/goembedded.go b/cmd/gen/conditions/tests/results/goembedded.go index 219259b..7a7754a 100644 --- a/cmd/gen/conditions/tests/results/goembedded.go +++ b/cmd/gen/conditions/tests/results/goembedded.go @@ -4,7 +4,6 @@ package conditions import ( goembedded "github.com/ditrit/badaas-cli/cmd/gen/conditions/tests/goembedded" orm "github.com/ditrit/badaas/orm" - gorm "gorm.io/gorm" "time" ) @@ -26,8 +25,8 @@ func GoEmbeddedUpdatedAt(operator orm.Operator[time.Time]) orm.WhereCondition[go Operator: operator, } } -func GoEmbeddedDeletedAt(operator orm.Operator[gorm.DeletedAt]) orm.WhereCondition[goembedded.GoEmbedded] { - return orm.FieldCondition[goembedded.GoEmbedded, gorm.DeletedAt]{ +func GoEmbeddedDeletedAt(operator orm.Operator[time.Time]) orm.WhereCondition[goembedded.GoEmbedded] { + return orm.FieldCondition[goembedded.GoEmbedded, time.Time]{ Field: "DeletedAt", Operator: operator, } diff --git a/cmd/gen/conditions/tests/results/gormembedded.go b/cmd/gen/conditions/tests/results/gormembedded.go index 349f1dd..2959092 100644 --- a/cmd/gen/conditions/tests/results/gormembedded.go +++ b/cmd/gen/conditions/tests/results/gormembedded.go @@ -4,7 +4,6 @@ package conditions import ( gormembedded "github.com/ditrit/badaas-cli/cmd/gen/conditions/tests/gormembedded" orm "github.com/ditrit/badaas/orm" - gorm "gorm.io/gorm" "time" ) @@ -26,8 +25,8 @@ func GormEmbeddedUpdatedAt(operator orm.Operator[time.Time]) orm.WhereCondition[ Operator: operator, } } -func GormEmbeddedDeletedAt(operator orm.Operator[gorm.DeletedAt]) orm.WhereCondition[gormembedded.GormEmbedded] { - return orm.FieldCondition[gormembedded.GormEmbedded, gorm.DeletedAt]{ +func GormEmbeddedDeletedAt(operator orm.Operator[time.Time]) orm.WhereCondition[gormembedded.GormEmbedded] { + return orm.FieldCondition[gormembedded.GormEmbedded, time.Time]{ Field: "DeletedAt", Operator: operator, } diff --git a/cmd/gen/conditions/tests/results/hasmany_company.go b/cmd/gen/conditions/tests/results/hasmany_company.go index 63ccfbf..1662523 100644 --- a/cmd/gen/conditions/tests/results/hasmany_company.go +++ b/cmd/gen/conditions/tests/results/hasmany_company.go @@ -4,7 +4,6 @@ package conditions import ( hasmany "github.com/ditrit/badaas-cli/cmd/gen/conditions/tests/hasmany" orm "github.com/ditrit/badaas/orm" - gorm "gorm.io/gorm" "time" ) @@ -26,8 +25,8 @@ func CompanyUpdatedAt(operator orm.Operator[time.Time]) orm.WhereCondition[hasma Operator: operator, } } -func CompanyDeletedAt(operator orm.Operator[gorm.DeletedAt]) orm.WhereCondition[hasmany.Company] { - return orm.FieldCondition[hasmany.Company, gorm.DeletedAt]{ +func CompanyDeletedAt(operator orm.Operator[time.Time]) orm.WhereCondition[hasmany.Company] { + return orm.FieldCondition[hasmany.Company, time.Time]{ Field: "DeletedAt", Operator: operator, } diff --git a/cmd/gen/conditions/tests/results/hasmany_seller.go b/cmd/gen/conditions/tests/results/hasmany_seller.go index 2cf1db3..2649ccc 100644 --- a/cmd/gen/conditions/tests/results/hasmany_seller.go +++ b/cmd/gen/conditions/tests/results/hasmany_seller.go @@ -4,7 +4,6 @@ package conditions import ( hasmany "github.com/ditrit/badaas-cli/cmd/gen/conditions/tests/hasmany" orm "github.com/ditrit/badaas/orm" - gorm "gorm.io/gorm" "time" ) @@ -26,8 +25,8 @@ func SellerUpdatedAt(operator orm.Operator[time.Time]) orm.WhereCondition[hasman Operator: operator, } } -func SellerDeletedAt(operator orm.Operator[gorm.DeletedAt]) orm.WhereCondition[hasmany.Seller] { - return orm.FieldCondition[hasmany.Seller, gorm.DeletedAt]{ +func SellerDeletedAt(operator orm.Operator[time.Time]) orm.WhereCondition[hasmany.Seller] { + return orm.FieldCondition[hasmany.Seller, time.Time]{ Field: "DeletedAt", Operator: operator, } diff --git a/cmd/gen/conditions/tests/results/hasone_city.go b/cmd/gen/conditions/tests/results/hasone_city.go index b496511..94be918 100644 --- a/cmd/gen/conditions/tests/results/hasone_city.go +++ b/cmd/gen/conditions/tests/results/hasone_city.go @@ -4,7 +4,6 @@ package conditions import ( hasone "github.com/ditrit/badaas-cli/cmd/gen/conditions/tests/hasone" orm "github.com/ditrit/badaas/orm" - gorm "gorm.io/gorm" "time" ) @@ -26,8 +25,8 @@ func CityUpdatedAt(operator orm.Operator[time.Time]) orm.WhereCondition[hasone.C Operator: operator, } } -func CityDeletedAt(operator orm.Operator[gorm.DeletedAt]) orm.WhereCondition[hasone.City] { - return orm.FieldCondition[hasone.City, gorm.DeletedAt]{ +func CityDeletedAt(operator orm.Operator[time.Time]) orm.WhereCondition[hasone.City] { + return orm.FieldCondition[hasone.City, time.Time]{ Field: "DeletedAt", Operator: operator, } diff --git a/cmd/gen/conditions/tests/results/hasone_country.go b/cmd/gen/conditions/tests/results/hasone_country.go index 7fcc3fb..1bf27ca 100644 --- a/cmd/gen/conditions/tests/results/hasone_country.go +++ b/cmd/gen/conditions/tests/results/hasone_country.go @@ -4,7 +4,6 @@ package conditions import ( hasone "github.com/ditrit/badaas-cli/cmd/gen/conditions/tests/hasone" orm "github.com/ditrit/badaas/orm" - gorm "gorm.io/gorm" "time" ) @@ -26,8 +25,8 @@ func CountryUpdatedAt(operator orm.Operator[time.Time]) orm.WhereCondition[hason Operator: operator, } } -func CountryDeletedAt(operator orm.Operator[gorm.DeletedAt]) orm.WhereCondition[hasone.Country] { - return orm.FieldCondition[hasone.Country, gorm.DeletedAt]{ +func CountryDeletedAt(operator orm.Operator[time.Time]) orm.WhereCondition[hasone.Country] { + return orm.FieldCondition[hasone.Country, time.Time]{ Field: "DeletedAt", Operator: operator, } diff --git a/cmd/gen/conditions/tests/results/multiplepackage_package1.go b/cmd/gen/conditions/tests/results/multiplepackage_package1.go index e4eb294..08a4efa 100644 --- a/cmd/gen/conditions/tests/results/multiplepackage_package1.go +++ b/cmd/gen/conditions/tests/results/multiplepackage_package1.go @@ -5,7 +5,6 @@ import ( package1 "github.com/ditrit/badaas-cli/cmd/gen/conditions/tests/multiplepackage/package1" package2 "github.com/ditrit/badaas-cli/cmd/gen/conditions/tests/multiplepackage/package2" orm "github.com/ditrit/badaas/orm" - gorm "gorm.io/gorm" "time" ) @@ -27,8 +26,8 @@ func Package1UpdatedAt(operator orm.Operator[time.Time]) orm.WhereCondition[pack Operator: operator, } } -func Package1DeletedAt(operator orm.Operator[gorm.DeletedAt]) orm.WhereCondition[package1.Package1] { - return orm.FieldCondition[package1.Package1, gorm.DeletedAt]{ +func Package1DeletedAt(operator orm.Operator[time.Time]) orm.WhereCondition[package1.Package1] { + return orm.FieldCondition[package1.Package1, time.Time]{ Field: "DeletedAt", Operator: operator, } diff --git a/cmd/gen/conditions/tests/results/nullabletypes.go b/cmd/gen/conditions/tests/results/nullabletypes.go new file mode 100644 index 0000000..43f7f13 --- /dev/null +++ b/cmd/gen/conditions/tests/results/nullabletypes.go @@ -0,0 +1,81 @@ +// Code generated by badaas-cli v0.0.0, DO NOT EDIT. +package conditions + +import ( + nullabletypes "github.com/ditrit/badaas-cli/cmd/gen/conditions/tests/nullabletypes" + orm "github.com/ditrit/badaas/orm" + "time" +) + +func NullableTypesId(operator orm.Operator[orm.UUID]) orm.WhereCondition[nullabletypes.NullableTypes] { + return orm.FieldCondition[nullabletypes.NullableTypes, orm.UUID]{ + Field: "ID", + Operator: operator, + } +} +func NullableTypesCreatedAt(operator orm.Operator[time.Time]) orm.WhereCondition[nullabletypes.NullableTypes] { + return orm.FieldCondition[nullabletypes.NullableTypes, time.Time]{ + Field: "CreatedAt", + Operator: operator, + } +} +func NullableTypesUpdatedAt(operator orm.Operator[time.Time]) orm.WhereCondition[nullabletypes.NullableTypes] { + return orm.FieldCondition[nullabletypes.NullableTypes, time.Time]{ + Field: "UpdatedAt", + Operator: operator, + } +} +func NullableTypesDeletedAt(operator orm.Operator[time.Time]) orm.WhereCondition[nullabletypes.NullableTypes] { + return orm.FieldCondition[nullabletypes.NullableTypes, time.Time]{ + Field: "DeletedAt", + Operator: operator, + } +} +func NullableTypesString(operator orm.Operator[string]) orm.WhereCondition[nullabletypes.NullableTypes] { + return orm.FieldCondition[nullabletypes.NullableTypes, string]{ + Field: "String", + Operator: operator, + } +} +func NullableTypesInt64(operator orm.Operator[int64]) orm.WhereCondition[nullabletypes.NullableTypes] { + return orm.FieldCondition[nullabletypes.NullableTypes, int64]{ + Field: "Int64", + Operator: operator, + } +} +func NullableTypesInt32(operator orm.Operator[int32]) orm.WhereCondition[nullabletypes.NullableTypes] { + return orm.FieldCondition[nullabletypes.NullableTypes, int32]{ + Field: "Int32", + Operator: operator, + } +} +func NullableTypesInt16(operator orm.Operator[int16]) orm.WhereCondition[nullabletypes.NullableTypes] { + return orm.FieldCondition[nullabletypes.NullableTypes, int16]{ + Field: "Int16", + Operator: operator, + } +} +func NullableTypesByte(operator orm.Operator[int8]) orm.WhereCondition[nullabletypes.NullableTypes] { + return orm.FieldCondition[nullabletypes.NullableTypes, int8]{ + Field: "Byte", + Operator: operator, + } +} +func NullableTypesFloat64(operator orm.Operator[float64]) orm.WhereCondition[nullabletypes.NullableTypes] { + return orm.FieldCondition[nullabletypes.NullableTypes, float64]{ + Field: "Float64", + Operator: operator, + } +} +func NullableTypesBool(operator orm.Operator[bool]) orm.WhereCondition[nullabletypes.NullableTypes] { + return orm.FieldCondition[nullabletypes.NullableTypes, bool]{ + Field: "Bool", + Operator: operator, + } +} +func NullableTypesTime(operator orm.Operator[time.Time]) orm.WhereCondition[nullabletypes.NullableTypes] { + return orm.FieldCondition[nullabletypes.NullableTypes, time.Time]{ + Field: "Time", + Operator: operator, + } +} diff --git a/cmd/gen/conditions/tests/results/overrideforeignkey.go b/cmd/gen/conditions/tests/results/overrideforeignkey.go index 005069a..a0188fe 100644 --- a/cmd/gen/conditions/tests/results/overrideforeignkey.go +++ b/cmd/gen/conditions/tests/results/overrideforeignkey.go @@ -4,7 +4,6 @@ package conditions import ( overrideforeignkey "github.com/ditrit/badaas-cli/cmd/gen/conditions/tests/overrideforeignkey" orm "github.com/ditrit/badaas/orm" - gorm "gorm.io/gorm" "time" ) @@ -26,8 +25,8 @@ func BicycleUpdatedAt(operator orm.Operator[time.Time]) orm.WhereCondition[overr Operator: operator, } } -func BicycleDeletedAt(operator orm.Operator[gorm.DeletedAt]) orm.WhereCondition[overrideforeignkey.Bicycle] { - return orm.FieldCondition[overrideforeignkey.Bicycle, gorm.DeletedAt]{ +func BicycleDeletedAt(operator orm.Operator[time.Time]) orm.WhereCondition[overrideforeignkey.Bicycle] { + return orm.FieldCondition[overrideforeignkey.Bicycle, time.Time]{ Field: "DeletedAt", Operator: operator, } diff --git a/cmd/gen/conditions/tests/results/overrideforeignkeyinverse.go b/cmd/gen/conditions/tests/results/overrideforeignkeyinverse.go index 4f98d70..434ba42 100644 --- a/cmd/gen/conditions/tests/results/overrideforeignkeyinverse.go +++ b/cmd/gen/conditions/tests/results/overrideforeignkeyinverse.go @@ -4,7 +4,6 @@ package conditions import ( overrideforeignkeyinverse "github.com/ditrit/badaas-cli/cmd/gen/conditions/tests/overrideforeignkeyinverse" orm "github.com/ditrit/badaas/orm" - gorm "gorm.io/gorm" "time" ) @@ -26,8 +25,8 @@ func UserUpdatedAt(operator orm.Operator[time.Time]) orm.WhereCondition[override Operator: operator, } } -func UserDeletedAt(operator orm.Operator[gorm.DeletedAt]) orm.WhereCondition[overrideforeignkeyinverse.User] { - return orm.FieldCondition[overrideforeignkeyinverse.User, gorm.DeletedAt]{ +func UserDeletedAt(operator orm.Operator[time.Time]) orm.WhereCondition[overrideforeignkeyinverse.User] { + return orm.FieldCondition[overrideforeignkeyinverse.User, time.Time]{ Field: "DeletedAt", Operator: operator, } diff --git a/cmd/gen/conditions/tests/results/overridereferences.go b/cmd/gen/conditions/tests/results/overridereferences.go index 8ee4092..7e81b2d 100644 --- a/cmd/gen/conditions/tests/results/overridereferences.go +++ b/cmd/gen/conditions/tests/results/overridereferences.go @@ -4,7 +4,6 @@ package conditions import ( overridereferences "github.com/ditrit/badaas-cli/cmd/gen/conditions/tests/overridereferences" orm "github.com/ditrit/badaas/orm" - gorm "gorm.io/gorm" "time" ) @@ -26,8 +25,8 @@ func PhoneUpdatedAt(operator orm.Operator[time.Time]) orm.WhereCondition[overrid Operator: operator, } } -func PhoneDeletedAt(operator orm.Operator[gorm.DeletedAt]) orm.WhereCondition[overridereferences.Phone] { - return orm.FieldCondition[overridereferences.Phone, gorm.DeletedAt]{ +func PhoneDeletedAt(operator orm.Operator[time.Time]) orm.WhereCondition[overridereferences.Phone] { + return orm.FieldCondition[overridereferences.Phone, time.Time]{ Field: "DeletedAt", Operator: operator, } diff --git a/cmd/gen/conditions/tests/results/overridereferencesinverse.go b/cmd/gen/conditions/tests/results/overridereferencesinverse.go index 412d804..7bfa455 100644 --- a/cmd/gen/conditions/tests/results/overridereferencesinverse.go +++ b/cmd/gen/conditions/tests/results/overridereferencesinverse.go @@ -4,7 +4,6 @@ package conditions import ( overridereferencesinverse "github.com/ditrit/badaas-cli/cmd/gen/conditions/tests/overridereferencesinverse" orm "github.com/ditrit/badaas/orm" - gorm "gorm.io/gorm" "time" ) @@ -26,8 +25,8 @@ func ComputerUpdatedAt(operator orm.Operator[time.Time]) orm.WhereCondition[over Operator: operator, } } -func ComputerDeletedAt(operator orm.Operator[gorm.DeletedAt]) orm.WhereCondition[overridereferencesinverse.Computer] { - return orm.FieldCondition[overridereferencesinverse.Computer, gorm.DeletedAt]{ +func ComputerDeletedAt(operator orm.Operator[time.Time]) orm.WhereCondition[overridereferencesinverse.Computer] { + return orm.FieldCondition[overridereferencesinverse.Computer, time.Time]{ Field: "DeletedAt", Operator: operator, } diff --git a/cmd/gen/conditions/tests/results/selfreferential.go b/cmd/gen/conditions/tests/results/selfreferential.go index d1ffb53..66c6028 100644 --- a/cmd/gen/conditions/tests/results/selfreferential.go +++ b/cmd/gen/conditions/tests/results/selfreferential.go @@ -4,7 +4,6 @@ package conditions import ( selfreferential "github.com/ditrit/badaas-cli/cmd/gen/conditions/tests/selfreferential" orm "github.com/ditrit/badaas/orm" - gorm "gorm.io/gorm" "time" ) @@ -26,8 +25,8 @@ func EmployeeUpdatedAt(operator orm.Operator[time.Time]) orm.WhereCondition[self Operator: operator, } } -func EmployeeDeletedAt(operator orm.Operator[gorm.DeletedAt]) orm.WhereCondition[selfreferential.Employee] { - return orm.FieldCondition[selfreferential.Employee, gorm.DeletedAt]{ +func EmployeeDeletedAt(operator orm.Operator[time.Time]) orm.WhereCondition[selfreferential.Employee] { + return orm.FieldCondition[selfreferential.Employee, time.Time]{ Field: "DeletedAt", Operator: operator, } diff --git a/cmd/gen/conditions/tests/results/uintmodel.go b/cmd/gen/conditions/tests/results/uintmodel.go index ea6fba5..181de46 100644 --- a/cmd/gen/conditions/tests/results/uintmodel.go +++ b/cmd/gen/conditions/tests/results/uintmodel.go @@ -4,7 +4,6 @@ package conditions import ( uintmodel "github.com/ditrit/badaas-cli/cmd/gen/conditions/tests/uintmodel" orm "github.com/ditrit/badaas/orm" - gorm "gorm.io/gorm" "time" ) @@ -26,8 +25,8 @@ func UintModelUpdatedAt(operator orm.Operator[time.Time]) orm.WhereCondition[uin Operator: operator, } } -func UintModelDeletedAt(operator orm.Operator[gorm.DeletedAt]) orm.WhereCondition[uintmodel.UintModel] { - return orm.FieldCondition[uintmodel.UintModel, gorm.DeletedAt]{ +func UintModelDeletedAt(operator orm.Operator[time.Time]) orm.WhereCondition[uintmodel.UintModel] { + return orm.FieldCondition[uintmodel.UintModel, time.Time]{ Field: "DeletedAt", Operator: operator, } diff --git a/cmd/gen/conditions/tests/results/uuidmodel.go b/cmd/gen/conditions/tests/results/uuidmodel.go index 299ab3b..0753173 100644 --- a/cmd/gen/conditions/tests/results/uuidmodel.go +++ b/cmd/gen/conditions/tests/results/uuidmodel.go @@ -4,7 +4,6 @@ package conditions import ( uuidmodel "github.com/ditrit/badaas-cli/cmd/gen/conditions/tests/uuidmodel" orm "github.com/ditrit/badaas/orm" - gorm "gorm.io/gorm" "time" ) @@ -26,8 +25,8 @@ func UUIDModelUpdatedAt(operator orm.Operator[time.Time]) orm.WhereCondition[uui Operator: operator, } } -func UUIDModelDeletedAt(operator orm.Operator[gorm.DeletedAt]) orm.WhereCondition[uuidmodel.UUIDModel] { - return orm.FieldCondition[uuidmodel.UUIDModel, gorm.DeletedAt]{ +func UUIDModelDeletedAt(operator orm.Operator[time.Time]) orm.WhereCondition[uuidmodel.UUIDModel] { + return orm.FieldCondition[uuidmodel.UUIDModel, time.Time]{ Field: "DeletedAt", Operator: operator, } diff --git a/cmd/gen/conditions/type.go b/cmd/gen/conditions/type.go index 8718476..22a576c 100644 --- a/cmd/gen/conditions/type.go +++ b/cmd/gen/conditions/type.go @@ -9,8 +9,25 @@ import ( "github.com/elliotchance/pie/v2" ) -// badaas/orm/baseModels.go -var badaasORMBaseModels = []string{"github.com/ditrit/badaas/orm.UUIDModel", "github.com/ditrit/badaas/orm.UIntModel", "gorm.io/gorm.Model"} +var ( + // badaas/orm/baseModels.go + badaasORMBaseModels = []string{"github.com/ditrit/badaas/orm.UUIDModel", "github.com/ditrit/badaas/orm.UIntModel", "gorm.io/gorm.Model"} + + // database/sql + nullString = "database/sql.NullString" + nullInt64 = "database/sql.NullInt64" + nullInt32 = "database/sql.NullInt32" + nullInt16 = "database/sql.NullInt16" + nullFloat64 = "database/sql.NullFloat64" + nullByte = "database/sql.NullByte" + nullBool = "database/sql.NullBool" + nullTime = "database/sql.NullTime" + deletedAt = "gorm.io/gorm.DeletedAt" + sqlNullableTypes = []string{ + nullString, nullInt64, nullInt32, nullInt16, nullFloat64, + nullByte, nullBool, nullTime, deletedAt, + } +) type Type struct { types.Type @@ -72,8 +89,10 @@ func (t Type) HasFK(field Field) (bool, error) { }), nil } -var scanMethod = regexp.MustCompile(`func \(\*.*\)\.Scan\([a-zA-Z0-9_-]* interface\{\}\) error$`) -var valueMethod = regexp.MustCompile(`func \(.*\)\.Value\(\) \(database/sql/driver\.Value\, error\)$`) +var ( + scanMethod = regexp.MustCompile(`func \(\*.*\)\.Scan\([a-zA-Z0-9_-]* (interface\{\}|any)\) error$`) + valueMethod = regexp.MustCompile(`func \(.*\)\.Value\(\) \(database/sql/driver\.Value\, error\)$`) +) // Returns true if the type is a Gorm Custom type (https://gorm.io/docs/data_types.html) func (t Type) IsGormCustomType() bool { @@ -96,3 +115,8 @@ func (t Type) IsGormCustomType() bool { return hasScanMethod && hasValueMethod } + +// Returns true if the type is a sql nullable type (sql.NullBool, sql.NullInt, etc.) +func (t Type) IsSQLNullableType() bool { + return pie.Contains(sqlNullableTypes, t.String()) +}