@@ -14,7 +14,7 @@ import Data.Hash.Murmur (murmur3)
14
14
import Data.List (find )
15
15
import Data.Map.Strict (Map , fromList )
16
16
import qualified Data.Map.Strict as Map
17
- import Data.Maybe (catMaybes , fromMaybe , listToMaybe , maybeToList )
17
+ import Data.Maybe (catMaybes , fromMaybe )
18
18
import Data.Text (Text )
19
19
import qualified Data.Text as Text
20
20
import Data.Text.Encoding (encodeUtf8 )
@@ -131,7 +131,7 @@ fromJsonFeature segmentMap jsonFeature =
131
131
132
132
fromJsonStrategy :: MonadIO m => FeatureToggleName -> Map Int [JsonTypes. Constraint ] -> JsonTypes. Strategy -> (JsonTypes. Context -> m Bool )
133
133
fromJsonStrategy featureToggleName segmentMap jsonStrategy =
134
- \ ctx -> liftA2 (&&) (strategyFunction ctx) (constraintsFunction ctx)
134
+ \ ctx -> liftA2 (&&) (strategyFunction ctx) (constraintsPredicate ctx)
135
135
where
136
136
strategyFunction :: MonadIO m => JsonTypes. Context -> m Bool
137
137
strategyFunction =
@@ -210,27 +210,24 @@ fromJsonStrategy featureToggleName segmentMap jsonStrategy =
210
210
_ -> pure . \ _ctx -> False
211
211
212
212
segmentsToConstraints :: [Int ] -> Map Int [JsonTypes. Constraint ] -> [Maybe JsonTypes. Constraint ]
213
- segmentsToConstraints segmentRefs segmentMap =
214
- concat $ sequence <$> ((flip Map. lookup ) segmentMap <$> segmentRefs)
215
-
216
- constraintsFunction :: MonadIO m => JsonTypes. Context -> m Bool
217
- constraintsFunction ctx = do
218
- let segmentRefs = concat jsonStrategy. segments
219
- segmentConstraints = segmentsToConstraints segmentRefs segmentMap
220
- justSegmentConstraints = catMaybes segmentConstraints
221
- nothingConstraints =
222
- maybeToList $
223
- listToMaybe $
224
- catMaybes $
225
- ( \ maybeConstraints -> case maybeConstraints of
226
- Nothing -> Just $ const False
227
- Just _ -> Nothing
228
- )
229
- <$> segmentConstraints
213
+ segmentsToConstraints segmentReferences segmentMap =
214
+ concat $ sequence <$> ((flip Map. lookup ) segmentMap <$> segmentReferences)
215
+
216
+ constraintsPredicate :: MonadIO m => JsonTypes. Context -> m Bool
217
+ constraintsPredicate ctx = do
218
+ let segmentReferences = concat jsonStrategy. segments
219
+ maybeSegmentConstraints = segmentsToConstraints segmentReferences segmentMap
220
+ segmentConstraints = catMaybes maybeSegmentConstraints
230
221
strategyConstraints = fromMaybe [] jsonStrategy. constraints
231
- allConstraints = justSegmentConstraints <> strategyConstraints
232
- constraints :: [JsonTypes. Context -> Bool ] = (fromJsonConstraint <$> allConstraints) <> nothingConstraints
233
- pure $ null constraints || and ((\ f -> f ctx) <$> constraints)
222
+ allConstraints = segmentConstraints <> strategyConstraints
223
+ allPredicates = fromJsonConstraint <$> allConstraints
224
+ allSegmentConstraintsAreReferredTo = not $ Nothing `elem` maybeSegmentConstraints
225
+ allPredicatesAreSatisfied = allSegmentConstraintsAreReferredTo && and (evaluatePredicate <$> allPredicates)
226
+ thereAreNoPredicates = null allPredicates
227
+ pure $ thereAreNoPredicates || allPredicatesAreSatisfied
228
+ where
229
+ evaluatePredicate :: (JsonTypes. Context -> Bool ) -> Bool
230
+ evaluatePredicate f = f ctx
234
231
235
232
fromJsonConstraint :: JsonTypes. Constraint -> (JsonTypes. Context -> Bool )
236
233
fromJsonConstraint constraint = \ ctx -> do
0 commit comments