mirror of
https://github.com/TecharoHQ/anubis.git
synced 2026-04-10 10:38:45 +00:00
44 lines
789 B
Go
44 lines
789 B
Go
package expression
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
|
|
"github.com/TecharoHQ/anubis/lib/checker"
|
|
)
|
|
|
|
func init() {
|
|
checker.Register("expression", Factory{})
|
|
}
|
|
|
|
type Factory struct{}
|
|
|
|
func (f Factory) Build(ctx context.Context, data json.RawMessage) (checker.Interface, error) {
|
|
var fc = &Config{}
|
|
|
|
if err := json.Unmarshal([]byte(data), fc); err != nil {
|
|
return nil, errors.Join(checker.ErrUnparseableConfig, err)
|
|
}
|
|
|
|
if err := fc.Valid(); err != nil {
|
|
return nil, errors.Join(checker.ErrInvalidConfig, err)
|
|
}
|
|
|
|
return New(fc)
|
|
}
|
|
|
|
func (f Factory) Valid(ctx context.Context, data json.RawMessage) error {
|
|
var fc = &Config{}
|
|
|
|
if err := json.Unmarshal([]byte(data), fc); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := fc.Valid(); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|