<commit_before>{-# LANGUAGE DeriveGeneric         #-}
{-# LANGUAGE FlexibleInstances     #-}
{-# LANGUAGE InstanceSigs          #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings     #-}
{-# LANGUAGE QuasiQuotes           #-}
{-# LANGUAGE ScopedTypeVariables   #-}
{-# LANGUAGE TemplateHaskell       #-}

module Gonimo.Server.Types where




import           Control.Error.Safe     (rightZ)

import           Data.Aeson.Types       (FromJSON (..), FromJSON, ToJSON (..),
                                         ToJSON (..), Value (String),
                                         defaultOptions, genericToJSON)

import           Data.ByteString        (ByteString)
import qualified Data.ByteString.Base64 as Base64
import           Data.Monoid
import           Data.Text              as T
import           Data.Text.Encoding     (decodeUtf8, encodeUtf8)
import           Database.Persist.TH
import           Debug.Trace            (trace)

import           GHC.Generics           (Generic)
import           Servant.PureScript     (jsonParseHeader, jsonParseUrlPiece,
                                         jsonToHeader, jsonToUrlPiece)
import           Web.HttpApiData        (FromHttpApiData (..),
                                         ToHttpApiData (..))

data DeviceType = NoBaby
                | Baby Text
                deriving (Show, Eq, Generic)

instance FromJSON DeviceType
instance ToJSON DeviceType

type SenderName = Text

newtype Secret = Secret ByteString deriving (Generic, Show, Read, Ord, Eq)

instance FromJSON Secret where
  parseJSON (String t) = Secret <$> (rightZ . Base64.decode . encodeUtf8 $ t)
  parseJSON _ = fail "Expecting a string when parsing a secret."

instance ToJSON Secret where
  toJSON (Secret bs) = String . decodeUtf8 . Base64.encode $ bs
  toEncoding (Secret bs) = toEncoding $ (decodeUtf8 . Base64.encode) bs

instance FromHttpApiData Secret where
  parseUrlPiece = jsonParseUrlPiece . logInput
    where logInput input = trace (T.unpack $ "FromHttpApiData Secret, got:" <> input) input
  parseHeader   = jsonParseHeader

instance ToHttpApiData Secret where
  toUrlPiece = jsonToUrlPiece
  toHeader   = jsonToHeader

derivePersistField "Secret"

-- Other auth methods might be added later on, like oauth bearer tokens:
data AuthToken = GonimoSecret Secret
               deriving (Read, Show, Generic)

derivePersistField "AuthToken"

instance FromJSON AuthToken
instance ToJSON AuthToken where
  toJSON = genericToJSON defaultOptions
--  toEncoding = genericToEncoding defaultOptions

instance FromHttpApiData AuthToken where
    parseUrlPiece :: Text -> Either Text AuthToken
    parseUrlPiece = jsonParseUrlPiece
    parseHeader   = jsonParseHeader

data Coffee = Tea deriving Generic
instance FromJSON Coffee
instance ToJSON Coffee where
  toJSON = genericToJSON defaultOptions
--  toEncoding = genericToEncoding defaultOptions



type FamilyName = Text

type EmailAddress = Text


--------------------------------------------------
data InvitationDelivery = EmailInvitation EmailAddress
                        | OtherDelivery
                        deriving (Read, Show, Generic)<commit_msg>Use toEncoding in a few more places<commit_after>{-# LANGUAGE DeriveGeneric         #-}
{-# LANGUAGE FlexibleInstances     #-}
{-# LANGUAGE InstanceSigs          #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings     #-}
{-# LANGUAGE QuasiQuotes           #-}
{-# LANGUAGE ScopedTypeVariables   #-}
{-# LANGUAGE TemplateHaskell       #-}

module Gonimo.Server.Types where




import           Control.Error.Safe     (rightZ)

import           Data.Aeson.Types       (FromJSON (..), FromJSON, ToJSON (..),
                                         ToJSON (..), Value (String),
                                         defaultOptions, genericToJSON, genericToEncoding)

import           Data.ByteString        (ByteString)
import qualified Data.ByteString.Base64 as Base64
import           Data.Monoid
import           Data.Text              as T
import           Data.Text.Encoding     (decodeUtf8, encodeUtf8)
import           Database.Persist.TH
import           Debug.Trace            (trace)

import           GHC.Generics           (Generic)
import           Servant.PureScript     (jsonParseHeader, jsonParseUrlPiece,
                                         jsonToHeader, jsonToUrlPiece)
import           Web.HttpApiData        (FromHttpApiData (..),
                                         ToHttpApiData (..))

data DeviceType = NoBaby
                | Baby Text
                deriving (Show, Eq, Generic)

instance FromJSON DeviceType
instance ToJSON DeviceType

type SenderName = Text

newtype Secret = Secret ByteString deriving (Generic, Show, Read, Ord, Eq)

instance FromJSON Secret where
  parseJSON (String t) = Secret <$> (rightZ . Base64.decode . encodeUtf8 $ t)
  parseJSON _ = fail "Expecting a string when parsing a secret."

instance ToJSON Secret where
  toJSON (Secret bs) = String . decodeUtf8 . Base64.encode $ bs
  toEncoding (Secret bs) = toEncoding $ (decodeUtf8 . Base64.encode) bs

instance FromHttpApiData Secret where
  parseUrlPiece = jsonParseUrlPiece . logInput
    where logInput input = trace (T.unpack $ "FromHttpApiData Secret, got:" <> input) input
  parseHeader   = jsonParseHeader

instance ToHttpApiData Secret where
  toUrlPiece = jsonToUrlPiece
  toHeader   = jsonToHeader

derivePersistField "Secret"

-- Other auth methods might be added later on, like oauth bearer tokens:
data AuthToken = GonimoSecret Secret
               deriving (Read, Show, Generic)

derivePersistField "AuthToken"

instance FromJSON AuthToken
instance ToJSON AuthToken where
  toJSON = genericToJSON defaultOptions
  toEncoding = genericToEncoding defaultOptions

instance FromHttpApiData AuthToken where
    parseUrlPiece :: Text -> Either Text AuthToken
    parseUrlPiece = jsonParseUrlPiece
    parseHeader   = jsonParseHeader

data Coffee = Tea deriving Generic
instance FromJSON Coffee
instance ToJSON Coffee where
  toJSON = genericToJSON defaultOptions
  toEncoding = genericToEncoding defaultOptions



type FamilyName = Text

type EmailAddress = Text


--------------------------------------------------
data InvitationDelivery = EmailInvitation EmailAddress
                        | OtherDelivery
                        deriving (Read, Show, Generic)
{'fraction_non_alphanumeric': 0.08287639041203196, 'fraction_numerical': 0.004699984333385555, 'mean_word_length': 2.277207392197125, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '893940', 'n_tokens_mistral': 1716, 'n_tokens_neox': 1668, 'n_words': 498}