Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
6c4af4006b
|
|||
8bf3a337cf
|
|||
16146494dc
|
|||
b0e443ad99
|
|||
9955eacf96
|
|||
f0347a9435
|
58
dataext/optional.go
Normal file
58
dataext/optional.go
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
package dataext
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
type JsonOpt[T any] struct {
|
||||||
|
isSet bool
|
||||||
|
value T
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalJSON returns m as the JSON encoding of m.
|
||||||
|
func (m JsonOpt[T]) MarshalJSON() ([]byte, error) {
|
||||||
|
if !m.isSet {
|
||||||
|
return []byte("null"), nil // actually this would be undefined - but undefined is not valid JSON
|
||||||
|
}
|
||||||
|
|
||||||
|
return json.Marshal(m.value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalJSON sets *m to a copy of data.
|
||||||
|
func (m *JsonOpt[T]) UnmarshalJSON(data []byte) error {
|
||||||
|
if m == nil {
|
||||||
|
return errors.New("JsonOpt: UnmarshalJSON on nil pointer")
|
||||||
|
}
|
||||||
|
|
||||||
|
return json.Unmarshal(data, &m.value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m JsonOpt[T]) IsSet() bool {
|
||||||
|
return m.isSet
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m JsonOpt[T]) IsUnset() bool {
|
||||||
|
return !m.isSet
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m JsonOpt[T]) Value() (T, bool) {
|
||||||
|
if !m.isSet {
|
||||||
|
return *new(T), false
|
||||||
|
}
|
||||||
|
return m.value, true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m JsonOpt[T]) ValueOrNil() *T {
|
||||||
|
if !m.isSet {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return &m.value
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m JsonOpt[T]) MustValue() T {
|
||||||
|
if !m.isSet {
|
||||||
|
panic("value not set")
|
||||||
|
}
|
||||||
|
return m.value
|
||||||
|
}
|
2
go.mod
2
go.mod
@@ -1,6 +1,6 @@
|
|||||||
module gogs.mikescher.com/BlackForestBytes/goext
|
module gogs.mikescher.com/BlackForestBytes/goext
|
||||||
|
|
||||||
go 1.21
|
go 1.22
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gin-gonic/gin v1.9.1
|
github.com/gin-gonic/gin v1.9.1
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
package goext
|
package goext
|
||||||
|
|
||||||
const GoextVersion = "0.0.417"
|
const GoextVersion = "0.0.423"
|
||||||
|
|
||||||
const GoextVersionTimestamp = "2024-03-20T08:58:59+0100"
|
const GoextVersionTimestamp = "2024-03-24T15:25:52+0100"
|
||||||
|
@@ -171,7 +171,7 @@ func createPaginationPipeline[TData any](coll *Coll[TData], token ct.CursorToken
|
|||||||
bson.M{*fieldSecondary: bson.M{"$gt": valueSecondary}},
|
bson.M{*fieldSecondary: bson.M{"$gt": valueSecondary}},
|
||||||
}})
|
}})
|
||||||
|
|
||||||
sort = append(sort, bson.E{Key: fieldPrimary, Value: +1})
|
sort = append(sort, bson.E{Key: *fieldSecondary, Value: +1})
|
||||||
|
|
||||||
} else if *sortSecondary == ct.SortDESC {
|
} else if *sortSecondary == ct.SortDESC {
|
||||||
|
|
||||||
@@ -181,7 +181,7 @@ func createPaginationPipeline[TData any](coll *Coll[TData], token ct.CursorToken
|
|||||||
bson.M{*fieldSecondary: bson.M{"$lt": valueSecondary}},
|
bson.M{*fieldSecondary: bson.M{"$lt": valueSecondary}},
|
||||||
}})
|
}})
|
||||||
|
|
||||||
sort = append(sort, bson.E{Key: fieldPrimary, Value: -1})
|
sort = append(sort, bson.E{Key: *fieldSecondary, Value: -1})
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user