Compare commits

...

10 Commits

Author SHA1 Message Date
52f7f6e690 v0.0.26 2022-11-30 23:38:42 +01:00
b1e3891256 v0.0.25 2022-11-30 23:35:47 +01:00
bdf5b53c20 v0.0.24 2022-11-30 23:34:16 +01:00
496c4e4f59 v0.0.23 2022-11-30 23:08:50 +01:00
deab986caf v0.0.22 2022-11-30 22:09:54 +01:00
9d9a6f1c6e v0.0.21 2022-11-19 16:58:18 +01:00
06a37f37b7 v0.0.20 2022-11-19 16:26:45 +01:00
b35d6ca0b0 v0.0.19 2022-11-19 13:34:21 +01:00
b643bded8a ArrFirstIndex, ArrLastIndex, ArrMap, ArrSum 2022-11-14 20:42:50 +01:00
93be4f9fdd added more supported GOOS 2022-10-29 15:44:51 +02:00
14 changed files with 465 additions and 38 deletions

View File

@@ -4,3 +4,6 @@ run:
test:
go test ./...
version:
_data/version.sh

27
_data/version.sh Executable file
View File

@@ -0,0 +1,27 @@
#!/bin/bash
set -o nounset # disallow usage of unset vars ( set -u )
set -o errexit # Exit immediately if a pipeline returns non-zero. ( set -e )
set -o errtrace # Allow the above trap be inherited by all functions in the script. ( set -E )
set -o pipefail # Return value of a pipeline is the value of the last (rightmost) command to exit with a non-zero status
IFS=$'\n\t' # Set $IFS to only newline and tab.
curr_vers=$(git describe --tags --abbrev=0 | sed 's/v//g')
next_ver=$(echo "$curr_vers" | awk -F. -v OFS=. 'NF==1{print ++$NF}; NF>1{if(length($NF+1)>length($NF))$(NF-1)++; $NF=sprintf("%0*d", length($NF), ($NF+1)%(10^length($NF))); print}')
echo ""
echo "> Current Version: ${curr_vers}"
echo "> Next Version: ${next_ver}"
echo ""
git add --verbose .
git commit -a -m "v${next_ver}"
git tag "v${next_ver}"
git push
git push --tags

35
dataext/merge.go Normal file
View File

@@ -0,0 +1,35 @@
package dataext
import (
"reflect"
)
func ObjectMerge[T1 any, T2 any](base T1, override T2) T1 {
reflBase := reflect.ValueOf(&base).Elem()
reflOvrd := reflect.ValueOf(&override).Elem()
for i := 0; i < reflBase.NumField(); i++ {
fieldBase := reflBase.Field(i)
fieldOvrd := reflOvrd.Field(i)
if fieldBase.Kind() != reflect.Ptr || fieldOvrd.Kind() != reflect.Ptr {
continue
}
kindBase := fieldBase.Type().Elem().Kind()
kindOvrd := fieldOvrd.Type().Elem().Kind()
if kindBase != kindOvrd {
continue
}
if !fieldOvrd.IsNil() {
fieldBase.Set(fieldOvrd.Elem().Addr())
}
}
return base
}

70
dataext/merge_test.go Normal file
View File

@@ -0,0 +1,70 @@
package dataext
import (
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"testing"
)
func TestObjectMerge(t *testing.T) {
type A struct {
Field1 *int
Field2 *string
Field3 *float64
Field4 *bool
OnlyA int64
DiffType int
}
type B struct {
Field1 *int
Field2 *string
Field3 *float64
Field4 *bool
OnlyB int64
DiffType string
}
valueA := A{
Field1: nil,
Field2: langext.Ptr("99"),
Field3: langext.Ptr(12.2),
Field4: nil,
OnlyA: 1,
DiffType: 2,
}
valueB := B{
Field1: langext.Ptr(12),
Field2: nil,
Field3: langext.Ptr(13.2),
Field4: nil,
OnlyB: 1,
DiffType: "X",
}
valueMerge := ObjectMerge(valueA, valueB)
assertPtrEqual(t, "Field1", valueMerge.Field1, valueB.Field1)
assertPtrEqual(t, "Field2", valueMerge.Field2, valueA.Field2)
assertPtrEqual(t, "Field3", valueMerge.Field3, valueB.Field3)
assertPtrEqual(t, "Field4", valueMerge.Field4, nil)
}
func assertPtrEqual[T1 comparable](t *testing.T, ident string, actual *T1, expected *T1) {
if actual == nil && expected == nil {
return
}
if actual != nil && expected != nil {
if *actual != *expected {
t.Errorf("[%s] values differ: Actual: '%v', Expected: '%v'", ident, *actual, *expected)
} else {
return
}
}
if actual == nil && expected != nil {
t.Errorf("[%s] values differ: Actual: nil, Expected: not-nil", ident)
}
if actual != nil && expected == nil {
t.Errorf("[%s] values differ: Actual: not-nil, Expected: nil", ident)
}
}

