go.temporal.io/server@v1.23.0/common/predicates/and.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Copyright (c) 2020 Uber Technologies, Inc.
     6  //
     7  // Permission is hereby granted, free of charge, to any person obtaining a copy
     8  // of this software and associated documentation files (the "Software"), to deal
     9  // in the Software without restriction, including without limitation the rights
    10  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11  // copies of the Software, and to permit persons to whom the Software is
    12  // furnished to do so, subject to the following conditions:
    13  //
    14  // The above copyright notice and this permission notice shall be included in
    15  // all copies or substantial portions of the Software.
    16  //
    17  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    20  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  // THE SOFTWARE.
    24  
    25  package predicates
    26  
    27  import (
    28  	"fmt"
    29  )
    30  
    31  type (
    32  	AndImpl[T any] struct {
    33  		// TODO: see if we can somehow order arbitrary predicats and store a sorted list
    34  		Predicates []Predicate[T]
    35  	}
    36  )
    37  
    38  func And[T any](
    39  	predicates ...Predicate[T],
    40  ) Predicate[T] {
    41  	if len(predicates) < 2 {
    42  		panic(fmt.Sprintf("And requires at least 2 predicates, got %v", len(predicates)))
    43  	}
    44  
    45  	flattened := make([]Predicate[T], 0, len(predicates))
    46  	for _, p := range predicates {
    47  		switch p := p.(type) {
    48  		case *AndImpl[T]:
    49  			flattened = appendPredicates(flattened, p.Predicates...)
    50  		case *UniversalImpl[T]:
    51  			continue
    52  		case *EmptyImpl[T]:
    53  			return p
    54  		default:
    55  			flattened = appendPredicates(flattened, p)
    56  		}
    57  	}
    58  
    59  	switch len(flattened) {
    60  	case 0:
    61  		return Universal[T]()
    62  	case 1:
    63  		return flattened[0]
    64  	default:
    65  		return &AndImpl[T]{
    66  			Predicates: flattened,
    67  		}
    68  	}
    69  }
    70  
    71  func (a *AndImpl[T]) Test(t T) bool {
    72  	for _, p := range a.Predicates {
    73  		if !p.Test(t) {
    74  			return false
    75  		}
    76  	}
    77  
    78  	return true
    79  }
    80  
    81  func (a *AndImpl[T]) Equals(
    82  	predicate Predicate[T],
    83  ) bool {
    84  	andPredicate, ok := predicate.(*AndImpl[T])
    85  	if !ok {
    86  		return false
    87  	}
    88  
    89  	return predicatesEqual(a.Predicates, andPredicate.Predicates)
    90  }
    91  
    92  // appendPredicates adds new predicates to the slice of existing predicates
    93  // dropping any duplicated predicates where duplication is determined by Predicate.Equals.
    94  // appendPredicates assumes that there's no duplication in new predicates.
    95  func appendPredicates[T any](
    96  	current []Predicate[T],
    97  	new ...Predicate[T],
    98  ) []Predicate[T] {
    99  	result := current
   100  
   101  AppendLoop:
   102  	for _, newPredicate := range new {
   103  		for _, currentPredicate := range current {
   104  			if currentPredicate.Equals(newPredicate) {
   105  				continue AppendLoop
   106  			}
   107  		}
   108  
   109  		result = append(result, newPredicate)
   110  	}
   111  
   112  	return result
   113  }
   114  
   115  // predicatesEqual assumes there's no duplication in the given slices of predicates
   116  func predicatesEqual[T any](
   117  	this []Predicate[T],
   118  	that []Predicate[T],
   119  ) bool {
   120  	if len(this) != len(that) {
   121  		return false
   122  	}
   123  
   124  MatchLoop:
   125  	for _, thisPredicate := range this {
   126  		for _, thatPredicate := range that {
   127  			if thisPredicate.Equals(thatPredicate) {
   128  				continue MatchLoop
   129  			}
   130  		}
   131  
   132  		return false
   133  	}
   134  
   135  	return true
   136  }