-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
43dc08e
commit 055afa6
Showing
4 changed files
with
60 additions
and
2 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,16 @@ | ||
# config | ||
|
||
`config` encapsulates the use of `viper` and parses the configuration file of the specified path into a structure. | ||
|
||
```go | ||
import( | ||
"github.com/huweihuang/golib/config" | ||
) | ||
|
||
func main() { | ||
err := config.InitConfigObjectByPath(configFile, &configs.GlobalConfig) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
``` |
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,43 @@ | ||
package db | ||
|
||
import "gorm.io/gorm" | ||
|
||
func Paginate(page int, pageSize int) func(db *gorm.DB) *gorm.DB { | ||
return func(db *gorm.DB) *gorm.DB { | ||
if page == 0 { | ||
page = 1 | ||
} | ||
switch { | ||
case pageSize > 500: | ||
pageSize = 500 | ||
case pageSize <= 0: | ||
pageSize = 10 | ||
} | ||
offset := (page - 1) * pageSize | ||
return db.Offset(offset).Limit(pageSize) | ||
} | ||
} | ||
|
||
func PaginatedFind(db *gorm.DB, page, pageSize int, target interface{}) (count int64, err error) { | ||
err = db.Count(&count).Error | ||
if err != nil { | ||
return 0, err | ||
} | ||
err = db.Scopes(Paginate(page, pageSize)).Find(target).Error | ||
return count, err | ||
} | ||
|
||
func ParamsToQuery(queryMap map[string]interface{}) (queryStr string, args []interface{}) { | ||
for key, value := range queryMap { | ||
queryStr = AppendQuery(queryStr, key) | ||
args = append(args, value) | ||
} | ||
return queryStr, args | ||
} | ||
|
||
func AppendQuery(query, newQuery string) string { | ||
if query != "" { | ||
return query + " AND " + newQuery | ||
} | ||
return newQuery | ||
} |
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