Maintainer | Brandon Chinn <brandon@leapyear.io> |
---|---|
Stability | experimental |
Portability | portable |
Safe Haskell | None |
Language | Haskell2010 |
Internal definitions for declaring JSON schemas.
Synopsis
- newtype Object (schema :: Schema) = UnsafeObject (KeyMap Dynamic)
- toMap :: IsSchema ('Schema schema) => Object ('Schema schema) -> Object
- type IsSchema (schema :: Schema) = (HasSchemaResult (ToSchemaObject schema), All HasSchemaResultPair (FromSchema schema), IsSchemaObjectMap (FromSchema schema), SchemaResult (ToSchemaObject schema) ~ Object schema)
- showSchema :: forall (schema :: Schema). IsSchema schema => String
- showSchemaType :: forall (schemaType :: SchemaType). HasSchemaResult schemaType => String
- type family SchemaResult (schema :: SchemaType) where ...
- type family SchemaResultList (xs :: [SchemaType]) where ...
- class IsSchemaType schema => HasSchemaResult (schema :: SchemaType) where
- parseValue :: [Key] -> Value -> Parser (SchemaResult schema)
- toValue :: SchemaResult schema -> Value
- showValue :: SchemaResult schema -> ShowS
- class ParseSumType xs where
- parseSumType :: [Key] -> Value -> Parser (SumType (SchemaResultList xs))
- toValueMap :: forall pairs. All HasSchemaResultPair pairs => Object ('Schema pairs) -> Object
- class HasSchemaResultPair (a :: (SchemaKey, SchemaType)) where
- parseFail :: forall (schema :: SchemaType) m a. (MonadFail m, HasSchemaResult schema) => [Key] -> Value -> m a
- data UnSchemaKey :: SchemaKey -> Exp Symbol
- type Lookup a = Map Snd <=< Find (TyEq a <=< Fst)
- type family LookupSchema (key :: Symbol) (schema :: Schema) :: SchemaType where ...
- getKey :: forall (key :: Symbol) (schema :: Schema) (endSchema :: SchemaType) result. (endSchema ~ LookupSchema key schema, result ~ SchemaResult endSchema, KnownSymbol key, Typeable result, Typeable endSchema) => Proxy key -> Object schema -> result
- unsafeGetKey :: forall (endSchema :: SchemaType) (key :: Symbol) (schema :: Schema). (KnownSymbol key, Typeable (SchemaResult endSchema)) => Proxy key -> Object schema -> SchemaResult endSchema
Documentation
newtype Object (schema :: Schema) Source #
The object containing JSON data and its schema.
Has a FromJSON
instance, so you can use the usual Data.Aeson
decoding functions.
obj = decode "{\"a\": 1}" :: Maybe (Object [schema| { a: Int } |])
type IsSchema (schema :: Schema) = (HasSchemaResult (ToSchemaObject schema), All HasSchemaResultPair (FromSchema schema), IsSchemaObjectMap (FromSchema schema), SchemaResult (ToSchemaObject schema) ~ Object schema) Source #
The constraint for most operations involving Object schema
. If you're writing functions
on general Objects, you should use this constraint. e.g.
logObject :: (MonadLogger m, IsSchema schema) => Object schema -> m () logObject = logInfoN . Text.pack . show
Since: 1.3.0
showSchema :: forall (schema :: Schema). IsSchema schema => String Source #
Show the given schema.
Usage:
type MySchema = [schema| { a: Int } |] showSchema @MySchema
showSchemaType :: forall (schemaType :: SchemaType). HasSchemaResult schemaType => String Source #
type family SchemaResult (schema :: SchemaType) where ... Source #
A type family mapping SchemaType to the corresponding Haskell type.
SchemaResult ('SchemaScalar inner) = inner | |
SchemaResult ('SchemaMaybe inner) = Maybe (SchemaResult inner) | |
SchemaResult ('SchemaTry inner) = Maybe (SchemaResult inner) | |
SchemaResult ('SchemaList inner) = [SchemaResult inner] | |
SchemaResult ('SchemaUnion schemas) = SumType (SchemaResultList schemas) | |
SchemaResult ('SchemaObject inner) = Object ('Schema inner) | |
SchemaResult ('SchemaInclude ('Right schema)) = SchemaResult (ToSchemaObject schema) |
type family SchemaResultList (xs :: [SchemaType]) where ... Source #
SchemaResultList '[] = '[] | |
SchemaResultList (x ': xs) = SchemaResult x ': SchemaResultList xs |
class IsSchemaType schema => HasSchemaResult (schema :: SchemaType) where Source #
A type-class for types that can be parsed from JSON for an associated schema type.
Nothing
parseValue :: [Key] -> Value -> Parser (SchemaResult schema) Source #
default parseValue :: FromJSON (SchemaResult schema) => [Key] -> Value -> Parser (SchemaResult schema) Source #
toValue :: SchemaResult schema -> Value Source #
default toValue :: ToJSON (SchemaResult schema) => SchemaResult schema -> Value Source #
showValue :: SchemaResult schema -> ShowS Source #
default showValue :: Show (SchemaResult schema) => SchemaResult schema -> ShowS Source #
Instances
class ParseSumType xs where Source #
parseSumType :: [Key] -> Value -> Parser (SumType (SchemaResultList xs)) Source #
Instances
ParseSumType ('[] :: [SchemaType]) Source # | |
Defined in Data.Aeson.Schema.Internal parseSumType :: [Key] -> Value -> Parser (SumType (SchemaResultList '[])) Source # | |
(HasSchemaResult schema, ParseSumType schemas) => ParseSumType (schema ': schemas) Source # | |
Defined in Data.Aeson.Schema.Internal parseSumType :: [Key] -> Value -> Parser (SumType (SchemaResultList (schema ': schemas))) Source # |
toValueMap :: forall pairs. All HasSchemaResultPair pairs => Object ('Schema pairs) -> Object Source #
class HasSchemaResultPair (a :: (SchemaKey, SchemaType)) where Source #
parseValuePair :: Proxy a -> [Key] -> Object -> Parser (Key, Dynamic) Source #
toValuePair :: Proxy a -> Object schema -> Object Source #
showValuePair :: Proxy a -> Object schema -> (String, ShowS) Source #
Instances
(IsSchemaKey key, HasSchemaResult inner, Typeable (SchemaResult inner)) => HasSchemaResultPair '(key, inner) Source # | |
Defined in Data.Aeson.Schema.Internal |
parseFail :: forall (schema :: SchemaType) m a. (MonadFail m, HasSchemaResult schema) => [Key] -> Value -> m a Source #
A helper for creating fail messages when parsing a schema.
data UnSchemaKey :: SchemaKey -> Exp Symbol Source #
Instances
type Eval (UnSchemaKey ('NormalKey key) :: Symbol -> Type) Source # | |
Defined in Data.Aeson.Schema.Internal | |
type Eval (UnSchemaKey ('PhantomKey key) :: Symbol -> Type) Source # | |
Defined in Data.Aeson.Schema.Internal |
type family LookupSchema (key :: Symbol) (schema :: Schema) :: SchemaType where ... Source #
The type-level function that return the schema of the given key in a SchemaObject
.
getKey :: forall (key :: Symbol) (schema :: Schema) (endSchema :: SchemaType) result. (endSchema ~ LookupSchema key schema, result ~ SchemaResult endSchema, KnownSymbol key, Typeable result, Typeable endSchema) => Proxy key -> Object schema -> result Source #
Get a key from the given Object
, returned as the type encoded in
its schema.
let o = .. :: Object ( 'SchemaObject '[ '("foo", 'SchemaInt) , '("bar", 'SchemaObject '[ '("name", 'SchemaText) ] , '("baz", 'SchemaMaybe 'SchemaBool) ] ) getKey (Proxy @"foo") o :: Bool getKey (Proxy @"bar") o :: Object ('SchemaObject '[ '("name", 'SchemaText) ]) getKey (Proxy @"name") $ getKey @"bar" o :: Text getKey (Proxy @"baz") o :: Maybe Bool
unsafeGetKey :: forall (endSchema :: SchemaType) (key :: Symbol) (schema :: Schema). (KnownSymbol key, Typeable (SchemaResult endSchema)) => Proxy key -> Object schema -> SchemaResult endSchema Source #