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,6 +20,7 @@ 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
|
||||||
@ -93,8 +95,8 @@ 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))
|
||||||
|
@ -26,16 +26,25 @@ data GeoObject = GeoObject
|
|||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
@ -55,7 +64,6 @@ instance mobilityJson :: DecodeJson Mobility where
|
|||||||
"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
|
||||||
obj <- decodeJson json
|
obj <- decodeJson json
|
||||||
@ -63,7 +71,6 @@ 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
|
||||||
|
@ -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,11 +111,9 @@ 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
|
||||||
@ -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