generated from tpl/purs
Upgrade to PureScript 0.14, remove incompatible readYAMLGeneric function
This commit is contained in:
parent
6fe0550250
commit
b3a681d84b
49
package-lock.json
generated
49
package-lock.json
generated
@ -1,7 +1,54 @@
|
||||
{
|
||||
"name": "purescript-yaml-next",
|
||||
"lockfileVersion": 2,
|
||||
"requires": true,
|
||||
"lockfileVersion": 1,
|
||||
"packages": {
|
||||
"": {
|
||||
"hasInstallScript": true,
|
||||
"license": "SEE LICENSE FILE",
|
||||
"dependencies": {
|
||||
"js-yaml": "^3.13.1"
|
||||
},
|
||||
"devDependencies": {}
|
||||
},
|
||||
"node_modules/argparse": {
|
||||
"version": "1.0.10",
|
||||
"resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz",
|
||||
"integrity": "sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg==",
|
||||
"dependencies": {
|
||||
"sprintf-js": "~1.0.2"
|
||||
}
|
||||
},
|
||||
"node_modules/esprima": {
|
||||
"version": "4.0.1",
|
||||
"resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.1.tgz",
|
||||
"integrity": "sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==",
|
||||
"bin": {
|
||||
"esparse": "bin/esparse.js",
|
||||
"esvalidate": "bin/esvalidate.js"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=4"
|
||||
}
|
||||
},
|
||||
"node_modules/js-yaml": {
|
||||
"version": "3.13.1",
|
||||
"resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.13.1.tgz",
|
||||
"integrity": "sha512-YfbcO7jXDdyj0DGxYVSlSeQNHbD7XPWvrVWeVUujrQEoZzWJIRrCPoyk6kL6IAjAG2IolMK4T0hNUe0HOUs5Jw==",
|
||||
"dependencies": {
|
||||
"argparse": "^1.0.7",
|
||||
"esprima": "^4.0.0"
|
||||
},
|
||||
"bin": {
|
||||
"js-yaml": "bin/js-yaml.js"
|
||||
}
|
||||
},
|
||||
"node_modules/sprintf-js": {
|
||||
"version": "1.0.3",
|
||||
"resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz",
|
||||
"integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw="
|
||||
}
|
||||
},
|
||||
"dependencies": {
|
||||
"argparse": {
|
||||
"version": "1.0.10",
|
||||
|
@ -1,5 +1,5 @@
|
||||
let upstream =
|
||||
https://github.com/purescript/package-sets/releases/download/psc-0.13.3-20191005/packages.dhall sha256:ba287d858ada09c4164792ad4e643013b742c208cbedf5de2e35ee27b64b6817
|
||||
https://github.com/purescript/package-sets/releases/download/psc-0.14.0-20210317/packages.dhall sha256:e2e744972f9b60188dcf07f41418661b505c9ee2e9f91e57e67daefad3a5ae09
|
||||
|
||||
let overrides = {=}
|
||||
|
||||
|
@ -6,7 +6,6 @@
|
||||
, "console"
|
||||
, "effect"
|
||||
, "foreign"
|
||||
, "foreign-generic"
|
||||
, "functions"
|
||||
, "ordered-collections"
|
||||
, "psci-support"
|
||||
|
@ -1,27 +1,23 @@
|
||||
module Data.YAML.Foreign.Decode (
|
||||
readYAMLGeneric,
|
||||
parseYAMLToJson
|
||||
) where
|
||||
module Data.YAML.Foreign.Decode (parseYAMLToJson)
|
||||
where
|
||||
|
||||
import Foreign (F, Foreign, ForeignError(..), fail)
|
||||
import Foreign.Generic (genericDecode)
|
||||
import Foreign.Generic.Class (class GenericDecode, Options)
|
||||
import Data.Function.Uncurried (Fn3, runFn3)
|
||||
import Data.Generic.Rep (class Generic)
|
||||
import Prelude ((>=>), (<<<), pure, (>>=))
|
||||
import Prelude (pure, (<<<), (>>=))
|
||||
import Unsafe.Coerce (unsafeCoerce)
|
||||
import Data.Argonaut.Core (Json)
|
||||
|
||||
foreign import parseYAMLImpl :: forall r. Fn3 (String -> r) (Foreign -> r) String r
|
||||
foreign import parseYAMLImpl :: forall r.
|
||||
Fn3 (String -> r) (Foreign -> r) String r
|
||||
|
||||
|
||||
-- | Attempt to parse a YAML string, returning the result as foreign data.
|
||||
parseYAML :: String -> F Foreign
|
||||
parseYAML yaml = runFn3 parseYAMLImpl (fail <<< ForeignError) pure yaml
|
||||
parseYAML yaml =
|
||||
runFn3 parseYAMLImpl (fail <<< ForeignError) pure yaml
|
||||
|
||||
|
||||
-- | Attempt to parse a YAML string, returning the result as Json
|
||||
parseYAMLToJson :: String -> F Json
|
||||
parseYAMLToJson yaml = parseYAML yaml >>= pure <<< unsafeCoerce
|
||||
|
||||
-- | Automatically generate a YAML parser for your data from a generic instance.
|
||||
readYAMLGeneric :: forall a rep. (Generic a rep) => (GenericDecode rep) => Options -> String -> F a
|
||||
readYAMLGeneric opts = parseYAML >=> genericDecode opts
|
||||
parseYAMLToJson yaml =
|
||||
parseYAML yaml >>= pure <<< unsafeCoerce
|
||||
|
@ -1,14 +1,14 @@
|
||||
module Test.Instances where
|
||||
|
||||
import Data.YAML.Foreign.Encode
|
||||
import Data.Argonaut.Core (toObject, toString)
|
||||
import Data.Argonaut.Decode (decodeJson)
|
||||
import Data.Argonaut.Decode.Combinators (getField)
|
||||
import Data.Argonaut.Decode.Class (class DecodeJson)
|
||||
import Data.Argonaut.Decode.Error (JsonDecodeError(TypeMismatch))
|
||||
import Data.Either (Either(..))
|
||||
import Data.Generic.Rep (class Generic)
|
||||
import Data.Generic.Rep.Eq (genericEq)
|
||||
import Data.Generic.Rep.Show (genericShow)
|
||||
import Data.Maybe (maybe)
|
||||
import Data.Eq.Generic (genericEq)
|
||||
import Data.Show.Generic (genericShow)
|
||||
import Prelude (class Eq, class Show, bind, pure, ($))
|
||||
|
||||
data Point = Point Int Int
|
||||
@ -38,8 +38,8 @@ instance showMobility :: Show Mobility where show = genericShow
|
||||
instance eqMobility :: Eq Mobility where eq = genericEq
|
||||
|
||||
instance geoJson :: DecodeJson GeoObject where
|
||||
decodeJson s = do
|
||||
obj <- maybe (Left "GeoObject is not an object.") Right (toObject s)
|
||||
decodeJson json = do
|
||||
obj <- decodeJson json
|
||||
name <- getField obj "Name"
|
||||
scale <- getField obj "Scale"
|
||||
points <- getField obj "Points"
|
||||
@ -48,16 +48,17 @@ instance geoJson :: DecodeJson GeoObject where
|
||||
pure $ GeoObject { name, scale, points, mobility, coverage }
|
||||
|
||||
instance mobilityJson :: DecodeJson Mobility where
|
||||
decodeJson s = do
|
||||
mob <- maybe (Left "Mobility is not a string.") Right (toString s)
|
||||
decodeJson json = do
|
||||
mob <- decodeJson json
|
||||
case mob of
|
||||
"Fix" -> pure Fix
|
||||
"Flex" -> pure Flex
|
||||
_ -> Left "Mobility must be either Flex or Fix"
|
||||
_ -> Left $ TypeMismatch "Mobility must be either Flex or Fix"
|
||||
|
||||
|
||||
instance pointJson :: DecodeJson Point where
|
||||
decodeJson s = do
|
||||
obj <- maybe (Left "Point is not an object.") Right (toObject s)
|
||||
decodeJson json = do
|
||||
obj <- decodeJson json
|
||||
x <- getField obj "X"
|
||||
y <- getField obj "Y"
|
||||
pure $ Point x y
|
||||
|
@ -2,6 +2,7 @@ module Test.Main where
|
||||
|
||||
import Control.Monad.Except (runExcept)
|
||||
import Data.Argonaut.Decode (class DecodeJson, decodeJson)
|
||||
import Data.Argonaut.Decode.Error (printJsonDecodeError)
|
||||
import Data.Either (Either(..))
|
||||
import Data.Map (Map)
|
||||
import Data.Map as Map
|
||||
@ -103,8 +104,10 @@ Y: 1
|
||||
|
||||
yamlToData :: forall a. (DecodeJson a) => String -> Either String a
|
||||
yamlToData s = case runExcept $ parseYAMLToJson s of
|
||||
Left err -> Left "Could not parse yaml"
|
||||
Right json -> decodeJson json
|
||||
Left err -> Left "Could not parse YAML"
|
||||
Right json -> case decodeJson json of
|
||||
Left error -> Left $ printJsonDecodeError error
|
||||
Right value -> Right value
|
||||
|
||||
|
||||
testMap :: Map String (Array GeoObject)
|
||||
|
Loading…
Reference in New Issue
Block a user