added the generic Valid function
Dieser Commit ist enthalten in:
Ursprung
af1af1afc2
Commit
2002010a1e
2
go.mod
2
go.mod
|
@ -1,6 +1,6 @@
|
||||||
module gitea.sebastian-tobie.de/sebastian/generic
|
module gitea.sebastian-tobie.de/sebastian/generic
|
||||||
|
|
||||||
go 1.18
|
go 1.19
|
||||||
|
|
||||||
require github.com/rs/zerolog v1.27.0
|
require github.com/rs/zerolog v1.27.0
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
Laden…
In neuem Issue referenzieren