Support older yaml
versions (#981)
Fixes https://github.com/dhall-lang/dhall-haskell/issues/969
This commit is contained in:
parent
e8ac5e3bd0
commit
98c7d4dfca
|
@ -53,6 +53,8 @@ Library
|
|||
Exposed-Modules:
|
||||
Dhall.JSON
|
||||
Dhall.JSONToDhall
|
||||
Other-Modules:
|
||||
Dhall.JSON.Compat
|
||||
GHC-Options: -Wall
|
||||
|
||||
if flag(yaml-pre-0_11)
|
||||
|
|
|
@ -196,11 +196,11 @@ import qualified Data.Vector
|
|||
import qualified Data.Yaml
|
||||
import qualified Dhall.Core
|
||||
import qualified Dhall.Import
|
||||
import qualified Dhall.JSON.Compat
|
||||
import qualified Dhall.Map
|
||||
import qualified Dhall.Parser
|
||||
import qualified Dhall.TypeCheck
|
||||
import qualified Options.Applicative
|
||||
import qualified Text.Libyaml
|
||||
|
||||
{-| This is the exception type for errors that might arise when translating
|
||||
Dhall to JSON
|
||||
|
@ -907,24 +907,8 @@ jsonToYaml
|
|||
jsonToYaml json documents quoted = case (documents, json) of
|
||||
(True, Data.Yaml.Array elems)
|
||||
-> Data.ByteString.intercalate "\n---\n"
|
||||
$ fmap (encodeYaml encodeOptions)
|
||||
$ fmap encodeYaml
|
||||
$ Data.Vector.toList elems
|
||||
_ -> encodeYaml encodeOptions json
|
||||
_ -> encodeYaml json
|
||||
where
|
||||
encodeYaml = Data.Yaml.encodeWith
|
||||
|
||||
customStyle = \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
|
||||
encodeYaml = Dhall.JSON.Compat.encodeYaml quoted
|
||||
|
|
37
dhall-json/src/Dhall/JSON/Compat.hs
Normal file
37
dhall-json/src/Dhall/JSON/Compat.hs
Normal file
|
@ -0,0 +1,37 @@
|
|||
{-# LANGUAGE CPP #-}
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
|
||||
module Dhall.JSON.Compat where
|
||||
|
||||
import Data.ByteString (ByteString)
|
||||
import Data.Yaml (Value)
|
||||
|
||||
import qualified Data.Yaml
|
||||
#if MIN_VERSION_yaml(0,10,2)
|
||||
import qualified Data.Text
|
||||
import qualified Text.Libyaml
|
||||
#endif
|
||||
|
||||
encodeYaml :: Bool -> Value -> ByteString
|
||||
encodeYaml _quoted =
|
||||
#if MIN_VERSION_yaml(0,10,2)
|
||||
Data.Yaml.encodeWith encodeOptions
|
||||
where
|
||||
customStyle = \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
|
||||
#else
|
||||
Data.Yaml.encode
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user