registry 0.1.3.0 → 0.1.3.1
raw patch · 5 files changed
+50/−4 lines, 5 files
Files
- registry.cabal +2/−2
- src/Data/Registry/Internal/Dynamic.hs +11/−0
- src/Data/Registry/Internal/Registry.hs +1/−1
- src/Data/Registry/Internal/Statistics.hs +7/−0
- test/Test/Data/Registry/Make/TweakingSpec.hs +29/−1
registry.cabal view
@@ -4,10 +4,10 @@ -- -- see: https://github.com/sol/hpack ----- hash: 9a42dc0b45e4a76624b48f457789494c6d57502a863d3397c88a28a0e85d57cf+-- hash: dc7abb1b0160688b046f2210abaffa3b985df621f1d47126e9920b573485fdb8 name: registry-version: 0.1.3.0+version: 0.1.3.1 synopsis: data structure for assembling components description: This library provides a "Registry" which is a data structure containing a list of functions and values representing dependencies in a directed acyclic graph. A `make` function can then be used to create a value of a specific type out of the registry. You can start with the [README](https://github.com/etorreborre/registry/blob/master/README.md) for a full description of the library.
src/Data/Registry/Internal/Dynamic.hs view
@@ -31,6 +31,17 @@ pure $ makeCreatedValue created description dependencies +-- | Apply a function modifying a single value and keeping its type+-- to be used with Modifiers+applyModification ::+ Function -- ^ function+ -> Value -- ^ inputs+ -> Either Text Value -- ^ result+applyModification function value =+ do created <- applyFunctionDyn (funDyn function) [valueDyn value]+ let description = ValueDescription (_outputType . funDescription $ function) Nothing+ pure $ CreatedValue created description (specializationContext value) (usedSpecialization value) (valDependencies value)+ -- | Apply a Dynamic function to a list of Dynamic values applyFunctionDyn :: Dynamic -- ^ function
src/Data/Registry/Internal/Registry.hs view
@@ -126,5 +126,5 @@ modifyValue :: Value -> [(SomeTypeRep, Function)] -> Stack Value modifyValue v [] = pure v modifyValue v ((_, f) : rest) = do- applied <- lift $ applyFunction f [v]+ applied <- lift $ applyModification f v modifyValue applied rest
src/Data/Registry/Internal/Statistics.hs view
@@ -1,3 +1,5 @@+{-# LANGUAGE AllowAmbiguousTypes #-}+ {- This module provides a set of statistics over the execution of the registry. This allows to get better insights over the execution@@ -7,6 +9,7 @@ import Data.Registry.Internal.Types import Protolude+import Type.Reflection -- * DATA TYPES @@ -69,3 +72,7 @@ (v :) <$> valuePaths d valuePaths _ = []++-- | Find the most recently created value of a given type+findMostRecentValue :: forall a . (Typeable a) => Statistics -> Maybe Value+findMostRecentValue stats = find (\v -> valueDynTypeRep v == someTypeRep (Proxy :: Proxy a)) $ unValues (values stats)
test/Test/Data/Registry/Make/TweakingSpec.hs view
@@ -1,9 +1,10 @@-{-# LANGUAGE DataKinds #-}+{-# LANGUAGE DataKinds #-} {-# OPTIONS_GHC -fno-warn-missing-signatures #-} module Test.Data.Registry.Make.TweakingSpec where import Data.Registry+import Data.Registry.Internal.Types import Protolude import Test.Tasty.Extensions @@ -26,3 +27,30 @@ newtype UseConfig1 = UseConfig1 { printConfig1 :: Config } newUseConfig1 config = UseConfig1 { printConfig1 = config }++-- * =========++test_tweak_non_lossy = test "a modified value must not lose its context, specialization or dependencies" $ do+ (a, stats) <- liftIO $+ do let r = val (C 1)+ +: fun B+ +: fun A+ +: end+ let r' = specialize @A @C (C 2) r+ let r'' = tweak (\(B (C _)) -> B (C 3)) r'+ pure (make @A r'', makeStatistics @A r'')++ -- The specialized value was 2 but after tweaking it is 3+ a === A (B (C 3))++ -- Get the value for the type C+ let cValue = findMostRecentValue @C stats+ isJust (specializationContext =<< cValue) === True+ isJust (usedSpecialization =<< cValue) === True++ -- this seems weird but a value is in the list of its dependencies+ (not . null) (valDependencies <$> cValue) === True++newtype A = A B deriving (Eq, Show)+newtype B = B C deriving (Eq, Show)+newtype C = C Int deriving (Eq, Show)