generated from mrz1836/go-template
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/bitcoin-sv/spv-wallet int…
…o BUX-246/Linters
- Loading branch information
Showing
8 changed files
with
877 additions
and
745 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package datastore | ||
|
||
import ( | ||
"sync" | ||
|
||
"gorm.io/gorm" | ||
"gorm.io/gorm/schema" | ||
) | ||
|
||
var modelsCache = sync.Map{} | ||
|
||
// GetColumnName checks if the model has provided columnName as DBName or (struct field)Name | ||
// Returns (DBName, true) if the column exists otherwise (_, false) | ||
// Uses global cache store (thread safe) | ||
// Checking is case-sensitive | ||
// The gdb param is optional. When is provided, the actual naming strategy is used; otherwise default | ||
func GetColumnName(columnName string, model interface{}, gdb *gorm.DB) (string, bool) { | ||
var namer schema.Namer | ||
if gdb != nil { | ||
namer = gdb.NamingStrategy | ||
} else { | ||
namer = schema.NamingStrategy{} | ||
} | ||
|
||
sch, err := schema.Parse(model, &modelsCache, namer) | ||
if err != nil { | ||
return "", false | ||
} | ||
if field, ok := sch.FieldsByDBName[columnName]; ok { | ||
return field.DBName, true | ||
} | ||
|
||
if field, ok := sch.FieldsByName[columnName]; ok { | ||
return field.DBName, true | ||
} | ||
|
||
return "", false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.