Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

removed lens and import cleanup #34

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ dependencies:
- http-client-tls >=0.3.5.3 && <0.4
- http-types >=0.12.3 && <0.13
- iso8601-time >=0.1.5 && <0.2
- lens >=4.17.1 && <4.20
- lrucache >=1.2.0.1 && <1.3
- monad-logger >=0.3.30 && <0.4
- mtl >=2.2.2 && <2.3
Expand Down
1 change: 0 additions & 1 deletion src/LaunchDarkly/Server/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import Control.Monad.Logger (LoggingT, runStdoutLoggingT)
import Data.Generics.Product (setField)
import Data.Set (Set)
import Data.Text (Text)
import Data.Monoid (mempty)
import GHC.Natural (Natural)
import Network.HTTP.Client (Manager)

Expand Down
10 changes: 5 additions & 5 deletions src/LaunchDarkly/Server/Evaluate.hs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
module LaunchDarkly.Server.Evaluate where

import Control.Lens ((%~))
import Data.Generics.Internal.VL (over)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Data.Generics.Internal.VL is not exposed until generic-lens 2.0.0.0, and the current minimum in package.yaml is 1.1.0.0.

import Control.Monad (mzero, msum)
import Control.Monad.Extra (ifM, anyM, allM, firstJustM)
import Crypto.Hash.SHA1 (hash)
import Data.Scientific (Scientific, floatingOrInteger)
import Data.Either (either, fromLeft)
import Data.Either (fromLeft)
import Data.Function ((&))
import Data.Aeson.Types (Value(..))
import Data.Maybe (maybe, fromJust, isJust, fromMaybe)
import Data.Maybe (fromJust, isJust, fromMaybe)
import Data.Text (Text)
import Data.Generics.Product (getField, field)
import Data.List (genericIndex, null, find)
import Data.List (genericIndex, find)
import qualified Data.Vector as V
import qualified Data.Text as T
import qualified Data.ByteString as B
Expand Down Expand Up @@ -130,7 +130,7 @@ getValueForVariationOrRollout :: Flag -> VariationOrRollout -> UserI -> Evaluati
getValueForVariationOrRollout flag vr user reason =
case variationIndexForUser vr user (getField @"key" flag) (getField @"salt" flag) of
(Nothing, _) -> errorDetail EvalErrorKindMalformedFlag
(Just x, inExperiment) -> (getVariation flag x reason) & field @"reason" %~ setInExperiment inExperiment
(Just x, inExperiment) -> (getVariation flag x reason) & field @"reason" `over` setInExperiment inExperiment

setInExperiment :: Bool -> EvaluationReason -> EvaluationReason
setInExperiment inExperiment reason = case reason of
Expand Down
9 changes: 5 additions & 4 deletions src/LaunchDarkly/Server/Events.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import Control.Concurrent.MVar (MVar, putMVar, swapMVar, n
import qualified Data.HashMap.Strict as HM
import Data.HashMap.Strict (HashMap)
import Data.Time.Clock.POSIX (getPOSIXTime)
import Control.Lens ((&), (%~))
import Data.Generics.Internal.VL (over)
import Data.Function ((&))
import Data.Maybe (fromMaybe)
import Data.Cache.LRU (LRU, newLRU)
import Control.Monad (when)
Expand Down Expand Up @@ -66,7 +67,7 @@ makeEventState config = do

convertFeatures :: HashMap Text (FlagSummaryContext (HashMap Text CounterContext))
-> HashMap Text (FlagSummaryContext [CounterContext])
convertFeatures summary = (flip HM.map) summary $ \context -> context & field @"counters" %~ HM.elems
convertFeatures summary = (flip HM.map) summary $ \context -> context & field @"counters" `over` HM.elems

queueEvent :: ConfigI -> EventState -> EventType -> IO ()
queueEvent config state event = if not (shouldSendEvents config) then pure () else
Expand Down Expand Up @@ -314,7 +315,7 @@ summarizeEvent context event unknown = result where
root = case HM.lookup (getField @"key" event) context of
(Just x) -> x; Nothing -> FlagSummaryContext (getField @"defaultValue" event) mempty
leaf = case HM.lookup key (getField @"counters" root) of
(Just x) -> x & field @"count" %~ (1 +)
(Just x) -> x & field @"count" `over` (1 +)
Nothing -> CounterContext
{ count = 1
, version = getField @"version" event
Expand All @@ -323,7 +324,7 @@ summarizeEvent context event unknown = result where
, unknown = unknown
}
result = flip (HM.insert $ getField @"key" event) context $
root & field @"counters" %~ HM.insert key leaf
root & field @"counters" `over` HM.insert key leaf

putIfEmptyMVar :: MVar a -> a -> IO ()
putIfEmptyMVar mvar value = tryTakeMVar mvar >>= \case Just x -> putMVar mvar x; Nothing -> putMVar mvar value;
Expand Down
31 changes: 15 additions & 16 deletions src/LaunchDarkly/Server/Operators.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@ module LaunchDarkly.Server.Operators
, getOperation
) where

