Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
fa6d73301e
|
|||
bfe62799d3
|
|||
ede912eb7b
|
|||
ff8f128fe8
|
|||
1971f1396f
|
|||
bf6c184d12
|
@@ -169,14 +169,32 @@ func (ee *ExErr) ShortLog(evt *zerolog.Event) {
|
|||||||
|
|
||||||
// RecursiveMessage returns the message to show
|
// RecursiveMessage returns the message to show
|
||||||
// = first error (top-down) that is not wrapping/foreign/empty
|
// = first error (top-down) that is not wrapping/foreign/empty
|
||||||
|
// = lowest level error (that is not empty)
|
||||||
|
// = fallback to self.message
|
||||||
func (ee *ExErr) RecursiveMessage() string {
|
func (ee *ExErr) RecursiveMessage() string {
|
||||||
|
|
||||||
|
// ==== [1] ==== first error (top-down) that is not wrapping/foreign/empty
|
||||||
|
|
||||||
for curr := ee; curr != nil; curr = curr.OriginalError {
|
for curr := ee; curr != nil; curr = curr.OriginalError {
|
||||||
if curr.Message != "" && curr.Category != CatWrap && curr.Category != CatForeign {
|
if curr.Message != "" && curr.Category != CatWrap && curr.Category != CatForeign {
|
||||||
return curr.Message
|
return curr.Message
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// fallback to self
|
// ==== [2] ==== lowest level error (that is not empty)
|
||||||
|
|
||||||
|
deepestMsg := ""
|
||||||
|
for curr := ee; curr != nil; curr = curr.OriginalError {
|
||||||
|
if curr.Message != "" {
|
||||||
|
deepestMsg = curr.Message
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if deepestMsg != "" {
|
||||||
|
return deepestMsg
|
||||||
|
}
|
||||||
|
|
||||||
|
// ==== [3] ==== fallback to self.message
|
||||||
|
|
||||||
return ee.Message
|
return ee.Message
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
package goext
|
package goext
|
||||||
|
|
||||||
const GoextVersion = "0.0.309"
|
const GoextVersion = "0.0.315"
|
||||||
|
|
||||||
const GoextVersionTimestamp = "2023-11-09T10:17:29+0100"
|
const GoextVersionTimestamp = "2023-11-12T03:10:55+0100"
|
||||||
|
@@ -5,6 +5,7 @@ import (
|
|||||||
"go.mongodb.org/mongo-driver/bson/bsoncodec"
|
"go.mongodb.org/mongo-driver/bson/bsoncodec"
|
||||||
"go.mongodb.org/mongo-driver/bson/bsontype"
|
"go.mongodb.org/mongo-driver/bson/bsontype"
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
|
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||||
"gogs.mikescher.com/BlackForestBytes/goext/rfctime"
|
"gogs.mikescher.com/BlackForestBytes/goext/rfctime"
|
||||||
"reflect"
|
"reflect"
|
||||||
)
|
)
|
||||||
@@ -18,9 +19,21 @@ func CreateGoExtBsonRegistry() *bsoncodec.Registry {
|
|||||||
rb.RegisterTypeDecoder(reflect.TypeOf(rfctime.RFC3339NanoTime{}), rfctime.RFC3339NanoTime{})
|
rb.RegisterTypeDecoder(reflect.TypeOf(rfctime.RFC3339NanoTime{}), rfctime.RFC3339NanoTime{})
|
||||||
rb.RegisterTypeDecoder(reflect.TypeOf(&rfctime.RFC3339NanoTime{}), rfctime.RFC3339NanoTime{})
|
rb.RegisterTypeDecoder(reflect.TypeOf(&rfctime.RFC3339NanoTime{}), rfctime.RFC3339NanoTime{})
|
||||||
|
|
||||||
|
rb.RegisterTypeDecoder(reflect.TypeOf(rfctime.UnixTime{}), rfctime.UnixTime{})
|
||||||
|
rb.RegisterTypeDecoder(reflect.TypeOf(&rfctime.UnixTime{}), rfctime.UnixTime{})
|
||||||
|
|
||||||
|
rb.RegisterTypeDecoder(reflect.TypeOf(rfctime.UnixMilliTime{}), rfctime.UnixMilliTime{})
|
||||||
|
rb.RegisterTypeDecoder(reflect.TypeOf(&rfctime.UnixMilliTime{}), rfctime.UnixMilliTime{})
|
||||||
|
|
||||||
|
rb.RegisterTypeDecoder(reflect.TypeOf(rfctime.UnixNanoTime{}), rfctime.UnixNanoTime{})
|
||||||
|
rb.RegisterTypeDecoder(reflect.TypeOf(&rfctime.UnixNanoTime{}), rfctime.UnixNanoTime{})
|
||||||
|
|
||||||
rb.RegisterTypeDecoder(reflect.TypeOf(rfctime.Date{}), rfctime.Date{})
|
rb.RegisterTypeDecoder(reflect.TypeOf(rfctime.Date{}), rfctime.Date{})
|
||||||
rb.RegisterTypeDecoder(reflect.TypeOf(&rfctime.Date{}), rfctime.Date{})
|
rb.RegisterTypeDecoder(reflect.TypeOf(&rfctime.Date{}), rfctime.Date{})
|
||||||
|
|
||||||
|
rb.RegisterTypeDecoder(reflect.TypeOf(rfctime.SecondsF64(0)), rfctime.SecondsF64(0))
|
||||||
|
rb.RegisterTypeDecoder(reflect.TypeOf(langext.Ptr(rfctime.SecondsF64(0))), rfctime.SecondsF64(0))
|
||||||
|
|
||||||
bsoncodec.DefaultValueEncoders{}.RegisterDefaultEncoders(rb)
|
bsoncodec.DefaultValueEncoders{}.RegisterDefaultEncoders(rb)
|
||||||
bsoncodec.DefaultValueDecoders{}.RegisterDefaultDecoders(rb)
|
bsoncodec.DefaultValueDecoders{}.RegisterDefaultDecoders(rb)
|
||||||
|
|
||||||
|
@@ -2,7 +2,14 @@ package rfctime
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
|
"go.mongodb.org/mongo-driver/bson/bsoncodec"
|
||||||
|
"go.mongodb.org/mongo-driver/bson/bsonrw"
|
||||||
|
"go.mongodb.org/mongo-driver/bson/bsontype"
|
||||||
"gogs.mikescher.com/BlackForestBytes/goext/timeext"
|
"gogs.mikescher.com/BlackForestBytes/goext/timeext"
|
||||||
|
"reflect"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -54,6 +61,63 @@ func (d SecondsF64) MarshalJSON() ([]byte, error) {
|
|||||||
return json.Marshal(secs)
|
return json.Marshal(secs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *SecondsF64) UnmarshalBSONValue(bt bsontype.Type, data []byte) error {
|
||||||
|
if bt == bson.TypeNull {
|
||||||
|
// we can't set nil in UnmarshalBSONValue (so we use default(struct))
|
||||||
|
// Use mongoext.CreateGoExtBsonRegistry if you need to unmarsh pointer values
|
||||||
|
// https://stackoverflow.com/questions/75167597
|
||||||
|
// https://jira.mongodb.org/browse/GODRIVER-2252
|
||||||
|
*d = SecondsF64(0)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if bt != bson.TypeDouble {
|
||||||
|
return errors.New(fmt.Sprintf("cannot unmarshal %v into SecondsF64", bt))
|
||||||
|
}
|
||||||
|
var tt float64
|
||||||
|
err := bson.RawValue{Type: bt, Value: data}.Unmarshal(&tt)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
*d = SecondsF64(tt)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d SecondsF64) MarshalBSONValue() (bsontype.Type, []byte, error) {
|
||||||
|
return bson.MarshalValue(d.Seconds())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d SecondsF64) DecodeValue(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
|
||||||
|
if val.Kind() == reflect.Ptr && val.IsNil() {
|
||||||
|
if !val.CanSet() {
|
||||||
|
return errors.New("ValueUnmarshalerDecodeValue")
|
||||||
|
}
|
||||||
|
val.Set(reflect.New(val.Type().Elem()))
|
||||||
|
}
|
||||||
|
|
||||||
|
tp, src, err := bsonrw.Copier{}.CopyValueToBytes(vr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if val.Kind() == reflect.Ptr && len(src) == 0 {
|
||||||
|
val.Set(reflect.Zero(val.Type()))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
err = d.UnmarshalBSONValue(tp, src)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if val.Kind() == reflect.Ptr {
|
||||||
|
val.Set(reflect.ValueOf(&d))
|
||||||
|
} else {
|
||||||
|
val.Set(reflect.ValueOf(d))
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func NewSecondsF64(t time.Duration) SecondsF64 {
|
func NewSecondsF64(t time.Duration) SecondsF64 {
|
||||||
return SecondsF64(t)
|
return SecondsF64(t)
|
||||||
}
|
}
|
||||||
|
@@ -2,6 +2,13 @@ package rfctime
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
|
"go.mongodb.org/mongo-driver/bson/bsoncodec"
|
||||||
|
"go.mongodb.org/mongo-driver/bson/bsonrw"
|
||||||
|
"go.mongodb.org/mongo-driver/bson/bsontype"
|
||||||
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -59,6 +66,63 @@ func (t *UnixTime) UnmarshalText(data []byte) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *UnixTime) UnmarshalBSONValue(bt bsontype.Type, data []byte) error {
|
||||||
|
if bt == bson.TypeNull {
|
||||||
|
// we can't set nil in UnmarshalBSONValue (so we use default(struct))
|
||||||
|
// Use mongoext.CreateGoExtBsonRegistry if you need to unmarsh pointer values
|
||||||
|
// https://stackoverflow.com/questions/75167597
|
||||||
|
// https://jira.mongodb.org/browse/GODRIVER-2252
|
||||||
|
*t = UnixTime{}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if bt != bson.TypeDateTime {
|
||||||
|
return errors.New(fmt.Sprintf("cannot unmarshal %v into UnixTime", bt))
|
||||||
|
}
|
||||||
|
var tt time.Time
|
||||||
|
err := bson.RawValue{Type: bt, Value: data}.Unmarshal(&tt)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
*t = UnixTime(tt)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t UnixTime) MarshalBSONValue() (bsontype.Type, []byte, error) {
|
||||||
|
return bson.MarshalValue(time.Time(t))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t UnixTime) DecodeValue(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
|
||||||
|
if val.Kind() == reflect.Ptr && val.IsNil() {
|
||||||
|
if !val.CanSet() {
|
||||||
|
return errors.New("ValueUnmarshalerDecodeValue")
|
||||||
|
}
|
||||||
|
val.Set(reflect.New(val.Type().Elem()))
|
||||||
|
}
|
||||||
|
|
||||||
|
tp, src, err := bsonrw.Copier{}.CopyValueToBytes(vr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if val.Kind() == reflect.Ptr && len(src) == 0 {
|
||||||
|
val.Set(reflect.Zero(val.Type()))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
err = t.UnmarshalBSONValue(tp, src)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if val.Kind() == reflect.Ptr {
|
||||||
|
val.Set(reflect.ValueOf(&t))
|
||||||
|
} else {
|
||||||
|
val.Set(reflect.ValueOf(t))
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (t UnixTime) Serialize() string {
|
func (t UnixTime) Serialize() string {
|
||||||
return strconv.FormatInt(t.Time().Unix(), 10)
|
return strconv.FormatInt(t.Time().Unix(), 10)
|
||||||
}
|
}
|
||||||
|
@@ -2,6 +2,13 @@ package rfctime
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
|
"go.mongodb.org/mongo-driver/bson/bsoncodec"
|
||||||
|
"go.mongodb.org/mongo-driver/bson/bsonrw"
|
||||||
|
"go.mongodb.org/mongo-driver/bson/bsontype"
|
||||||
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -59,6 +66,63 @@ func (t *UnixMilliTime) UnmarshalText(data []byte) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *UnixMilliTime) UnmarshalBSONValue(bt bsontype.Type, data []byte) error {
|
||||||
|
if bt == bson.TypeNull {
|
||||||
|
// we can't set nil in UnmarshalBSONValue (so we use default(struct))
|
||||||
|
// Use mongoext.CreateGoExtBsonRegistry if you need to unmarsh pointer values
|
||||||
|
// https://stackoverflow.com/questions/75167597
|
||||||
|
// https://jira.mongodb.org/browse/GODRIVER-2252
|
||||||
|
*t = UnixMilliTime{}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if bt != bson.TypeDateTime {
|
||||||
|
return errors.New(fmt.Sprintf("cannot unmarshal %v into UnixMilliTime", bt))
|
||||||
|
}
|
||||||
|
var tt time.Time
|
||||||
|
err := bson.RawValue{Type: bt, Value: data}.Unmarshal(&tt)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
*t = UnixMilliTime(tt)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t UnixMilliTime) MarshalBSONValue() (bsontype.Type, []byte, error) {
|
||||||
|
return bson.MarshalValue(time.Time(t))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t UnixMilliTime) DecodeValue(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
|
||||||
|
if val.Kind() == reflect.Ptr && val.IsNil() {
|
||||||
|
if !val.CanSet() {
|
||||||
|
return errors.New("ValueUnmarshalerDecodeValue")
|
||||||
|
}
|
||||||
|
val.Set(reflect.New(val.Type().Elem()))
|
||||||
|
}
|
||||||
|
|
||||||
|
tp, src, err := bsonrw.Copier{}.CopyValueToBytes(vr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if val.Kind() == reflect.Ptr && len(src) == 0 {
|
||||||
|
val.Set(reflect.Zero(val.Type()))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
err = t.UnmarshalBSONValue(tp, src)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if val.Kind() == reflect.Ptr {
|
||||||
|
val.Set(reflect.ValueOf(&t))
|
||||||
|
} else {
|
||||||
|
val.Set(reflect.ValueOf(t))
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (t UnixMilliTime) Serialize() string {
|
func (t UnixMilliTime) Serialize() string {
|
||||||
return strconv.FormatInt(t.Time().UnixMilli(), 10)
|
return strconv.FormatInt(t.Time().UnixMilli(), 10)
|
||||||
}
|
}
|
||||||
|
@@ -2,6 +2,13 @@ package rfctime
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
|
"go.mongodb.org/mongo-driver/bson/bsoncodec"
|
||||||
|
"go.mongodb.org/mongo-driver/bson/bsonrw"
|
||||||
|
"go.mongodb.org/mongo-driver/bson/bsontype"
|
||||||
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -59,6 +66,63 @@ func (t *UnixNanoTime) UnmarshalText(data []byte) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *UnixNanoTime) UnmarshalBSONValue(bt bsontype.Type, data []byte) error {
|
||||||
|
if bt == bson.TypeNull {
|
||||||
|
// we can't set nil in UnmarshalBSONValue (so we use default(struct))
|
||||||
|
// Use mongoext.CreateGoExtBsonRegistry if you need to unmarsh pointer values
|
||||||
|
// https://stackoverflow.com/questions/75167597
|
||||||
|
// https://jira.mongodb.org/browse/GODRIVER-2252
|
||||||
|
*t = UnixNanoTime{}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if bt != bson.TypeDateTime {
|
||||||
|
return errors.New(fmt.Sprintf("cannot unmarshal %v into UnixNanoTime", bt))
|
||||||
|
}
|
||||||
|
var tt time.Time
|
||||||
|
err := bson.RawValue{Type: bt, Value: data}.Unmarshal(&tt)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
*t = UnixNanoTime(tt)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t UnixNanoTime) MarshalBSONValue() (bsontype.Type, []byte, error) {
|
||||||
|
return bson.MarshalValue(time.Time(t))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t UnixNanoTime) DecodeValue(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
|
||||||
|
if val.Kind() == reflect.Ptr && val.IsNil() {
|
||||||
|
if !val.CanSet() {
|
||||||
|
return errors.New("ValueUnmarshalerDecodeValue")
|
||||||
|
}
|
||||||
|
val.Set(reflect.New(val.Type().Elem()))
|
||||||
|
}
|
||||||
|
|
||||||
|
tp, src, err := bsonrw.Copier{}.CopyValueToBytes(vr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if val.Kind() == reflect.Ptr && len(src) == 0 {
|
||||||
|
val.Set(reflect.Zero(val.Type()))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
err = t.UnmarshalBSONValue(tp, src)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if val.Kind() == reflect.Ptr {
|
||||||
|
val.Set(reflect.ValueOf(&t))
|
||||||
|
} else {
|
||||||
|
val.Set(reflect.ValueOf(t))
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (t UnixNanoTime) Serialize() string {
|
func (t UnixNanoTime) Serialize() string {
|
||||||
return strconv.FormatInt(t.Time().UnixNano(), 10)
|
return strconv.FormatInt(t.Time().UnixNano(), 10)
|
||||||
}
|
}
|
||||||
|
@@ -1,33 +1,28 @@
|
|||||||
package syncext
|
package syncext
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type AtomicBool struct {
|
type Atomic[T any] struct {
|
||||||
v bool
|
v T
|
||||||
listener map[string]chan bool
|
lock sync.RWMutex
|
||||||
lock sync.Mutex
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAtomicBool(value bool) *AtomicBool {
|
func NewAtomic[T any](value T) *Atomic[T] {
|
||||||
return &AtomicBool{
|
return &Atomic[T]{
|
||||||
v: value,
|
v: value,
|
||||||
listener: make(map[string]chan bool),
|
lock: sync.RWMutex{},
|
||||||
lock: sync.Mutex{},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *AtomicBool) Get() bool {
|
func (a *Atomic[T]) Get() T {
|
||||||
a.lock.Lock()
|
a.lock.RLock()
|
||||||
defer a.lock.Unlock()
|
defer a.lock.RUnlock()
|
||||||
return a.v
|
return a.v
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *AtomicBool) Set(value bool) bool {
|
func (a *Atomic[T]) Set(value T) T {
|
||||||
a.lock.Lock()
|
a.lock.Lock()
|
||||||
defer a.lock.Unlock()
|
defer a.lock.Unlock()
|
||||||
|
|
||||||
@@ -35,79 +30,5 @@ func (a *AtomicBool) Set(value bool) bool {
|
|||||||
|
|
||||||
a.v = value
|
a.v = value
|
||||||
|
|
||||||
for k, v := range a.listener {
|
|
||||||
select {
|
|
||||||
case v <- value:
|
|
||||||
// message sent
|
|
||||||
default:
|
|
||||||
// no receiver on channel
|
|
||||||
delete(a.listener, k)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return oldValue
|
return oldValue
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *AtomicBool) Wait(waitFor bool) {
|
|
||||||
_ = a.WaitWithContext(context.Background(), waitFor)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *AtomicBool) WaitWithTimeout(timeout time.Duration, waitFor bool) error {
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
|
||||||
defer cancel()
|
|
||||||
return a.WaitWithContext(ctx, waitFor)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *AtomicBool) WaitWithContext(ctx context.Context, waitFor bool) error {
|
|
||||||
if err := ctx.Err(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if a.Get() == waitFor {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
uuid, _ := langext.NewHexUUID()
|
|
||||||
|
|
||||||
waitchan := make(chan bool)
|
|
||||||
|
|
||||||
a.lock.Lock()
|
|
||||||
a.listener[uuid] = waitchan
|
|
||||||
a.lock.Unlock()
|
|
||||||
defer func() {
|
|
||||||
a.lock.Lock()
|
|
||||||
delete(a.listener, uuid)
|
|
||||||
a.lock.Unlock()
|
|
||||||
}()
|
|
||||||
|
|
||||||
for {
|
|
||||||
if err := ctx.Err(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
timeOut := 1024 * time.Millisecond
|
|
||||||
|
|
||||||
if dl, ok := ctx.Deadline(); ok {
|
|
||||||
timeOutMax := dl.Sub(time.Now())
|
|
||||||
if timeOutMax <= 0 {
|
|
||||||
timeOut = 0
|
|
||||||
} else if 0 < timeOutMax && timeOutMax < timeOut {
|
|
||||||
timeOut = timeOutMax
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if v, ok := ReadChannelWithTimeout(waitchan, timeOut); ok {
|
|
||||||
if v == waitFor {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if err := ctx.Err(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if a.Get() == waitFor {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
113
syncext/bool.go
Normal file
113
syncext/bool.go
Normal file
@@ -0,0 +1,113 @@
|
|||||||
|
package syncext
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type AtomicBool struct {
|
||||||
|
v bool
|
||||||
|
listener map[string]chan bool
|
||||||
|
lock sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewAtomicBool(value bool) *AtomicBool {
|
||||||
|
return &AtomicBool{
|
||||||
|
v: value,
|
||||||
|
listener: make(map[string]chan bool),
|
||||||
|
lock: sync.Mutex{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *AtomicBool) Get() bool {
|
||||||
|
a.lock.Lock()
|
||||||
|
defer a.lock.Unlock()
|
||||||
|
return a.v
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *AtomicBool) Set(value bool) bool {
|
||||||
|
a.lock.Lock()
|
||||||
|
defer a.lock.Unlock()
|
||||||
|
|
||||||
|
oldValue := a.v
|
||||||
|
|
||||||
|
a.v = value
|
||||||
|
|
||||||
|
for k, v := range a.listener {
|
||||||
|
select {
|
||||||
|
case v <- value:
|
||||||
|
// message sent
|
||||||
|
default:
|
||||||
|
// no receiver on channel
|
||||||
|
delete(a.listener, k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return oldValue
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *AtomicBool) Wait(waitFor bool) {
|
||||||
|
_ = a.WaitWithContext(context.Background(), waitFor)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *AtomicBool) WaitWithTimeout(timeout time.Duration, waitFor bool) error {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||||
|
defer cancel()
|
||||||
|
return a.WaitWithContext(ctx, waitFor)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *AtomicBool) WaitWithContext(ctx context.Context, waitFor bool) error {
|
||||||
|
if err := ctx.Err(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if a.Get() == waitFor {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
uuid, _ := langext.NewHexUUID()
|
||||||
|
|
||||||
|
waitchan := make(chan bool)
|
||||||
|
|
||||||
|
a.lock.Lock()
|
||||||
|
a.listener[uuid] = waitchan
|
||||||
|
a.lock.Unlock()
|
||||||
|
defer func() {
|
||||||
|
a.lock.Lock()
|
||||||
|
delete(a.listener, uuid)
|
||||||
|
a.lock.Unlock()
|
||||||
|
}()
|
||||||
|
|
||||||
|
for {
|
||||||
|
if err := ctx.Err(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
timeOut := 1024 * time.Millisecond
|
||||||
|
|
||||||
|
if dl, ok := ctx.Deadline(); ok {
|
||||||
|
timeOutMax := dl.Sub(time.Now())
|
||||||
|
if timeOutMax <= 0 {
|
||||||
|
timeOut = 0
|
||||||
|
} else if 0 < timeOutMax && timeOutMax < timeOut {
|
||||||
|
timeOut = timeOutMax
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if v, ok := ReadChannelWithTimeout(waitchan, timeOut); ok {
|
||||||
|
if v == waitFor {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if err := ctx.Err(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if a.Get() == waitFor {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -40,7 +40,7 @@ func (c *Coll[TData]) Paginate(ctx context.Context, filter pag.Filter, page int,
|
|||||||
}
|
}
|
||||||
|
|
||||||
pipelineCount := mongo.Pipeline{}
|
pipelineCount := mongo.Pipeline{}
|
||||||
pipelinePaginate = append(pipelinePaginate, bson.D{{Key: "$count", Value: "count"}})
|
pipelineCount = append(pipelineCount, bson.D{{Key: "$count", Value: "count"}})
|
||||||
|
|
||||||
pipelineList := langext.ArrConcat(mongo.Pipeline{}, pipelineFilter, pipelinePaginate, c.extraModPipeline)
|
pipelineList := langext.ArrConcat(mongo.Pipeline{}, pipelineFilter, pipelinePaginate, c.extraModPipeline)
|
||||||
pipelineTotalCount := langext.ArrConcat(mongo.Pipeline{}, pipelineFilter, pipelineCount)
|
pipelineTotalCount := langext.ArrConcat(mongo.Pipeline{}, pipelineFilter, pipelineCount)
|
||||||
|
Reference in New Issue
Block a user