Closed jfroche closed 8 years ago
This is ugly as hell but it works I would think:
joinKeysToValues :: [PValue] -> InterpreterMonad PValue
joinKeysToValues [PHash h, PString separator] = do
let foldfunc acc k v =
(PString <$> (mappend (k <> separator) <$> resolvePValueString v)) : acc
xs = sequence $ V.fromList $ reverse $ HM.foldlWithKey' foldfunc [] h
PArray <$> xs
joinKeysToValues _ = throwPosError "join_keys_to_values(): expects 2 arguments, an hash and a string"
I will try to clean it up a bit but please fire your suggestion already ;-) I guess there is a cleaner way to write the fold function.
How should the keys be sorted ?
How should the keys be sorted ?
My understanding is that you keep the original order (from the PHash input).
I think this shot is better because we should be able to 'show' all PValue:
joinKeysToValues :: [PValue] -> InterpreterMonad PValue
joinKeysToValues [PHash h, PString separator] = do
let
joinKeysToValue k s v = PString $ k <> s <> T.pack (show v)
xs = V.fromList $ HM.foldrWithKey (\k v s -> joinKeysToValue k separator v : s) [] h
return $ PArray xs
joinKeysToValues _ = throwPosError "join_keys_to_values(): expects 2 arguments, an hash and a string"
But we probably need to use something like a "compact pretty (no colors ...) instead of show for PValue
I am not sure all values should be allowed ... anyway, why an explicit foldr instead of something like :
fmap (PArray . V.fromList) $ forM (itoList h) $ \(k,v) -> ...
If the original ordering is to be kept, it means that a PHash
should be stored as a [(Text, PValue)]
...
Ordering is not to be preserved according to the puppetlabs test suite.
itoList
is quite handy indeed. Thanks !
https://github.com/danzilio/puppet-letsencrypt fails now on this error:
I thought it would be easy but I don't know enough haskell yet. I tried (based on the any2array function):
https://forge.puppetlabs.com/puppetlabs/stdlib#join_keys_to_values ruby code: https://github.com/puppetlabs/puppetlabs-stdlib/blob/master/lib/puppet/parser/functions/join_keys_to_values.rb