Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type-safety in operators #8

Merged
merged 12 commits into from
Apr 12, 2023

Conversation

System-Glitch
Copy link
Member

@System-Glitch System-Glitch commented Mar 30, 2023

fixes #7

Type safety

Added type-safety. The operators now generate valid SQL matching the column's data type. The data type is determined by the new struct tag fieldType. If not provided, the type will be determined from GORM's data type. If GORM's data type is a custom type or a type that is not directly supported by this library, the type will fall back to - (unsupported).

See the updated README for more details.

Breaking change

This change breaks custom operators: the function signature changed.

//Before
func(tx *gorm.DB, filter *filter.Filter, column string, dataType schema.DataType) *gorm.DB

// After
func(tx *gorm.DB, f *filter.Filter, column string, dataType filter.DataType) *gorm.DB

You may need to update your models by adding the new fieldType struct tag on non-native types (such as arrays and enum types). Structures implementing driver.Valuer should work without needing for a change.

type MyModel struct {
	ID         uint
	SomeArray  []string     `gorm:"type:VARCHAR(255)[]" filterType:"text[]"`
	SomeEnum   MyCustomEnum `gorm:"type:my_custom_enum" filterType:"enum"`
}

Fields having a custom database type set (gorm:"type:DOUBLE PRECISION" for example) should work too as the type detection is based on GORMDataType and not on the actual DataType.

Types from the guregu/null library should work without having to add the fieldType tag since they implement driver.Valuer.

You may also need to update computed columns that would return a type that doesn't exactly match the struct field. This is common when working with JSON nested fields. Database engines usually always return text types from JSON operations, which is incompatible with numbers.

// This example is compatible with PostgreSQL.
// JSON processing may be different if you are using another database engine. 
type MyModel struct {
	ID            uint
	JSONColumn    datatypes.JSON
	SomeJSONField null.Int `gorm:"->;-:migration" computed:"(~~~ct~~~.json_column->>'fieldName')::int"`
}

@System-Glitch System-Glitch added the bug Something isn't working label Mar 30, 2023
@System-Glitch System-Glitch self-assigned this Mar 30, 2023
@coveralls
Copy link

coveralls commented Mar 30, 2023

Pull Request Test Coverage Report for Build 4636597426

  • 192 of 192 (100.0%) changed or added relevant lines in 5 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.2%) to 99.241%

Totals Coverage Status
Change from base Build 3386715886: 0.2%
Covered Lines: 915
Relevant Lines: 922

💛 - Coveralls

@System-Glitch System-Glitch force-pushed the bug/mismatching-data-type-sql-error branch 4 times, most recently from 3396308 to d194c07 Compare March 30, 2023 17:50
@System-Glitch System-Glitch force-pushed the bug/mismatching-data-type-sql-error branch from d194c07 to 318fd84 Compare March 30, 2023 17:51
@System-Glitch System-Glitch force-pushed the bug/mismatching-data-type-sql-error branch from 4c12ed4 to 2f49682 Compare April 4, 2023 15:28
@System-Glitch System-Glitch force-pushed the bug/mismatching-data-type-sql-error branch from 6337601 to b4da5c2 Compare April 5, 2023 09:36
@System-Glitch System-Glitch force-pushed the bug/mismatching-data-type-sql-error branch from b4da5c2 to cb3c4a1 Compare April 5, 2023 09:36
@System-Glitch System-Glitch force-pushed the bug/mismatching-data-type-sql-error branch from 0da9fdd to 307a3d7 Compare April 5, 2023 13:53
@System-Glitch System-Glitch marked this pull request as ready for review April 5, 2023 14:04
operator.go Outdated Show resolved Hide resolved
@System-Glitch System-Glitch merged commit 60c7424 into master Apr 12, 2023
@System-Glitch System-Glitch deleted the bug/mismatching-data-type-sql-error branch June 11, 2024 14:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Filters on non-string types can result in SQL errors
2 participants