Compare commits

...

3 Commits

Author SHA1 Message Date
c13db6802e v0.0.102 2023-04-13 14:40:07 +02:00
c5e23ab451 v0.0.101 2023-04-08 19:39:13 +02:00
c266d9204b v0.0.100 2023-04-04 17:10:38 +02:00
3 changed files with 49 additions and 0 deletions

View File

@@ -217,6 +217,15 @@ func ArrFirst[T any](arr []T, comp func(v T) bool) (T, bool) {
return *new(T), false
}
func ArrFirstOrNil[T any](arr []T, comp func(v T) bool) *T {
for _, v := range arr {
if comp(v) {
return Ptr(v)
}
}
return nil
}
func ArrLast[T any](arr []T, comp func(v T) bool) (T, bool) {
found := false
result := *new(T)
@@ -229,6 +238,22 @@ func ArrLast[T any](arr []T, comp func(v T) bool) (T, bool) {
return result, found
}
func ArrLastOrNil[T any](arr []T, comp func(v T) bool) *T {
found := false
result := *new(T)
for _, v := range arr {
if comp(v) {
found = true
result = v
}
}
if found {
return Ptr(result)
} else {
return nil
}
}
func ArrFirstIndex[T comparable](arr []T, needle T) int {
for i, v := range arr {
if v == needle {
@@ -356,3 +381,19 @@ func ArrCastToAny[T1 any](arr []T1) []any {
}
return r
}
func ArrConcat[T any](arr ...[]T) []T {
c := 0
for _, v := range arr {
c += len(v)
}
r := make([]T, c)
i := 0
for _, av := range arr {
for _, v := range av {
r[i] = v
i++
}
}
return r
}

View File

@@ -66,6 +66,10 @@ func (t *RFC3339Time) UnmarshalText(data []byte) error {
}
func (t *RFC3339Time) UnmarshalBSONValue(bt bsontype.Type, data []byte) error {
if bt == bsontype.Null {
*t = RFC3339Time{}
return nil
}
if bt != bsontype.DateTime {
return errors.New(fmt.Sprintf("cannot unmarshal %v into RFC3339Time", bt))
}

View File

@@ -66,6 +66,10 @@ func (t *RFC3339NanoTime) UnmarshalText(data []byte) error {
}
func (t *RFC3339NanoTime) UnmarshalBSONValue(bt bsontype.Type, data []byte) error {
if bt == bsontype.Null {
*t = RFC3339NanoTime{}
return nil
}
if bt != bsontype.DateTime {
return errors.New(fmt.Sprintf("cannot unmarshal %v into RFC3339NanoTime", bt))
}