import Data.Maybe (fromMaybe, isJust)
import Data.Either (fromRight)
import Data.Text as T
import Data.Text (Text, isPrefixOf, isInfixOf, isSuffixOf, unpack)
import Data.Char (isDigit)
import Data.Text.Encoding (encodeUtf8)
import Data.Scientific (Scientific, toRealFloat)
import Data.Aeson.Types (Value(..), FromJSON, ToJSON(..), withText, parseJSON)
import Data.Time.ISO8601 (parseISO8601)
import Data.Time.Clock (UTCTime)
import Data.Time.Clock.POSIX (POSIXTime, posixSecondsToUTCTime)
import Data.SemVer (Version, fromText, toText, metadata)
import Control.Monad (liftM2)
import Control.Lens ((.~))
import GHC.Generics (Generic)
import Text.Regex.PCRE.Light (compileM, match)
import Data.Maybe (fromMaybe, isJust)
import Data.Either (fromRight)
import Data.Text as T
import Data.Char (isDigit)
import Data.Text.Encoding (encodeUtf8)
import Data.Scientific (Scientific, toRealFloat)
import Data.Aeson.Types (Value(..), FromJSON, ToJSON(..), withText, parseJSON)
import Data.Time.ISO8601 (parseISO8601)
import Data.Time.Clock (UTCTime)
import Data.Time.Clock.POSIX (POSIXTime, posixSecondsToUTCTime)
import Data.SemVer (Version, fromText, toText, metadata)
import Control.Monad (liftM2)
import Data.Generics.Internal.VL ((.~))
import GHC.Generics (Generic)
import Text.Regex.PCRE.Light (compileM, match)

data Op =
OpIn
Expand Down
22 changes: 11 additions & 11 deletions src/LaunchDarkly/Server/Store/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module LaunchDarkly.Server.Store.Internal
) where

import Control.Monad (void)
import Control.Lens (Lens', (%~), (^.))
import Data.Generics.Internal.VL (Lens', (^.), over)
import Data.Aeson (ToJSON, FromJSON, encode, decode)
import Data.IORef (IORef, readIORef, atomicModifyIORef', newIORef)
import Data.ByteString (ByteString)
Expand Down Expand Up @@ -179,10 +179,10 @@ data Store = Store

expireAllItems :: Store -> IO ()
expireAllItems store = atomicModifyIORef' (getField @"state" store) $ \state -> (, ()) $ state
& field @"allFlags" %~ expire
& field @"initialized" %~ expire
& field @"flags" %~ HM.map expire
& field @"segments" %~ HM.map expire
& field @"allFlags" `over` expire
& field @"initialized" `over` expire
& field @"flags" `over` HM.map expire
& field @"segments" `over` HM.map expire
where expire = setField @"forceExpire" True

isExpired :: Store -> TimeSpec -> Expirable a -> Bool
Expand All @@ -208,7 +208,7 @@ initialize store flags segments = case getField @"backend" store of
raw = HM.empty
& HM.insert "flags" (HM.map versionedToRaw $ c flags)
& HM.insert "segments" (HM.map versionedToRaw $ c segments)
c x = HM.map (\f -> f & field @"value" %~ Just) x
c x = HM.map (\f -> f & field @"value" `over` Just) x

rawToVersioned :: (FromJSON a) => RawFeature -> Maybe (Versioned (Maybe a))
rawToVersioned raw = case rawFeatureBuffer raw of
Expand Down Expand Up @@ -250,7 +250,7 @@ getGeneric store namespace key lens = do
updateFromBackend backend now = tryGetBackend backend namespace key >>= \case
Left err -> pure $ Left err
Right v -> do
atomicModifyIORef' (getField @"state" store) $ \stateRef -> (, ()) $ stateRef & lens %~
atomicModifyIORef' (getField @"state" store) $ \stateRef -> (, ()) $ stateRef & lens `over`
(HM.insert key (Expirable v False now))
pure $ Right $ getField @"value" v

Expand All @@ -276,7 +276,7 @@ upsertGeneric store namespace key versioned lens action = do
Right updated -> if not updated then pure (Right ()) else do
now <- getMonotonicTime
void $ atomicModifyIORef' (getField @"state" store) $ \stateRef -> (, ()) $ stateRef
& lens %~ (HM.insert key (Expirable versioned False now))
& lens `over` (HM.insert key (Expirable versioned False now))
& action True
pure $ Right ()
where
Expand All @@ -285,14 +285,14 @@ upsertGeneric store namespace key versioned lens action = do
Just existing -> if (getField @"version" $ getField @"value" existing) < getField @"version" versioned
then updateMemory state else state
updateMemory state = state
& lens %~ (HM.insert key (Expirable versioned False 0))
& lens `over` (HM.insert key (Expirable versioned False 0))
& action False

upsertFlag :: Store -> Text -> Versioned (Maybe Flag) -> StoreResult ()
upsertFlag store key versioned = upsertGeneric store "flags" key versioned (field @"flags") postAction where
postAction external state = if external
then state & field @"allFlags" %~ (setField @"forceExpire" True)
else state & (field @"allFlags" . field @"value") %~ updateAllFlags
then state & field @"allFlags" `over` (setField @"forceExpire" True)
else state & (field @"allFlags" . field @"value") `over` updateAllFlags
updateAllFlags allFlags = case getField @"value" versioned of
Nothing -> HM.delete key allFlags
Just flag -> HM.insert key flag allFlags
Expand Down
2 changes: 1 addition & 1 deletion src/LaunchDarkly/Server/User/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module LaunchDarkly.Server.User.Internal
) where

import Data.Aeson (FromJSON, ToJSON, Value(..), (.:), (.:?), withObject, object, parseJSON, toJSON)
import Data.Foldable (fold, or)
import Data.Foldable (fold)
import Data.Generics.Product (getField)
import qualified Data.HashMap.Strict as HM
import Data.HashMap.Strict (HashMap)
Expand Down