|
|
@ -14,20 +14,40 @@ 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 {
|
|
|
|
return fmt.Errorf(`missing "key" field`)
|
|
|
|
if len(defaultValue) > 0 {
|
|
|
|
|
|
|
|
return defaultValue[0], nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var zero T
|
|
|
|
|
|
|
|
return zero, fmt.Errorf(`missing "%s" field`, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
key, ok := v.(string)
|
|
|
|
|
|
|
|
|
|
|
|
value, ok := v.(T)
|
|
|
|
if !ok {
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(`expected string but got "%v" of type %T`, v, v)
|
|
|
|
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.Key = key
|
|
|
|
op.CategoryVariable, err = getKey(config, "bind", "Category")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
@ -69,7 +89,7 @@ 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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|