{-# LANGUAGE FlexibleInstances #-}

module Argo.Class.ToValue where

import qualified Argo.Type.Array as Array
import qualified Argo.Type.Boolean as Boolean
import qualified Argo.Type.Number as Number
import qualified Argo.Type.Object as Object
import qualified Argo.Type.Pair as Pair
import qualified Argo.Type.String as String
import qualified Argo.Type.Value as Value
import qualified Data.Array
import qualified Data.Text as Text

class ToValue a where
    toValue :: a -> Value.Value

instance ToValue Value.Value where
    toValue :: Value -> Value
toValue = Value -> Value
forall a. a -> a
id

instance ToValue Bool where
    toValue :: Bool -> Value
toValue = Boolean -> Value
Value.Boolean (Boolean -> Value) -> (Bool -> Boolean) -> Bool -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> Boolean
Boolean.Boolean

instance ToValue Integer where
    toValue :: Integer -> Value
toValue = Number -> Value
Value.Number (Number -> Value) -> (Integer -> Number) -> Integer -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Number -> Number
Number.normalize (Number -> Number) -> (Integer -> Number) -> Integer -> Number
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Integer -> Integer -> Number) -> Integer -> Integer -> Number
forall a b c. (a -> b -> c) -> b -> a -> c
flip Integer -> Integer -> Number
Number.Number Integer
0

instance ToValue Text.Text where
    toValue :: Text -> Value
toValue = String -> Value
Value.String (String -> Value) -> (Text -> String) -> Text -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
String.String

instance ToValue a => ToValue (Data.Array.Array Int a) where
    toValue :: Array Int a -> Value
toValue = Array Value -> Value
Value.Array (Array Value -> Value)
-> (Array Int a -> Array Value) -> Array Int a -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array Int Value -> Array Value
forall a. Array Int a -> Array a
Array.Array (Array Int Value -> Array Value)
-> (Array Int a -> Array Int Value) -> Array Int a -> Array Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Value) -> Array Int a -> Array Int Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Value
forall a. ToValue a => a -> Value
toValue

instance ToValue a => ToValue (Data.Array.Array Int (Pair.Pair String.String a)) where
    toValue :: Array Int (Pair String a) -> Value
toValue = Object Value -> Value
Value.Object (Object Value -> Value)
-> (Array Int (Pair String a) -> Object Value)
-> Array Int (Pair String a)
-> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array Int (Pair String Value) -> Object Value
forall a. Array Int (Pair String a) -> Object a
Object.Object (Array Int (Pair String Value) -> Object Value)
-> (Array Int (Pair String a) -> Array Int (Pair String Value))
-> Array Int (Pair String a)
-> Object Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Pair String a -> Pair String Value)
-> Array Int (Pair String a) -> Array Int (Pair String Value)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\ (Pair.Pair (String
k, a
v)) -> (String, Value) -> Pair String Value
forall k v. (k, v) -> Pair k v
Pair.Pair (String
k, a -> Value
forall a. ToValue a => a -> Value
toValue a
v))