Last active
October 9, 2020 05:25
-
-
Save lgastako/0ae600d7025f7efa7af81a65d31f8676 to your computer and use it in GitHub Desktop.
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 OverloadedStrings #-} | |
module GetIn where | |
import Prelude hiding ( lookup ) | |
import Control.Monad ( (<=<) | |
, join | |
) | |
import Data.Aeson ( Value( Array | |
, Object | |
) | |
, decode | |
) | |
import Data.HashMap.Strict ( lookup ) | |
import Data.Maybe ( fromMaybe ) | |
import Data.Text ( Text | |
, unpack | |
) | |
import Data.Vector ( (!?) ) | |
import Text.Read ( readMaybe ) | |
getIn :: [Text] -> Value -> Maybe Value | |
getIn [] v = Just v | |
getIn (k:ks) v = case v of | |
Object m -> maybe Nothing (getIn ks) . lookup k $ m | |
Array a -> join . fmap (getIn ks) . (a !?) <=< readMaybe . unpack $ k | |
_ -> Nothing | |
example :: Value | |
example = fromMaybe (error "boom") . decode | |
$ "{\"a\":{\"b\":{\"c\":[\"foo\",\"bar\",\"baz\"]}}}" | |
example2 :: Value | |
example2 = fromMaybe (error "boom") . decode | |
$ "{\"a\":{\"b\":{\"c\":[\"foo\",{\"d\":7},\"baz\"]}}}" | |
-- λ> getIn ["a", "b", "c"] example | |
-- Array [String "foo",String "bar",String "baz"] | |
-- λ> getIn ["a", "b", "c", "1"] example | |
-- String "bar" | |
-- λ> getIn ["a", "b", "c", "1", "d"] example2 | |
-- Number 7.0 | |
-- λ> getIn ["a", "b", "q"] example | |
-- Nothing | |
-- λ> getIn ["a", "b", "c", "d"] example | |
-- Nothing |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment