<commit_before>{-# LANGUAGE DataKinds                  #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE KindSignatures             #-}
{-# LANGUAGE OverloadedStrings          #-}
{-# LANGUAGE RoleAnnotations            #-}
{-# LANGUAGE TupleSections              #-}
{-# LANGUAGE TypeFamilies               #-}
{-# LANGUAGE ScopedTypeVariables        #-}

-- Module      : Network.AWS.Data.Internal.Map
-- Copyright   : (c) 2013-2014 <NAME> <<EMAIL>>
-- License     : This Source Code Form is subject to the terms of
--               the Mozilla Public License, v. 2.0.
--               A copy of the MPL can be found in the LICENSE file or
--               you can obtain it at http://mozilla.org/MPL/2.0/.
-- Maintainer  : <NAME> <<EMAIL>>
-- Stability   : experimental
-- Portability : non-portable (GHC extensions)

module Network.AWS.Data.Internal.Map
    ( Map (..)
    , _Map
    ) where
    -- ( Map (..)
    -- , _Map
    -- , (~::)
    -- ) where

import           Control.Applicative
import           Control.Lens                         hiding (coerce, element)
import           Data.Aeson
import           Data.Bifunctor
import qualified Data.ByteString.Char8                as BS
import qualified Data.CaseInsensitive                 as CI
import           Data.Coerce
import           Data.Foldable                        (Foldable)
import           Data.HashMap.Strict                  (HashMap)
import qualified Data.HashMap.Strict                  as Map
import           Data.Hashable                        (Hashable)
import           Data.List
import           Data.Monoid
import           Data.Proxy
import           Data.Semigroup                       (Semigroup)
import           Data.String
import           Data.Text                            (Text)
import qualified Data.Text                            as Text
import qualified Data.Text.Encoding                   as Text
import           GHC.TypeLits
import           Network.AWS.Data.Internal.ByteString
import           Network.AWS.Data.Internal.Header
import           Network.AWS.Data.Internal.Query
import           Network.AWS.Data.Internal.Text
import           Network.AWS.Data.Internal.XML
import           Network.HTTP.Types.Header            (Header)
import           Text.XML

newtype Map (e :: Symbol) (i :: Symbol) (j :: Symbol) k v = Map
    { toHashMap :: HashMap k v
    } deriving (Eq, Show, Monoid, Semigroup)

type role Map phantom phantom phantom nominal representational

_Map :: (Coercible a b, Coercible b a) => Iso' (Map e i j k a) (HashMap k b)
_Map = iso (coerce . toHashMap) (Map . coerce)

fromList :: (Eq k, Hashable k) => [(k, v)] -> Map e i j k v
fromList = Map . Map.fromList

toList :: Map e i j k v -> [(k, v)]
toList = Map.toList . toHashMap

instance ( KnownSymbol e
         , KnownSymbol i
         , KnownSymbol j
         , Eq k
         , Hashable k
         , ToQuery k
         , ToQuery v
         ) => ToQuery (Map e i j k v) where
    toQuery m = toBS e =? (mconcat . zipWith go idx $ toList m)
      where
        go n (k, v) = toBS n =? toQuery (i, k) <> toQuery (j, v)

        idx = [1..] :: [Integer]

        i = BS.pack $ symbolVal (Proxy :: Proxy i)
        j = BS.pack $ symbolVal (Proxy :: Proxy j)
        e = BS.pack $ symbolVal (Proxy :: Proxy e)

instance ( Eq k
         , Hashable k
         , FromText k
         , FromJSON v
         ) => FromJSON (Map e i j k v) where
    parseJSON = withObject "HashMap" (fmap fromList . traverse g . Map.toList)
      where
        g (k, v) = (,)
            <$> either fail return (fromText k)
            <*> parseJSON v

instance ( Eq k
         , Hashable k
         , ToText k
         , ToJSON v
         ) => ToJSON (Map e i j k v) where
    toJSON = Object . Map.fromList . map (bimap toText toJSON) . toList

instance ( KnownSymbol e
         , KnownSymbol i
         , KnownSymbol j
         , Eq k
         , Hashable k<commit_msg>Removing JSON instances for Map<commit_after>{-# LANGUAGE DataKinds                  #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE KindSignatures             #-}
{-# LANGUAGE OverloadedStrings          #-}
{-# LANGUAGE RoleAnnotations            #-}
{-# LANGUAGE TupleSections              #-}
{-# LANGUAGE TypeFamilies               #-}
{-# LANGUAGE ScopedTypeVariables        #-}

-- Module      : Network.AWS.Data.Internal.Map
-- Copyright   : (c) 2013-2014 <NAME> <<EMAIL>>
-- License     : This Source Code Form is subject to the terms of
--               the Mozilla Public License, v. 2.0.
--               A copy of the MPL can be found in the LICENSE file or
--               you can obtain it at http://mozilla.org/MPL/2.0/.
-- Maintainer  : <NAME> <<EMAIL>>
-- Stability   : experimental
-- Portability : non-portable (GHC extensions)

module Network.AWS.Data.Internal.Map
    ( Map (..)
    , _Map
    ) where
    -- ( Map (..)
    -- , _Map
    -- , (~::)
    -- ) where

import           Control.Applicative
import           Control.Lens                         hiding (coerce, element)
import           Data.Aeson
import           Data.Bifunctor
import qualified Data.ByteString.Char8                as BS
import qualified Data.CaseInsensitive                 as CI
import           Data.Coerce
import           Data.Foldable                        (Foldable)
import           Data.HashMap.Strict                  (HashMap)
import qualified Data.HashMap.Strict                  as Map
import           Data.Hashable                        (Hashable)
import           Data.List
import           Data.Monoid
import           Data.Proxy
import           Data.Semigroup                       (Semigroup)
import           Data.String
import           Data.Text                            (Text)
import qualified Data.Text                            as Text
import qualified Data.Text.Encoding                   as Text
import           GHC.TypeLits
import           Network.AWS.Data.Internal.ByteString
import           Network.AWS.Data.Internal.Header
import           Network.AWS.Data.Internal.Query
import           Network.AWS.Data.Internal.Text
import           Network.AWS.Data.Internal.XML
import           Network.HTTP.Types.Header            (Header)
import           Text.XML

newtype Map (e :: Symbol) (i :: Symbol) (j :: Symbol) k v = Map
    { toHashMap :: HashMap k v
    } deriving (Eq, Show, Monoid, Semigroup)

type role Map phantom phantom phantom nominal representational

_Map :: (Coercible a b, Coercible b a) => Iso' (Map e i j k a) (HashMap k b)
_Map = iso (coerce . toHashMap) (Map . coerce)

fromList :: (Eq k, Hashable k) => [(k, v)] -> Map e i j k v
fromList = Map . Map.fromList

toList :: Map e i j k v -> [(k, v)]
toList = Map.toList . toHashMap

instance ( KnownSymbol e
         , KnownSymbol i
         , KnownSymbol j
         , Eq k
         , Hashable k
         , ToQuery k
         , ToQuery v
         ) => ToQuery (Map e i j k v) where
    toQuery m = toBS e =? (mconcat . zipWith go idx $ toList m)
      where
        go n (k, v) = toBS n =? toQuery (i, k) <> toQuery (j, v)

        idx = [1..] :: [Integer]

        i = BS.pack $ symbolVal (Proxy :: Proxy i)
        j = BS.pack $ symbolVal (Proxy :: Proxy j)
        e = BS.pack $ symbolVal (Proxy :: Proxy e)

-- instance ( Eq k
--          , Hashable k
--          , FromText k
--          , FromJSON v
--          ) => FromJSON (Map e i j k v) where
--     parseJSON = withObject "HashMap" (fmap fromList . traverse g . Map.toList)
--       where
--         g (k, v) = (,)
--             <$> either fail return (fromText k)
--             <*> parseJSON v

-- instance ( Eq k
--          , Hashable k
--          , ToText k
--          , ToJSON v
--          ) => ToJSON (Map e i j k v) where
--     toJSON = Object . Map.fromList . map (bimap toText toJSON) . toList

instance ( KnownSymbol e
         , KnownSymbol i
         , KnownSymbol j
         , Eq k
         , Hashable k
{'fraction_non_alphanumeric': 0.10251889168765743, 'fraction_numerical': 0.003526448362720403, 'mean_word_length': 1.5281757402101241, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7046619', 'n_tokens_mistral': 2414, 'n_tokens_neox': 2325, 'n_words': 749}