-- | 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
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
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
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
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 :: forall a. ToJSON a => a -> Custom
toCustom = Value -> Custom
Custom forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. ToJSON a => a -> Value
toJSON

-- | Read a 'Custom' value to a type using 'FromJSON'
fromCustom :: FromJSON a => Custom -> Either String a
fromCustom :: forall a. FromJSON a => Custom -> Either String a
fromCustom (Custom Value
v) = case forall a. FromJSON a => Value -> Result a
fromJSON Value
v of
  Error String
e -> forall a b. a -> Either a b
Left String
e
  Success a
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 forall a b. (a -> b) -> a -> b
$ Object -> Value
Object forall a b. (a -> b) -> a -> b
$ Object
b forall a. Semigroup a => a -> a -> a
<> Object
a
  Custom
_ <> Custom
b = Custom
b