<commit_before>{-# LANGUAGE ExistentialQuantification, RankNTypes, GADTs, OverloadedStrings #-}
-----------------------------------------------------------------------------
-- Copyright 2017, GRACeFUL project team. This file is distributed under the
-- terms of the Apache License 2.0. For more information, see the files
-- "LICENSE.txt" and "NOTICE.txt", which are included in the distribution.
-----------------------------------------------------------------------------
-- |
-- Maintainer  :  <EMAIL>
-- Stability   :  experimental
-- Portability :  portable (depends on ghc)
--
-- A small data type for expressing components.
--
-----------------------------------------------------------------------------

module Library 
    ( module Types
    , Library(..)
    , Item(..)
    ) where

import GL hiding (Item)
import Types
import Utils

import Data.Aeson

type Id  = String
type URL = String

data Library = Library
    { libraryId :: Id
    , items     :: [Item]
    } deriving Show

instance ToJSON Library where
    toJSON (Library n is) = object
        [ "library" .= toJSONList is]

data Item = Item
    { itemId  :: Id
    , comment :: String
    , icon    :: URL
    , f       :: TypedValue 
    } deriving Show 

instance ToJSON Item where
    toJSON (Item n c i (f ::: t)) = object 
        [ "name"       .= n
        , "parameters" .= parameters t
        , "interface"  .= ports t 
        , "comment"    .= c
        , "icon"       .= i
        ]

parameters :: Type a -> Value
parameters = toJSONList . rec 
  where
    rec :: Type a -> [Value]
    rec tp = case tp of
        t@(Tag n t1) :-> t2 -> tag t : rec t2
        _                   -> []

tag :: Type a -> Value
tag (Tag n t) = object ["name" .= n, "type" .= show t]
tag _         = Null

ports :: Type a -> [Value]
ports tp = case tp of
    -- base
    Port' t@(Tag _ _) -> [tag t]
    -- recurse
    Tag _ t    -> ports t
    GCM t      -> ports t
    List t     -> ports t 
    Pair t1 t2 -> ports t1 ++ ports t2
    _ :-> t2   -> ports t2<commit_msg>Fixed bug in JSON interface representation.<commit_after>{-# LANGUAGE ExistentialQuantification, RankNTypes, GADTs, OverloadedStrings #-}
-----------------------------------------------------------------------------
-- Copyright 2017, GRACeFUL project team. This file is distributed under the
-- terms of the Apache License 2.0. For more information, see the files
-- "LICENSE.txt" and "NOTICE.txt", which are included in the distribution.
-----------------------------------------------------------------------------
-- |
-- Maintainer  :  <EMAIL>
-- Stability   :  experimental
-- Portability :  portable (depends on ghc)
--
-- A small data type for expressing components.
--
-----------------------------------------------------------------------------

module Library 
    ( module Types
    , Library(..)
    , Item(..)
    ) where

import GL hiding (Item)
import Types
import Utils

import Data.Aeson

type Id  = String
type URL = String

data Library = Library
    { libraryId :: Id
    , items     :: [Item]
    } deriving Show

instance ToJSON Library where
    toJSON (Library n is) = object
        [ "library" .= toJSONList is]

data Item = Item
    { itemId  :: Id
    , comment :: String
    , icon    :: URL
    , f       :: TypedValue 
    } deriving Show 

instance ToJSON Item where
    toJSON (Item n c i (f ::: t)) = object 
        [ "name"       .= n
        , "parameters" .= parameters t
        , "interface"  .= ports t 
        , "comment"    .= c
        , "icon"       .= i
        ]

parameters :: Type a -> Value
parameters = toJSONList . rec 
  where
    rec :: Type a -> [Value]
    rec tp = case tp of
        t@(Tag n t1) :-> t2 -> tag t : rec t2
        _                   -> []

tag :: Type a -> Value
tag (Tag n t) = object ["name" .= n, "type" .= show t]
tag _         = Null

ports :: Type a -> [Value]
ports tp = case tp of
    -- base
    Tag n (Port' t) -> [tag tp]
    -- recurse
    Tag _ t    -> ports t
    GCM t      -> ports t
    List t     -> ports t 
    Pair t1 t2 -> ports t1 ++ ports t2
    _ :-> t2   -> ports t2
    Iso _ t    -> ports t   
{'fraction_non_alphanumeric': 0.21701304978250363, 'fraction_numerical': 0.007249879168680522, 'mean_word_length': 1.3678489702517163, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 34, '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': '7411089', 'n_tokens_mistral': 1205, 'n_tokens_neox': 1179, 'n_words': 436}