Code cleanup and readme update.

This commit is contained in:
Dominick Gendill 2017-05-07 09:19:50 -06:00
parent fecfce4880
commit fab2d38e08
2 changed files with 34 additions and 34 deletions

View File

@ -18,31 +18,32 @@ data GeoObject = GeoObject
## Decode YAML ## Decode YAML
Write `IsForeign` instances for your data structures. Write functions to read your data from foreign values.
```purescript ```purescript
instance pointIsForeign :: IsForeign Point where readPoint :: Foreign -> F Point
read value = do readPoint value = do
x <- readProp "X" value x <- readInt =<< readProp "X" value
y <- readProp "Y" value y <- readInt =<< readProp "Y" value
return $ Point x y pure $ Point x y
instance mobilityIsForeign :: IsForeign Mobility where readMobility :: Foreign -> F Mobility
read value = do readMobility value = do
mob <- readString value mob <- readString value
case mob of case mob of
"Fix" -> return Fix "Fix" -> pure Fix
"Flex" -> return Flex "Flex" -> pure Flex
_ -> Left $ JSONError "Mobility must be either Flex or Fix" _ -> fail $ JSONError "Mobility must be either Flex or Fix"
readGeoObject :: Foreign -> F GeoObject
readGeoObject value = do
name <- readString =<< readProp "Name" value
scale <- readNumber =<< readProp "Scale" value
points <- traverse readPoint =<< readArray =<< readProp "Points" value
mobility <- readMobility =<< readProp "Mobility" value
coverage <- readNumber =<< readProp "Coverage" value
pure $ GeoObject { name, scale, points, mobility, coverage }
instance archiObjectIsForeign :: IsForeign GeoObject where
read value = do
name <- readProp "Name" value
scale <- readProp "Scale" value
points <- readProp "Points" value
mobility <- readProp "Mobility" value
coverage <- readProp "Coverage" value
return $ GeoObject { name, scale, points, mobility, coverage }
``` ```
Read the YAML into your data structures. Read the YAML into your data structures.
@ -74,7 +75,10 @@ yamlInput = """
Coverage: 10 Coverage: 10
""" """
decoded = (readYAML yamlInput) :: F (Array GeoObject) let decoded =
(parseYAML yamlInput) >>=
readArray >>=
traverse readGeoObject
``` ```
## Encode YAML ## Encode YAML

View File

@ -44,21 +44,17 @@ readGeoObject value = do
readPoint :: Foreign -> F Point readPoint :: Foreign -> F Point
readPoint value = do readPoint value = do
-- instance pointIsForeign :: IsForeign Point where x <- readInt =<< readProp "X" value
-- read value = do y <- readInt =<< readProp "Y" value
x <- readInt =<< readProp "X" value pure $ Point x y
y <- readInt =<< readProp "Y" value
pure $ Point x y
readMobility :: Foreign -> F Mobility readMobility :: Foreign -> F Mobility
readMobility value = do readMobility value = do
-- instance mobilityIsForeign :: IsForeign Mobility where mob <- readString value
-- read value = do case mob of
mob <- readString value "Fix" -> pure Fix
case mob of "Flex" -> pure Flex
"Fix" -> pure Fix _ -> fail $ JSONError "Mobility must be either Flex or Fix"
"Flex" -> pure Flex
_ -> fail $ JSONError "Mobility must be either Flex or Fix"
instance pointToYAML :: ToYAML Point where instance pointToYAML :: ToYAML Point where
toYAML (Point x y) = toYAML (Point x y) =