I don't like the logic to update [Repository]
list here because I need to map whole list to change only specific field of specific node, I especially don't like that I need to provide all other fields in changing node alike (location x)
etc... Major trouble there is that if I will edit data
model I will be forced to edit a lot of code where I use it like this.
hashupdate :: String -> String -> IO ()
hashupdate hash rep =
withConfig $ \ymlx ->
let ymlprocess = ifSo $ do
rsdata <- yDecode ymlx :: IO [Repository]
let ed = map enR rsdata
where enR x = if rep == (location x)
then (Repository (location x)
(branches x)
(upstream x)
(enabled x)
(clean x)
(post_rebuild x)
(syncGroup x)
(Just hash))
else x
yEncode ymlx ed
in doesFileExist ymlx >>= ymlprocess
The other definitions used there is here:
data Repository = Repository { location :: String
, branches :: [String]
, upstream :: String
, enabled :: Maybe Bool
, clean :: Maybe Bool
, post_rebuild :: Maybe [String]
, syncGroup :: Maybe String
, hash :: Maybe String
} deriving (Show, Eq)
yEncode :: ToJSON iToJSONable => FilePath -> iToJSONable -> IO()
yEncode fnm dat = do
let bs = Data.Yaml.encode dat
BS.writeFile fnm bs
1 Answer 1
I don't like that I need to provide all other fields in changing node alike
(location x)
etc...
You can "update" a field of a record like so:
where enR x = if rep == location x then x { hash = Just hash } else x
I would recommend against names like fnm
, dat
, ed
, enR
, etc. You should also use consistent casing (postRebuild
instead of post_rebuild
).