generated from tpl/purs
Code cleanup and readme update.
This commit is contained in:
parent
fecfce4880
commit
fab2d38e08
44
README.md
44
README.md
@ -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
|
||||||
|
@ -44,16 +44,12 @@ readGeoObject value = do
|
|||||||
|
|
||||||
readPoint :: Foreign -> F Point
|
readPoint :: Foreign -> F Point
|
||||||
readPoint value = do
|
readPoint value = do
|
||||||
-- instance pointIsForeign :: IsForeign Point where
|
|
||||||
-- read value = do
|
|
||||||
x <- readInt =<< readProp "X" value
|
x <- readInt =<< readProp "X" value
|
||||||
y <- readInt =<< readProp "Y" value
|
y <- readInt =<< readProp "Y" value
|
||||||
pure $ Point x y
|
pure $ Point x y
|
||||||
|
|
||||||
readMobility :: Foreign -> F Mobility
|
readMobility :: Foreign -> F Mobility
|
||||||
readMobility value = do
|
readMobility value = do
|
||||||
-- instance mobilityIsForeign :: IsForeign Mobility where
|
|
||||||
-- read value = do
|
|
||||||
mob <- readString value
|
mob <- readString value
|
||||||
case mob of
|
case mob of
|
||||||
"Fix" -> pure Fix
|
"Fix" -> pure Fix
|
||||||
|
Loading…
Reference in New Issue
Block a user