-- | Wrapper for the schema-less 'Value' used for @custom@ in Job payloads
--
-- This type's 'Semigroup' will merge two Objects. It is right-biased, as we are
-- generally throughout this library, so called /last-wins/ semantics.
--
module Faktory.Job.Custom
    ( Custom
    , toCustom
    , fromCustom
    ) where

import Faktory.Prelude

import Data.Aeson

newtype Custom = Custom Value
  deriving stock (Custom -> Custom -> Bool
(Custom -> Custom -> Bool)
-> (Custom -> Custom -> Bool) -> Eq Custom
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Custom -> Custom -> Bool
$c/= :: Custom -> Custom -> Bool
== :: Custom -> Custom -> Bool
$c== :: Custom -> Custom -> Bool
Eq, Int -> Custom -> ShowS
[Custom] -> ShowS
Custom -> String
(Int -> Custom -> ShowS)
-> (Custom -> String) -> ([Custom] -> ShowS) -> Show Custom
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Custom] -> ShowS
$cshowList :: [Custom] -> ShowS
show :: Custom -> String
$cshow :: Custom -> String
showsPrec :: Int -> Custom -> ShowS
$cshowsPrec :: Int -> Custom -> ShowS
Show)
  deriving newtype (Value -> Parser [Custom]
Value -> Parser Custom
(Value -> Parser Custom)
-> (Value -> Parser [Custom]) -> FromJSON Custom
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [Custom]
$cparseJSONList :: Value -> Parser [Custom]
parseJSON :: Value -> Parser Custom
$cparseJSON :: Value -> Parser Custom
FromJSON, [Custom] -> Encoding
[Custom] -> Value
Custom -> Encoding
Custom -> Value
(Custom -> Value)
-> (Custom -> Encoding)
-> ([Custom] -> Value)
-> ([Custom] -> Encoding)
-> ToJSON Custom
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [Custom] -> Encoding
$ctoEncodingList :: [Custom] -> Encoding
toJSONList :: [Custom] -> Value
$ctoJSONList :: [Custom] -> Value
toEncoding :: Custom -> Encoding
$ctoEncoding :: Custom -> Encoding
toJSON :: Custom -> Value
$ctoJSON :: Custom -> Value
ToJSON)

toCustom :: ToJSON a => a -> Custom
toCustom :: a -> Custom
toCustom = Value -> Custom
Custom (Value -> Custom) -> (a -> Value) -> a -> Custom
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Value
forall a. ToJSON a => a -> Value
toJSON

-- | Read a 'Custom' value to a type using 'FromJSON'
fromCustom :: FromJSON a => Custom -> Either String a
fromCustom :: Custom -> Either String a
fromCustom (Custom Value
v) = case Value -> Result a
forall a. FromJSON a => Value -> Result a
fromJSON Value
v of
  Error String
e -> String -> Either String a
forall a b. a -> Either a b
Left String
e
  Success a
a -> a -> Either String a
forall a b. b -> Either a b
Right a
a

instance Semigroup Custom where
  (Custom (Object Object
a)) <> :: Custom -> Custom -> Custom
<> (Custom (Object Object
b)) = Value -> Custom
Custom (Value -> Custom) -> Value -> Custom
forall a b. (a -> b) -> a -> b
$ Object -> Value
Object (Object -> Value) -> Object -> Value
forall a b. (a -> b) -> a -> b
$ Object
b Object -> Object -> Object
forall a. Semigroup a => a -> a -> a
<> Object
a
  Custom
_ <> Custom
b = Custom
b