1
0
Fork 0

Commits vergleichen

...

8 Commits
v1.0.0 ... main

9 geänderte Dateien mit 230 neuen und 23 gelöschten Zeilen

2
doc.go Normale Datei
Datei anzeigen

@ -0,0 +1,2 @@
// Package generic provides functions and other helpers that don't fit into my other repos
package generic

18
go.mod
Datei anzeigen

@ -1,19 +1,15 @@
module gitea.sebastian-tobie.de/sebastian/generic
module go.sebtobie.de/generic
go 1.18
go 1.19
require github.com/rs/zerolog v1.27.0
require github.com/rs/zerolog v1.28.0
require (
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
)
require github.com/x448/float16 v0.8.4 // indirect
require (
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534
github.com/fxamacker/cbor/v2 v2.4.0
github.com/hashicorp/go-multierror v1.1.1
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
golang.org/x/sys v0.2.0 // indirect
)

20
go.sum
Datei anzeigen

@ -3,20 +3,20 @@ github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1
github.com/fxamacker/cbor/v2 v2.4.0 h1:ri0ArlOR+5XunOP8CRUowT0pSJOwhW098ZCUyskZD88=
github.com/fxamacker/cbor/v2 v2.4.0/go.mod h1:TA1xS00nchWmaBnEIxPSE5oHLuJBAVvqrtAnWBwBCVo=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.27.0 h1:1T7qCieN22GVc8S4Q2yuexzBb1EqjbgjSH9RohbMjKs=
github.com/rs/zerolog v1.27.0/go.mod h1:7frBqO0oezxmnO7GF86FY++uy8I0Tk/If5ni1G9Qc0U=
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.28.0 h1:MirSo27VyNi7RJYP3078AA1+Cyzd2GB66qy3aUHvsWY=
github.com/rs/zerolog v1.28.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0=
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 h1:foEbQz/B0Oz6YIqu/69kfXPYeFQAuuMYFkjaqXzl5Wo=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

Datei anzeigen

