Skip to content

Commit d81f509

Browse files
committed
wb | allow to code overlays
1 parent f41a596 commit d81f509

File tree

4 files changed

+57
-44
lines changed

4 files changed

+57
-44
lines changed

bench/cardano-profile/app/cardano-profile.hs

+27-22
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ playgroundProfiles = profilesNoEraPlayground
7777
--------------------------------------------------------------------------------
7878

7979
data Cli =
80-
Names
81-
| NamesNoEra
80+
NamesNoEra
8281
| NamesCloudNoEra
82+
| Names
8383
| All
8484
| ByName PrettyPrint String
8585
| LibMK
@@ -94,11 +94,14 @@ data PrettyPrint =
9494
--------------------------------------------------------------------------------
9595

9696
-- | Construct Map with profile name as key, without eras (in name and object).
97-
toMap :: HasCallStack => Aeson.Object -> [Types.Profile] -> Map.Map String Types.Profile
98-
toMap obj ps = Map.fromList $ map
97+
toMap :: HasCallStack => Maybe Aeson.Object -> [Types.Profile] -> Map.Map String Types.Profile
98+
toMap maybeObj ps = Map.fromList $ map
9999
(\p ->
100100
( Types.name p
101-
, Profile.realize obj p
101+
, Profile.realize $
102+
case maybeObj of
103+
Nothing -> p
104+
(Just obj) -> (p {Types.overlay = obj})
102105
)
103106
)
104107
ps
@@ -132,20 +135,22 @@ main :: IO ()
132135
main = do
133136
cli <- getOpts
134137
case cli of
135-
-- Print all profile names (does not apply overlays).
136-
Names -> BSL8.putStrLn $ Aeson.encode $ Map.keys $ addEras $ toMap mempty allProfiles
137138
-- Print all profile names without the era suffix (does not apply overlays).
138139
NamesNoEra -> BSL8.putStrLn $ Aeson.encode $ map Types.name allProfiles
139-
-- Print all cloud profile (-nomadperf) names.
140+
-- Print all cloud profile (-nomadperf) names (does not apply overlays).
140141
NamesCloudNoEra -> BSL8.putStrLn $ Aeson.encode $ map Types.name cloudProfiles
142+
-- Print all profile names (applies overlays!!!!!).
143+
Names -> do
144+
maybeObj <- lookupOverlay -- Ignored by `NamesNoEra` and `NamesCloudNoEra`.
145+
BSL8.putStrLn $ Aeson.encode $ Map.keys $ addEras $ toMap maybeObj allProfiles
141146
-- Print a map with all profiles, with an optional overlay.
142147
All -> do
143-
obj <- lookupOverlay
144-
BSL8.putStrLn $ Aeson.encode $ addEras $ toMap obj allProfiles
148+
maybeObj <- lookupOverlay -- Ignored by `NamesNoEra` and `NamesCloudNoEra`.
149+
BSL8.putStrLn $ Aeson.encode $ addEras $ toMap maybeObj allProfiles
145150
-- Print a single profiles, with an optional overlay.
146151
(ByName prettyPrint profileName) -> do
147-
obj <- lookupOverlay
148-
let profiles = addEras $ toMap obj allProfiles
152+
maybeObj <- lookupOverlay -- Ignored by `NamesNoEra` and `NamesCloudNoEra`.
153+
let profiles = addEras $ toMap maybeObj allProfiles
149154
case Map.lookup profileName profiles of
150155
Nothing -> error $ "No profile named \"" ++ profileName ++ "\""
151156
(Just profile) ->
@@ -181,13 +186,13 @@ main = do
181186
(Left errorMsg) -> fail errorMsg
182187
(Right profile) -> print (profile :: Types.Profile)
183188

184-
lookupOverlay :: IO Aeson.Object
189+
lookupOverlay :: IO (Maybe Aeson.Object)
185190
lookupOverlay = do
186191
maybeOverlay <- lookupEnv "WB_PROFILE_OVERLAY"
187-
case maybeOverlay of
188-
Nothing -> mempty
192+
return $ case maybeOverlay of
193+
Nothing -> Nothing
189194
(Just str) -> case Aeson.decode (BSL8.pack str) of
190-
(Just (Aeson.Object keyMap)) -> return keyMap
195+
(Just (Aeson.Object keyMap)) -> Just keyMap
191196
_ -> error ""
192197

193198
getOpts :: IO Cli
@@ -200,12 +205,6 @@ getOpts = OA.execParser $
200205

