Compare commits

...

6 Commits

Author SHA1 Message Date
b156052e6f v0.0.97 2023-03-29 19:53:53 +02:00
dda2418255 v0.0.96 2023-03-29 19:53:10 +02:00
8e40deae6a add git-pull to Makefile 2023-03-28 16:30:56 +02:00
289b9f47a2 v0.0.95 2023-03-28 16:29:16 +02:00
007c44df85 v0.0.94 2023-03-21 16:00:15 +01:00
a6252f0743 v0.0.93 2023-03-15 15:41:55 +01:00
11 changed files with 230 additions and 54 deletions

View File

@@ -21,6 +21,8 @@ if [ "$( git rev-parse --abbrev-ref HEAD )" != "master" ]; then
exit 1 exit 1
fi fi
git pull --ff
curr_vers=$(git describe --tags --abbrev=0 | sed 's/v//g') 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}') 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}')

View File

@@ -265,6 +265,26 @@ func ArrMap[T1 any, T2 any](arr []T1, conv func(v T1) T2) []T2 {
return r return r
} }
func MapMap[TK comparable, TV any, TR any](inmap map[TK]TV, conv func(k TK, v TV) TR) []TR {
r := make([]TR, 0, len(inmap))
for k, v := range inmap {
r = append(r, conv(k, v))
}
return r
}
func MapMapErr[TK comparable, TV any, TR any](inmap map[TK]TV, conv func(k TK, v TV) (TR, error)) ([]TR, error) {
r := make([]TR, 0, len(inmap))
for k, v := range inmap {
elem, err := conv(k, v)
if err != nil {
return nil, err
}
r = append(r, elem)
}
return r, nil
}
func ArrMapExt[T1 any, T2 any](arr []T1, conv func(idx int, v T1) T2) []T2 { func ArrMapExt[T1 any, T2 any](arr []T1, conv func(idx int, v T1) T2) []T2 {
r := make([]T2, len(arr)) r := make([]T2, len(arr))
for i, v := range arr { for i, v := range arr {

View File

@@ -60,3 +60,12 @@ func CoalesceStringer(s fmt.Stringer, def string) string {
return s.String() return s.String()
} }
} }
func SafeCast[T any](v any, def T) T {
switch r := v.(type) {
case T:
return r
default:
return def
}
}

View File

@@ -1,6 +1,9 @@
package rext package rext
import "regexp" import (
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"regexp"
)
type Regex interface { type Regex interface {
IsMatch(haystack string) bool IsMatch(haystack string) bool
@@ -29,6 +32,10 @@ type RegexMatchGroup struct {
end int end int
} }
type OptRegexMatchGroup struct {
v *RegexMatchGroup
}
func W(rex *regexp.Regexp) Regex { func W(rex *regexp.Regexp) Regex {
return &regexWrapper{rex: rex, subnames: rex.SubexpNames()} return &regexWrapper{rex: rex, subnames: rex.SubexpNames()}
} }
@@ -97,7 +104,7 @@ func (m RegexMatch) GroupByIndex(idx int) RegexMatchGroup {
return RegexMatchGroup{haystack: m.haystack, start: m.submatchesIndex[idx*2], end: m.submatchesIndex[idx*2+1]} return RegexMatchGroup{haystack: m.haystack, start: m.submatchesIndex[idx*2], end: m.submatchesIndex[idx*2+1]}
} }
// GroupByName returns the value of a matched group (group 0 == whole match) // GroupByName returns the value of a matched group (panics if not found!)
func (m RegexMatch) GroupByName(name string) RegexMatchGroup { func (m RegexMatch) GroupByName(name string) RegexMatchGroup {
for idx, subname := range m.subnames { for idx, subname := range m.subnames {
if subname == name { if subname == name {
@@ -107,6 +114,16 @@ func (m RegexMatch) GroupByName(name string) RegexMatchGroup {
panic("failed to find regex-group by name") panic("failed to find regex-group by name")
} }
// GroupByName returns the value of a matched group (returns empty OptRegexMatchGroup if not found)
func (m RegexMatch) GroupByNameOrEmpty(name string) OptRegexMatchGroup {
for idx, subname := range m.subnames {
if subname == name {
return OptRegexMatchGroup{&RegexMatchGroup{haystack: m.haystack, start: m.submatchesIndex[idx*2], end: m.submatchesIndex[idx*2+1]}}
}
}
return OptRegexMatchGroup{}
}
// --------------------------------------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------------------------------------
func (g RegexMatchGroup) Value() string { func (g RegexMatchGroup) Value() string {
@@ -128,3 +145,47 @@ func (g RegexMatchGroup) Range() (int, int) {
func (g RegexMatchGroup) Length() int { func (g RegexMatchGroup) Length() int {
return g.end - g.start return g.end - g.start
} }
// ---------------------------------------------------------------------------------------------------------------------
func (g OptRegexMatchGroup) Value() string {
return g.v.Value()
}
func (g OptRegexMatchGroup) ValueOrEmpty() string {
if g.v == nil {
return ""
}
return g.v.Value()
}
func (g OptRegexMatchGroup) ValueOrNil() *string {
if g.v == nil {
return nil
}
return langext.Ptr(g.v.Value())
}
func (g OptRegexMatchGroup) IsEmpty() bool {
return g.v == nil
}
func (g OptRegexMatchGroup) Exists() bool {
return g.v != nil
}
func (g OptRegexMatchGroup) Start() int {
return g.v.Start()
}
func (g OptRegexMatchGroup) End() int {
return g.v.End()
}
func (g OptRegexMatchGroup) Range() (int, int) {
return g.v.Range()
}
func (g OptRegexMatchGroup) Length() int {
return g.v.Length()
}

View File

@@ -3,24 +3,27 @@ package rfctime
import "time" import "time"
type RFCTime interface { type RFCTime interface {
AnyTime
Time() time.Time Time() time.Time
Serialize() string Serialize() string
UnmarshalJSON(bytes []byte) error After(u AnyTime) bool
Before(u AnyTime) bool
Equal(u AnyTime) bool
Sub(u AnyTime) time.Duration
}
type AnyTime interface {
MarshalJSON() ([]byte, error) MarshalJSON() ([]byte, error)
MarshalBinary() ([]byte, error) MarshalBinary() ([]byte, error)
UnmarshalBinary(data []byte) error
GobEncode() ([]byte, error) GobEncode() ([]byte, error)
GobDecode(data []byte) error
MarshalText() ([]byte, error) MarshalText() ([]byte, error)
UnmarshalText(data []byte) error
After(u RFCTime) bool
Before(u RFCTime) bool
Equal(u RFCTime) bool
IsZero() bool IsZero() bool
Date() (year int, month time.Month, day int) Date() (year int, month time.Month, day int)
Year() int Year() int
@@ -34,7 +37,6 @@ type RFCTime interface {
Second() int Second() int
Nanosecond() int Nanosecond() int
YearDay() int YearDay() int
Sub(u RFCTime) time.Duration
Unix() int64 Unix() int64
UnixMilli() int64 UnixMilli() int64
UnixMicro() int64 UnixMicro() int64
@@ -42,6 +44,8 @@ type RFCTime interface {
Format(layout string) string Format(layout string) string
GoString() string GoString() string
String() string String() string
Location() *time.Location
} }
type RFCDuration interface { type RFCDuration interface {
@@ -60,9 +64,9 @@ type RFCDuration interface {
MarshalText() ([]byte, error) MarshalText() ([]byte, error)
UnmarshalText(data []byte) error UnmarshalText(data []byte) error
After(u RFCTime) bool After(u AnyTime) bool
Before(u RFCTime) bool Before(u AnyTime) bool
Equal(u RFCTime) bool Equal(u AnyTime) bool
IsZero() bool IsZero() bool
Date() (year int, month time.Month, day int) Date() (year int, month time.Month, day int)
Year() int Year() int
@@ -76,7 +80,7 @@ type RFCDuration interface {
Second() int Second() int
Nanosecond() int Nanosecond() int
YearDay() int YearDay() int
Sub(u RFCTime) time.Duration Sub(u AnyTime) time.Duration
Unix() int64 Unix() int64
UnixMilli() int64 UnixMilli() int64
UnixMicro() int64 UnixMicro() int64
@@ -85,3 +89,13 @@ type RFCDuration interface {
GoString() string GoString() string
String() string String() string
} }
func tt(v AnyTime) time.Time {
if r, ok := v.(time.Time); ok {
return r
}
if r, ok := v.(RFCTime); ok {
return r.Time()
}
return time.Unix(0, v.UnixNano()).In(v.Location())
}

50
rfctime/interface_test.go Normal file
View File

@@ -0,0 +1,50 @@
package rfctime
import (
"testing"
"time"
)
func TestAnyTimeInterface(t *testing.T) {
var v AnyTime
v = NowRFC3339Nano()
assertEqual(t, v.String(), v.String())
v = NowRFC3339()
assertEqual(t, v.String(), v.String())
v = NowUnix()
assertEqual(t, v.String(), v.String())
v = NowUnixMilli()
assertEqual(t, v.String(), v.String())
v = NowUnixNano()
assertEqual(t, v.String(), v.String())
v = time.Now()
assertEqual(t, v.String(), v.String())
}
func TestRFCTimeInterface(t *testing.T) {
var v RFCTime
v = NowRFC3339Nano()
assertEqual(t, v.String(), v.String())
v = NowRFC3339()
assertEqual(t, v.String(), v.String())
v = NowUnix()
assertEqual(t, v.String(), v.String())
v = NowUnixMilli()
assertEqual(t, v.String(), v.String())
v = NowUnixNano()
assertEqual(t, v.String(), v.String())
}

View File

@@ -90,16 +90,16 @@ func (t RFC3339Time) FormatStr() string {
return time.RFC3339 return time.RFC3339
} }
func (t RFC3339Time) After(u RFCTime) bool { func (t RFC3339Time) After(u AnyTime) bool {
return t.Time().After(u.Time()) return t.Time().After(tt(u))
} }
func (t RFC3339Time) Before(u RFCTime) bool { func (t RFC3339Time) Before(u AnyTime) bool {
return t.Time().Before(u.Time()) return t.Time().Before(tt(u))
} }
func (t RFC3339Time) Equal(u RFCTime) bool { func (t RFC3339Time) Equal(u AnyTime) bool {
return t.Time().Equal(u.Time()) return t.Time().Equal(tt(u))
} }
func (t RFC3339Time) IsZero() bool { func (t RFC3339Time) IsZero() bool {
@@ -158,8 +158,8 @@ func (t RFC3339Time) Add(d time.Duration) RFC3339Time {
return RFC3339Time(t.Time().Add(d)) return RFC3339Time(t.Time().Add(d))
} }
func (t RFC3339Time) Sub(u RFCTime) time.Duration { func (t RFC3339Time) Sub(u AnyTime) time.Duration {
return t.Time().Sub(u.Time()) return t.Time().Sub(tt(u))
} }
func (t RFC3339Time) AddDate(years int, months int, days int) RFC3339Time { func (t RFC3339Time) AddDate(years int, months int, days int) RFC3339Time {
@@ -194,6 +194,10 @@ func (t RFC3339Time) String() string {
return t.Time().String() return t.Time().String()
} }
func (t RFC3339Time) Location() *time.Location {
return t.Time().Location()
}
func NewRFC3339(t time.Time) RFC3339Time { func NewRFC3339(t time.Time) RFC3339Time {
return RFC3339Time(t) return RFC3339Time(t)
} }

View File

@@ -90,16 +90,16 @@ func (t RFC3339NanoTime) FormatStr() string {
return time.RFC3339Nano return time.RFC3339Nano
} }
func (t RFC3339NanoTime) After(u RFCTime) bool { func (t RFC3339NanoTime) After(u AnyTime) bool {
return t.Time().After(u.Time()) return t.Time().After(tt(u))
} }
func (t RFC3339NanoTime) Before(u RFCTime) bool { func (t RFC3339NanoTime) Before(u AnyTime) bool {
return t.Time().Before(u.Time()) return t.Time().Before(tt(u))
} }
func (t RFC3339NanoTime) Equal(u RFCTime) bool { func (t RFC3339NanoTime) Equal(u AnyTime) bool {
return t.Time().Equal(u.Time()) return t.Time().Equal(tt(u))
} }
func (t RFC3339NanoTime) IsZero() bool { func (t RFC3339NanoTime) IsZero() bool {
@@ -158,8 +158,8 @@ func (t RFC3339NanoTime) Add(d time.Duration) RFC3339NanoTime {
return RFC3339NanoTime(t.Time().Add(d)) return RFC3339NanoTime(t.Time().Add(d))
} }
func (t RFC3339NanoTime) Sub(u RFCTime) time.Duration { func (t RFC3339NanoTime) Sub(u AnyTime) time.Duration {
return t.Time().Sub(u.Time()) return t.Time().Sub(tt(u))
} }
func (t RFC3339NanoTime) AddDate(years int, months int, days int) RFC3339NanoTime { func (t RFC3339NanoTime) AddDate(years int, months int, days int) RFC3339NanoTime {
@@ -194,6 +194,10 @@ func (t RFC3339NanoTime) String() string {
return t.Time().String() return t.Time().String()
} }
func (t RFC3339NanoTime) Location() *time.Location {
return t.Time().Location()
}
func NewRFC3339Nano(t time.Time) RFC3339NanoTime { func NewRFC3339Nano(t time.Time) RFC3339NanoTime {
return RFC3339NanoTime(t) return RFC3339NanoTime(t)
} }

View File

@@ -63,16 +63,16 @@ func (t UnixTime) Serialize() string {
return strconv.FormatInt(t.Time().Unix(), 10) return strconv.FormatInt(t.Time().Unix(), 10)
} }
func (t UnixTime) After(u RFCTime) bool { func (t UnixTime) After(u AnyTime) bool {
return t.Time().After(u.Time()) return t.Time().After(tt(u))
} }
func (t UnixTime) Before(u RFCTime) bool { func (t UnixTime) Before(u AnyTime) bool {
return t.Time().Before(u.Time()) return t.Time().Before(tt(u))
} }
func (t UnixTime) Equal(u RFCTime) bool { func (t UnixTime) Equal(u AnyTime) bool {
return t.Time().Equal(u.Time()) return t.Time().Equal(tt(u))
} }
func (t UnixTime) IsZero() bool { func (t UnixTime) IsZero() bool {
@@ -131,8 +131,8 @@ func (t UnixTime) Add(d time.Duration) UnixTime {
return UnixTime(t.Time().Add(d)) return UnixTime(t.Time().Add(d))
} }
func (t UnixTime) Sub(u RFCTime) time.Duration { func (t UnixTime) Sub(u AnyTime) time.Duration {
return t.Time().Sub(u.Time()) return t.Time().Sub(tt(u))
} }
func (t UnixTime) AddDate(years int, months int, days int) UnixTime { func (t UnixTime) AddDate(years int, months int, days int) UnixTime {
@@ -167,6 +167,10 @@ func (t UnixTime) String() string {
return t.Time().String() return t.Time().String()
} }
func (t UnixTime) Location() *time.Location {
return t.Time().Location()
}
func NewUnix(t time.Time) UnixTime { func NewUnix(t time.Time) UnixTime {
return UnixTime(t) return UnixTime(t)
} }

View File

@@ -63,16 +63,16 @@ func (t UnixMilliTime) Serialize() string {
return strconv.FormatInt(t.Time().UnixMilli(), 10) return strconv.FormatInt(t.Time().UnixMilli(), 10)
} }
func (t UnixMilliTime) After(u RFCTime) bool { func (t UnixMilliTime) After(u AnyTime) bool {
return t.Time().After(u.Time()) return t.Time().After(tt(u))
} }
func (t UnixMilliTime) Before(u RFCTime) bool { func (t UnixMilliTime) Before(u AnyTime) bool {
return t.Time().Before(u.Time()) return t.Time().Before(tt(u))
} }
func (t UnixMilliTime) Equal(u RFCTime) bool { func (t UnixMilliTime) Equal(u AnyTime) bool {
return t.Time().Equal(u.Time()) return t.Time().Equal(tt(u))
} }
func (t UnixMilliTime) IsZero() bool { func (t UnixMilliTime) IsZero() bool {
@@ -131,8 +131,8 @@ func (t UnixMilliTime) Add(d time.Duration) UnixMilliTime {
return UnixMilliTime(t.Time().Add(d)) return UnixMilliTime(t.Time().Add(d))
} }
func (t UnixMilliTime) Sub(u RFCTime) time.Duration { func (t UnixMilliTime) Sub(u AnyTime) time.Duration {
return t.Time().Sub(u.Time()) return t.Time().Sub(tt(u))
} }
func (t UnixMilliTime) AddDate(years int, months int, days int) UnixMilliTime { func (t UnixMilliTime) AddDate(years int, months int, days int) UnixMilliTime {
@@ -167,6 +167,10 @@ func (t UnixMilliTime) String() string {
return t.Time().String() return t.Time().String()
} }
func (t UnixMilliTime) Location() *time.Location {
return t.Time().Location()
}
func NewUnixMilli(t time.Time) UnixMilliTime { func NewUnixMilli(t time.Time) UnixMilliTime {
return UnixMilliTime(t) return UnixMilliTime(t)
} }

View File

@@ -63,16 +63,16 @@ func (t UnixNanoTime) Serialize() string {
return strconv.FormatInt(t.Time().UnixNano(), 10) return strconv.FormatInt(t.Time().UnixNano(), 10)
} }
func (t UnixNanoTime) After(u RFCTime) bool { func (t UnixNanoTime) After(u AnyTime) bool {
return t.Time().After(u.Time()) return t.Time().After(tt(u))
} }
func (t UnixNanoTime) Before(u RFCTime) bool { func (t UnixNanoTime) Before(u AnyTime) bool {
return t.Time().Before(u.Time()) return t.Time().Before(tt(u))
} }
func (t UnixNanoTime) Equal(u RFCTime) bool { func (t UnixNanoTime) Equal(u AnyTime) bool {
return t.Time().Equal(u.Time()) return t.Time().Equal(tt(u))
} }
func (t UnixNanoTime) IsZero() bool { func (t UnixNanoTime) IsZero() bool {
@@ -131,8 +131,8 @@ func (t UnixNanoTime) Add(d time.Duration) UnixNanoTime {
return UnixNanoTime(t.Time().Add(d)) return UnixNanoTime(t.Time().Add(d))
} }
func (t UnixNanoTime) Sub(u RFCTime) time.Duration { func (t UnixNanoTime) Sub(u AnyTime) time.Duration {
return t.Time().Sub(u.Time()) return t.Time().Sub(tt(u))
} }
func (t UnixNanoTime) AddDate(years int, months int, days int) UnixNanoTime { func (t UnixNanoTime) AddDate(years int, months int, days int) UnixNanoTime {
@@ -167,6 +167,10 @@ func (t UnixNanoTime) String() string {
return t.Time().String() return t.Time().String()
} }
func (t UnixNanoTime) Location() *time.Location {
return t.Time().Location()
}
func NewUnixNano(t time.Time) UnixNanoTime { func NewUnixNano(t time.Time) UnixNanoTime {
return UnixNanoTime(t) return UnixNanoTime(t)
} }