From b3a681d84b3fce03e0a7968f612da4a44cf393c6 Mon Sep 17 00:00:00 2001 From: Adrian Sieber Date: Thu, 18 Mar 2021 19:27:06 +0000 Subject: [PATCH] Upgrade to PureScript 0.14, remove incompatible readYAMLGeneric function --- package-lock.json | 49 ++++++++++++++++++++++++++++++- packages.dhall | 2 +- spago.dhall | 1 - src/Data/YAML/Foreign/Decode.purs | 26 +++++++--------- test/Instances.purs | 23 ++++++++------- test/Main.purs | 7 +++-- 6 files changed, 77 insertions(+), 31 deletions(-) diff --git a/package-lock.json b/package-lock.json index 4a1ed1e..b3e2cde 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/packages.dhall b/packages.dhall index f5795de..b1c6097 100644 --- a/packages.dhall +++ b/packages.dhall @@ -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 = {=} diff --git a/spago.dhall b/spago.dhall index 0dc8780..475de22 100644 --- a/spago.dhall +++ b/spago.dhall @@ -6,7 +6,6 @@ , "console" , "effect" , "foreign" - , "foreign-generic" , "functions" , "ordered-collections" , "psci-support" diff --git a/src/Data/YAML/Foreign/Decode.purs b/src/Data/YAML/Foreign/Decode.purs index 8483716..fa12d7f 100644 --- a/src/Data/YAML/Foreign/Decode.purs +++ b/src/Data/YAML/Foreign/Decode.purs @@ -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 diff --git a/test/Instances.purs b/test/Instances.purs index b4d0ba4..79c1ea2 100644 --- a/test/Instances.purs +++ b/test/Instances.purs @@ -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 diff --git a/test/Main.purs b/test/Main.purs index e3d0d9a..ac14fc7 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -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)