secret
Created

  • Download Gist
foo.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
{-# LANGUAGE DeriveGeneric #-} -- for JTask and Fields ToJSON instances:w!
{-# LANGUAGE FlexibleInstances #-} -- for the HashMap ToJSON instances
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE OverloadedStrings #-}
 
import Prelude
import Data.ByteString
import GHC.Generics (Generic )
import Data.Aeson
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HashMap
import Data.Hashable
import Data.ByteString.Char8 ()
 
data JTask = JTask {fields :: Fields} deriving (Generic)
data Fields = Fields { project :: HashMap Key Project
, summary :: ByteString
, issuetype :: HashMap Name Task
, versions :: [HashMap Name Version]
, description :: ByteString
} deriving (Generic)
 
data Key = Key deriving (Generic)
 
instance Hashable Key where
hash _ = 0
 
instance Show Key where
show Key = "key"
 
data Name = Name deriving (Generic)
 
instance Hashable Name where
hash _ = 0
 
instance Show Name where
show Name = "name"
 
data Task = Task deriving (Generic)
 
instance Hashable Task where
hash _ = 0
 
type Version = ByteString -- Placeholder type. Probably using Day for realsies.
 
data Project = BNAP deriving (Generic) -- Fill this out as we go
 
instance ToJSON Fields
instance ToJSON JTask
 
instance ToJSON Key
instance ToJSON Name
instance ToJSON Task
instance ToJSON Project
 
mapfst :: (a -> b) -> [(a, v)] -> [(b, v)]
mapfst f = fmap $ \ (k, v) -> (f k, v)
 
instance ToJSON a => ToJSON (HashMap Key a) where
toJSON = toJSON . HashMap.fromList . mapfst show . HashMap.toList
 
instance ToJSON a => ToJSON (HashMap Name a) where
toJSON = toJSON . HashMap.fromList . mapfst show . HashMap.toList
 
createObject :: Value
createObject =
toJSON
. JTask
$ Fields { project = HashMap.singleton Key BNAP
, summary = "summary"
, issuetype = HashMap.singleton Name Task
, versions = [HashMap.singleton Name ("version")]
, description = "description"
}
gistfile1.txt
1 2
*Main Data.ByteString.Lazy> Data.ByteString.Lazy.putStrLn (encode createObject)
{"fields":{"project":{"key":[]},"description":"description","issuetype":{"name":[]},"summary":"summary","versions":[{"name":"version"}]}}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.