feat: the categorize category variable can now be configured with bind

main
Antonio De Lucreziis 2 years ago
parent 165fffe91b
commit 3252d73235

@ -13,21 +13,41 @@ func init() {
} }
type Categorize struct { type Categorize struct {
Key string Key string
CategoryVariable string
} }
func (op *Categorize) Load(config map[string]any) error { func getKey[T any](m map[string]any, key string, defaultValue ...T) (T, error) {
{ v, ok := m[key]
v, ok := config["key"] if !ok {
if !ok { if len(defaultValue) > 0 {
return fmt.Errorf(`missing "key" field`) return defaultValue[0], nil
}
key, ok := v.(string)
if !ok {
return fmt.Errorf(`expected string but got "%v" of type %T`, v, v)
} }
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 return nil
@ -69,8 +89,8 @@ func (op *Categorize) ProcessList(contents []cabret.Content) ([]cabret.Content,
result = append(result, cabret.Content{ result = append(result, cabret.Content{
Type: "metadata-only", Type: "metadata-only",
Metadata: cabret.Map{ Metadata: cabret.Map{
"Category": name, op.CategoryVariable: name,
"Items": contents, "Items": contents,
}, },
}) })
} }

Loading…
Cancel
Save