From 3252d73235851e88641376fcb82abdcaab5c6906 Mon Sep 17 00:00:00 2001 From: Antonio De Lucreziis Date: Sat, 31 Dec 2022 18:34:05 +0100 Subject: [PATCH] feat: the categorize category variable can now be configured with bind --- operation/categorize.go | 46 +++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/operation/categorize.go b/operation/categorize.go index 7de9786..bf22ba2 100644 --- a/operation/categorize.go +++ b/operation/categorize.go @@ -13,21 +13,41 @@ func init() { } type Categorize struct { - Key string + Key string + CategoryVariable string } -func (op *Categorize) Load(config map[string]any) error { - { - v, ok := config["key"] - if !ok { - return fmt.Errorf(`missing "key" field`) - } - key, ok := v.(string) - if !ok { - return fmt.Errorf(`expected string but got "%v" of type %T`, v, v) +func getKey[T any](m map[string]any, key string, defaultValue ...T) (T, error) { + v, ok := m[key] + if !ok { + if len(defaultValue) > 0 { + return defaultValue[0], nil } - op.Key = key + var zero T + return zero, fmt.Errorf(`missing "%s" field`, key) + } + + value, ok := v.(T) + if !ok { + var zero T + return zero, fmt.Errorf(`expected %T but got "%v" of type %T`, zero, v, v) + } + + return value, nil +} + +func (op *Categorize) Load(config map[string]any) error { + var err error + + op.Key, err = getKey[string](config, "key") + if err != nil { + return err + } + + op.CategoryVariable, err = getKey(config, "bind", "Category") + if err != nil { + return err } return nil @@ -69,8 +89,8 @@ func (op *Categorize) ProcessList(contents []cabret.Content) ([]cabret.Content, result = append(result, cabret.Content{ Type: "metadata-only", Metadata: cabret.Map{ - "Category": name, - "Items": contents, + op.CategoryVariable: name, + "Items": contents, }, }) }