{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE TypeFamilies #-}

{- |
Module      :  OpenTelemetry.Attributes.Map
Copyright   :  (c) Ian Duncan, 2021
License     :  BSD-3
Description :  Key-value pair metadata without limits
Maintainer  :  Ian Duncan
Stability   :  experimental
Portability :  non-portable (GHC extensions)
-}
module OpenTelemetry.Attributes.Map (
  AttributeMap,
  insertByKey,
  insertAttributeByKey,
  lookupByKey,
  lookupAttributeByKey,
  module H,
) where

import Data.HashMap.Strict as H
import Data.Text (Text)
import OpenTelemetry.Attributes.Attribute (
  Attribute,
  FromAttribute (fromAttribute),
  ToAttribute (toAttribute),
 )
import OpenTelemetry.Attributes.Key (
  AttributeKey (AttributeKey),
 )
import Prelude hiding (lookup, map)


type AttributeMap = H.HashMap Text Attribute


insertByKey :: ToAttribute a => AttributeKey a -> a -> AttributeMap -> AttributeMap
insertByKey :: forall a.
ToAttribute a =>
AttributeKey a -> a -> AttributeMap -> AttributeMap
insertByKey (AttributeKey !Text
k) !a
v = Text -> Attribute -> AttributeMap -> AttributeMap
forall k v.
(Eq k, Hashable k) =>
k -> v -> HashMap k v -> HashMap k v
H.insert Text
k (Attribute -> AttributeMap -> AttributeMap)
-> Attribute -> AttributeMap -> AttributeMap
forall a b. (a -> b) -> a -> b
$ a -> Attribute
forall a. ToAttribute a => a -> Attribute
toAttribute a
v


insertAttributeByKey :: AttributeKey a -> Attribute -> AttributeMap -> AttributeMap
insertAttributeByKey :: forall a.
AttributeKey a -> Attribute -> AttributeMap -> AttributeMap
insertAttributeByKey (AttributeKey !Text
k) !Attribute
v = Text -> Attribute -> AttributeMap -> AttributeMap
forall k v.
(Eq k, Hashable k) =>
k -> v -> HashMap k v -> HashMap k v
H.insert Text
k Attribute
v


lookupByKey :: FromAttribute a => AttributeKey a -> AttributeMap -> Maybe a
lookupByKey :: forall a.
FromAttribute a =>
AttributeKey a -> AttributeMap -> Maybe a
lookupByKey (AttributeKey Text
k) AttributeMap
attributes = Text -> AttributeMap -> Maybe Attribute
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
H.lookup Text
k AttributeMap
attributes Maybe Attribute -> (Attribute -> Maybe a) -> Maybe a
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Attribute -> Maybe a
forall a. FromAttribute a => Attribute -> Maybe a
fromAttribute


lookupAttributeByKey :: AttributeKey a -> AttributeMap -> Maybe Attribute
lookupAttributeByKey :: forall a. AttributeKey a -> AttributeMap -> Maybe Attribute
lookupAttributeByKey (AttributeKey Text
k) = Text -> AttributeMap -> Maybe Attribute
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
H.lookup Text
k