View File

@@ -163,6 +163,25 @@ func ArrLast[T comparable](arr []T, comp func(v T) bool) (T, bool) {
return result, found
}
func ArrFirstIndex[T comparable](arr []T, needle T) int {
for i, v := range arr {
if v == needle {
return i
}
}
return -1
}
func ArrLastIndex[T comparable](arr []T, needle T) int {
result := -1
for i, v := range arr {
if v == needle {
result = i
}
}
return result
}
func AddToSet[T comparable](set []T, add T) []T {
for _, v := range set {
if v == add {
@@ -171,3 +190,19 @@ func AddToSet[T comparable](set []T, add T) []T {
}
return append(set, add)
}
func ArrMap[T1 any, T2 any](arr []T1, conv func(v T1) T2) []T2 {
r := make([]T2, len(arr))
for i, v := range arr {
r[i] = conv(v)
}
return r
}
func ArrSum[T NumberConstraint](arr []T) T {
var r T = 0
for _, v := range arr {
r += v
}
return r
}

View File

@@ -7,3 +7,11 @@ func FormatBool(v bool, strTrue string, strFalse string) string {
return strFalse
}
}
func Conditional[T any](v bool, resTrue T, resFalse T) T {
if v {
return resTrue
} else {
return resFalse
}
}

View File

@@ -29,3 +29,21 @@ func Min[T langext.OrderedConstraint](v1 T, v2 T) T {
return v2
}
}
func Abs[T langext.NumberConstraint](v T) T {
if v < 0 {
return -v
} else {
return v
}
}
func Clamp[T langext.NumberConstraint](v T, min T, max T) T {
if v < min {
return min
} else if v > max {
return max
} else {
return v
}
}

104
syncext/atomic.go Normal file
View File

@@ -0,0 +1,104 @@
package syncext
import (
"context"
"sync/atomic"
"time"
)
type AtomicBool struct {
v int32
waiter chan bool // unbuffered
}
func NewAtomicBool(value bool) *AtomicBool {
if value {
return &AtomicBool{v: 0, waiter: make(chan bool)}
} else {
return &AtomicBool{v: 1, waiter: make(chan bool)}
}
}
func (a *AtomicBool) Get() bool {
return atomic.LoadInt32(&a.v) == 1
}
func (a *AtomicBool) Set(value bool) {
if value {
atomic.StoreInt32(&a.v, 1)
} else {
atomic.StoreInt32(&a.v, 0)
}
select {
case a.waiter <- value:
// message sent
default:
// no receiver on channel
}
}
func (a *AtomicBool) Wait(waitFor bool) {
if a.Get() == waitFor {
return
}
for {
if v, ok := ReadChannelWithTimeout(a.waiter, 128*time.Millisecond); ok {
if v == waitFor {
return
}
} else {
if a.Get() == waitFor {
return
}
}
}
}
func (a *AtomicBool) WaitWithTimeout(timeout time.Duration, waitFor bool) error {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
return a.WaitWithContext(ctx, waitFor)
}
func (a *AtomicBool) WaitWithContext(ctx context.Context, waitFor bool) error {
if err := ctx.Err(); err != nil {
return err
}
if a.Get() == waitFor {
return nil
}
for {
if err := ctx.Err(); err != nil {
return err
}
timeOut := 128 * time.Millisecond
if dl, ok := ctx.Deadline(); ok {
timeOutMax := dl.Sub(time.Now())
if timeOutMax <= 0 {
timeOut = 0
} else if 0 < timeOutMax && timeOutMax < timeOut {
timeOut = timeOutMax
}
}
if v, ok := ReadChannelWithTimeout(a.waiter, timeOut); ok {
if v == waitFor {
return nil
}
} else {
if err := ctx.Err(); err != nil {
return err
}
if a.Get() == waitFor {
return nil
}
}
}
}

14
syncext/channel.go Normal file
View File

@@ -0,0 +1,14 @@
package syncext
import "time"
func ReadChannelWithTimeout[T any](c chan T, timeout time.Duration) (T, bool) {
afterCh := time.After(timeout)
select {
case rv := <-c:
return rv, true
case <-afterCh:
return *new(T), false
}
}

121
syncext/channel_test.go Normal file
View File

