{-|
Module: BattlePlace.Util
Description: General utilities.
License: MIT
-}

{-# LANGUAGE LambdaCase #-}

module BattlePlace.Util
        ( jsonOptions
        , jsonOptionsWithTag
        ) where

import qualified Data.Aeson as J

jsonOptions :: J.Options
jsonOptions = jsonOptionsWithTag "status"

jsonOptionsWithTag :: String -> J.Options
jsonOptionsWithTag tag = J.defaultOptions
        { J.fieldLabelModifier = dropBeforeUnderscore
        , J.constructorTagModifier = dropBeforeUnderscore
        , J.sumEncoding = J.TaggedObject
                { J.tagFieldName = tag
                , J.contentsFieldName = "contents"
                }
        }

dropBeforeUnderscore :: String -> String
dropBeforeUnderscore = \case
        x : xs -> case x of
                '_' -> xs
                _ -> dropBeforeUnderscore xs
        [] -> []