{-|
Module      :  Data.Aeson.Schema.Key
Maintainer  :  Brandon Chinn <brandon@leapyear.io>
Stability   :  experimental
Portability :  portable

Defines a SchemaKey.
-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveLift #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeInType #-}

module Data.Aeson.Schema.Key
  ( SchemaKey'(..)
  , SchemaKeyV
  , fromSchemaKeyV
  , showSchemaKeyV
  , getContext
  , toContext
  , SchemaKey
  , IsSchemaKey(..)
  , fromSchemaKey
  , showSchemaKey
  ) where

import qualified Data.Aeson as Aeson
import Data.Hashable (Hashable)
import qualified Data.HashMap.Strict as HashMap
import Data.Proxy (Proxy(..))
import qualified Data.Text as Text
import GHC.Generics (Generic)
import GHC.TypeLits (KnownSymbol, Symbol, symbolVal)
import Language.Haskell.TH.Syntax (Lift)

import Data.Aeson.Schema.Utils.Invariant (unreachable)

-- | A key in a JSON object schema.
data SchemaKey' s
  = NormalKey s
  | PhantomKey s
    -- ^ A key that doesn't actually exist in the object, but whose content should be parsed from
    -- the current object.
  deriving (Int -> SchemaKey' s -> ShowS
[SchemaKey' s] -> ShowS
SchemaKey' s -> String
(Int -> SchemaKey' s -> ShowS)
-> (SchemaKey' s -> String)
-> ([SchemaKey' s] -> ShowS)
-> Show (SchemaKey' s)
forall s. Show s => Int -> SchemaKey' s -> ShowS
forall s. Show s => [SchemaKey' s] -> ShowS
forall s. Show s => SchemaKey' s -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SchemaKey' s] -> ShowS
$cshowList :: forall s. Show s => [SchemaKey' s] -> ShowS
show :: SchemaKey' s -> String
$cshow :: forall s. Show s => SchemaKey' s -> String
showsPrec :: Int -> SchemaKey' s -> ShowS
$cshowsPrec :: forall s. Show s => Int -> SchemaKey' s -> ShowS
Show, SchemaKey' s -> SchemaKey' s -> Bool
(SchemaKey' s -> SchemaKey' s -> Bool)
-> (SchemaKey' s -> SchemaKey' s -> Bool) -> Eq (SchemaKey' s)
forall s. Eq s => SchemaKey' s -> SchemaKey' s -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SchemaKey' s -> SchemaKey' s -> Bool
$c/= :: forall s. Eq s => SchemaKey' s -> SchemaKey' s -> Bool
== :: SchemaKey' s -> SchemaKey' s -> Bool
$c== :: forall s. Eq s => SchemaKey' s -> SchemaKey' s -> Bool
Eq, (forall x. SchemaKey' s -> Rep (SchemaKey' s) x)
-> (forall x. Rep (SchemaKey' s) x -> SchemaKey' s)
-> Generic (SchemaKey' s)
forall x. Rep (SchemaKey' s) x -> SchemaKey' s
forall x. SchemaKey' s -> Rep (SchemaKey' s) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall s x. Rep (SchemaKey' s) x -> SchemaKey' s
forall s x. SchemaKey' s -> Rep (SchemaKey' s) x
$cto :: forall s x. Rep (SchemaKey' s) x -> SchemaKey' s
$cfrom :: forall s x. SchemaKey' s -> Rep (SchemaKey' s) x
Generic, Int -> SchemaKey' s -> Int
SchemaKey' s -> Int
(Int -> SchemaKey' s -> Int)
-> (SchemaKey' s -> Int) -> Hashable (SchemaKey' s)
forall s. Hashable s => Int -> SchemaKey' s -> Int
forall s. Hashable s => SchemaKey' s -> Int
forall a. (Int -> a -> Int) -> (a -> Int) -> Hashable a
hash :: SchemaKey' s -> Int
$chash :: forall s. Hashable s => SchemaKey' s -> Int
hashWithSalt :: Int -> SchemaKey' s -> Int
$chashWithSalt :: forall s. Hashable s => Int -> SchemaKey' s -> Int
Hashable, SchemaKey' s -> Q Exp
SchemaKey' s -> Q (TExp (SchemaKey' s))
(SchemaKey' s -> Q Exp)
-> (SchemaKey' s -> Q (TExp (SchemaKey' s))) -> Lift (SchemaKey' s)
forall s. Lift s => SchemaKey' s -> Q Exp
forall s. Lift s => SchemaKey' s -> Q (TExp (SchemaKey' s))
forall t. (t -> Q Exp) -> (t -> Q (TExp t)) -> Lift t
liftTyped :: SchemaKey' s -> Q (TExp (SchemaKey' s))
$cliftTyped :: forall s. Lift s => SchemaKey' s -> Q (TExp (SchemaKey' s))
lift :: SchemaKey' s -> Q Exp
$clift :: forall s. Lift s => SchemaKey' s -> Q Exp
Lift)

-- | A value-level SchemaKey
type SchemaKeyV = SchemaKey' String

fromSchemaKeyV :: SchemaKeyV -> String
fromSchemaKeyV :: SchemaKeyV -> String
fromSchemaKeyV (NormalKey String
key) = String
key
fromSchemaKeyV (PhantomKey String
key) = String
key

showSchemaKeyV :: SchemaKeyV -> String
showSchemaKeyV :: SchemaKeyV -> String
showSchemaKeyV (NormalKey String
key) = ShowS
forall a. Show a => a -> String
show String
key
showSchemaKeyV (PhantomKey String
key) = String
"[" String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
key String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"]"

-- | Given schema `{ key: innerSchema }` for JSON data `{ key: val1 }`, get the JSON
-- Value that `innerSchema` should parse.
getContext :: SchemaKeyV -> Aeson.Object -> Aeson.Value
getContext :: SchemaKeyV -> Object -> Value
getContext = \case
  -- `innerSchema` should parse `val1`
  NormalKey String
key -> Value -> Text -> Object -> Value
forall k v. (Eq k, Hashable k) => v -> k -> HashMap k v -> v
HashMap.lookupDefault Value
Aeson.Null (String -> Text
Text.pack String
key)
  -- `innerSchema` should parse the same object that `key` is in
  PhantomKey String
_ -> Object -> Value
Aeson.Object

-- | Given JSON data `val` adhering to `innerSchema`, get the JSON object that should be
-- merged with the outer JSON object.
toContext :: SchemaKeyV -> Aeson.Value -> Aeson.Object
toContext :: SchemaKeyV -> Value -> Object
toContext = \case
  -- `val` should be inserted with key `key`
  NormalKey String
key -> Text -> Value -> Object
forall k v. Hashable k => k -> v -> HashMap k v
HashMap.singleton (String -> Text
Text.pack String
key)
  -- If `val` is an object, it should be merged with the outer JSON object
  PhantomKey String
_ -> \case
    Aeson.Object Object
o -> Object
o
    -- `Try` schema could store `Nothing`, which would return `Null`. In this case, there is no
    -- context to merge
    Value
Aeson.Null -> Object
forall a. Monoid a => a
mempty
    Value
v -> String -> Object
forall a. String -> a
unreachable (String -> Object) -> String -> Object
forall a b. (a -> b) -> a -> b
$ String
"Invalid value for phantom key: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Value -> String
forall a. Show a => a -> String
show Value
v

-- | A type-level SchemaKey
type SchemaKey = SchemaKey' Symbol

class KnownSymbol (FromSchemaKey key) => IsSchemaKey (key :: SchemaKey) where
  type FromSchemaKey key :: Symbol
  toSchemaKeyV :: Proxy key -> SchemaKeyV

instance KnownSymbol key => IsSchemaKey ('NormalKey key) where
  type FromSchemaKey ('NormalKey key) = key
  toSchemaKeyV :: Proxy ('NormalKey key) -> SchemaKeyV
toSchemaKeyV Proxy ('NormalKey key)
_ = String -> SchemaKeyV
forall s. s -> SchemaKey' s
NormalKey (String -> SchemaKeyV) -> String -> SchemaKeyV
forall a b. (a -> b) -> a -> b
$ Proxy key -> String
forall (n :: Symbol) (proxy :: Symbol -> *).
KnownSymbol n =>
proxy n -> String
symbolVal (Proxy key -> String) -> Proxy key -> String
forall a b. (a -> b) -> a -> b
$ Proxy key
forall k (t :: k). Proxy t
Proxy @key

instance KnownSymbol key => IsSchemaKey ('PhantomKey key) where
  type FromSchemaKey ('PhantomKey key) = key
  toSchemaKeyV :: Proxy ('PhantomKey key) -> SchemaKeyV
toSchemaKeyV Proxy ('PhantomKey key)
_ = String -> SchemaKeyV
forall s. s -> SchemaKey' s
PhantomKey (String -> SchemaKeyV) -> String -> SchemaKeyV
forall a b. (a -> b) -> a -> b
$ Proxy key -> String
forall (n :: Symbol) (proxy :: Symbol -> *).
KnownSymbol n =>
proxy n -> String
symbolVal (Proxy key -> String) -> Proxy key -> String
forall a b. (a -> b) -> a -> b
$ Proxy key
forall k (t :: k). Proxy t
Proxy @key

fromSchemaKey :: forall key. IsSchemaKey key => String
fromSchemaKey :: String
fromSchemaKey = SchemaKeyV -> String
fromSchemaKeyV (SchemaKeyV -> String) -> SchemaKeyV -> String
forall a b. (a -> b) -> a -> b
$ Proxy key -> SchemaKeyV
forall (key :: SchemaKey).
IsSchemaKey key =>
Proxy key -> SchemaKeyV
toSchemaKeyV (Proxy key -> SchemaKeyV) -> Proxy key -> SchemaKeyV
forall a b. (a -> b) -> a -> b
$ Proxy key
forall k (t :: k). Proxy t
Proxy @key

showSchemaKey :: forall key. IsSchemaKey key => String
showSchemaKey :: String
showSchemaKey = SchemaKeyV -> String
showSchemaKeyV (SchemaKeyV -> String) -> SchemaKeyV -> String
forall a b. (a -> b) -> a -> b
$ Proxy key -> SchemaKeyV
forall (key :: SchemaKey).
IsSchemaKey key =>
Proxy key -> SchemaKeyV
toSchemaKeyV (Proxy key -> SchemaKeyV) -> Proxy key -> SchemaKeyV
forall a b. (a -> b) -> a -> b
$ Proxy key
forall k (t :: k). Proxy t
Proxy @key