Migrate to the HsYAML package (#1248)

This commit is contained in:
Vijay Tadikamalla 2019-10-06 03:22:19 +05:30 committed by Gabriel Gonzalez
parent d172e4b677
commit cced515494
10 changed files with 94 additions and 47 deletions

View File

@ -35,10 +35,6 @@ Source-Repository head
Type: git
Location: https://github.com/dhall-lang/dhall-haskell/tree/master/dhall-json
Flag yaml-pre-0_11
Default: False
Manual: False
Library
Hs-Source-Dirs: src
Build-Depends:
@ -76,13 +72,9 @@ Library
Other-Modules: Dhall.Yaml.Eta
Java-Sources: java/Utils.java
else
if flag(yaml-pre-0_11)
Build-Depends:
yaml >= 0.5.0 && < 0.11
else
Build-Depends:
libyaml >= 0.1.1.0 && < 0.2 ,
yaml >= 0.11.0 && < 0.12
HsYAML >= 0.2 && <0.3,
HsYAML-aeson >= 0.2 && <0.3
Executable dhall-to-json
Hs-Source-Dirs: dhall-to-json

View File

@ -14,6 +14,7 @@ import Data.Monoid ((<>))
import Data.Text (Text)
import Dhall.JSON (Conversion(..), SpecialDoubleMode(..), codeToValue)
import Options.Applicative (Parser)
import Data.ByteString.Lazy (toStrict)
import qualified Data.Aeson
import qualified Data.ByteString
@ -23,11 +24,12 @@ import qualified Options.Applicative
#if defined(ETA_VERSION)
import Dhall.Yaml.Eta ( jsonToYaml )
#else
import qualified Data.Yaml
# if MIN_VERSION_yaml(0,10,2)
import qualified Data.Text
import qualified Text.Libyaml
# endif
import qualified Data.YAML.Aeson
import qualified Data.YAML as Y
import qualified Data.YAML.Event as YE
import qualified Data.YAML.Token as YT
import qualified Data.YAML.Schema as YS
import qualified Data.Text as Text
#endif
@ -92,32 +94,38 @@ jsonToYaml
jsonToYaml json documents quoted =
case (documents, json) of
(True, Data.Yaml.Array elems)
(True, Data.Aeson.Array elems)
-> Data.ByteString.intercalate "\n---\n"
$ fmap (encodeYaml encodeOptions)
$ fmap (Data.ByteString.Lazy.toStrict. (Data.YAML.Aeson.encodeValue' schemaEncoder YT.UTF8). (:[]))
$ Data.Vector.toList elems
_ -> encodeYaml encodeOptions json
_ -> Data.ByteString.Lazy.toStrict (Data.YAML.Aeson.encodeValue' schemaEncoder YT.UTF8 [json])
where
# if !MIN_VERSION_yaml(0,10,2)
encodeYaml = Data.Yaml.encode
# else
encodeYaml = Data.Yaml.encodeWith
defaultSchemaEncoder = YS.setScalarStyle style Y.coreSchemaEncoder
customStyle = \s -> case () of
defaultEncodeStr s = case () of
()
| "\n" `Text.isInfixOf` s -> Right (YE.untagged, YE.Literal YE.Clip YE.IndentAuto, s)
| YS.isAmbiguous Y.coreSchemaResolver s -> Right (YE.untagged, YE.SingleQuoted, s)
| otherwise -> Right (YE.untagged, YE.Plain, s)
style s = case s of
Y.SNull -> Right (YE.untagged, YE.Plain, "null")
Y.SBool bool -> Right (YE.untagged, YE.Plain, YS.encodeBool bool)
Y.SFloat double -> Right (YE.untagged, YE.Plain, YS.encodeDouble double)
Y.SInt int -> Right (YE.untagged, YE.Plain, YS.encodeInt int)
Y.SStr text -> defaultEncodeStr text
Y.SUnknown t v -> Right (t, YE.SingleQuoted, v)
customStyle (Y.SStr s) = case () of
()
| "\n" `Data.Text.isInfixOf` s -> ( noTag, literal )
| otherwise -> ( noTag, Text.Libyaml.SingleQuoted )
where
noTag = Text.Libyaml.NoTag
literal = Text.Libyaml.Literal
quotedOptions = Data.Yaml.setStringStyle
customStyle
Data.Yaml.defaultEncodeOptions
encodeOptions = if quoted
then quotedOptions
else Data.Yaml.defaultEncodeOptions
# endif
| "\n" `Text.isInfixOf` s -> Right (YE.untagged, YE.Literal YE.Clip YE.IndentAuto, s)
| otherwise -> Right (YE.untagged, YE.SingleQuoted, s)
customStyle scalar = (YS.schemaEncoderScalar defaultSchemaEncoder) scalar
customSchemaEncoder = YS.setScalarStyle customStyle defaultSchemaEncoder
schemaEncoder = if quoted
then customSchemaEncoder
else defaultSchemaEncoder
#endif

View File

@ -30,9 +30,8 @@ import Dhall.TypeCheck(X)
import Dhall.Yaml.Eta ( yamlToJson, showYaml )
#else
import Data.Aeson (Value)
import Data.Bifunctor (bimap)
import qualified Data.ByteString.Char8 as BS8
import qualified Data.Yaml
import qualified Data.YAML.Aeson
#endif
-- | Options to parametrize conversion
@ -69,10 +68,11 @@ dhallFromYaml Options{..} yaml = do
#if !defined(ETA_VERSION)
yamlToJson :: ByteString -> Either String Data.Aeson.Value
yamlToJson =
bimap Data.Yaml.prettyPrintParseException id . Data.Yaml.decodeEither'
yamlToJson s = case Data.YAML.Aeson.decode1Strict s of
Right v -> Right v
Left (pos, err) -> Left (show pos ++ err)
showYaml :: Value -> String
showYaml value = BS8.unpack (Data.Yaml.encode value)
showYaml value = BS8.unpack (Data.YAML.Aeson.encode1Strict value)
#endif

View File

@ -1,5 +1,5 @@
bool_value: true
int_value: 1
string_value: 2000-01-01
text: |
Plain text
string_value: 2000-01-01
int_value: 1

View File

@ -1,5 +1,5 @@
'bool_value': true
'int_value': 1
'string_value': '2000-01-01'
'text': |
Plain text
'string_value': '2000-01-01'
'int_value': 1

14
nix/HsYAML-aeson.nix Normal file
View File

@ -0,0 +1,14 @@
{ mkDerivation, aeson, base, bytestring, containers, HsYAML, mtl
, scientific, stdenv, text, unordered-containers, vector
}:
mkDerivation {
pname = "HsYAML-aeson";
version = "0.2.0.0";
sha256 = "cfb9634b43fcaddb5a520838119ba4b02b18423a35471fef5a805d6004e75d8b";
libraryHaskellDepends = [
aeson base bytestring containers HsYAML mtl scientific text
unordered-containers vector
];
description = "JSON to YAML Adapter";
license = stdenv.lib.licenses.gpl2Plus;
}

20
nix/HsYAML.nix Normal file
View File

@ -0,0 +1,20 @@
{ mkDerivation, base, bytestring, containers, deepseq, mtl, parsec
, QuickCheck, stdenv, tasty, tasty-quickcheck, text
}:
mkDerivation {
pname = "HsYAML";
version = "0.2.0.0";
sha256 = "1862d7860d6a8255c92c7aa081d4946b0e838907b2d12730b96aba8d95cd1ff5";
isLibrary = true;
isExecutable = true;
libraryHaskellDepends = [
base bytestring containers deepseq mtl parsec text
];
testHaskellDepends = [
base bytestring containers mtl QuickCheck tasty tasty-quickcheck
text
];
homepage = "https://github.com/hvr/HsYAML";
description = "Pure Haskell YAML 1.2 processor";
license = stdenv.lib.licenses.gpl2;
}

View File

@ -0,0 +1,9 @@
{ mkDerivation, base, containers, stdenv }:
mkDerivation {
pname = "ordered-containers";
version = "0.2.2";
sha256 = "c75ac7330e70cd5d6ac0062b68033779cf15cd986d4ca20f838e016d466d22c9";
libraryHaskellDepends = [ base containers ];
description = "Set- and Map-like types that remember the order elements were inserted";
license = stdenv.lib.licenses.bsd3;
}

View File

@ -28,7 +28,7 @@ in
# of the compiler
shared_7_10_3.dhall
shared_7_10_3.dhall-bash
shared_7_10_3.dhall-json
# shared_7_10_3.dhall-json
# `base-noprelude` depends on a specific version of `base`
# shared_7_10_3.dhall-lsp-server
# `hnix` depends on `lens-family-th`, which doesn't support GHC 7.10.3

View File

@ -17,6 +17,10 @@ extra-deps:
- monadlist-0.0.2@sha256:978305e3f03dd5f65c673b551124dac0c39a247c2c14d84739ca9a3405fbb63a,904
- ref-tf-0.4.0.1@sha256:369e5c6237e4b6fe330fde021b94a9f98971690cbec5b910faa9af3cbcb15610,1133
- generic-random-1.3.0.0
- HsYAML-0.2.0.0@sha256:4e554ee481650156a26a71b40f233979cd943f22ee887b70dae3b8b24de2932f,5273
- HsYAML-aeson-0.2.0.0@sha256:04796abfc01cffded83f37a10e6edba4f0c0a15d45bef44fc5bb4313d9c87757,1791
- ordered-containers-0.2.2@sha256:ebf2be3f592d9cf148ea6b8375f8af97148d44f82d8d04476899285e965afdbf,810
nix:
packages:
- ncurses