@@ -0,0 +1,121 @@
package syncext
import (
"testing"
"time"
)
func TestTimeoutReadBuffered(t *testing.T) {
c := make(chan int, 1)
go func() {
time.Sleep(200 * time.Millisecond)
c <- 112
}()
_, ok := ReadChannelWithTimeout(c, 100*time.Millisecond)
if ok {
t.Error("Read success, but should timeout")
}
}
func TestTimeoutReadBigBuffered(t *testing.T) {
c := make(chan int, 128)
go func() {
time.Sleep(200 * time.Millisecond)
c <- 112
}()
_, ok := ReadChannelWithTimeout(c, 100*time.Millisecond)
if ok {
t.Error("Read success, but should timeout")
}
}
func TestTimeoutReadUnbuffered(t *testing.T) {
c := make(chan int)
go func() {
time.Sleep(200 * time.Millisecond)
c <- 112
}()
_, ok := ReadChannelWithTimeout(c, 100*time.Millisecond)
if ok {
t.Error("Read success, but should timeout")
}
}
func TestNoTimeoutAfterStartReadBuffered(t *testing.T) {
c := make(chan int, 1)
go func() {
time.Sleep(10 * time.Millisecond)
c <- 112
}()
_, ok := ReadChannelWithTimeout(c, 100*time.Millisecond)
if !ok {
t.Error("Read timeout, but should have succeeded")
}
}
func TestNoTimeoutAfterStartReadBigBuffered(t *testing.T) {
c := make(chan int, 128)
go func() {
time.Sleep(10 * time.Millisecond)
c <- 112
}()
_, ok := ReadChannelWithTimeout(c, 100*time.Millisecond)
if !ok {
t.Error("Read timeout, but should have succeeded")
}
}
func TestNoTimeoutAfterStartReadUnbuffered(t *testing.T) {
c := make(chan int)
go func() {
time.Sleep(10 * time.Millisecond)
c <- 112
}()
_, ok := ReadChannelWithTimeout(c, 100*time.Millisecond)
if !ok {
t.Error("Read timeout, but should have succeeded")
}
}
func TestNoTimeoutBeforeStartReadBuffered(t *testing.T) {
c := make(chan int, 1)
c <- 112
_, ok := ReadChannelWithTimeout(c, 10*time.Millisecond)
if !ok {
t.Error("Read timeout, but should have succeeded")
}
}
func TestNoTimeoutBeforeStartReadBigBuffered(t *testing.T) {
c := make(chan int, 128)
c <- 112
_, ok := ReadChannelWithTimeout(c, 10*time.Millisecond)
if !ok {
t.Error("Read timeout, but should have succeeded")
}
}

View File

@@ -0,0 +1,5 @@
package termext
func enableColor() bool {
return true
}

5
termext/osutil_netbsd.go Normal file
View File

@@ -0,0 +1,5 @@
package termext
func enableColor() bool {
return true
}

View File

@@ -0,0 +1,5 @@
package termext
func enableColor() bool {
return true
}

View File

@@ -2,59 +2,36 @@ package timeext
import (
"fmt"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"time"
)
func FromSeconds(v int) time.Duration {
return time.Duration(int64(v) * int64(time.Second))
func FromNanoseconds[T langext.NumberConstraint](v T) time.Duration {
return time.Duration(int64(float64(v) * float64(time.Nanosecond)))
}
func FromSecondsInt32(v int32) time.Duration {
return time.Duration(int64(v) * int64(time.Second))
func FromMicroseconds[T langext.NumberConstraint](v T) time.Duration {
return time.Duration(int64(float64(v) * float64(time.Microsecond)))
}
func FromSecondsInt64(v int64) time.Duration {
return time.Duration(v * int64(time.Second))
func FromMilliseconds[T langext.NumberConstraint](v T) time.Duration {
return time.Duration(int64(float64(v) * float64(time.Millisecond)))
}
func FromSecondsFloat32(v float32) time.Duration {
return time.Duration(int64(v * float32(time.Second)))
func FromSeconds[T langext.NumberConstraint](v T) time.Duration {
return time.Duration(int64(float64(v) * float64(time.Second)))
}
func FromSecondsFloat64(v float64) time.Duration {
return time.Duration(int64(v * float64(time.Second)))
func FromMinutes[T langext.NumberConstraint](v T) time.Duration {
return time.Duration(int64(float64(v) * float64(time.Minute)))
}
func FromSecondsFloat(v float64) time.Duration {
return time.Duration(int64(v * float64(time.Second)))
func FromHours[T langext.NumberConstraint](v T) time.Duration {
return time.Duration(int64(float64(v) * float64(time.Hour)))
}
func FromMinutes(v int) time.Duration {
return time.Duration(int64(v) * int64(time.Minute))
}
func FromMinutesFloat(v float64) time.Duration {
return time.Duration(int64(v * float64(time.Minute)))
}
func FromMinutesFloat64(v float64) time.Duration {
return time.Duration(int64(v * float64(time.Minute)))
}
func FromHoursFloat64(v float64) time.Duration {
return time.Duration(int64(v * float64(time.Hour)))
}
func FromDays(v int) time.Duration {
return time.Duration(int64(v) * int64(24) * int64(time.Hour))
}
func FromMilliseconds(v int) time.Duration {
return time.Duration(int64(v) * int64(time.Millisecond))
}
func FromMillisecondsFloat(v float64) time.Duration {
return time.Duration(int64(v * float64(time.Millisecond)))
func FromDays[T langext.NumberConstraint](v T) time.Duration {
return time.Duration(int64(float64(v) * float64(24) * float64(time.Hour)))
}
func FormatNaturalDurationEnglish(iv time.Duration) string {