Minor changes to util functions

This commit is contained in:
David Allen 2024-09-20 16:56:15 -06:00
parent 601089672c
commit 751a2facdb
Signed by: towk
GPG key ID: 793B2924A49B3A3F
2 changed files with 25 additions and 5 deletions

View file

@ -11,7 +11,7 @@ type Params map[string]any
type Option func(Params)
// Extract all parameters from the options passed as map[string]any.
func GetParams(opts ...Option) Params {
func ToDict(opts ...Option) Params {
params := Params{}
for _, opt := range opts {
opt(params)
@ -45,8 +45,8 @@ func WithDefault[T any](v T) Option {
}
}
// Syntactic sugar generic function to get parameter from util.Params.
func Get[T any](params Params, key string, opts ...Option) *T {
// Sugary generic function to get parameter from util.Params.
func Get[T any](params Params, key string) *T {
if v, ok := params[key].(T); ok {
return &v
}
@ -55,3 +55,16 @@ func Get[T any](params Params, key string, opts ...Option) *T {
}
return nil
}
func GetOpt[T any](opts []Option, key string) *T {
return Get[T](ToDict(opts...), "required_claims")
}
func (p Params) GetVerbose() bool {
if verbose, ok := p["verbose"].(bool); ok {
return verbose
}
// default setting
return false
}