Eventually-decentralized project hosting and management platform
Clone
HTTPS:
darcs clone https://vervis.peers.community/repos/WvWbo
SSH:
darcs clone USERNAME@vervis.peers.community:WvWbo
Tags
TODO
Local.hs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | {- This file is part of Vervis.
-
- Written in 2019, 2022 by fr33domlover <fr33domlover@riseup.net>.
-
- ♡ Copying is an act of love. Please copy, reuse and share.
-
- The author(s) have dedicated all copyright and related and neighboring
- rights to this software to the public domain worldwide. This software is
- distributed without any warranty.
-
- You should have received a copy of the CC0 Public Domain Dedication along
- with this software. If not, see
- <http://creativecommons.org/publicdomain/zero/1.0/>.
-}
module Data.Aeson.Local
( Either' (..)
, toEither
, fromEither
, (.:|)
, (.:|?)
, (.:+)
, (.:+?)
, (.:*)
, (.:*+)
, (.=?)
, (.=%)
, (.=+)
, (.=+?)
, (.=*)
, (.=*+)
, WithValue (..)
)
where
import Control.Applicative
import Data.Aeson
import Data.Aeson.Types (Parser)
import Data.List.NonEmpty (NonEmpty (..))
import Data.Text (Text)
import Network.URI
import qualified Data.Text as T (unpack)
data Either' a b = Left' a | Right' b
instance (FromJSON a, FromJSON b) => FromJSON (Either' a b) where
parseJSON v = Left' <$> parseJSON v <|> Right' <$> parseJSON v
instance (ToJSON a, ToJSON b) => ToJSON (Either' a b) where
toJSON = error "toJSON Either'"
toEncoding (Left' x) = toEncoding x
toEncoding (Right' y) = toEncoding y
toEither :: Either' a b -> Either a b
toEither (Left' x) = Left x
toEither (Right' y) = Right y
fromEither :: Either a b -> Either' a b
fromEither (Left x) = Left' x
fromEither (Right y) = Right' y
(.:|) :: FromJSON a => Object -> Text -> Parser a
o .:| t = o .: t <|> o .: (frg <> t)
where
frg = "https://forgefed.org/ns#"
(.:|?) :: FromJSON a => Object -> Text -> Parser (Maybe a)
o .:|? t = optional $ o .:| t
(.:+) :: (FromJSON a, FromJSON b) => Object -> Text -> Parser (Either a b)
o .:+ t = Left <$> o .: t <|> Right <$> o .: t
(.:+?)
:: (FromJSON a, FromJSON b)
=> Object -> Text -> Parser (Maybe (Either a b))
o .:+? t = optional $ o .:+ t
-- | For JSON-LD properties that aren't functional, i.e. can have any number of
-- values
(.:*) :: FromJSON a => Object -> Text -> Parser [a]
o .:* t = do
maybeOneOrArray <- o .:+? t
case maybeOneOrArray of
Nothing -> return []
Just (Left v) -> return [v]
Just (Right vs) -> return vs
-- | For JSON-LD properties that aren't functional, i.e. can have any number of
-- values
(.:*+) :: FromJSON a => Object -> Text -> Parser (NonEmpty a)
o .:*+ t = do
oneOrArray <- o .:+ t
case oneOrArray of
Left v -> return $ v :| []
Right [] -> fail $ "No values for " ++ T.unpack t
Right (v:vs) -> return $ v :| vs
infixr 8 .=?
(.=?) :: ToJSON v => Text -> Maybe v -> Series
_ .=? Nothing = mempty
k .=? (Just v) = k .= v
infixr 8 .=%
(.=%) :: ToJSON v => Text -> [v] -> Series
k .=% v =
if null v
then mempty
else k .= v
infixr 8 .=+
(.=+) :: (ToJSON a, ToJSON b) => Text -> Either a b -> Series
k .=+ Left x = k .= x
k .=+ Right y = k .= y
infixr 8 .=+?
(.=+?) :: (ToJSON a, ToJSON b) => Text -> Maybe (Either a b) -> Series
k .=+? Nothing = mempty
k .=+? (Just v) = k .=+ v
infixr 8 .=*
(.=*) :: ToJSON a => Text -> [a] -> Series
_ .=* [] = mempty
k .=* [v] = k .= v
k .=* vs = k .= vs
infixr 8 .=*+
(.=*+) :: ToJSON a => Text -> NonEmpty a -> Series
k .=*+ (v :| []) = k .= v
k .=*+ (v :| vs) = k .= (v:vs)
data WithValue a = WithValue
{ wvRaw :: Object
, wvParsed :: a
}
instance FromJSON a => FromJSON (WithValue a) where
parseJSON v =
flip WithValue
<$> parseJSON v
<*> withObject "WithValue" pure v
|