Use named returns for the code style consistency

This commit is contained in:
Nikita Tokarchuk
2020-07-13 03:12:39 +02:00
parent 9cf3551c20
commit 72e74a65b6
23 changed files with 145 additions and 146 deletions
+12 -12
View File
@@ -11,27 +11,27 @@ import (
)
// Compose is a function to compose filters into a single query
func Compose(filters ...interface{}) *Query {
func Compose(filters ...interface{}) (query *Query) {
q := &Query{}
query = &Query{}
for _, f := range filters {
if !Push(q, f) {
if !Push(query, f) {
panic(fmt.Errorf("unknown filter %v", f))
}
}
return q
return
}
// Push applies single filter to a query
func Push(q *Query, f interface{}) bool {
func Push(q *Query, f interface{}) (ok bool) {
if utils.IsNil(f) {
return true
}
ok := false
ok = false
ok = ok || applyBson(q, f)
ok = ok || applyLimit(q, f)
ok = ok || applySort(q, f)
@@ -43,7 +43,7 @@ func Push(q *Query, f interface{}) bool {
}
// applyBson is a fallback for a custom bson.M
func applyBson(q *Query, f interface{}) bool {
func applyBson(q *Query, f interface{}) (ok bool) {
if f, ok := f.(bson.M); ok {
q.And(f)
@@ -54,7 +54,7 @@ func applyBson(q *Query, f interface{}) bool {
}
// applyLimits extends query with a limiter
func applyLimit(q *Query, f interface{}) bool {
func applyLimit(q *Query, f interface{}) (ok bool) {
if f, ok := f.(Limiter); ok {
q.limiter = f
@@ -65,7 +65,7 @@ func applyLimit(q *Query, f interface{}) bool {
}
// applySort extends query with a sort rule
func applySort(q *Query, f interface{}) bool {
func applySort(q *Query, f interface{}) (ok bool) {
if f, ok := f.(Sorter); ok {
q.sorter = f
@@ -76,7 +76,7 @@ func applySort(q *Query, f interface{}) bool {
}
// applySkip extends query with a skip number
func applySkip(q *Query, f interface{}) bool {
func applySkip(q *Query, f interface{}) (ok bool) {
if f, ok := f.(Skipper); ok {
q.skipper = f
@@ -86,7 +86,7 @@ func applySkip(q *Query, f interface{}) bool {
return false
}
func applyProtection(q *Query, f interface{}) bool {
func applyProtection(q *Query, f interface{}) (ok bool) {
var x *primitive.ObjectID
var v *int64
@@ -114,7 +114,7 @@ func applyProtection(q *Query, f interface{}) bool {
return true
}
func applyPreloader(q *Query, f interface{}) bool {
func applyPreloader(q *Query, f interface{}) (ok bool) {
if f, ok := f.(Preloader); ok {
q.preloader = f
+8 -6
View File
@@ -2,7 +2,7 @@ package query
// Limiter is a filter to limit the result
type Limiter interface {
Limit() *int64
Limit() (limit *int64)
}
// Limit is a simple implementation of the Limiter filter
@@ -11,12 +11,14 @@ type Limit int64
var _ Limiter = Limit(0)
// Limit returns a limit
func (l Limit) Limit() *int64 {
func (l Limit) Limit() (limit *int64) {
lim := int64(l)
if lim <= 0 {
return nil
if l <= 0 {
return
}
return &lim
limit = new(int64)
*limit = int64(l)
return
}
+2 -2
View File
@@ -2,7 +2,7 @@ package query
// Preloader is a filter to skip the result
type Preloader interface {
Preload() []string
Preload() (preloads []string)
}
// Preload is a simple implementation of the Skipper filter
@@ -11,6 +11,6 @@ type Preload []string
var _ Preloader = Preload{}
// Preload returns a preload list
func (l Preload) Preload() []string {
func (l Preload) Preload() (preloads []string) {
return l
}
+13 -16
View File
@@ -14,7 +14,7 @@ type Query struct {
}
// And function pushes the elem query to the $and array of the query
func (q *Query) And(elem bson.M) *Query {
func (q *Query) And(elem bson.M) (query *Query) {
if q.m == nil {
q.m = bson.M{}
@@ -33,57 +33,54 @@ func (q *Query) And(elem bson.M) *Query {
}
// Limiter returns limiter value or nil
func (q *Query) Limiter() *int64 {
func (q *Query) Limiter() (limit *int64) {
if q.limiter == nil {
return nil
return
}
return q.limiter.Limit()
}
// Sorter is a sort rule for a query
func (q *Query) Sorter() interface{} {
func (q *Query) Sorter() (sort interface{}) {
if q.sorter == nil {
return nil
return
}
return q.sorter.Sort()
}
// Skipper is a skipper for a query
func (q *Query) Skipper() *int64 {
func (q *Query) Skipper() (skip *int64) {
if q.skipper == nil {
return nil
return
}
return q.skipper.Skip()
}
// Preloader is a preloader list for a query
func (q *Query) Preloader() (empty bool, preloader []string) {
func (q *Query) Preloader() (ok bool, preloads []string) {
if q.preloader == nil {
return false, nil
}
preloader = q.preloader.Preload()
preloads = q.preloader.Preload()
ok = len(preloads) > 0
if len(preloader) == 0 {
return false, nil
}
return true, preloader
return
}
// Empty checks the query for any content
func (q *Query) Empty() bool {
func (q *Query) Empty() (isEmpty bool) {
return len(q.m) == 0
}
// M returns underlying query map
func (q *Query) M() bson.M {
func (q *Query) M() (m bson.M) {
return q.m
}
+8 -6
View File
@@ -2,7 +2,7 @@ package query
// Skipper is a filter to skip the result
type Skipper interface {
Skip() *int64
Skip() (skip *int64)
}
// Skip is a simple implementation of the Skipper filter
@@ -11,12 +11,14 @@ type Skip int64
var _ Skipper = Skip(0)
// Skip returns a skip number
func (l Skip) Skip() *int64 {
func (l Skip) Skip() (skip *int64) {
lim := int64(l)
if lim <= 0 {
return nil
if l <= 0 {
return
}
return &lim
skip = new(int64)
*skip = int64(l)
return
}
+2 -2
View File
@@ -6,7 +6,7 @@ import (
// Sorter is a filter to sort the data before query
type Sorter interface {
Sort() bson.M
Sort() (sort bson.M)
}
// Sort is a simple implementations of the Sorter filter
@@ -15,6 +15,6 @@ type Sort bson.M
var _ Sorter = &Sort{}
// Sort returns a slice of fields which have to be sorted
func (f Sort) Sort() bson.M {
func (f Sort) Sort() (sort bson.M) {
return bson.M(f)
}