http urls monitor.

compound.go 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright (c) 2012-present The upper.io/db authors. All rights reserved.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining
  4. // a copy of this software and associated documentation files (the
  5. // "Software"), to deal in the Software without restriction, including
  6. // without limitation the rights to use, copy, modify, merge, publish,
  7. // distribute, sublicense, and/or sell copies of the Software, and to
  8. // permit persons to whom the Software is furnished to do so, subject to
  9. // the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be
  12. // included in all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. package db
  22. import (
  23. "upper.io/db.v3/internal/immutable"
  24. )
  25. // Compound represents an statement that has one or many sentences joined by by
  26. // an operator like "AND" or "OR". This is an exported interface but it's
  27. // rarely used directly, you may want to use the `db.And()` or `db.Or()`
  28. // functions instead.
  29. type Compound interface {
  30. // Sentences returns child sentences.
  31. Sentences() []Compound
  32. // Operator returns the operator that joins the compound's child sentences.
  33. Operator() CompoundOperator
  34. // Empty returns true if the compound has zero children, false otherwise.
  35. Empty() bool
  36. }
  37. // CompoundOperator represents the operation on a compound statement.
  38. type CompoundOperator uint
  39. // Compound operators.
  40. const (
  41. OperatorNone CompoundOperator = iota
  42. OperatorAnd
  43. OperatorOr
  44. )
  45. type compound struct {
  46. prev *compound
  47. fn func(*[]Compound) error
  48. }
  49. func newCompound(conds ...Compound) *compound {
  50. c := &compound{}
  51. if len(conds) == 0 {
  52. return c
  53. }
  54. return c.frame(func(in *[]Compound) error {
  55. *in = append(*in, conds...)
  56. return nil
  57. })
  58. }
  59. // Sentences returns each one of the conditions as a compound.
  60. func (c *compound) Sentences() []Compound {
  61. conds, err := immutable.FastForward(c)
  62. if err == nil {
  63. return *(conds.(*[]Compound))
  64. }
  65. return nil
  66. }
  67. // Operator returns no operator.
  68. func (c *compound) Operator() CompoundOperator {
  69. return OperatorNone
  70. }
  71. // Empty returns true if this condition has no elements. False otherwise.
  72. func (c *compound) Empty() bool {
  73. if c.fn != nil {
  74. return false
  75. }
  76. if c.prev != nil {
  77. return c.prev.Empty()
  78. }
  79. return true
  80. }
  81. func (c *compound) frame(fn func(*[]Compound) error) *compound {
  82. return &compound{prev: c, fn: fn}
  83. }
  84. // Prev is for internal usage.
  85. func (c *compound) Prev() immutable.Immutable {
  86. if c == nil {
  87. return nil
  88. }
  89. return c.prev
  90. }
  91. // Fn is for internal usage.
  92. func (c *compound) Fn(in interface{}) error {
  93. if c.fn == nil {
  94. return nil
  95. }
  96. return c.fn(in.(*[]Compound))
  97. }
  98. // Base is for internal usage.
  99. func (c *compound) Base() interface{} {
  100. return &[]Compound{}
  101. }
  102. func defaultJoin(in ...Compound) []Compound {
  103. for i := range in {
  104. if cond, ok := in[i].(Cond); ok && len(cond) > 1 {
  105. in[i] = And(cond)
  106. }
  107. }
  108. return in
  109. }
  110. var _ = immutable.Immutable(&compound{})
  111. var _ Compound = Cond{}