generated from tpl/purs
Allow YAML to be converted into Json
This commit is contained in:
parent
fab2d38e08
commit
849484bf3f
@ -15,7 +15,8 @@
|
|||||||
"purescript-functions": "^3.0.0",
|
"purescript-functions": "^3.0.0",
|
||||||
"purescript-foreign": "^4.0.0",
|
"purescript-foreign": "^4.0.0",
|
||||||
"purescript-foreign-generic": "^4.1.0",
|
"purescript-foreign-generic": "^4.1.0",
|
||||||
"purescript-unsafe-coerce": "^3.0.0"
|
"purescript-unsafe-coerce": "^3.0.0",
|
||||||
|
"purescript-argonaut-core": "^3.1.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"purescript-console": "^3.0.0",
|
"purescript-console": "^3.0.0",
|
||||||
|
@ -1,12 +1,14 @@
|
|||||||
module Data.YAML.Foreign.Decode (parseYAML, readYAMLGeneric) where
|
module Data.YAML.Foreign.Decode (parseYAML, readYAMLGeneric, parseYAMLToJson) where
|
||||||
|
|
||||||
import Data.Foreign (F, Foreign, ForeignError(..), fail)
|
import Data.Foreign (F, Foreign, ForeignError(..), fail)
|
||||||
import Data.Foreign.Generic.Class (class GenericDecode)
|
|
||||||
import Data.Foreign.Generic (genericDecode)
|
import Data.Foreign.Generic (genericDecode)
|
||||||
|
import Data.Foreign.Generic.Class (class GenericDecode)
|
||||||
import Data.Foreign.Generic.Types (Options)
|
import Data.Foreign.Generic.Types (Options)
|
||||||
import Data.Function.Uncurried (Fn3, runFn3)
|
import Data.Function.Uncurried (Fn3, runFn3)
|
||||||
import Data.Generic.Rep (class Generic)
|
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
|
||||||
|
|
||||||
@ -14,6 +16,10 @@ foreign import parseYAMLImpl :: forall r. Fn3 (String -> r) (Foreign -> r) Strin
|
|||||||
parseYAML :: String -> F Foreign
|
parseYAML :: String -> F Foreign
|
||||||
parseYAML yaml = runFn3 parseYAMLImpl (fail <<< JSONError) pure yaml
|
parseYAML yaml = runFn3 parseYAMLImpl (fail <<< JSONError) 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.
|
-- | 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 :: forall a rep. (Generic a rep) => (GenericDecode rep) => Options -> String -> F a
|
||||||
readYAMLGeneric opts = parseYAML >=> genericDecode opts
|
readYAMLGeneric opts = parseYAML >=> genericDecode opts
|
||||||
|
Loading…
Reference in New Issue
Block a user