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

import Faktory.Prelude

import Data.Aeson
import qualified Data.HashMap.Strict as HashMap

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

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 -> Object -> Object
forall k v.
(Eq k, Hashable k) =>
HashMap k v -> HashMap k v -> HashMap k v
HashMap.union Object
b Object
a
  Custom
_ <> Custom
b = Custom
b