2017-05-20 21:57:28 +00:00
|
|
|
# purescript-yaml-next
|
2015-12-20 14:35:04 +00:00
|
|
|
|
2017-05-20 21:32:55 +00:00
|
|
|
## Install
|
2015-12-20 14:35:04 +00:00
|
|
|
|
2017-05-20 21:32:55 +00:00
|
|
|
```
|
2021-03-20 11:13:39 +00:00
|
|
|
spago install yaml-next
|
2015-12-20 14:35:04 +00:00
|
|
|
```
|
2017-05-22 15:25:09 +00:00
|
|
|
|
2017-05-20 21:32:55 +00:00
|
|
|
|
|
|
|
## YAML to Data Type Usage
|
|
|
|
|
2017-05-20 21:41:06 +00:00
|
|
|
Assuming we have the following `Point` data type and YAML string...
|
2015-12-20 14:35:04 +00:00
|
|
|
|
|
|
|
```purescript
|
2017-05-20 21:32:55 +00:00
|
|
|
data Point = Point Int Int
|
|
|
|
|
|
|
|
yamlPoint :: String
|
|
|
|
yamlPoint = """
|
|
|
|
X: 1
|
|
|
|
Y: 1
|
2015-12-20 14:35:04 +00:00
|
|
|
"""
|
2017-05-20 21:32:55 +00:00
|
|
|
```
|
|
|
|
|
2017-05-20 21:41:06 +00:00
|
|
|
We can read a `Point` from the YAML by converting the YAML into JSON
|
2021-03-20 11:13:39 +00:00
|
|
|
and then using [purescript-argonaut]'s encoding functionality to get the
|
|
|
|
type we need (specifically, [purescript-argonaut-codecs]
|
2017-05-20 21:32:55 +00:00
|
|
|
functionality).
|
|
|
|
|
2021-03-20 11:13:39 +00:00
|
|
|
[purescript-argonaut]: https://github.com/purescript-contrib/purescript-argonaut
|
|
|
|
[purescript-argonaut-codecs]:
|
|
|
|
https://github.com/purescript-contrib/purescript-argonaut-codecs
|
|
|
|
|
2017-05-20 21:41:06 +00:00
|
|
|
```purescript
|
2017-05-20 21:32:55 +00:00
|
|
|
getPoint :: Either String Point
|
|
|
|
getPoint = case runExcept $ parseYAMLToJson yamlPoint of
|
|
|
|
Left err -> Left "Could not parse yaml"
|
|
|
|
Right json -> decodeJson json
|
|
|
|
|
|
|
|
instance pointJson :: DecodeJson Point where
|
|
|
|
decodeJson s = do
|
|
|
|
obj <- maybe (Left "Point is not an object.") Right (toObject s)
|
|
|
|
x <- getField obj "X"
|
|
|
|
y <- getField obj "Y"
|
|
|
|
pure $ Point x y
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## Data Type to YAML Usage
|
2015-12-20 14:35:04 +00:00
|
|
|
|
2017-05-20 21:32:55 +00:00
|
|
|
YAML is represented with the following data type.
|
|
|
|
|
2017-05-20 21:41:06 +00:00
|
|
|
```purescript
|
2017-05-20 21:32:55 +00:00
|
|
|
data YValue
|
|
|
|
= YObject (M.Map String YValue)
|
|
|
|
| YArray (Array YValue)
|
|
|
|
| YString String
|
|
|
|
| YNumber Number
|
|
|
|
| YInt Int
|
|
|
|
| YBoolean Boolean
|
|
|
|
| YNull
|
2015-12-20 14:35:04 +00:00
|
|
|
```
|
|
|
|
|
2021-03-20 11:13:39 +00:00
|
|
|
To convert data into a `YValue`, create instances of the `ToYAML` class for your
|
2017-05-20 21:32:55 +00:00
|
|
|
data types.
|
2015-12-20 14:35:04 +00:00
|
|
|
|
|
|
|
```purescript
|
2017-05-20 21:32:55 +00:00
|
|
|
class ToYAML a where
|
|
|
|
toYAML :: a -> YValue
|
|
|
|
```
|
|
|
|
|
|
|
|
For example to take a `Point` to `YValue`
|
|
|
|
|
|
|
|
```purescript
|
|
|
|
import Data.YAML.Foreign.Encode (object, entry, class ToYAML)
|
|
|
|
|
2015-12-20 14:35:04 +00:00
|
|
|
instance pointToYAML :: ToYAML Point where
|
|
|
|
toYAML (Point x y) =
|
|
|
|
object
|
2017-05-20 21:32:55 +00:00
|
|
|
[ "X" `entry` x
|
|
|
|
, "Y" `entry` y
|
2015-12-20 14:35:04 +00:00
|
|
|
]
|
2017-05-20 21:32:55 +00:00
|
|
|
```
|
2015-12-20 14:35:04 +00:00
|
|
|
|
2017-05-20 21:32:55 +00:00
|
|
|
You can find helper functions for converting basic types into `YValue`
|
2021-03-20 11:13:39 +00:00
|
|
|
in the `Data.YAML.Foreign.Encode` module.
|
2017-05-20 21:32:55 +00:00
|
|
|
|
2017-05-20 21:41:06 +00:00
|
|
|
Finally, if you want to convert `YValue` into a String, you can use the
|
2021-03-20 11:13:39 +00:00
|
|
|
`printYAML` function from `Data.YAML.Foreign.Encode`.
|
2015-12-20 14:35:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
```purescript
|
2017-05-20 21:32:55 +00:00
|
|
|
printYAML :: forall a. (ToYAML a) => a -> String
|
|
|
|
```
|
|
|
|
|
2021-03-20 11:13:39 +00:00
|
|
|
|
2017-05-20 21:32:55 +00:00
|
|
|
## Summary
|
|
|
|
|
2021-03-20 11:13:39 +00:00
|
|
|
Using the previous code and the type classes we defined earlier,
|
|
|
|
we can go full circle from a YAML string to a PureScript Data Type
|
|
|
|
and back to a YAML string.
|
2017-05-20 21:32:55 +00:00
|
|
|
|
2017-05-20 21:41:06 +00:00
|
|
|
```purescript
|
2017-05-20 21:32:55 +00:00
|
|
|
fullCircle :: String -> Either String String
|
|
|
|
fullCircle yamlString = (readPoint yamlString) >>= pure <<< printYAML
|
|
|
|
```
|
|
|
|
|
|
|
|
|
2021-03-20 11:13:39 +00:00
|
|
|
## Contributing
|
2017-05-20 21:32:55 +00:00
|
|
|
|
2021-03-20 11:13:39 +00:00
|
|
|
1. Check out the repo
|
|
|
|
1. Run `npm install`
|
|
|
|
1. Make changes
|
|
|
|
1. Test them by running `npm test`
|