HiveBrain v1.2.0
Get Started
← Back to all entries
patternyamlMinor

Update only specific field of specific item (data) in List from YAML

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
itemfieldupdateyamlspecificlistfromdataonly

Problem

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 >= 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

Solution

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).

Code Snippets

where enR x = if rep == location x then x { hash = Just hash } else x

Context

StackExchange Code Review Q#61148, answer score: 3

Revisions (0)

No revisions yet.