thresholds.go 851 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package policy
  2. import (
  3. "github.com/TecharoHQ/anubis/lib/config"
  4. "github.com/TecharoHQ/anubis/lib/policy/expressions"
  5. "github.com/google/cel-go/cel"
  6. )
  7. type Threshold struct {
  8. config.Threshold
  9. Program cel.Program
  10. }
  11. func ParsedThresholdFromConfig(t config.Threshold) (*Threshold, error) {
  12. result := &Threshold{
  13. Threshold: t,
  14. }
  15. env, err := expressions.ThresholdEnvironment()
  16. if err != nil {
  17. return nil, err
  18. }
  19. program, err := expressions.Compile(env, t.Expression.String())
  20. if err != nil {
  21. return nil, err
  22. }
  23. result.Program = program
  24. return result, nil
  25. }
  26. type ThresholdRequest struct {
  27. Weight int
  28. }
  29. func (tr *ThresholdRequest) Parent() cel.Activation { return nil }
  30. func (tr *ThresholdRequest) ResolveName(name string) (any, bool) {
  31. switch name {
  32. case "weight":
  33. return tr.Weight, true
  34. default:
  35. return nil, false
  36. }
  37. }