{-# LANGUAGE DataKinds         #-}
{-# LANGUAGE DeriveGeneric     #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards   #-}
{-# LANGUAGE StrictData        #-}

module OpenTracing.Zipkin.Types
    ( Endpoint (..)

    , defaultZipkinAddr
    )
where

import Data.Aeson
import Data.Aeson.Encoding
import Data.Maybe          (catMaybes)
import Data.Text           (Text)
import GHC.Generics        (Generic)
import OpenTracing.Types


data Endpoint = Endpoint
    { Endpoint -> Text
serviceName :: Text
    , Endpoint -> IPv4
ipv4        :: IPv4
    , Endpoint -> Maybe IPv6
ipv6        :: Maybe IPv6
    , Endpoint -> Maybe Port
port        :: Maybe Port
    } deriving (Endpoint -> Endpoint -> Bool
(Endpoint -> Endpoint -> Bool)
-> (Endpoint -> Endpoint -> Bool) -> Eq Endpoint
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Endpoint -> Endpoint -> Bool
$c/= :: Endpoint -> Endpoint -> Bool
== :: Endpoint -> Endpoint -> Bool
$c== :: Endpoint -> Endpoint -> Bool
Eq, Int -> Endpoint -> ShowS
[Endpoint] -> ShowS
Endpoint -> String
(Int -> Endpoint -> ShowS)
-> (Endpoint -> String) -> ([Endpoint] -> ShowS) -> Show Endpoint
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Endpoint] -> ShowS
$cshowList :: [Endpoint] -> ShowS
show :: Endpoint -> String
$cshow :: Endpoint -> String
showsPrec :: Int -> Endpoint -> ShowS
$cshowsPrec :: Int -> Endpoint -> ShowS
Show, (forall x. Endpoint -> Rep Endpoint x)
-> (forall x. Rep Endpoint x -> Endpoint) -> Generic Endpoint
forall x. Rep Endpoint x -> Endpoint
forall x. Endpoint -> Rep Endpoint x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Endpoint x -> Endpoint
$cfrom :: forall x. Endpoint -> Rep Endpoint x
Generic)

instance ToJSON Endpoint where
    toEncoding :: Endpoint -> Encoding
toEncoding Endpoint{Maybe IPv6
Maybe Port
Text
IPv4
port :: Maybe Port
ipv6 :: Maybe IPv6
ipv4 :: IPv4
serviceName :: Text
port :: Endpoint -> Maybe Port
ipv6 :: Endpoint -> Maybe IPv6
ipv4 :: Endpoint -> IPv4
serviceName :: Endpoint -> Text
..} = Series -> Encoding
pairs (Series -> Encoding)
-> ([Maybe Series] -> Series) -> [Maybe Series] -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Series] -> Series
forall a. Monoid a => [a] -> a
mconcat ([Series] -> Series)
-> ([Maybe Series] -> [Series]) -> [Maybe Series] -> Series
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Maybe Series] -> [Series]
forall a. [Maybe a] -> [a]
catMaybes ([Maybe Series] -> Encoding) -> [Maybe Series] -> Encoding
forall a b. (a -> b) -> a -> b
$
        [ Text -> Encoding -> Series
pair Text
"serviceName" (Encoding -> Series) -> (Text -> Encoding) -> Text -> Series
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Encoding
forall a. Text -> Encoding' a
text (Text -> Series) -> Maybe Text -> Maybe Series
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Maybe Text
forall a. a -> Maybe a
Just Text
serviceName
        , Text -> Encoding -> Series
pair Text
"ipv4" (Encoding -> Series) -> (IPv4 -> Encoding) -> IPv4 -> Series
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IPv4 -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding  (IPv4 -> Series) -> Maybe IPv4 -> Maybe Series
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IPv4 -> Maybe IPv4
forall a. a -> Maybe a
Just IPv4
ipv4
        , Text -> Encoding -> Series
pair Text
"ipv6" (Encoding -> Series) -> (IPv6 -> Encoding) -> IPv6 -> Series
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IPv6 -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding  (IPv6 -> Series) -> Maybe IPv6 -> Maybe Series
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe IPv6
ipv6
        , Text -> Encoding -> Series
pair Text
"port" (Encoding -> Series) -> (Port -> Encoding) -> Port -> Series
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Port -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding  (Port -> Series) -> Maybe Port -> Maybe Series
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Port
port
        ]

defaultZipkinAddr :: Addr 'HTTP
defaultZipkinAddr :: Addr 'HTTP
defaultZipkinAddr = String -> Port -> Bool -> Addr 'HTTP
HTTPAddr String
"127.0.0.1" Port
9411 Bool
False