Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
9491b72b8d
|
|||
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.424"
|
||||||
|
|
||||||
const GoextVersionTimestamp = "2024-03-20T08:58:59+0100"
|
const GoextVersionTimestamp = "2024-03-30T03:01:55+0100"
|
||||||
|
@@ -146,3 +146,37 @@ func UnixFloatSeconds(v float64) time.Time {
|
|||||||
func FloorTime(t time.Time) time.Time {
|
func FloorTime(t time.Time) time.Time {
|
||||||
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
|
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SubtractYears(t time.Time, yearCount float64, tz *time.Location) time.Time {
|
||||||
|
t = t.In(tz)
|
||||||
|
|
||||||
|
if yearCount < 0 {
|
||||||
|
return AddYears(t, -yearCount, tz)
|
||||||
|
}
|
||||||
|
|
||||||
|
intCount, floatCount := math.Modf(yearCount)
|
||||||
|
|
||||||
|
t.AddDate(-int(intCount), 0, 0)
|
||||||
|
|
||||||
|
t0 := TimeToYearStart(t, tz)
|
||||||
|
t1 := TimeToYearEnd(t, tz)
|
||||||
|
|
||||||
|
return t.Add(time.Duration(float64(t1.Sub(t0)) * floatCount * -1))
|
||||||
|
}
|
||||||
|
|
||||||
|
func AddYears(t time.Time, yearCount float64, tz *time.Location) time.Time {
|
||||||
|
t = t.In(tz)
|
||||||
|
|
||||||
|
if yearCount < 0 {
|
||||||
|
return SubtractYears(t, -yearCount, tz)
|
||||||
|
}
|
||||||
|
|
||||||
|
intCount, floatCount := math.Modf(yearCount)
|
||||||
|
|
||||||
|
t.AddDate(int(intCount), 0, 0)
|
||||||
|
|
||||||
|
t0 := TimeToYearStart(t, tz)
|
||||||
|
t1 := TimeToYearEnd(t, tz)
|
||||||
|
|
||||||
|
return t.Add(time.Duration(float64(t1.Sub(t0)) * floatCount))
|
||||||
|
}
|
||||||
|
@@ -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