{-# LANGUAGE OverloadedStrings #-}

-- |
-- Module:      Network.Riak.Content
-- Copyright:   (c) 2011 MailRank, Inc.
-- License:     Apache
-- Maintainer:  Mark Hibberd <mark@hibberd.id.au>, Nathan Hunter <nhunter@janrain.com>
-- Stability:   experimental
-- Portability: portable
--
-- Low-level content and link types and functions.

module Network.Riak.Content
    (
    -- * Types
      Proto.RpbContent
    , Proto.RpbLink
    -- * Functions
    , empty
    , binary
    , json
    , link
    ) where

import Data.Aeson (encode)
import Data.Aeson.Types (ToJSON)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Lazy as LazyByteString
import qualified Data.Riak.Proto as Proto
import Network.Riak.Lens
import Network.Riak.Types.Internal (Bucket, Key, Tag)

-- | Create a link.
link :: Bucket -> Key -> Tag -> Proto.RpbLink
link :: Bucket -> Bucket -> Bucket -> RpbLink
link Bucket
bucket Bucket
key Bucket
tag = RpbLink
forall msg. Message msg => msg
Proto.defMessage
                        RpbLink -> (RpbLink -> RpbLink) -> RpbLink
forall a b. a -> (a -> b) -> b
& LensLike' Identity RpbLink Bucket
forall (f :: * -> *) s a.
(Functor f, HasField s "bucket" a) =>
LensLike' f s a
Proto.bucket LensLike' Identity RpbLink Bucket -> Bucket -> RpbLink -> RpbLink
forall s a. Setter s a -> a -> s -> s
.~ Bucket
bucket
                        RpbLink -> (RpbLink -> RpbLink) -> RpbLink
forall a b. a -> (a -> b) -> b
& LensLike' Identity RpbLink Bucket
forall (f :: * -> *) s a.
(Functor f, HasField s "key" a) =>
LensLike' f s a
Proto.key LensLike' Identity RpbLink Bucket -> Bucket -> RpbLink -> RpbLink
forall s a. Setter s a -> a -> s -> s
.~ Bucket
key
                        RpbLink -> (RpbLink -> RpbLink) -> RpbLink
forall a b. a -> (a -> b) -> b
& LensLike' Identity RpbLink Bucket
forall (f :: * -> *) s a.
(Functor f, HasField s "tag" a) =>
LensLike' f s a
Proto.tag LensLike' Identity RpbLink Bucket -> Bucket -> RpbLink -> RpbLink
forall s a. Setter s a -> a -> s -> s
.~ Bucket
tag
{-# INLINE link #-}

-- | An empty piece of content.
empty :: Proto.RpbContent
empty :: RpbContent
empty = RpbContent
forall msg. Message msg => msg
Proto.defMessage

-- | Content encoded as @application/octet-stream@.
binary :: ByteString -> Proto.RpbContent
binary :: Bucket -> RpbContent
binary Bucket
bs = RpbContent
empty RpbContent -> (RpbContent -> RpbContent) -> RpbContent
forall a b. a -> (a -> b) -> b
& LensLike' Identity RpbContent Bucket
forall (f :: * -> *) s a.
(Functor f, HasField s "value" a) =>
LensLike' f s a
Proto.value LensLike' Identity RpbContent Bucket
-> Bucket -> RpbContent -> RpbContent
forall s a. Setter s a -> a -> s -> s
.~ Bucket
bs
                  RpbContent -> (RpbContent -> RpbContent) -> RpbContent
forall a b. a -> (a -> b) -> b
& LensLike' Identity RpbContent Bucket
forall (f :: * -> *) s a.
(Functor f, HasField s "contentType" a) =>
LensLike' f s a
Proto.contentType LensLike' Identity RpbContent Bucket
-> Bucket -> RpbContent -> RpbContent
forall s a. Setter s a -> a -> s -> s
.~ Bucket
"application/octet-stream"

-- | Content encoded as @application/json@.
json :: ToJSON a => a -> Proto.RpbContent
json :: a -> RpbContent
json a
j = RpbContent
empty RpbContent -> (RpbContent -> RpbContent) -> RpbContent
forall a b. a -> (a -> b) -> b
& LensLike' Identity RpbContent Bucket
forall (f :: * -> *) s a.
(Functor f, HasField s "value" a) =>
LensLike' f s a
Proto.value LensLike' Identity RpbContent Bucket
-> Bucket -> RpbContent -> RpbContent
forall s a. Setter s a -> a -> s -> s
.~ ByteString -> Bucket
LazyByteString.toStrict (a -> ByteString
forall a. ToJSON a => a -> ByteString
encode a
j)
               RpbContent -> (RpbContent -> RpbContent) -> RpbContent
forall a b. a -> (a -> b) -> b
& LensLike' Identity RpbContent Bucket
forall (f :: * -> *) s a.
(Functor f, HasField s "contentType" a) =>
LensLike' f s a
Proto.contentType LensLike' Identity RpbContent Bucket
-> Bucket -> RpbContent -> RpbContent
forall s a. Setter s a -> a -> s -> s
.~ Bucket
"application/json"