generated from tpl/purs
chore: fmt
This commit is contained in:
parent
30b219d237
commit
00fb682314
@ -1,10 +1,9 @@
|
|||||||
import yaml from 'js-yaml'
|
import yaml from 'js-yaml'
|
||||||
|
|
||||||
export function parseYAMLImpl (left, right, str) {
|
export function parseYAMLImpl(left, right, str) {
|
||||||
try {
|
try {
|
||||||
return right(yaml.load(str))
|
return right(yaml.load(str))
|
||||||
}
|
} catch (e) {
|
||||||
catch (e) {
|
|
||||||
return left(e.toString())
|
return left(e.toString())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
module Data.YAML.Foreign.Decode (parseYAMLToJson)
|
module Data.YAML.Foreign.Decode (parseYAMLToJson) where
|
||||||
where
|
|
||||||
|
|
||||||
import Foreign (F, Foreign, ForeignError(..), fail)
|
import Foreign (F, Foreign, ForeignError(..), fail)
|
||||||
import Data.Function.Uncurried (Fn3, runFn3)
|
import Data.Function.Uncurried (Fn3, runFn3)
|
||||||
@ -7,16 +6,15 @@ import Prelude (pure, (<<<), (>>=))
|
|||||||
import Unsafe.Coerce (unsafeCoerce)
|
import Unsafe.Coerce (unsafeCoerce)
|
||||||
import Data.Argonaut.Core (Json)
|
import Data.Argonaut.Core (Json)
|
||||||
|
|
||||||
foreign import parseYAMLImpl :: forall r.
|
foreign import parseYAMLImpl
|
||||||
Fn3 (String -> r) (Foreign -> r) String r
|
:: forall r
|
||||||
|
. Fn3 (String -> r) (Foreign -> r) String r
|
||||||
|
|
||||||
-- | Attempt to parse a YAML string, returning the result as foreign data.
|
-- | Attempt to parse a YAML string, returning the result as foreign data.
|
||||||
parseYAML :: String -> F Foreign
|
parseYAML :: String -> F Foreign
|
||||||
parseYAML yaml =
|
parseYAML yaml =
|
||||||
runFn3 parseYAMLImpl (fail <<< ForeignError) pure yaml
|
runFn3 parseYAMLImpl (fail <<< ForeignError) pure yaml
|
||||||
|
|
||||||
|
|
||||||
-- | Attempt to parse a YAML string, returning the result as Json
|
-- | Attempt to parse a YAML string, returning the result as Json
|
||||||
parseYAMLToJson :: String -> F Json
|
parseYAMLToJson :: String -> F Json
|
||||||
parseYAMLToJson yaml =
|
parseYAMLToJson yaml =
|
||||||
|
@ -2,17 +2,17 @@ import yaml from 'js-yaml'
|
|||||||
|
|
||||||
export const jsNull = null
|
export const jsNull = null
|
||||||
|
|
||||||
export function objToHash (valueToYAMLImpl, fst, snd, obj) {
|
export function objToHash(valueToYAMLImpl, fst, snd, obj) {
|
||||||
const hash = {}
|
const hash = {}
|
||||||
|
|
||||||
for(let i = 0; i < obj.length; i++) {
|
for (let i = 0; i < obj.length; i++) {
|
||||||
hash[fst(obj[i])] = valueToYAMLImpl(snd(obj[i]))
|
hash[fst(obj[i])] = valueToYAMLImpl(snd(obj[i]))
|
||||||
}
|
}
|
||||||
|
|
||||||
return hash
|
return hash
|
||||||
}
|
}
|
||||||
|
|
||||||
export function toYAMLImpl (a) {
|
export function toYAMLImpl(a) {
|
||||||
// noCompatMode does not support YAML 1.1
|
// noCompatMode does not support YAML 1.1
|
||||||
return yaml.dump(a, {noCompatMode : true})
|
return yaml.dump(a, { noCompatMode: true })
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
module Data.YAML.Foreign.Encode (
|
module Data.YAML.Foreign.Encode
|
||||||
YValue,
|
( YValue
|
||||||
class ToYAML,
|
, class ToYAML
|
||||||
toYAML,
|
, toYAML
|
||||||
entry, (:=),
|
, entry
|
||||||
object,
|
, (:=)
|
||||||
printYAML
|
, object
|
||||||
|
, printYAML
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Data.Map as M
|
import Data.Map as M
|
||||||
@ -19,62 +20,63 @@ import Unsafe.Coerce (unsafeCoerce)
|
|||||||
|
|
||||||
type YObject = M.Map String YValue
|
type YObject = M.Map String YValue
|
||||||
type YArray = Array YValue
|
type YArray = Array YValue
|
||||||
|
|
||||||
foreign import data YAML :: Type
|
foreign import data YAML :: Type
|
||||||
|
|
||||||
data YValue
|
data YValue
|
||||||
= YObject YObject
|
= YObject YObject
|
||||||
| YArray YArray
|
| YArray YArray
|
||||||
| YString String
|
| YString String
|
||||||
| YNumber Number
|
| YNumber Number
|
||||||
| YInt Int
|
| YInt Int
|
||||||
| YBoolean Boolean
|
| YBoolean Boolean
|
||||||
| YNull
|
| YNull
|
||||||
|
|
||||||
instance showYValue :: Show YValue where
|
instance showYValue :: Show YValue where
|
||||||
show (YObject m) = "YObject " <> show m
|
show (YObject m) = "YObject " <> show m
|
||||||
show (YArray vs) = "YArray " <> show vs
|
show (YArray vs) = "YArray " <> show vs
|
||||||
show (YString s) = "YString " <> show s
|
show (YString s) = "YString " <> show s
|
||||||
show (YNumber n) = "YNumber " <> show n
|
show (YNumber n) = "YNumber " <> show n
|
||||||
show (YInt i) = "YInt " <> show i
|
show (YInt i) = "YInt " <> show i
|
||||||
show (YBoolean b) = "YBoolean " <> show b
|
show (YBoolean b) = "YBoolean " <> show b
|
||||||
show YNull = "YNull"
|
show YNull = "YNull"
|
||||||
|
|
||||||
instance eqYValue :: Eq YValue where
|
instance eqYValue :: Eq YValue where
|
||||||
eq (YObject a) (YObject b) = a == b
|
eq (YObject a) (YObject b) = a == b
|
||||||
eq (YArray a) (YArray b) = a == b
|
eq (YArray a) (YArray b) = a == b
|
||||||
eq (YString a) (YString b) = a == b
|
eq (YString a) (YString b) = a == b
|
||||||
eq (YNumber a) (YNumber b) = a == b
|
eq (YNumber a) (YNumber b) = a == b
|
||||||
eq (YInt a) (YInt b) = a == b
|
eq (YInt a) (YInt b) = a == b
|
||||||
eq (YBoolean a) (YBoolean b) = a == b
|
eq (YBoolean a) (YBoolean b) = a == b
|
||||||
eq YNull YNull = true
|
eq YNull YNull = true
|
||||||
eq _ _ = false
|
eq _ _ = false
|
||||||
|
|
||||||
class ToYAML a where
|
class ToYAML a where
|
||||||
toYAML :: a -> YValue
|
toYAML :: a -> YValue
|
||||||
|
|
||||||
instance mapToYAML :: (ToYAML a) => ToYAML (Map String a) where
|
instance mapToYAML :: (ToYAML a) => ToYAML (Map String a) where
|
||||||
toYAML m = YObject $ map (\value -> toYAML value) m
|
toYAML m = YObject $ map (\value -> toYAML value) m
|
||||||
|
|
||||||
instance booleanToYAML :: ToYAML Boolean where
|
instance booleanToYAML :: ToYAML Boolean where
|
||||||
toYAML = YBoolean
|
toYAML = YBoolean
|
||||||
|
|
||||||
instance intToYAML :: ToYAML Int where
|
instance intToYAML :: ToYAML Int where
|
||||||
toYAML = YInt
|
toYAML = YInt
|
||||||
|
|
||||||
instance numberToYAML :: ToYAML Number where
|
instance numberToYAML :: ToYAML Number where
|
||||||
toYAML = YNumber
|
toYAML = YNumber
|
||||||
|
|
||||||
instance stringToYAML :: ToYAML String where
|
instance stringToYAML :: ToYAML String where
|
||||||
toYAML = YString
|
toYAML = YString
|
||||||
|
|
||||||
instance arrayToYAML :: (ToYAML a) => ToYAML (Array a) where
|
instance arrayToYAML :: (ToYAML a) => ToYAML (Array a) where
|
||||||
toYAML = YArray <<< map toYAML
|
toYAML = YArray <<< map toYAML
|
||||||
|
|
||||||
instance maybeToYAML :: (ToYAML a) => ToYAML (Maybe a) where
|
instance maybeToYAML :: (ToYAML a) => ToYAML (Maybe a) where
|
||||||
toYAML = maybe YNull toYAML
|
toYAML = maybe YNull toYAML
|
||||||
|
|
||||||
instance yvalueToYAML :: ToYAML YValue where
|
instance yvalueToYAML :: ToYAML YValue where
|
||||||
toYAML = identity
|
toYAML = identity
|
||||||
|
|
||||||
type Pair = Tuple String YValue
|
type Pair = Tuple String YValue
|
||||||
|
|
||||||
@ -93,12 +95,12 @@ object :: Array Pair -> YValue
|
|||||||
object ps = YObject $ M.fromFoldable (toUnfoldable ps :: List Pair)
|
object ps = YObject $ M.fromFoldable (toUnfoldable ps :: List Pair)
|
||||||
|
|
||||||
foreign import jsNull :: YAML
|
foreign import jsNull :: YAML
|
||||||
foreign import objToHash ::
|
foreign import objToHash
|
||||||
Fn4 (YValue -> YAML)
|
:: Fn4 (YValue -> YAML)
|
||||||
(Tuple String YValue -> String)
|
(Tuple String YValue -> String)
|
||||||
(Tuple String YValue -> YValue)
|
(Tuple String YValue -> YValue)
|
||||||
(Array (Tuple String YValue))
|
(Array (Tuple String YValue))
|
||||||
YAML
|
YAML
|
||||||
|
|
||||||
valueToYAML :: YValue -> YAML
|
valueToYAML :: YValue -> YAML
|
||||||
valueToYAML (YObject o) = runFn4 objToHash valueToYAML fst snd $ M.toUnfoldable o
|
valueToYAML (YObject o) = runFn4 objToHash valueToYAML fst snd $ M.toUnfoldable o
|
||||||
|
@ -14,28 +14,37 @@ import Prelude (class Eq, class Show, bind, pure, ($))
|
|||||||
data Point = Point Int Int
|
data Point = Point Int Int
|
||||||
|
|
||||||
data Mobility
|
data Mobility
|
||||||
= Fix
|
= Fix
|
||||||
| Flex
|
| Flex
|
||||||
|
|
||||||
data GeoObject = GeoObject
|
data GeoObject = GeoObject
|
||||||
{ name :: String
|
{ name :: String
|
||||||
, scale :: Number
|
, scale :: Number
|
||||||
, points :: Array Point
|
, points :: Array Point
|
||||||
, mobility :: Mobility
|
, mobility :: Mobility
|
||||||
, coverage :: Number
|
, coverage :: Number
|
||||||
}
|
}
|
||||||
|
|
||||||
derive instance genericGeoObject :: Generic GeoObject _
|
derive instance genericGeoObject :: Generic GeoObject _
|
||||||
instance showGeoObject :: Show GeoObject where show = genericShow
|
instance showGeoObject :: Show GeoObject where
|
||||||
instance eqGeoObject :: Eq GeoObject where eq = genericEq
|
show = genericShow
|
||||||
|
|
||||||
|
instance eqGeoObject :: Eq GeoObject where
|
||||||
|
eq = genericEq
|
||||||
|
|
||||||
derive instance genericPoint :: Generic Point _
|
derive instance genericPoint :: Generic Point _
|
||||||
instance showPoint :: Show Point where show = genericShow
|
instance showPoint :: Show Point where
|
||||||
instance eqPoint :: Eq Point where eq = genericEq
|
show = genericShow
|
||||||
|
|
||||||
|
instance eqPoint :: Eq Point where
|
||||||
|
eq = genericEq
|
||||||
|
|
||||||
derive instance genericMobility :: Generic Mobility _
|
derive instance genericMobility :: Generic Mobility _
|
||||||
instance showMobility :: Show Mobility where show = genericShow
|
instance showMobility :: Show Mobility where
|
||||||
instance eqMobility :: Eq Mobility where eq = genericEq
|
show = genericShow
|
||||||
|
|
||||||
|
instance eqMobility :: Eq Mobility where
|
||||||
|
eq = genericEq
|
||||||
|
|
||||||
instance geoJson :: DecodeJson GeoObject where
|
instance geoJson :: DecodeJson GeoObject where
|
||||||
decodeJson json = do
|
decodeJson json = do
|
||||||
@ -51,10 +60,9 @@ instance mobilityJson :: DecodeJson Mobility where
|
|||||||
decodeJson json = do
|
decodeJson json = do
|
||||||
mob <- decodeJson json
|
mob <- decodeJson json
|
||||||
case mob of
|
case mob of
|
||||||
"Fix" -> pure Fix
|
"Fix" -> pure Fix
|
||||||
"Flex" -> pure Flex
|
"Flex" -> pure Flex
|
||||||
_ -> Left $ TypeMismatch "Mobility must be either Flex or Fix"
|
_ -> Left $ TypeMismatch "Mobility must be either Flex or Fix"
|
||||||
|
|
||||||
|
|
||||||
instance pointJson :: DecodeJson Point where
|
instance pointJson :: DecodeJson Point where
|
||||||
decodeJson json = do
|
decodeJson json = do
|
||||||
@ -63,24 +71,23 @@ instance pointJson :: DecodeJson Point where
|
|||||||
y <- getField obj "Y"
|
y <- getField obj "Y"
|
||||||
pure $ Point x y
|
pure $ Point x y
|
||||||
|
|
||||||
|
|
||||||
instance pointToYAML :: ToYAML Point where
|
instance pointToYAML :: ToYAML Point where
|
||||||
toYAML (Point x y) =
|
toYAML (Point x y) =
|
||||||
object
|
object
|
||||||
[ "X" := x
|
[ "X" := x
|
||||||
, "Y" := y
|
, "Y" := y
|
||||||
]
|
]
|
||||||
|
|
||||||
instance mobilityToYAML :: ToYAML Mobility where
|
instance mobilityToYAML :: ToYAML Mobility where
|
||||||
toYAML Fix = toYAML "Fix"
|
toYAML Fix = toYAML "Fix"
|
||||||
toYAML Flex = toYAML "Flex"
|
toYAML Flex = toYAML "Flex"
|
||||||
|
|
||||||
instance archiObjectToYAML :: ToYAML GeoObject where
|
instance archiObjectToYAML :: ToYAML GeoObject where
|
||||||
toYAML (GeoObject o) =
|
toYAML (GeoObject o) =
|
||||||
object
|
object
|
||||||
[ "Name" := o.name
|
[ "Name" := o.name
|
||||||
, "Scale" := o.scale
|
, "Scale" := o.scale
|
||||||
, "Points" := o.points
|
, "Points" := o.points
|
||||||
, "Mobility" := o.mobility
|
, "Mobility" := o.mobility
|
||||||
, "Coverage" := o.coverage
|
, "Coverage" := o.coverage
|
||||||
]
|
]
|
||||||
|
@ -17,9 +17,9 @@ import Test.Spec.Assertions (shouldEqual)
|
|||||||
import Test.Spec.Reporter.Console (consoleReporter)
|
import Test.Spec.Reporter.Console (consoleReporter)
|
||||||
import Test.Spec.Runner (runSpec)
|
import Test.Spec.Runner (runSpec)
|
||||||
|
|
||||||
|
|
||||||
yamlInput :: String
|
yamlInput :: String
|
||||||
yamlInput = """
|
yamlInput =
|
||||||
|
"""
|
||||||
- Name: House
|
- Name: House
|
||||||
Scale: 9.5
|
Scale: 9.5
|
||||||
# Points describe the outer limit of an object.
|
# Points describe the outer limit of an object.
|
||||||
@ -46,7 +46,8 @@ yamlInput = """
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
yamlOutput :: String
|
yamlOutput :: String
|
||||||
yamlOutput = """- Coverage: 10
|
yamlOutput =
|
||||||
|
"""- Coverage: 10
|
||||||
Mobility: Fix
|
Mobility: Fix
|
||||||
Name: House
|
Name: House
|
||||||
Points:
|
Points:
|
||||||
@ -70,9 +71,9 @@ yamlOutput = """- Coverage: 10
|
|||||||
Scale: 1
|
Scale: 1
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
yamlMapOutput :: String
|
yamlMapOutput :: String
|
||||||
yamlMapOutput = """key:
|
yamlMapOutput =
|
||||||
|
"""key:
|
||||||
- Coverage: 10
|
- Coverage: 10
|
||||||
Mobility: Fix
|
Mobility: Fix
|
||||||
Name: House
|
Name: House
|
||||||
@ -98,7 +99,8 @@ yamlMapOutput = """key:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
pointYaml :: String
|
pointYaml :: String
|
||||||
pointYaml = """X: 1
|
pointYaml =
|
||||||
|
"""X: 1
|
||||||
Y: 1
|
Y: 1
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@ -109,28 +111,26 @@ yamlToData s = case runExcept $ parseYAMLToJson s of
|
|||||||
Left error -> Left $ printJsonDecodeError error
|
Left error -> Left $ printJsonDecodeError error
|
||||||
Right value -> Right value
|
Right value -> Right value
|
||||||
|
|
||||||
|
|
||||||
testMap :: Map String (Array GeoObject)
|
testMap :: Map String (Array GeoObject)
|
||||||
testMap = Map.singleton "key" parsedData
|
testMap = Map.singleton "key" parsedData
|
||||||
|
|
||||||
|
|
||||||
parsedData :: Array GeoObject
|
parsedData :: Array GeoObject
|
||||||
parsedData =
|
parsedData =
|
||||||
[ GeoObject
|
[ GeoObject
|
||||||
{ coverage: 10.0
|
{ coverage: 10.0
|
||||||
, mobility: Fix
|
, mobility: Fix
|
||||||
, name: "House"
|
, name: "House"
|
||||||
, points: [ Point 10 10, Point 20 10, Point 5 5 ]
|
, points: [ Point 10 10, Point 20 10, Point 5 5 ]
|
||||||
, scale: 9.5
|
, scale: 9.5
|
||||||
}
|
}
|
||||||
, GeoObject
|
, GeoObject
|
||||||
{ coverage: 10.0
|
{ coverage: 10.0
|
||||||
, mobility: Fix
|
, mobility: Fix
|
||||||
, name: "Tree"
|
, name: "Tree"
|
||||||
, points: [ Point 1 1, Point 2 2, Point 0 0 ]
|
, points: [ Point 1 1, Point 2 2, Point 0 0 ]
|
||||||
, scale: 1.0
|
, scale: 1.0
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
readPoint :: String -> Either String Point
|
readPoint :: String -> Either String Point
|
||||||
readPoint = yamlToData
|
readPoint = yamlToData
|
||||||
@ -139,7 +139,7 @@ fullCircle :: String -> Either String String
|
|||||||
fullCircle yamlString = (readPoint yamlString) >>= pure <<< printYAML
|
fullCircle yamlString = (readPoint yamlString) >>= pure <<< printYAML
|
||||||
|
|
||||||
main :: Effect Unit
|
main :: Effect Unit
|
||||||
main = launchAff_ $ runSpec [consoleReporter] do
|
main = launchAff_ $ runSpec [ consoleReporter ] do
|
||||||
describe "purescript-yaml" do
|
describe "purescript-yaml" do
|
||||||
describe "decode" do
|
describe "decode" do
|
||||||
it "Decodes YAML" do
|
it "Decodes YAML" do
|
||||||
|
Loading…
Reference in New Issue
Block a user