@ -1,9 +1,9 @@
//go:build !windows
// +build !windows
// Package logging is the implementation of an better systemdlogger for zerolog
// This package consists of parts of github.com/rs/zerolog i copied from.
// I removed the JSON argument part and added some more relevant data (The position of the caller and the goroutine)
package logging
import (
@ -19,6 +19,11 @@ import (
"github.com/rs/zerolog"
)
var replacements = map[string]string{
"-": "_",
" ": "_",
}
const defaultJournalDPrio = journal.PriNotice
func binaryFmt(p []byte) bool {
@ -66,6 +71,14 @@ func levelToJPrio(zLevel string) journal.Priority {
return defaultJournalDPrio
}
func replace(input string) (output string) {
output = input
for key, value := range replacements {
output = strings.ReplaceAll(output, key, value)
}
return
}
func (w journalWriter) Write(p []byte) (n int, err error) {
var event map[string]interface{}
origPLen := len(p)
@ -87,7 +100,7 @@ func (w journalWriter) Write(p []byte) (n int, err error) {
msg := ""
for key, value := range event {
jKey := strings.ToUpper(key)
jKey := replace(strings.ToUpper(key))
switch key {
case zerolog.LevelFieldName, zerolog.TimestampFieldName:
continue

16
validators.go Normale Datei
Datei anzeigen

@ -0,0 +1,16 @@
package generic
// Valid returns true if the argument is not nil and can be converted to the type.
// if additional validators are required they can be given as arguments
func Valid[T any](item any, validator ...func(T) bool) (ok bool) {
if item != nil {
var v, ok = item.(T)
if len(validator) != 0 {
for _, vali := range validator {
ok = ok && vali(v)
}
}
return ok
}
return false
}

56
weightedrandom/common.go Normale Datei
Datei anzeigen

@ -0,0 +1,56 @@
package weightedrandom
import (
"crypto/rand"
"math"
"math/big"
)
type ordered interface {
int | int8 | int16 | int32 | int64 |
uint | uint8 | uint16 | uint32 | uint64 |
float32 | float64 |
string
}
// RandomFunc is the Type for random values required for this
type RandomFunc func(int) int
// AdjustFunc is the function that returns an adjusted value for the weighting
type AdjustFunc func(itemweight int, totalWeight int) int
// AccessModifier is executed before an item is returned and returns the modified weight of an item.
type AccessModifier func(oldweight int) int
// RandInt is the standard function for returning random numbers. It returns an random number between 0 and max
func RandInt(max int) int {
out, err := rand.Int(rand.Reader, big.NewInt(int64(max)))
if err != nil {
panic(err)
}
return int(out.Int64())
}
// StandardAdjustFunc returns the normal weight for the adjustment
func StandardAdjustFunc(standardweight, _ int) int {
return standardweight
}
// ReverseWeightAdjustFunc returns the value relative to the total weight
func ReverseWeightAdjustFunc(weight, totalWeight int) int {
return totalWeight / weight
}
// Addone adds one until the max value is reached, after that it returns only the max value
func Addone(oldweight int) int {
if oldweight != math.MaxInt {
return oldweight + 1
}
return oldweight
}
type weightitem[T any] struct {
weight int
adjusted int
value T
}

2
weightedrandom/doc.go Normale Datei
Datei anzeigen

@ -0,0 +1,2 @@
// Package weightedrandom provides multiple weighted random lists
package weightedrandom

11
weightedrandom/new.go Normale Datei
Datei anzeigen

@ -0,0 +1,11 @@
package weightedrandom
// New returns an new instance of WeightRandom
func New[T any](af AdjustFunc, rf RandomFunc, acf AccessModifier) *WeightRandom[T] {
return &WeightRandom[T]{
data: []*weightitem[T]{},
weightFunc: af,
randomFunc: rf,
accessfunc: acf,
}
}

Datei anzeigen

@ -0,0 +1,111 @@
package weightedrandom
import "sort"
var (
_ sort.Interface = &wflist[int]{}
)
type wflist[T any] []*weightitem[T]
func (wfl wflist[T]) Less(i, j int) bool {
return wfl[i].adjusted < wfl[j].adjusted
}
func (wfl wflist[T]) Len() int {
return len(wfl)
}
func (wfl wflist[T]) Swap(i, j int) {
wfl[i], wfl[j] = wfl[j], wfl[i]
}
// WeightRandom is the Type for an weighted random list
type WeightRandom[T any] struct {
data wflist[T]
weightFunc AdjustFunc
randomFunc RandomFunc
sorted bool
totalWeight int
adjustedWeight int
sorti sort.Interface
accessfunc AccessModifier
}
func (wr *WeightRandom[T]) reweight() {
wr.updatetotal()
for _, item := range wr.data {
item.adjusted = wr.weightFunc(item.weight, wr.totalWeight)
}
}
func (wr *WeightRandom[T]) sort() {
if wr.sorted {
return
}
if wr.sorti == nil {
wr.sorti = sort.Reverse(&wr.data)
}
wr.reweight()
sort.Stable(wr.sorti)
wr.sorted = true
}
func (wr *WeightRandom[T]) updatetotal() {
newtotal := 0
newadjusted := 0
for _, item := range wr.data {
newtotal += item.weight
newadjusted += item.adjusted
}
wr.adjustedWeight = newadjusted
wr.totalWeight = newtotal
}
// AddElement adds an single Element to the list
func (wr *WeightRandom[T]) AddElement(item T, weight int) {
wr.sorted = false
wr.totalWeight += weight
wr.data = append(wr.data, &weightitem[T]{weight: weight, value: item})
}
// AddElements adds an list of elements to the list
func (wr *WeightRandom[T]) AddElements(weight int, items ...T) {
for _, item := range items {
wr.AddElement(item, weight)
}
wr.reweight()
wr.sort()
}
// Get returns an random value of the list of items or the null value of the type in case no element was added
func (wr *WeightRandom[T]) Get() (t T) {
if !wr.sorted {
wr.reweight()
wr.sort()
}
pos := wr.randomFunc(wr.data.Len())
for _, item := range wr.data {
pos -= item.adjusted
if pos <= 0 {
if wr.accessfunc != nil {
wr.sorted = false
item.weight = wr.accessfunc(item.weight)
}
return item.value
}
}
return
}
// Cleanup deletes the items where the the function returns true
func (wr *WeightRandom[T]) Cleanup(f func(T) bool) {
nl := make(wflist[T], 0, wr.data.Len())
for _, item := range wr.data {
if !f(item.value) {
nl = append(nl, item)
}
}
wr.data = nl
}