mirror of
https://github.com/aykhans/dodo.git
synced 2025-09-03 18:03:34 +00:00
🔨 Add 'types' package
This commit is contained in:
41
types/durations.go
Normal file
41
types/durations.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Durations []time.Duration
|
||||
|
||||
func (d Durations) Sort(ascending ...bool) {
|
||||
// If ascending is provided and is false, sort in descending order
|
||||
if len(ascending) > 0 && ascending[0] == false {
|
||||
sort.Slice(d, func(i, j int) bool {
|
||||
return d[i] > d[j]
|
||||
})
|
||||
} else { // Otherwise, sort in ascending order
|
||||
sort.Slice(d, func(i, j int) bool {
|
||||
return d[i] < d[j]
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (d Durations) First() *time.Duration {
|
||||
return &d[0]
|
||||
}
|
||||
|
||||
func (d Durations) Last() *time.Duration {
|
||||
return &d[len(d)-1]
|
||||
}
|
||||
|
||||
func (d Durations) Sum() time.Duration {
|
||||
sum := time.Duration(0)
|
||||
for _, duration := range d {
|
||||
sum += duration
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
func (d Durations) Avg() time.Duration {
|
||||
return d.Sum() / time.Duration(len(d))
|
||||
}
|
87
types/option.go
Normal file
87
types/option.go
Normal file
@@ -0,0 +1,87 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
)
|
||||
|
||||
type NonNilT interface {
|
||||
~int | ~float64 | ~string | ~bool
|
||||
}
|
||||
|
||||
type Option[T NonNilT] interface {
|
||||
IsNone() bool
|
||||
ValueOrErr() (T, error)
|
||||
ValueOr(def T) T
|
||||
ValueOrPanic() T
|
||||
UnmarshalJSON(data []byte) error
|
||||
}
|
||||
|
||||
// Don't call this struct directly, use NewOption[T] or NewNoneOption[T] instead.
|
||||
type option[T NonNilT] struct {
|
||||
// value holds the actual value of the Option if it is not None.
|
||||
value T
|
||||
// none indicates whether the Option is None (i.e., has no value).
|
||||
none bool
|
||||
}
|
||||
|
||||
func (o *option[T]) IsNone() bool {
|
||||
return o.none
|
||||
}
|
||||
|
||||
// If the Option is None, it will return zero value of the type and an error.
|
||||
func (o *option[T]) ValueOrErr() (T, error) {
|
||||
if o.IsNone() {
|
||||
return o.value, errors.New("Option is None")
|
||||
}
|
||||
return o.value, nil
|
||||
}
|
||||
|
||||
// If the Option is None, it will return the default value.
|
||||
func (o *option[T]) ValueOr(def T) T {
|
||||
if o.IsNone() {
|
||||
return def
|
||||
}
|
||||
return o.value
|
||||
}
|
||||
|
||||
// If the Option is None, it will panic.
|
||||
func (o *option[T]) ValueOrPanic() T {
|
||||
if o.IsNone() {
|
||||
panic("Option is None")
|
||||
}
|
||||
return o.value
|
||||
}
|
||||
|
||||
func (o *option[T]) SetValue(value T) {
|
||||
o.value = value
|
||||
o.none = false
|
||||
}
|
||||
|
||||
func (o *option[T]) SetNone() {
|
||||
var zeroValue T
|
||||
o.value = zeroValue
|
||||
o.none = true
|
||||
}
|
||||
|
||||
func (o *option[T]) UnmarshalJSON(data []byte) error {
|
||||
if string(data) == "null" || len(data) == 0 {
|
||||
o.SetNone()
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(data, &o.value); err != nil {
|
||||
o.SetNone()
|
||||
return err
|
||||
}
|
||||
o.none = false
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewOption[T NonNilT](value T) *option[T] {
|
||||
return &option[T]{value: value}
|
||||
}
|
||||
|
||||
func NewNoneOption[T NonNilT]() *option[T] {
|
||||
return &option[T]{none: true}
|
||||
}
|
Reference in New Issue
Block a user