Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
36b71dfaf3
|
|||
9491b72b8d
|
|||
6c4af4006b
|
|||
8bf3a337cf
|
|||
16146494dc
|
|||
b0e443ad99
|
|||
9955eacf96
|
|||
f0347a9435
|
|||
7c869c65f3
|
|||
14f39a9162
|
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
|
||||||
|
}
|
@@ -194,3 +194,9 @@ func (w *GinWrapper) ServeHTTP(req *http.Request) *httptest.ResponseRecorder {
|
|||||||
w.engine.ServeHTTP(respRec, req)
|
w.engine.ServeHTTP(respRec, req)
|
||||||
return respRec
|
return respRec
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ForwardRequest manually inserts a request into this router
|
||||||
|
// = behaves as if the request came from the outside (and writes the response to `writer`)
|
||||||
|
func (w *GinWrapper) ForwardRequest(writer http.ResponseWriter, req *http.Request) {
|
||||||
|
w.engine.ServeHTTP(writer, req)
|
||||||
|
}
|
||||||
|
4
go.mod
4
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
|
||||||
@@ -36,7 +36,7 @@ require (
|
|||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||||
github.com/montanaflynn/stats v0.7.1 // indirect
|
github.com/montanaflynn/stats v0.7.1 // indirect
|
||||||
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
|
github.com/pelletier/go-toml/v2 v2.2.0 // indirect
|
||||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||||
github.com/ugorji/go/codec v1.2.12 // indirect
|
github.com/ugorji/go/codec v1.2.12 // indirect
|
||||||
|
5
go.sum
5
go.sum
@@ -104,6 +104,8 @@ github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8
|
|||||||
github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
|
github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
|
||||||
github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI=
|
github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI=
|
||||||
github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
|
github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
|
||||||
|
github.com/pelletier/go-toml/v2 v2.2.0 h1:QLgLl2yMN7N+ruc31VynXs1vhMZa7CeHHejIeBAsoHo=
|
||||||
|
github.com/pelletier/go-toml/v2 v2.2.0/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
|
||||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
@@ -118,6 +120,7 @@ github.com/rs/zerolog v1.32.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWR
|
|||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||||
|
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
@@ -126,6 +129,8 @@ github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o
|
|||||||
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||||
|
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||||
|
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
||||||
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
||||||
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
|
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
package goext
|
package goext
|
||||||
|
|
||||||
const GoextVersion = "0.0.415"
|
const GoextVersion = "0.0.425"
|
||||||
|
|
||||||
const GoextVersionTimestamp = "2024-03-18T10:42:00+0100"
|
const GoextVersionTimestamp = "2024-03-30T14:24:53+0100"
|
||||||
|
@@ -453,6 +453,15 @@ func ArrConcat[T any](arr ...[]T) []T {
|
|||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ArrAppend works similar to append(x, y, z) - but doe snot touch the old array and creates a new one
|
||||||
|
func ArrAppend[T any](arr []T, add ...T) []T {
|
||||||
|
r := ArrCopy(arr)
|
||||||
|
for _, v := range add {
|
||||||
|
r = append(r, v)
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
// ArrCopy does a shallow copy of the 'in' array
|
// ArrCopy does a shallow copy of the 'in' array
|
||||||
func ArrCopy[T any](in []T) []T {
|
func ArrCopy[T any](in []T) []T {
|
||||||
out := make([]T, len(in))
|
out := make([]T, len(in))
|
||||||
|
@@ -1,19 +1,30 @@
|
|||||||
package reflectext
|
package reflectext
|
||||||
|
|
||||||
import "reflect"
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"reflect"
|
||||||
|
)
|
||||||
|
|
||||||
func ConvertStructToMap(v any) any {
|
type ConvertStructToMapOpt struct {
|
||||||
return reflectToMap(reflect.ValueOf(v))
|
KeepJsonMarshalTypes bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func reflectToMap(fv reflect.Value) any {
|
func ConvertStructToMap(v any, opts ...ConvertStructToMapOpt) any {
|
||||||
|
opt := ConvertStructToMapOpt{}
|
||||||
|
if len(opts) > 0 {
|
||||||
|
opt = opts[0]
|
||||||
|
}
|
||||||
|
return reflectToMap(reflect.ValueOf(v), opt)
|
||||||
|
}
|
||||||
|
|
||||||
|
func reflectToMap(fv reflect.Value, opt ConvertStructToMapOpt) any {
|
||||||
|
|
||||||
if fv.Kind() == reflect.Ptr {
|
if fv.Kind() == reflect.Ptr {
|
||||||
|
|
||||||
if fv.IsNil() {
|
if fv.IsNil() {
|
||||||
return nil
|
return nil
|
||||||
} else {
|
} else {
|
||||||
return reflectToMap(fv.Elem())
|
return reflectToMap(fv.Elem(), opt)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -30,7 +41,7 @@ func reflectToMap(fv reflect.Value) any {
|
|||||||
arrlen := fv.Len()
|
arrlen := fv.Len()
|
||||||
arr := make([]any, arrlen)
|
arr := make([]any, arrlen)
|
||||||
for i := 0; i < arrlen; i++ {
|
for i := 0; i < arrlen; i++ {
|
||||||
arr[i] = reflectToMap(fv.Index(i))
|
arr[i] = reflectToMap(fv.Index(i), opt)
|
||||||
}
|
}
|
||||||
return arr
|
return arr
|
||||||
|
|
||||||
@@ -41,7 +52,7 @@ func reflectToMap(fv reflect.Value) any {
|
|||||||
arrlen := fv.Len()
|
arrlen := fv.Len()
|
||||||
arr := make([]any, arrlen)
|
arr := make([]any, arrlen)
|
||||||
for i := 0; i < arrlen; i++ {
|
for i := 0; i < arrlen; i++ {
|
||||||
arr[i] = reflectToMap(fv.Index(i))
|
arr[i] = reflectToMap(fv.Index(i), opt)
|
||||||
}
|
}
|
||||||
return arr
|
return arr
|
||||||
|
|
||||||
@@ -56,11 +67,15 @@ func reflectToMap(fv reflect.Value) any {
|
|||||||
|
|
||||||
if fv.Kind() == reflect.Struct {
|
if fv.Kind() == reflect.Struct {
|
||||||
|
|
||||||
|
if opt.KeepJsonMarshalTypes && fv.Type().Implements(reflect.TypeFor[json.Marshaler]()) {
|
||||||
|
return fv.Interface()
|
||||||
|
}
|
||||||
|
|
||||||
res := make(map[string]any)
|
res := make(map[string]any)
|
||||||
|
|
||||||
for i := 0; i < fv.NumField(); i++ {
|
for i := 0; i < fv.NumField(); i++ {
|
||||||
if fv.Type().Field(i).IsExported() {
|
if fv.Type().Field(i).IsExported() {
|
||||||
res[fv.Type().Field(i).Name] = reflectToMap(fv.Field(i))
|
res[fv.Type().Field(i).Name] = reflectToMap(fv.Field(i), opt)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -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