201206
cliParser :: OA.Parser Cli
202207
cliParser = OA.hsubparser $
203-
OA.command "names"
204-
(OA.info
205-
(pure Names)
206-
(OA.fullDesc <> OA.header "names" <> OA.progDesc "All profiles names")
207-
)
208-
<>
209208
OA.command "names-noera"
210209
(OA.info
211210
(pure NamesNoEra)
@@ -217,6 +216,12 @@ cliParser = OA.hsubparser $
217216
(pure NamesCloudNoEra)
218217
(OA.fullDesc <> OA.header "names-cloud-noera" <> OA.progDesc "All cloud profiles names (no era suffix)")
219218
)
219+
<>
220+
OA.command "names"
221+
(OA.info
222+
(pure Names)
223+
(OA.fullDesc <> OA.header "names" <> OA.progDesc "All profiles names")
224+
)
220225
<>
221226
OA.command "all"
222227
(OA.info

bench/cardano-profile/src/Cardano/Benchmarking/Profile.hs

+20-19
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,22 @@ import qualified Paths_cardano_profile as Paths
2525

2626
--------------------------------------------------------------------------------
2727

28-
realize :: HasCallStack => Aeson.Object -> Types.Profile -> Types.Profile
29-
realize obj profile =
28+
realize :: HasCallStack => Types.Profile -> Types.Profile
29+
realize =
3030
-- Compose the profile in the same order as the `jq` profile machinery!
3131
-- 1) `addUnusedDefaults`: Adds all properties that are the same for all
3232
-- profiles. This are all candidates to be removed
3333
-- when we finally switch from `jq` to this.
34-
-- 2) `overlay`: Applies an optional JSON object as an "overlay". The
35-
-- object is read from an envar ("WB_PROFILE_OVERLAY") in
36-
-- the `main` function and can override anything (some may
37-
-- overridden by later steps) as long as the result is a
38-
-- valid `Profile`.
39-
-- 3) `shelleyAlonzoConway`: Given an epoch number ("pparamsEpoch"
34+
-- 2) `shelleyAlonzoConway`: Given an epoch number ("pparamsEpoch"
4035
-- property) creates the "genesis" property
4136
-- using "epoch-timeline.json" and applying the
4237
-- genesis specific overlays ("pparamsOverlays"
4338
-- property).
39+
-- 3) `overlay`: Applies an optional JSON object as an "overlay". The
40+
-- object is read from an envar ("WB_PROFILE_OVERLAY") in
41+
-- the `main` function and can override anything (some may
42+
-- overridden by later steps) as long as the result is a
43+
-- valid `Profile`.
4444
-- 4) `derive`: Fills the "derive" property.
4545
-- 5) `finalize`: Applies fixes (porting infelicities) needed to fill
4646
-- the "cli_args" property that is also filled here.
@@ -50,10 +50,9 @@ realize obj profile =
5050
preset
5151
. finalize
5252
. derive
53+
. overlay
5354
. shelleyAlonzoConway
54-
. overlay obj
5555
. addUnusedDefaults
56-
$ profile
5756

5857
{-
5958
@@ -106,15 +105,6 @@ addUnusedDefaults p =
106105
-- Step 2.
107106
--------------------------------------------------------------------------------
108107

109-
-- Merges the profile with a JSON file and stores the overlay contents in the
110-
-- profile.
111-
overlay :: HasCallStack => Aeson.Object -> Types.Profile -> Types.Profile
112-
overlay overlaykeyMap profile =
113-
(applyOverlay overlaykeyMap profile) {Types.overlay = overlaykeyMap}
114-
115-
-- Step 3.
116-
--------------------------------------------------------------------------------
117-
118108
-- | Fill the "genesis" object "shelley", "alonzo" and "conway" properties
119109
-- using the profile's epoch number and overlay names.
120110
shelleyAlonzoConway :: Types.Profile -> Types.Profile
@@ -194,6 +184,17 @@ genesisOverlay overlayName epochParams = do
194184
KeyMap.unionWithKey unionWithKey epochParams keyMap
195185
_ -> error $ "Not an Aeson Object: \"" ++ fp ++ "\""
196186

187+
-- Step 3.
188+
--------------------------------------------------------------------------------
189+
190+
-- Merges the profile with a JSON object stored in the "overlay" property.
191+
overlay :: HasCallStack => Types.Profile -> Types.Profile
192+
overlay profile =
193+
let overlaykeyMap = Types.overlay profile -- An `Aeson.Object`.
194+
in if overlaykeyMap /= mempty
195+
then applyOverlay overlaykeyMap profile
196+
else profile
197+
197198
-- Step 4.
198199
--------------------------------------------------------------------------------
199200

bench/cardano-profile/src/Cardano/Benchmarking/Profile/Primitives.hs

+8-1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ module Cardano.Benchmarking.Profile.Primitives (
9999
, cBlockMinimumAdoptions
100100

101101
, preset
102+
, overlay
102103

103104
) where
104105

@@ -914,8 +915,14 @@ cBlockMinimumAdoptions i = analysisExpressionAppend
914915

915916
--------------------------------------------------------------------------------
916917

917-
preset :: String -> Types.Profile -> Types.Profile
918+
preset :: HasCallStack => String -> Types.Profile -> Types.Profile
918919
preset str p =
919920
if isJust (Types.preset p)
920921
then error "preset: `preset` already set (not Nothing)."
921922
else p {Types.preset = Just str}
923+
924+
overlay :: HasCallStack => Aeson.Object -> Types.Profile -> Types.Profile
925+
overlay obj p =
926+
if Types.overlay p /= mempty
927+
then error "overlay: `overlay` already set (not an empty JSON object)."
928+
else p {Types.overlay = obj}

bench/cardano-profile/test/Main.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ profiles :: Map.Map String Types.Profile
477477
profiles = Map.fromList $ map
478478
(\p ->
479479
( Types.name p
480-
, Profile.realize mempty p
480+
, Profile.realize p -- No overlay added!
481481
)
482482
)
483483
(
@@ -740,7 +740,7 @@ testGroupOverlay = Tasty.testGroup
740740
case eitherAns of
741741
(Left err) -> fail err
742742
(Right profile) -> do
743-
let profileWithOverlay = Profile.realize overlay profile
743+
let profileWithOverlay = Profile.realize (profile {Types.overlay = overlay})
744744
assertEqual "New name"
745745
"HOLA!"
746746
(Types.name profileWithOverlay)

0 commit comments

Comments
 (0)