2015-12-20 14:35:04 +00:00
|
|
|
module Test.Instances where
|
|
|
|
|
2017-05-07 04:25:58 +00:00
|
|
|
import Prelude (class Eq, class Show, bind, pure, ($), (=<<), (<$>), map, (<=<))
|
|
|
|
import Data.Traversable (traverse)
|
|
|
|
import Data.Foreign (readArray, readNumber, readString, readInt, F, Foreign, ForeignError(..), fail, readString)
|
|
|
|
import Data.Foreign.Index (readProp)
|
2017-02-04 18:47:10 +00:00
|
|
|
import Data.Generic (class Generic, gShow, gEq)
|
2015-12-20 14:35:04 +00:00
|
|
|
import Data.YAML.Foreign.Encode
|
|
|
|
|
|
|
|
data Point = Point Int Int
|
|
|
|
|
|
|
|
data Mobility
|
|
|
|
= Fix
|
|
|
|
| Flex
|
|
|
|
|
|
|
|
data GeoObject = GeoObject
|
|
|
|
{ name :: String
|
|
|
|
, scale :: Number
|
|
|
|
, points :: Array Point
|
|
|
|
, mobility :: Mobility
|
|
|
|
, coverage :: Number
|
|
|
|
}
|
|
|
|
|
|
|
|
derive instance genericGeoObject :: Generic GeoObject
|
|
|
|
instance showGeoObject :: Show GeoObject where show = gShow
|
|
|
|
instance eqGeoObject :: Eq GeoObject where eq = gEq
|
|
|
|
|
|
|
|
derive instance genericPoint :: Generic Point
|
|
|
|
instance showPoint :: Show Point where show = gShow
|
|
|
|
instance eqPoint :: Eq Point where eq = gEq
|
|
|
|
|
|
|
|
derive instance genericMobility :: Generic Mobility
|
|
|
|
instance showMobility :: Show Mobility where show = gShow
|
|
|
|
instance eqMobility :: Eq Mobility where eq = gEq
|
|
|
|
|
2017-05-07 04:25:58 +00:00
|
|
|
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 }
|
2015-12-20 14:35:04 +00:00
|
|
|
|
2017-05-07 04:25:58 +00:00
|
|
|
readPoint :: Foreign -> F Point
|
|
|
|
readPoint value = do
|
|
|
|
-- instance pointIsForeign :: IsForeign Point where
|
|
|
|
-- read value = do
|
|
|
|
x <- readInt =<< readProp "X" value
|
|
|
|
y <- readInt =<< readProp "Y" value
|
2017-02-04 18:47:10 +00:00
|
|
|
pure $ Point x y
|
2015-12-20 14:35:04 +00:00
|
|
|
|
2017-05-07 04:25:58 +00:00
|
|
|
readMobility :: Foreign -> F Mobility
|
|
|
|
readMobility value = do
|
|
|
|
-- instance mobilityIsForeign :: IsForeign Mobility where
|
|
|
|
-- read value = do
|
2015-12-20 14:35:04 +00:00
|
|
|
mob <- readString value
|
|
|
|
case mob of
|
2017-02-04 18:47:10 +00:00
|
|
|
"Fix" -> pure Fix
|
|
|
|
"Flex" -> pure Flex
|
|
|
|
_ -> fail $ JSONError "Mobility must be either Flex or Fix"
|
2015-12-20 14:35:04 +00:00
|
|
|
|
|
|
|
instance pointToYAML :: ToYAML Point where
|
|
|
|
toYAML (Point x y) =
|
|
|
|
object
|
|
|
|
[ "X" := x
|
|
|
|
, "Y" := y
|
|
|
|
]
|
|
|
|
|
|
|
|
instance mobilityToYAML :: ToYAML Mobility where
|
|
|
|
toYAML Fix = toYAML "Fix"
|
|
|
|
toYAML Flex = toYAML "Flex"
|
|
|
|
|
|
|
|
instance archiObjectToYAML :: ToYAML GeoObject where
|
|
|
|
toYAML (GeoObject o) =
|
|
|
|
object
|
|
|
|
[ "Name" := o.name
|
|
|
|
, "Scale" := o.scale
|
|
|
|
, "Points" := o.points
|
|
|
|
, "Mobility" := o.mobility
|
|
|
|
, "Coverage" := o.coverage
|
|
|
|
]
|