any_of.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2011 Aaron Jacobs. All Rights Reserved.
  2. // Author: aaronjjacobs@gmail.com (Aaron Jacobs)
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. package oglematchers
  16. import (
  17. "errors"
  18. "fmt"
  19. "reflect"
  20. "strings"
  21. )
  22. // AnyOf accepts a set of values S and returns a matcher that follows the
  23. // algorithm below when considering a candidate c:
  24. //
  25. // 1. If there exists a value m in S such that m implements the Matcher
  26. // interface and m matches c, return true.
  27. //
  28. // 2. Otherwise, if there exists a value v in S such that v does not implement
  29. // the Matcher interface and the matcher Equals(v) matches c, return true.
  30. //
  31. // 3. Otherwise, if there is a value m in S such that m implements the Matcher
  32. // interface and m returns a fatal error for c, return that fatal error.
  33. //
  34. // 4. Otherwise, return false.
  35. //
  36. // This is akin to a logical OR operation for matchers, with non-matchers x
  37. // being treated as Equals(x).
  38. func AnyOf(vals ...interface{}) Matcher {
  39. // Get ahold of a type variable for the Matcher interface.
  40. var dummy *Matcher
  41. matcherType := reflect.TypeOf(dummy).Elem()
  42. // Create a matcher for each value, or use the value itself if it's already a
  43. // matcher.
  44. wrapped := make([]Matcher, len(vals))
  45. for i, v := range vals {
  46. t := reflect.TypeOf(v)
  47. if t != nil && t.Implements(matcherType) {
  48. wrapped[i] = v.(Matcher)
  49. } else {
  50. wrapped[i] = Equals(v)
  51. }
  52. }
  53. return &anyOfMatcher{wrapped}
  54. }
  55. type anyOfMatcher struct {
  56. wrapped []Matcher
  57. }
  58. func (m *anyOfMatcher) Description() string {
  59. wrappedDescs := make([]string, len(m.wrapped))
  60. for i, matcher := range m.wrapped {
  61. wrappedDescs[i] = matcher.Description()
  62. }
  63. return fmt.Sprintf("or(%s)", strings.Join(wrappedDescs, ", "))
  64. }
  65. func (m *anyOfMatcher) Matches(c interface{}) (err error) {
  66. err = errors.New("")
  67. // Try each matcher in turn.
  68. for _, matcher := range m.wrapped {
  69. wrappedErr := matcher.Matches(c)
  70. // Return immediately if there's a match.
  71. if wrappedErr == nil {
  72. err = nil
  73. return
  74. }
  75. // Note the fatal error, if any.
  76. if _, isFatal := wrappedErr.(*FatalError); isFatal {
  77. err = wrappedErr
  78. }
  79. }
  80. return
  81. }