v0.0.521 ctxext
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 2m56s
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 2m56s
This commit is contained in:
parent
9f85a243e8
commit
361dca5c85
@ -20,6 +20,7 @@ Potentially needs `export GOPRIVATE="gogs.mikescher.com"`
|
|||||||
| zipext | Mike | Utility for zip/gzip/tar etc |
|
| zipext | Mike | Utility for zip/gzip/tar etc |
|
||||||
| reflectext | Mike | Utility for golang reflection |
|
| reflectext | Mike | Utility for golang reflection |
|
||||||
| fsext | Mike | Utility for filesytem access |
|
| fsext | Mike | Utility for filesytem access |
|
||||||
|
| ctxext | Mike | Utility for context.Context |
|
||||||
| | | |
|
| | | |
|
||||||
| mongoext | Mike | Utility/Helper functions for mongodb (kinda abandoned) |
|
| mongoext | Mike | Utility/Helper functions for mongodb (kinda abandoned) |
|
||||||
| cursortoken | Mike | MongoDB cursortoken implementation |
|
| cursortoken | Mike | MongoDB cursortoken implementation |
|
||||||
|
27
ctxext/getter.go
Normal file
27
ctxext/getter.go
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package ctxext
|
||||||
|
|
||||||
|
import "context"
|
||||||
|
|
||||||
|
func Value[T any](ctx context.Context, key any) (T, bool) {
|
||||||
|
v := ctx.Value(key)
|
||||||
|
if v == nil {
|
||||||
|
return *new(T), false
|
||||||
|
}
|
||||||
|
if tv, ok := v.(T); !ok {
|
||||||
|
return *new(T), false
|
||||||
|
} else {
|
||||||
|
return tv, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ValueOrDefault[T any](ctx context.Context, key any, def T) T {
|
||||||
|
v := ctx.Value(key)
|
||||||
|
if v == nil {
|
||||||
|
return def
|
||||||
|
}
|
||||||
|
if tv, ok := v.(T); !ok {
|
||||||
|
return def
|
||||||
|
} else {
|
||||||
|
return tv
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,5 @@
|
|||||||
package goext
|
package goext
|
||||||
|
|
||||||
const GoextVersion = "0.0.520"
|
const GoextVersion = "0.0.521"
|
||||||
|
|
||||||
const GoextVersionTimestamp = "2024-10-05T01:02:25+0200"
|
const GoextVersionTimestamp = "2024-10-05T01:06:36+0200"
|
||||||
|
Loading…
Reference in New Issue
Block a user