<commit_before><commit_msg>Added utility types/functions for servant-purescript codegen<commit_after>-- | Utility functions and types needed by servant-purescript code generator.
module Servant.Subscriber.Util where

import Prelude
import Data.Argonaut.Generic.Aeson (decodeJson)
import Data.Maybe (Maybe(..))
import Data.Tuple (Tuple(Tuple))
import Data.Generic (class Generic)
import Data.Traversable (traverse)
import Servant.PureScript.Settings (gDefaultToURLPiece)
import Data.Array (concatMap)

import Servant.Subscriber (ToUserType)

type TypedToUser a b = Maybe a -> b


toUserType :: forall a b. Generic a => TypedToUser a b -> ToUserType b
toUserType typed = map typed <<< traverse decodeJson


subGenNormalQuery :: forall a . Generic a => String -> a -> Array (Tuple String (Maybe String))
subGenNormalQuery fName val = [ Tuple fName ((Just <<< gDefaultToURLPiece) val) ]

subGenListQuery :: forall a . Generic a => String -> Array a -> Array (Tuple String (Maybe String))
subGenListQuery fName = concatMap (subGenNormalQuery fName)

subGenFlagQuery :: String -> Boolean -> Array (Tuple String (Maybe String))
subGenFlagQuery fName false = []
subGenFlagQuery fName true  = [ Tuple fName Nothing ]
{'fraction_non_alphanumeric': 0.09693454846727423, 'fraction_numerical': 0.0, 'mean_word_length': 5.131979695431472, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 14, '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': '5428197', 'n_tokens_mistral': 359, 'n_tokens_neox': 352, 'n_words': 124}