Skip to content

Commit

Permalink
Lookup field names by DB name or by struct field name
Browse files Browse the repository at this point in the history
  • Loading branch information
System-Glitch committed Nov 2, 2023
1 parent 7c17576 commit 09be077
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 8 deletions.
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -254,11 +254,11 @@ It is important to make sure your JSON expression returns a value that has a typ

## Tips

### Model recommendations
### Model and DTO recommendations

- Use `json:",omitempty"` on all model fields.
- Use `json:",omitempty"` on all DTO fields.
- *Note: using `omitempty` on slices will remove them from the json result if they are not nil and empty. There is currently no solution to this problem using the standard json package.*
- Use `json:"-"` on foreign keys.
- Don't include the foreign keys in the DTO.
- Use `*null.Time` from the [`gopkg.in/guregu/null.v4`](https://github.com/guregu/null) library instead of `sql.NullTime`.
- Always specify `gorm:"foreignKey"`, otherwise falls back to "ID".
- Don't use `gorm.Model` and add the necessary fields manually. You get better control over json struct tags this way.
Expand Down
24 changes: 23 additions & 1 deletion filter_test.go
Expand Up @@ -50,10 +50,14 @@ func TestFilterWhereOr(t *testing.T) {
func TestFilterScope(t *testing.T) {
db := openDryRunDB(t)
filter := &Filter{Field: "notacolumn", Args: []string{"val1"}, Operator: Operators["$eq"]}
field := &schema.Field{Name: "Name", DBName: "name", GORMDataType: schema.String}
schema := &schema.Schema{
DBNames: []string{"name"},
FieldsByDBName: map[string]*schema.Field{
"name": {Name: "Name", DBName: "name", GORMDataType: schema.String},
"name": field,
},
FieldsByName: map[string]*schema.Field{
"Name": field,
},
Table: "test_scope_models",
}
Expand All @@ -77,6 +81,24 @@ func TestFilterScope(t *testing.T) {
},
}
assert.Equal(t, expected, db.Statement.Clauses)

// Using struct field name
filter.Field = "Name"

results = []map[string]any{}
db = openDryRunDB(t)
db = db.Scopes(filter.Scope(Blacklist{}, schema)).Find(results)
expected = map[string]clause.Clause{
"WHERE": {
Name: "WHERE",
Expression: clause.Where{
Exprs: []clause.Expression{
clause.Expr{SQL: "`test_scope_models`.`name` = ?", Vars: []any{"val1"}},
},
},
},
}
assert.Equal(t, expected, db.Statement.Clauses)
}

func TestFilterScopeBlacklisted(t *testing.T) {
Expand Down
12 changes: 12 additions & 0 deletions search_test.go
Expand Up @@ -29,6 +29,11 @@ func TestSearchScope(t *testing.T) {
"email": {Name: "Email", DBName: "email", GORMDataType: schema.String},
"role": {Name: "Role", DBName: "role", GORMDataType: schema.String},
},
FieldsByName: map[string]*schema.Field{
"Name": {Name: "Name", DBName: "name", GORMDataType: schema.String},
"Email": {Name: "Email", DBName: "email", GORMDataType: schema.String},
"Role": {Name: "Role", DBName: "role", GORMDataType: schema.String},
},
Table: "test_models",
}

Expand Down Expand Up @@ -73,6 +78,13 @@ func TestSearchScope(t *testing.T) {
},
}
assert.Equal(t, expected, db.Statement.Clauses)

// Using struct field names
search.Fields = []string{"Name", "Email"}

db = openDryRunDB(t)
db = db.Scopes(search.Scope(schema)).Table("table").Find(nil)
assert.Equal(t, expected, db.Statement.Clauses)
}

func TestSearchScopeEmptyField(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions settings.go
Expand Up @@ -401,8 +401,8 @@ func getField(field string, sch *schema.Schema, blacklist *Blacklist) (*schema.F
if blacklist != nil && lo.Contains(blacklist.FieldsBlacklist, field) {
return nil, nil, ""
}
col, ok := s.FieldsByDBName[field]
if !ok {
col := s.LookUpField(field)
if col == nil {
return nil, nil, ""
}
return col, s, joinName
Expand Down
14 changes: 12 additions & 2 deletions sort_test.go
Expand Up @@ -11,9 +11,13 @@ import (
func TestSortScope(t *testing.T) {
db := openDryRunDB(t)
sort := &Sort{Field: "notacolumn", Order: SortAscending}
field := &schema.Field{Name: "Name", DBName: "name"}
schema := &schema.Schema{
FieldsByDBName: map[string]*schema.Field{
"name": {Name: "Name", DBName: "name"},
"name": field,
},
FieldsByName: map[string]*schema.Field{
"Name": field,
},
Table: "test_models",
}
Expand Down Expand Up @@ -50,11 +54,17 @@ func TestSortScope(t *testing.T) {
assert.Equal(t, expected, db.Statement.Clauses)

sort.Order = SortDescending
results = []map[string]any{}
db = openDryRunDB(t)
db = db.Scopes(sort.Scope(Blacklist{}, schema)).Table("table").Find(&results)
expected["ORDER BY"].Expression.(clause.OrderBy).Columns[0].Desc = true
assert.Equal(t, expected, db.Statement.Clauses)

// Using struct field name
sort.Field = "Name"
results = []map[string]any{}
db = openDryRunDB(t)
db = db.Scopes(sort.Scope(Blacklist{}, schema)).Table("table").Find(&results)
expected["ORDER BY"].Expression.(clause.OrderBy).Columns[0].Desc = true
assert.Equal(t, expected, db.Statement.Clauses)
}

Expand Down

0 comments on commit 09be077

Please sign in to comment.