Compare commits
13 Commits
Author | SHA1 | Date | |
---|---|---|---|
6602f86b43
|
|||
24d9f0fdc7
|
|||
8446b2da22
|
|||
758e5a67b5
|
|||
678ddd7124
|
|||
36b71dfaf3
|
|||
9491b72b8d
|
|||
6c4af4006b
|
|||
8bf3a337cf
|
|||
16146494dc
|
|||
b0e443ad99
|
|||
9955eacf96
|
|||
f0347a9435
|
59
dataext/optional.go
Normal file
59
dataext/optional.go
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
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")
|
||||||
|
}
|
||||||
|
|
||||||
|
m.isSet = true
|
||||||
|
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
|
||||||
|
}
|
14
go.mod
14
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
|
||||||
@@ -9,9 +9,9 @@ require (
|
|||||||
github.com/rs/xid v1.5.0
|
github.com/rs/xid v1.5.0
|
||||||
github.com/rs/zerolog v1.32.0
|
github.com/rs/zerolog v1.32.0
|
||||||
go.mongodb.org/mongo-driver v1.14.0
|
go.mongodb.org/mongo-driver v1.14.0
|
||||||
golang.org/x/crypto v0.21.0
|
golang.org/x/crypto v0.22.0
|
||||||
golang.org/x/sys v0.18.0
|
golang.org/x/sys v0.19.0
|
||||||
golang.org/x/term v0.18.0
|
golang.org/x/term v0.19.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
@@ -28,7 +28,7 @@ require (
|
|||||||
github.com/golang/snappy v0.0.4 // indirect
|
github.com/golang/snappy v0.0.4 // indirect
|
||||||
github.com/google/uuid v1.5.0 // indirect
|
github.com/google/uuid v1.5.0 // indirect
|
||||||
github.com/json-iterator/go v1.1.12 // indirect
|
github.com/json-iterator/go v1.1.12 // indirect
|
||||||
github.com/klauspost/compress v1.17.7 // indirect
|
github.com/klauspost/compress v1.17.8 // indirect
|
||||||
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
|
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
|
||||||
github.com/leodido/go-urn v1.4.0 // indirect
|
github.com/leodido/go-urn v1.4.0 // indirect
|
||||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||||
@@ -45,8 +45,8 @@ require (
|
|||||||
github.com/xdg-go/stringprep v1.0.4 // indirect
|
github.com/xdg-go/stringprep v1.0.4 // indirect
|
||||||
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
|
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
|
||||||
golang.org/x/arch v0.7.0 // indirect
|
golang.org/x/arch v0.7.0 // indirect
|
||||||
golang.org/x/net v0.22.0 // indirect
|
golang.org/x/net v0.24.0 // indirect
|
||||||
golang.org/x/sync v0.6.0 // indirect
|
golang.org/x/sync v0.7.0 // indirect
|
||||||
golang.org/x/text v0.14.0 // indirect
|
golang.org/x/text v0.14.0 // indirect
|
||||||
google.golang.org/protobuf v1.33.0 // indirect
|
google.golang.org/protobuf v1.33.0 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
|
12
go.sum
12
go.sum
@@ -73,6 +73,8 @@ github.com/klauspost/compress v1.17.6 h1:60eq2E/jlfwQXtvZEeBUYADs+BwKBWURIY+Gj2e
|
|||||||
github.com/klauspost/compress v1.17.6/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM=
|
github.com/klauspost/compress v1.17.6/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM=
|
||||||
github.com/klauspost/compress v1.17.7 h1:ehO88t2UGzQK66LMdE8tibEd1ErmzZjNEqWkjLAKQQg=
|
github.com/klauspost/compress v1.17.7 h1:ehO88t2UGzQK66LMdE8tibEd1ErmzZjNEqWkjLAKQQg=
|
||||||
github.com/klauspost/compress v1.17.7/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
|
github.com/klauspost/compress v1.17.7/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
|
||||||
|
github.com/klauspost/compress v1.17.8 h1:YcnTYrq7MikUT7k0Yb5eceMmALQPYBW/Xltxn0NAMnU=
|
||||||
|
github.com/klauspost/compress v1.17.8/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
|
||||||
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||||
github.com/klauspost/cpuid/v2 v2.2.6 h1:ndNyv040zDGIDh8thGkXYjnFtiN02M1PVVF+JE/48xc=
|
github.com/klauspost/cpuid/v2 v2.2.6 h1:ndNyv040zDGIDh8thGkXYjnFtiN02M1PVVF+JE/48xc=
|
||||||
github.com/klauspost/cpuid/v2 v2.2.6/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
|
github.com/klauspost/cpuid/v2 v2.2.6/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
|
||||||
@@ -166,6 +168,8 @@ golang.org/x/crypto v0.20.0 h1:jmAMJJZXr5KiCw05dfYK9QnqaqKLYXijU23lsEdcQqg=
|
|||||||
golang.org/x/crypto v0.20.0/go.mod h1:Xwo95rrVNIoSMx9wa1JroENMToLWn3RNVrTBpLHgZPQ=
|
golang.org/x/crypto v0.20.0/go.mod h1:Xwo95rrVNIoSMx9wa1JroENMToLWn3RNVrTBpLHgZPQ=
|
||||||
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
|
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
|
||||||
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
|
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
|
||||||
|
golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30=
|
||||||
|
golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
|
||||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
@@ -180,10 +184,14 @@ golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
|
|||||||
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
|
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
|
||||||
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
|
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
|
||||||
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
|
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
|
||||||
|
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
|
||||||
|
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
|
||||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
|
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
|
||||||
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||||
|
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
|
||||||
|
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
@@ -201,6 +209,8 @@ golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
|
|||||||
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
|
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
|
||||||
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
|
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
|
||||||
|
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||||
golang.org/x/term v0.16.0 h1:m+B6fahuftsE9qjo0VWp2FW0mB3MTJvR0BaMQrq0pmE=
|
golang.org/x/term v0.16.0 h1:m+B6fahuftsE9qjo0VWp2FW0mB3MTJvR0BaMQrq0pmE=
|
||||||
@@ -209,6 +219,8 @@ golang.org/x/term v0.17.0 h1:mkTF7LCd6WGJNL3K1Ad7kwxNfYAW6a8a8QqtMblp/4U=
|
|||||||
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
|
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
|
||||||
golang.org/x/term v0.18.0 h1:FcHjZXDMxI8mM3nwhX9HlKop4C0YQvCVCdwYl2wOtE8=
|
golang.org/x/term v0.18.0 h1:FcHjZXDMxI8mM3nwhX9HlKop4C0YQvCVCdwYl2wOtE8=
|
||||||
golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58=
|
golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58=
|
||||||
|
golang.org/x/term v0.19.0 h1:+ThwsDv+tYfnJFhF4L8jITxu1tdTWRTZpdsWgEgjL6Q=
|
||||||
|
golang.org/x/term v0.19.0/go.mod h1:2CuTdWZ7KHSQwUzKva0cbMg6q2DMI3Mmxp+gKJbskEk=
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
package goext
|
package goext
|
||||||
|
|
||||||
const GoextVersion = "0.0.417"
|
const GoextVersion = "0.0.430"
|
||||||
|
|
||||||
const GoextVersionTimestamp = "2024-03-20T08:58:59+0100"
|
const GoextVersionTimestamp = "2024-04-10T15:27:41+0200"
|
||||||
|
@@ -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))
|
||||||
|
@@ -63,3 +63,27 @@ func PatchRemJson[JV string | []byte](rawjson JV, key string) (JV, error) {
|
|||||||
|
|
||||||
return JV(newjson), nil
|
return JV(newjson), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func MarshalJsonOrPanic(v any) string {
|
||||||
|
bin, err := json.Marshal(v)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return string(bin)
|
||||||
|
}
|
||||||
|
|
||||||
|
func MarshalJsonOrDefault(v any, def string) string {
|
||||||
|
bin, err := json.Marshal(v)
|
||||||
|
if err != nil {
|
||||||
|
return def
|
||||||
|
}
|
||||||
|
return string(bin)
|
||||||
|
}
|
||||||
|
|
||||||
|
func MarshalJsonOrNil(v any) *string {
|
||||||
|
bin, err := json.Marshal(v)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return Ptr(string(bin))
|
||||||
|
}
|
||||||
|
19
langext/object.go
Normal file
19
langext/object.go
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package langext
|
||||||
|
|
||||||
|
import "encoding/json"
|
||||||
|
|
||||||
|
func DeepCopyByJson[T any](v T) (T, error) {
|
||||||
|
|
||||||
|
bin, err := json.Marshal(v)
|
||||||
|
if err != nil {
|
||||||
|
return *new(T), err
|
||||||
|
}
|
||||||
|
|
||||||
|
var result T
|
||||||
|
err = json.Unmarshal(bin, &result)
|
||||||
|
if err != nil {
|
||||||
|
return *new(T), err
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
@@ -2,6 +2,7 @@ package reflectext
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||||
"reflect"
|
"reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -9,12 +10,21 @@ type ConvertStructToMapOpt struct {
|
|||||||
KeepJsonMarshalTypes bool
|
KeepJsonMarshalTypes bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func ConvertStructToMap(v any, opts ...ConvertStructToMapOpt) any {
|
func ConvertStructToMap(v any, opts ...ConvertStructToMapOpt) map[string]any {
|
||||||
opt := ConvertStructToMapOpt{}
|
opt := ConvertStructToMapOpt{}
|
||||||
if len(opts) > 0 {
|
if len(opts) > 0 {
|
||||||
opt = opts[0]
|
opt = opts[0]
|
||||||
}
|
}
|
||||||
return reflectToMap(reflect.ValueOf(v), opt)
|
|
||||||
|
res := reflectToMap(reflect.ValueOf(v), opt)
|
||||||
|
|
||||||
|
if v, ok := res.(map[string]any); ok {
|
||||||
|
return v
|
||||||
|
} else if langext.IsNil(res) {
|
||||||
|
return nil
|
||||||
|
} else {
|
||||||
|
panic("not an object")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func reflectToMap(fv reflect.Value, opt ConvertStructToMapOpt) any {
|
func reflectToMap(fv reflect.Value, opt ConvertStructToMapOpt) any {
|
||||||
|
42
reflectext/convertToMap_test.go
Normal file
42
reflectext/convertToMap_test.go
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
package reflectext
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestConvertStructToMap(t *testing.T) {
|
||||||
|
|
||||||
|
type tst struct {
|
||||||
|
FieldA int
|
||||||
|
FieldB string
|
||||||
|
FieldC time.Time
|
||||||
|
FieldD []float64
|
||||||
|
FieldE1 *int
|
||||||
|
FieldE2 **int
|
||||||
|
FieldE3 *int
|
||||||
|
FieldE4 **int
|
||||||
|
FieldE5 *int
|
||||||
|
FieldE6 **int
|
||||||
|
}
|
||||||
|
|
||||||
|
value := tst{
|
||||||
|
FieldA: 123,
|
||||||
|
FieldB: "hello",
|
||||||
|
FieldC: time.Date(2020, 05, 12, 8, 30, 0, 0, time.UTC),
|
||||||
|
FieldD: []float64{1, 2, 3, 4, 5, 6, 7},
|
||||||
|
FieldE1: nil,
|
||||||
|
FieldE2: nil,
|
||||||
|
FieldE3: langext.Ptr(12),
|
||||||
|
FieldE4: langext.DblPtr(12),
|
||||||
|
FieldE5: nil,
|
||||||
|
FieldE6: langext.DblPtrNil[int](),
|
||||||
|
}
|
||||||
|
|
||||||
|
valueOut := ConvertStructToMap(value, ConvertStructToMapOpt{KeepJsonMarshalTypes: true})
|
||||||
|
|
||||||
|
fmt.Printf("%+v\n", valueOut)
|
||||||
|
|
||||||
|
}
|
@@ -47,9 +47,3 @@ func TestGetMapField(t *testing.T) {
|
|||||||
tst.AssertEqual(t, fmt.Sprint(GetMapField[PseudoInt](maany2, "Test")), "12 true")
|
tst.AssertEqual(t, fmt.Sprint(GetMapField[PseudoInt](maany2, "Test")), "12 true")
|
||||||
tst.AssertEqual(t, fmt.Sprint(GetMapField[PseudoInt](maany2, "Test2")), "0 false")
|
tst.AssertEqual(t, fmt.Sprint(GetMapField[PseudoInt](maany2, "Test2")), "0 false")
|
||||||
}
|
}
|
||||||
|
|
||||||
func main2() {
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
}
|
|
||||||
|
@@ -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