Created
January 13, 2015 02:14
-
-
Save mithrandi/23a81851889ee09603bf to your computer and use it in GitHub Desktop.
Lens example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE TemplateHaskell, Rank2Types, NoMonomorphismRestriction #-} | |
module Main where | |
import Control.Lens | |
import qualified Data.Map.Strict as M | |
data Deployment = Deployment | |
{ _deploymentName :: String | |
, _nodes :: [Node] | |
} deriving Show | |
data Node = Node | |
{ _nodeName :: String | |
, _applications :: [Application] | |
} deriving Show | |
data Application = Application | |
{ _applicationName :: String | |
, _volume :: Volume | |
} deriving Show | |
data Volume = Volume { _dataset :: Dataset } deriving Show | |
data Dataset = Dataset | |
{ _datasetName :: Maybe String | |
, _datasetId :: Maybe String | |
} deriving Show | |
makeLenses ''Deployment | |
makeLenses ''Node | |
makeLenses ''Application | |
makeLenses ''Volume | |
makeLenses ''Dataset | |
myDeployment = Deployment "deployment1" [node1, node2, node3] | |
node1 = Node "node1" [app1, app2] | |
node2 = Node "node2" [app2, app3] | |
node3 = Node "node3" [app1, app3] | |
app1 = Application "app1" (Volume (Dataset (Just "dataset1") Nothing)) | |
app2 = Application "app2" (Volume (Dataset Nothing Nothing)) | |
app3 = Application "app3" (Volume (Dataset (Just "dataset3") Nothing)) | |
datasets = M.fromList | |
[("dataset1", "9ae635d6-b24d-41b6-a8e5-4b2da67498e9"), | |
("dataset3", "34117f32-e679-4234-92dc-a39246bf436e")] | |
findDataset (Just name) = M.findWithDefault "MISSING" name datasets | |
findDataset Nothing = "MISSING" | |
update_dataset_id dataset = dataset & datasetId .~ Just (findDataset (dataset ^. datasetName)) | |
update_all_dataset_ids deployment = deployment & nodes.mapped.applications.mapped.volume.dataset %~ update_dataset_id | |
main = print $ update_all_dataset_ids myDeployment |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Deployment | |
{ _deploymentName = "deployment1" | |
, _nodes = | |
[ Node | |
{ _nodeName = "node1" | |
, _applications = | |
[ Application | |
{ _applicationName = "app1" | |
, _volume = | |
Volume | |
{ _dataset = | |
Dataset | |
{ _datasetName = Just "dataset1" | |
, _datasetId = Just "9ae635d6-b24d-41b6-a8e5-4b2da67498e9" | |
} | |
} | |
} | |
, Application | |
{ _applicationName = "app2" | |
, _volume = | |
Volume | |
{ _dataset = | |
Dataset { _datasetName = Nothing , _datasetId = Just "MISSING" } | |
} | |
} | |
] | |
} | |
, Node | |
{ _nodeName = "node2" | |
, _applications = | |
[ Application | |
{ _applicationName = "app2" | |
, _volume = | |
Volume | |
{ _dataset = | |
Dataset { _datasetName = Nothing , _datasetId = Just "MISSING" } | |
} | |
} | |
, Application | |
{ _applicationName = "app3" | |
, _volume = | |
Volume | |
{ _dataset = | |
Dataset | |
{ _datasetName = Just "dataset3" | |
, _datasetId = Just "34117f32-e679-4234-92dc-a39246bf436e" | |
} | |
} | |
} | |
] | |
} | |
, Node | |
{ _nodeName = "node3" | |
, _applications = | |
[ Application | |
{ _applicationName = "app1" | |
, _volume = | |
Volume | |
{ _dataset = | |
Dataset | |
{ _datasetName = Just "dataset1" | |
, _datasetId = Just "9ae635d6-b24d-41b6-a8e5-4b2da67498e9" | |
} | |
} | |
} | |
, Application | |
{ _applicationName = "app3" | |
, _volume = | |
Volume | |
{ _dataset = | |
Dataset | |
{ _datasetName = Just "dataset3" | |
, _datasetId = Just "34117f32-e679-4234-92dc-a39246bf436e" | |
} | |
} | |
} | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment