mu-schema 0.1.0.0 → 0.2.0.0
raw patch · 3 files changed
+77/−45 lines, 3 filesnew-uploader
Files
- mu-schema.cabal +47/−43
- src/Mu/Schema/Class.hs +21/−2
- src/Mu/Schema/Definition.hs +9/−0
mu-schema.cabal view
@@ -1,51 +1,55 @@-cabal-version: >=1.10-name: mu-schema-version: 0.1.0.0-synopsis: Format-independent schemas for serialization-description: With @mu-schema@ you can describe schemas using type-level constructs, and derive serializers from those. See @mu-avro@, @mu-protobuf@ for the actual adapters.--- bug-reports:-license: Apache-2.0-license-file: LICENSE-author: Alejandro Serrano, Flavio Corpa-maintainer: alejandro.serrano@47deg.com-copyright: Copyright © 2019-2020 <http://47deg.com 47 Degrees>-category: Network-build-type: Simple-extra-source-files: CHANGELOG.md-homepage: https://higherkindness.io/mu-haskell/-bug-reports: https://github.com/higherkindness/mu-haskell/issues+name: mu-schema+version: 0.2.0.0+synopsis: Format-independent schemas for serialization+description:+ With @mu-schema@ you can describe schemas using type-level constructs, and derive serializers from those. See @mu-avro@, @mu-protobuf@ for the actual adapters. +license: Apache-2.0+license-file: LICENSE+author: Alejandro Serrano, Flavio Corpa+maintainer: alejandro.serrano@47deg.com+copyright: Copyright © 2019-2020 <http://47deg.com 47 Degrees>+cabal-version: >=1.10+category: Network+build-type: Simple+extra-source-files: CHANGELOG.md+homepage: https://higherkindness.io/mu-haskell/+bug-reports: https://github.com/higherkindness/mu-haskell/issues+ source-repository head type: git location: https://github.com/higherkindness/mu-haskell library- exposed-modules: Mu.Schema- , Mu.Schema.Definition- , Mu.Schema.Interpretation- , Mu.Schema.Interpretation.Schemaless- , Mu.Schema.Interpretation.Anonymous- , Mu.Schema.Class- , Mu.Schema.Registry- , Mu.Schema.Conversion.TypesToSchema- , Mu.Schema.Conversion.SchemaToTypes- , Mu.Schema.Examples- , Mu.Schema.Annotations- , Mu.Adapter.Json- , Data.Functor.MaybeLike+ exposed-modules:+ Data.Functor.MaybeLike+ Mu.Adapter.Json+ Mu.Schema+ Mu.Schema.Annotations+ Mu.Schema.Class+ Mu.Schema.Conversion.SchemaToTypes+ Mu.Schema.Conversion.TypesToSchema+ Mu.Schema.Definition+ Mu.Schema.Examples+ Mu.Schema.Interpretation+ Mu.Schema.Interpretation.Anonymous+ Mu.Schema.Interpretation.Schemaless+ Mu.Schema.Registry+ -- other-modules: -- other-extensions:- build-depends: base >=4.12 && <5- , sop-core- , containers- , unordered-containers- , bytestring- , vector- , text- , aeson- , template-haskell >= 2.12- , th-abstraction- hs-source-dirs: src- default-language: Haskell2010- ghc-options: -Wall- -fprint-potential-instances+ build-depends:+ aeson+ , base >=4.12 && <5+ , bytestring+ , containers+ , sop-core+ , template-haskell >=2.12+ , text+ , th-abstraction+ , unordered-containers+ , vector++ hs-source-dirs: src+ default-language: Haskell2010+ ghc-options: -Wall -fprint-potential-instances
src/Mu/Schema/Class.hs view
@@ -32,7 +32,7 @@ does not suit you, use 'CustomFieldMapping'. -} module Mu.Schema.Class (- WithSchema(..)+ WithSchema(..), unWithSchema , FromSchema(..), fromSchema' , ToSchema(..), toSchema' , CustomFieldMapping(..)@@ -54,8 +54,14 @@ -- | Tags a value with its schema. -- For usage with @deriving via@.-newtype WithSchema (w :: Type -> Type) (sch :: Schema tn fn) (sty :: tn) a = WithSchema a+newtype WithSchema (w :: Type -> Type) (sch :: Schema tn fn) (sty :: tn) a where+ WithSchema :: forall tn fn (w :: Type -> Type) (sch :: Schema tn fn) (sty :: tn) a.+ a -> WithSchema w sch sty a +unWithSchema :: forall tn fn (w :: Type -> Type) (sch :: Schema tn fn) (sty :: tn) a.+ WithSchema w sch sty a -> a+unWithSchema (WithSchema x) = x+ -- | Defines the conversion of a type @t@ into a 'Term' -- which follows the schema @sch@. -- You can give an optional mapping between the@@ -85,6 +91,19 @@ fromSchema :: (Generic t, GFromSchemaTypeDef w sch '[] (sch :/: sty) (Rep t) ) => Term w sch (sch :/: sty) -> t fromSchema x = to (fromSchemaTypeDef (Proxy @'[]) x)++instance (sch :/: sty ~ 'DRecord sty fields)+ => ToSchema w sch sty (Term w sch ('DRecord sty fields)) where+ toSchema = id+instance (sch :/: sty ~ 'DEnum sty choices)+ => ToSchema w sch sty (Term w sch ('DEnum sty choices)) where+ toSchema = id+instance (sch :/: sty ~ 'DRecord sty fields)+ => FromSchema w sch sty (Term w sch ('DRecord sty fields)) where+ fromSchema = id+instance (sch :/: sty ~ 'DEnum sty choices)+ => FromSchema w sch sty (Term w sch ('DEnum sty choices)) where+ fromSchema = id -- | Conversion from Haskell type to schema term. -- This version is intended for usage with @TypeApplications@:
src/Mu/Schema/Definition.hs view
@@ -135,6 +135,15 @@ -- | Represents a choice between types. | TUnion [FieldTypeB builtin typeName] +instance KnownName n => KnownName ('DRecord n fields) where+ nameVal _ = nameVal (Proxy @n)+instance KnownName n => KnownName ('DEnum n choices) where+ nameVal _ = nameVal (Proxy @n)+instance KnownName n => KnownName ('ChoiceDef n) where+ nameVal _ = nameVal (Proxy @n)+instance KnownName n => KnownName ('FieldDef n t) where+ nameVal _ = nameVal (Proxy @n)+ -- | Lookup a type in a schema by its name. type family (sch :: Schema t f) :/: (name :: t) :: TypeDef t f where '[] :/: name = TypeError ('Text "Cannot find type " ':<>: 'ShowType name ':<>: 'Text " in the schema")