Add Serialize and VariableValue value instances

- `Serialize` instance for `Type.Definition.Value`.
- `VariableValue` instance for `Type.Definition.Value`.

It makes it possible to use the library without an additional
serialization format like JSON.
This commit is contained in:
Eugen Wissner 2021-12-22 08:56:01 +01:00
parent 930b8f10b7
commit df078a59d0
Signed by: belka
GPG Key ID: A27FDC1E8EE902C0
2 changed files with 24 additions and 0 deletions

View File

@ -6,6 +6,11 @@ The format is based on
and this project adheres to and this project adheres to
[Haskell Package Versioning Policy](https://pvp.haskell.org/). [Haskell Package Versioning Policy](https://pvp.haskell.org/).
## [Unreleased]
### Added
- `Serialize` instance for `Type.Definition.Value`.
- `VariableValue` instance for `Type.Definition.Value`.
## [1.0.1.0] - 2021-09-27 ## [1.0.1.0] - 2021-09-27
### Added ### Added
- Custom `Show` instance for `Type.Definition.Value` (for error - Custom `Show` instance for `Type.Definition.Value` (for error
@ -459,6 +464,7 @@ and this project adheres to
### Added ### Added
- Data types for the GraphQL language. - Data types for the GraphQL language.
[Unreleased]: https://www.caraus.tech/projects/pub-graphql/repository/23/diff?rev=master&rev_to=v1.0.1.0
[1.0.1.0]: https://www.caraus.tech/projects/pub-graphql/repository/23/diff?rev=v1.0.1.0&rev_to=v1.0.0.0 [1.0.1.0]: https://www.caraus.tech/projects/pub-graphql/repository/23/diff?rev=v1.0.1.0&rev_to=v1.0.0.0
[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 [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.1.0]: https://www.caraus.tech/projects/pub-graphql/repository/23/diff?rev=v0.11.1.0&rev_to=v0.11.0.0

View File

@ -3,6 +3,7 @@
obtain one at https://mozilla.org/MPL/2.0/. -} obtain one at https://mozilla.org/MPL/2.0/. -}
{-# LANGUAGE ExplicitForAll #-} {-# LANGUAGE ExplicitForAll #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ViewPatterns #-} {-# LANGUAGE ViewPatterns #-}
@ -61,6 +62,9 @@ class VariableValue a where
-> a -- ^ Variable value being coerced. -> a -- ^ Variable value being coerced.
-> Maybe Type.Value -- ^ Coerced value on success, 'Nothing' otherwise. -> Maybe Type.Value -- ^ Coerced value on success, 'Nothing' otherwise.
instance VariableValue Type.Value where
coerceVariableValue = const Just
instance VariableValue Aeson.Value where instance VariableValue Aeson.Value where
coerceVariableValue _ Aeson.Null = Just Type.Null coerceVariableValue _ Aeson.Null = Just Type.Null
coerceVariableValue (In.ScalarBaseType scalarType) value coerceVariableValue (In.ScalarBaseType scalarType) value
@ -216,6 +220,20 @@ data Output a
instance forall a. IsString (Output a) where instance forall a. IsString (Output a) where
fromString = String . fromString fromString = String . fromString
instance Serialize Type.Value where
null = Type.Null
serialize _ = \case
Int int -> Just $ Type.Int int
Float float -> Just $ Type.Float float
String string -> Just $ Type.String string
Boolean boolean -> Just $ Type.Boolean boolean
Enum enum -> Just $ Type.Enum enum
List list -> Just $ Type.List list
Object object -> Just
$ Type.Object
$ HashMap.fromList
$ OrderedMap.toList object
instance Serialize Aeson.Value where instance Serialize Aeson.Value where
serialize (Out.ScalarBaseType scalarType) value serialize (Out.ScalarBaseType scalarType) value
| Type.ScalarType "Int" _ <- scalarType | Type.ScalarType "Int" _ <- scalarType