chained.go 975 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package rule
  2. import (
  3. "github.com/kataras/iris/context"
  4. )
  5. // chainedRule is a Rule with next Rule
  6. type chainedRule struct {
  7. Rule
  8. next Rule
  9. }
  10. var _ Rule = &chainedRule{}
  11. // chainedSingle returns a new rule witch has a next rule too
  12. func chainedSingle(rule Rule, next Rule) Rule {
  13. if next == nil {
  14. next = Satisfied()
  15. }
  16. return &chainedRule{
  17. Rule: rule,
  18. next: next,
  19. }
  20. }
  21. // Chained returns a new rule which has more than one coming next ruleset
  22. func Chained(rule Rule, next ...Rule) Rule {
  23. if len(next) == 0 {
  24. return chainedSingle(rule, nil)
  25. }
  26. c := chainedSingle(rule, next[0])
  27. for i := 1; i < len(next); i++ {
  28. c = chainedSingle(c, next[i])
  29. }
  30. return c
  31. }
  32. // Claim validator
  33. func (c *chainedRule) Claim(ctx context.Context) bool {
  34. if !c.Rule.Claim(ctx) {
  35. return false
  36. }
  37. return c.next.Claim(ctx)
  38. }
  39. // Valid validator
  40. func (c *chainedRule) Valid(ctx context.Context) bool {
  41. if !c.Rule.Valid(ctx) {
  42. return false
  43. }
  44. return c.next.Valid(ctx)
  45. }