remove ginext/mongoext (no-dep lib)

This commit is contained in:
2022-10-27 16:07:42 +02:00
parent 568d7bd5e3
commit 47f123b86f
31 changed files with 6 additions and 123 deletions

77
timeext/range.go Normal file
View File

@@ -0,0 +1,77 @@
package timeext
import (
"fmt"
"time"
)
type OpenTimeRange struct {
From *time.Time
To *time.Time
}
func (r OpenTimeRange) String() string {
if r.From == nil && r.To == nil {
return "[]"
} else if r.From != nil && r.To != nil {
return fmt.Sprintf("[%v - %v]", r.From, r.To)
} else if r.From != nil {
return fmt.Sprintf("[%v - *]", r.From)
} else if r.To != nil {
return fmt.Sprintf("[* - %v]", r.To)
} else {
return "[??]"
}
}
func (r OpenTimeRange) Contains(v time.Time) bool {
if r.From != nil && v.Before(*r.From) {
return false
}
if r.To != nil && !v.Before(*r.To) {
return false
}
return true
}
func NewOpenTimeRange(from *time.Time, to *time.Time) *OpenTimeRange {
if from == nil && to == nil {
return nil
}
return &OpenTimeRange{
From: from,
To: to,
}
}
func (r OpenTimeRange) ToMongoPipeline(key string) []interface{} {
type bsonM map[string]interface{}
type bsonE struct {
Key string
Value interface{}
}
type bsonD []bsonE
pipeline := make([]interface{}, 0)
if r.From != nil {
pipeline = append(pipeline, bsonD{{Key: "$match", Value: bsonM{key: bsonM{"$ne": nil, "$gt": r.From}}}})
}
if r.To != nil {
pipeline = append(pipeline, bsonD{{Key: "$match", Value: bsonM{key: bsonM{"$ne": nil, "$lt": r.To}}}})
}
return pipeline
}
func (r *OpenTimeRange) AppendToMongoPipeline(pipeline []interface{}, key string) []interface{} {
if r == nil {
return pipeline
}
for _, v := range r.ToMongoPipeline(key) {
pipeline = append(pipeline, v)
}
return pipeline
}