on master help sign in
Create encoders and decoders for Elm.Package and Elm.Compiler.Module
commit11bd592ee9400003d80aa142a6b835f6c3cb0433 authorEvan Czaplicki <evancz@users.noreply.github.com> dateMon 3 Apr 2017, 18:08 (10 years ago) parentf775024
3 files changed+77−114
src/Elm/Compiler/Module.hs+16−27 src/Elm/Package.hs+61−55 src/Json/Decode.hs−32
src/Elm/Compiler/Module.hs+16−27
@@ -15,15 +15,14 @@ module Elm.Compiler.Module
1515 , nameFromText
1616 , hyphenate
1717 , dehyphenate
18− , RawForJson(..)
19− , fromJson
18+ , encode
19+ , decoder
2020
2121 -- canonical names
2222 , ModuleName.Canonical(..)
2323 )
2424 where
2525
26−import qualified Data.Aeson as Json
2726import qualified Data.Char as Char
2827import qualified Data.List as List
2928import qualified Data.Map as Map
@@ -35,6 +34,8 @@ import qualified AST.Module as Module
3534import qualified AST.Module.Name as ModuleName
3635import qualified Elm.Compiler.Type as PublicType
3736import qualified Elm.Compiler.Type.Extract as Extract
37+import qualified Json.Decode as Decode
38+import qualified Json.Encode as Encode
3839
3940
4041
@@ -105,32 +106,20 @@ isGoodChar char =
105106
106107
107108
108−-- JSON for NAME
109+-- JSON
109110
110111
111−newtype RawForJson =
112− RawForJson ModuleName.Raw
112+encode :: ModuleName.Raw -> Encode.Value
113+encode =
114+ Encode.text
113115
114116
115−fromJson :: RawForJson -> ModuleName.Raw
116−fromJson (RawForJson raw) =
117− raw
118−
119−
120−instance Json.ToJSON RawForJson where
121− toJSON (RawForJson name) =
122− Json.toJSON (nameToString name)
123−
124−
125−instance Json.FromJSON RawForJson where
126− parseJSON (Json.String text) =
127− case nameFromText text of
128− Nothing ->
129− fail (Text.unpack text ++ " is not a valid module name")
130−
131− Just name ->
132− return (RawForJson name)
133−
134− parseJSON _ =
135− fail "expecting the module name to be a string"
117+decoder :: Decode.Decoder ModuleName.Raw
118+decoder =
119+ do txt <- Decode.text
120+ case nameFromText txt of
121+ Nothing ->
122+ Decode.fail "a module name like \"Html.Events\""
136123
124+ Just name ->
125+ Decode.succeed name
src/Elm/Package.hs+61−55
@@ -11,11 +11,15 @@ module Elm.Package
1111 , bumpPatch, bumpMinor, bumpMajor
1212 , filterLatest, majorAndMinor
1313 , versionToString, versionToText, versionFromText
14+ , decoder
15+ , encode
16+ , versionDecoder
17+ , encodeVersion
1418 )
1519 where
1620
17−import qualified Data.Aeson as Json
18−import Data.Binary
21+
22+import Data.Binary (Binary, get, getWord8, put, putWord8)
1923import qualified Data.Char as Char
2024import Data.Function (on)
2125import qualified Data.List as List
@@ -26,7 +30,10 @@ import qualified Data.Text.Lazy as B (toStrict)
2630import qualified Data.Text.Lazy.Builder as B (singleton, toLazyText)
2731import qualified Data.Text.Lazy.Builder.Int as B (decimal)
2832import Data.Text (Text)
33+import Data.Word (Word16)
2934import System.FilePath ((</>))
35+import qualified Json.Decode as Decode
36+import qualified Json.Encode as Encode
3037
3138
3239
@@ -117,44 +124,6 @@ validateProjectName text =
117124 Right text
118125
119126
120−instance Binary Name where
121− get =
122− Name <$> get <*> get
123−
124− put (Name user project) =
125− do put user
126− put project
127−
128−
129−instance Binary Package where
130− get =
131− Package <$> get <*> get
132−
133− put (Package name version) =
134− do put name
135− put version
136−
137−
138−instance Json.FromJSON Name where
139− parseJSON (Json.String text) =
140− case fromText text of
141− Left msg ->
142− fail $
143− "Ran into an invalid package name: "
144− ++ Text.unpack text ++ "\n\n" ++ msg
145−
146− Right name ->
147− return name
148−
149− parseJSON _ =
150− fail "Project name must be a string."
151−
152−
153−instance Json.ToJSON Name where
154− toJSON name =
155− Json.String (toText name)
156−
157−
158127
159128-- PACKAGE VERSIONS
160129
@@ -248,6 +217,28 @@ toNumber txt =
248217 Left "Must have format MAJOR.MINOR.PATCH (e.g. 1.0.2)"
249218
250219
220+
221+-- BINARY
222+
223+
224+instance Binary Name where
225+ get =
226+ Name <$> get <*> get
227+
228+ put (Name user project) =
229+ do put user
230+ put project
231+
232+
233+instance Binary Package where
234+ get =
235+ Package <$> get <*> get
236+
237+ put (Package name version) =
238+ do put name
239+ put version
240+
241+
251242instance Binary Version where
252243 get =
253244 do word <- getWord8
@@ -270,23 +261,38 @@ instance Binary Version where
270261 put patch
271262
272263
273−instance Json.FromJSON Version where
274− parseJSON (Json.String text) =
275− case versionFromText text of
276− Right version ->
277− return version
278264
279− Left problem ->
280− fail $ unlines $
281− [ "Ran into an invalid version number: " ++ Text.unpack text
282− , problem
283− ]
265+-- JSON
266+
267+
268+decoder :: Decode.Decoder Name
269+decoder =
270+ do txt <- Decode.text
271+ case fromText txt of
272+ Left _ ->
273+ Decode.fail "a valid project name, like \"elm-lang/core\""
274+
275+ Right name ->
276+ Decode.succeed name
277+
278+
279+encode :: Name -> Encode.Value
280+encode name =
281+ Encode.text (toText name)
282+
283+
284284
285− parseJSON _ =
286− fail "Version number must be stored as a string."
285+versionDecoder :: Decode.Decoder Version
286+versionDecoder =
287+ do txt <- Decode.text
288+ case versionFromText txt of
289+ Right version ->
290+ Decode.succeed version
287291
292+ Left _ ->
293+ Decode.fail "a version number, like 1.0.0"
288294
289−instance Json.ToJSON Version where
290− toJSON version =
291− Json.String (versionToText version)
292295
296+encodeVersion :: Version -> Encode.Value
297+encodeVersion version =
298+ Encode.text (versionToText version)
src/Json/Decode.hs−32
@@ -6,8 +6,6 @@ module Json.Decode
66 , list, dict, maybe
77 , field, at
88 , map, map2, succeed, fail, andThen
9− -- elm specific stuff
10− , moduleName, packageName
119 )
1210 where
1311
@@ -23,9 +21,6 @@ import qualified Data.Scientific as Scientific
2321import qualified Data.Text as Text
2422import qualified Data.Vector as Vector
2523
26−import qualified Elm.Compiler.Module as Module
27−import qualified Elm.Package as Pkg
28−
2924
3025
3126-- DECODERS
@@ -251,30 +246,3 @@ andThen callback (Decoder runA) =
251246 do a <- runA mkError value
252247 let (Decoder runB) = callback a
253248 runB mkError value
254−
255−
256−
257−-- ELM SPECIFIC STUFF
258−
259−
260−moduleName :: Decoder Module.Raw
261−moduleName =
262− do txt <- text
263− case Module.nameFromText txt of
264− Nothing ->
265− fail "a module name like \"Html.Events\""
266−
267− Just name ->
268− succeed name
269−
270−
271−packageName :: Decoder Pkg.Name
272−packageName =
273− do txt <- text
274− case Pkg.fromText txt of
275− Left _ ->
276− fail "a valid project name, like \"elm-lang/core\""
277−
278− Right name ->
279− succeed name
280−