Provide a custom Show instance for output Value

This commit is contained in:
Eugen Wissner 2021-08-31 17:29:20 +02:00
parent dd6fdf69f6
commit cc8f14f122
Signed by: belka
GPG Key ID: A27FDC1E8EE902C0
2 changed files with 30 additions and 2 deletions

View File

@ -6,6 +6,11 @@ The format is based on
and this project adheres to
[Haskell Package Versioning Policy](https://pvp.haskell.org/).
## [Unreleased]
### Fixed
- Provide a custom `Show` instance for `Type.Definition.Value` (for error
messages).
## [1.0.0.0]
### Added
- `Language.GraphQL.Execute.OrderedMap` is a map data structure, that preserves
@ -443,6 +448,7 @@ and this project adheres to
### Added
- Data types for the GraphQL language.
[Unreleased]: https://www.caraus.tech/projects/pub-graphql/repository/23/diff?rev=v1.0.0.0&rev_to=master
[1.0.0.0]: https://www.caraus.tech/projects/pub-graphql/repository/23/diff?rev=v1.0.0.0&rev_to=v0.11.1.0
[0.11.1.0]: https://www.caraus.tech/projects/pub-graphql/repository/23/diff?rev=v0.11.1.0&rev_to=v0.11.0.0
[0.11.0.0]: https://www.caraus.tech/projects/pub-graphql/repository/23/diff?rev=v0.11.0.0&rev_to=v0.10.0.0

View File

@ -20,10 +20,12 @@ module Language.GraphQL.Type.Definition
import Data.Int (Int32)
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HashMap
import Data.List (intercalate)
import Data.String (IsString(..))
import Data.Text (Text)
import qualified Data.Text as Text
import Language.GraphQL.AST (Name)
import Language.GraphQL.AST (Name, escape)
import Numeric (showFloat)
import Prelude hiding (id)
-- | Represents accordingly typed GraphQL values.
@ -36,7 +38,27 @@ data Value
| Enum Name
| List [Value] -- ^ Arbitrary nested list.
| Object (HashMap Name Value)
deriving (Eq, Show)
deriving Eq
instance Show Value where
showList = mappend . showList'
where
showList' list = "[" ++ intercalate ", " (show <$> list) ++ "]"
show (Int integer) = show integer
show (Float float') = showFloat float' mempty
show (String text) = "\"" <> Text.foldr (mappend . escape) "\"" text
show (Boolean boolean') = show boolean'
show Null = "null"
show (Enum name) = Text.unpack name
show (List list) = show list
show (Object fields) = unwords
[ "{"
, intercalate ", " (HashMap.foldrWithKey showObject [] fields)
, "}"
]
where
showObject key value accumulator =
concat [Text.unpack key, ": ", show value] : accumulator
instance IsString Value where
fromString = String . fromString