forked from haskell-github/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContent.hs
More file actions
209 lines (173 loc) · 6.32 KB
/
Content.hs
File metadata and controls
209 lines (173 loc) · 6.32 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
{-# LANGUAGE RecordWildCards #-}
-----------------------------------------------------------------------------
-- |
-- License : BSD-3-Clause
-- Maintainer : Oleg Grenrus <oleg.grenrus@iki.fi>
--
module GitHub.Data.Content where
import GitHub.Data.GitData
import GitHub.Data.URL
import GitHub.Internal.Prelude
import Prelude ()
import Data.Aeson.Types (Pair)
import Data.Maybe (maybe)
import qualified Data.Text as T
data Content
= ContentFile !ContentFileData
| ContentDirectory !(Vector ContentItem)
deriving (Show, Data, Typeable, Eq, Ord, Generic)
instance NFData Content where rnf = genericRnf
instance Binary Content
data ContentFileData = ContentFileData {
contentFileInfo :: !ContentInfo
,contentFileEncoding :: !Text
,contentFileSize :: !Int
,contentFileContent :: !Text
} deriving (Show, Data, Typeable, Eq, Ord, Generic)
instance NFData ContentFileData where rnf = genericRnf
instance Binary ContentFileData
-- | An item in a directory listing.
data ContentItem = ContentItem {
contentItemType :: !ContentItemType
,contentItemInfo :: !ContentInfo
} deriving (Show, Data, Typeable, Eq, Ord, Generic)
instance NFData ContentItem where rnf = genericRnf
instance Binary ContentItem
data ContentItemType = ItemFile | ItemDir
deriving (Show, Data, Typeable, Eq, Ord, Generic)
instance NFData ContentItemType where rnf = genericRnf
instance Binary ContentItemType
-- | Information common to both kinds of Content: files and directories.
data ContentInfo = ContentInfo {
contentName :: !Text
,contentPath :: !Text
,contentSha :: !Text
,contentUrl :: !URL
,contentGitUrl :: !URL
,contentHtmlUrl :: !URL
} deriving (Show, Data, Typeable, Eq, Ord, Generic)
instance NFData ContentInfo where rnf = genericRnf
instance Binary ContentInfo
data ContentResultInfo = ContentResultInfo
{ contentResultInfo :: !ContentInfo
, contentResultSize :: !Int
} deriving (Show, Data, Typeable, Eq, Ord, Generic)
instance NFData ContentResultInfo where rnf = genericRnf
instance Binary ContentResultInfo
data ContentResult = ContentResult
{ contentResultContent :: !ContentResultInfo
, contentResultCommit :: !GitCommit
} deriving (Show, Data, Typeable, Eq, Ord, Generic)
instance NFData ContentResult where rnf = genericRnf
instance Binary ContentResult
data Author = Author
{ authorName :: !Text
, authorEmail :: !Text
}
deriving (Eq, Ord, Show, Data, Typeable, Generic)
instance NFData Author where rnf = genericRnf
instance Binary Author
data CreateFile = CreateFile
{ createFilePath :: !Text
, createFileMessage :: !Text
, createFileContent :: !Text
, createFileBranch :: !(Maybe Text)
, createFileAuthor :: !(Maybe Author)
, createFileCommitter :: !(Maybe Author)
}
deriving (Eq, Ord, Show, Data, Typeable, Generic)
instance NFData CreateFile where rnf = genericRnf
instance Binary CreateFile
data UpdateFile = UpdateFile
{ updateFilePath :: !Text
, updateFileMessage :: !Text
, updateFileContent :: !Text
, updateFileSHA :: !Text
, updateFileBranch :: !(Maybe Text)
, updateFileAuthor :: !(Maybe Author)
, updateFileCommitter :: !(Maybe Author)
}
deriving (Eq, Ord, Show, Data, Typeable, Generic)
instance NFData UpdateFile where rnf = genericRnf
instance Binary UpdateFile
data DeleteFile = DeleteFile
{ deleteFilePath :: !Text
, deleteFileMessage :: !Text
, deleteFileSHA :: !Text
, deleteFileBranch :: !(Maybe Text)
, deleteFileAuthor :: !(Maybe Author)
, deleteFileCommitter :: !(Maybe Author)
}
deriving (Eq, Ord, Show, Data, Typeable, Generic)
instance NFData DeleteFile where rnf = genericRnf
instance Binary DeleteFile
instance FromJSON Content where
parseJSON o@(Object _) = ContentFile <$> parseJSON o
parseJSON (Array os) = ContentDirectory <$> traverse parseJSON os
parseJSON _ = fail "Could not build a Content"
instance FromJSON ContentFileData where
parseJSON = withObject "ContentFileData" $ \o ->
ContentFileData <$> parseJSON (Object o)
<*> o .: "encoding"
<*> o .: "size"
<*> o .: "content"
instance FromJSON ContentItem where
parseJSON = withObject "ContentItem" $ \o ->
ContentItem <$> o .: "type"
<*> parseJSON (Object o)
instance FromJSON ContentItemType where
parseJSON = withText "ContentItemType" $ \t -> case T.toLower t of
"file" -> pure ItemFile
"dir" -> pure ItemDir
_ -> fail $ "Unknown ContentItemType: " <> T.unpack t
instance FromJSON ContentInfo where
parseJSON = withObject "ContentInfo" $ \o ->
ContentInfo <$> o .: "name"
<*> o .: "path"
<*> o .: "sha"
<*> o .: "url"
<*> o .: "git_url"
<*> o .: "html_url"
instance FromJSON ContentResultInfo where
parseJSON = withObject "ContentResultInfo" $ \o ->
ContentResultInfo <$> parseJSON (Object o)
<*> o .: "size"
instance FromJSON ContentResult where
parseJSON = withObject "ContentResult" $ \o ->
ContentResult <$> o .: "content"
<*> o .: "commit"
instance ToJSON Author where
toJSON Author {..} = object
[ "name" .= authorName
, "email" .= authorEmail
]
instance ToJSON CreateFile where
toJSON CreateFile {..} = object $
[ "path" .= createFilePath
, "message" .= createFileMessage
, "content" .= createFileContent
]
++ "branch" .=? createFileBranch
++ "author" .=? createFileAuthor
++ "committer" .=? createFileCommitter
instance ToJSON UpdateFile where
toJSON UpdateFile {..} = object $
[ "path" .= updateFilePath
, "message" .= updateFileMessage
, "content" .= updateFileContent
, "sha" .= updateFileSHA
]
++ "branch" .=? updateFileBranch
++ "author" .=? updateFileAuthor
++ "committer" .=? updateFileCommitter
instance ToJSON DeleteFile where
toJSON DeleteFile {..} = object $
[ "path" .= deleteFilePath
, "message" .= deleteFileMessage
, "sha" .= deleteFileSHA
]
++ "branch" .=? deleteFileBranch
++ "author" .=? deleteFileAuthor
++ "committer" .=? deleteFileCommitter
(.=?) :: ToJSON v => Text -> Maybe v -> [Pair]
name .=? value = maybe [] (pure . (name .=)) value