aeson-typescript 0.5.0.0 → 0.6.0.0
raw patch · 5 files changed
+93/−3 lines, 5 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
Files
- CHANGELOG.md +4/−1
- aeson-typescript.cabal +8/−2
- src/Data/Aeson/TypeScript/Instances.hs +49/−0
- src/Data/Aeson/TypeScript/Recursive.hs +1/−0
- test/TestBoilerplate.hs +31/−0
CHANGELOG.md view
@@ -1,7 +1,10 @@ # Change log -## (unreleased)+## 0.6.0.0++* New word instances: Word, Word16, Word32, Word64+* New instances from Data.Functor: Compose, Const, Identity, Product ## 0.5.0.0
aeson-typescript.cabal view
@@ -5,7 +5,7 @@ -- see: https://github.com/sol/hpack name: aeson-typescript-version: 0.5.0.0+version: 0.6.0.0 synopsis: Generate TypeScript definition files from your ADTs description: Please see the README on Github at <https://github.com/codedownio/aeson-typescript#readme> category: Text, Web, JSON@@ -18,7 +18,13 @@ license-file: LICENSE build-type: Simple tested-with:- GHC == 9.0.1, GHC == 8.10.4, GHC == 8.10.3, GHC == 8.8.4, GHC == 8.8.3+ GHC == 9.6.1+ , GHC == 9.4.4+ , GHC == 9.2.7+ , GHC == 9.0.2+ , GHC == 8.10.7+ , GHC == 8.8.4+ , GHC == 8.6.5 extra-source-files: README.md CHANGELOG.md
src/Data/Aeson/TypeScript/Instances.hs view
@@ -3,6 +3,7 @@ {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE OverlappingInstances #-} {-# LANGUAGE CPP #-}+{-# LANGUAGE PolyKinds #-} {-# OPTIONS_GHC -fno-warn-orphans #-} -- Note: the OverlappingInstances pragma is only here so the overlapping instances in this file@@ -13,9 +14,15 @@ import qualified Data.Aeson as A import Data.Aeson.TypeScript.Types import Data.Data+import Data.Functor.Compose (Compose)+import Data.Functor.Const (Const)+import Data.Functor.Identity (Identity)+import Data.Functor.Product (Product) import Data.HashMap.Strict import Data.HashSet+import Data.Kind (Type) import qualified Data.List as L+import Data.List.NonEmpty (NonEmpty) import Data.Map.Strict import Data.Set import Data.String.Interpolate@@ -24,6 +31,7 @@ import Data.Void import Data.Word import GHC.Int+import Numeric.Natural (Natural) #if !MIN_VERSION_base(4,11,0) import Data.Monoid@@ -49,6 +57,9 @@ instance TypeScript Integer where getTypeScriptType _ = "number" +instance TypeScript Natural where+ getTypeScriptType _ = "number"+ instance TypeScript Float where getTypeScriptType _ = "number" @@ -73,13 +84,29 @@ instance TypeScript Char where getTypeScriptType _ = "string" +instance TypeScript Word where+ getTypeScriptType _ = "number"+ instance TypeScript Word8 where getTypeScriptType _ = "number" +instance TypeScript Word16 where+ getTypeScriptType _ = "number"++instance TypeScript Word32 where+ getTypeScriptType _ = "number"++instance TypeScript Word64 where+ getTypeScriptType _ = "number"+ instance {-# OVERLAPPABLE #-} (TypeScript a) => TypeScript [a] where getTypeScriptType _ = (getTypeScriptType (Proxy :: Proxy a)) ++ "[]" getParentTypes _ = [TSType (Proxy :: Proxy a)] +instance (TypeScript a) => TypeScript (NonEmpty a) where+ getTypeScriptType _ = getTypeScriptType (Proxy :: Proxy [a])+ getParentTypes _ = [TSType (Proxy :: Proxy a)]+ instance {-# OVERLAPPING #-} TypeScript [Char] where getTypeScriptType _ = "string" @@ -112,6 +139,28 @@ , (TSType (Proxy :: Proxy b)) , (TSType (Proxy :: Proxy c)) , (TSType (Proxy :: Proxy d))+ ]++instance forall a k (b :: k). (Typeable k, Typeable b, TypeScript a) => TypeScript (Const a b) where+ getTypeScriptType _ = getTypeScriptType (Proxy :: Proxy a)+ getParentTypes _ = [TSType (Proxy :: Proxy a)]++instance (TypeScript a) => TypeScript (Identity a) where+ getTypeScriptType _ = getTypeScriptType (Proxy :: Proxy a)+ getParentTypes _ = [TSType (Proxy :: Proxy a)]++instance forall k k1 (f :: k -> Type) (g :: k1 -> k) a. (+ Typeable k, Typeable k1, Typeable f, Typeable g, Typeable a, TypeScript (f (g a)), TypeScript a+ ) => TypeScript (Compose f g a) where+ getTypeScriptType _ = getTypeScriptType (Proxy :: Proxy (f (g a)))+ getParentTypes _ = getParentTypes (Proxy :: Proxy (f (g a)))++instance forall k (f :: k -> Type) (g :: k -> Type) a. (+ Typeable k, Typeable f, Typeable g, Typeable a, TypeScript (f a), TypeScript (g a)+ ) => TypeScript (Product f g a) where+ getTypeScriptType _ = getTypeScriptType (Proxy :: Proxy (f a, g a))+ getParentTypes _ = L.nub [ (TSType (Proxy :: Proxy (f a)))+ , (TSType (Proxy :: Proxy (g a))) ] instance (TypeScript a) => TypeScript (Maybe a) where
src/Data/Aeson/TypeScript/Recursive.hs view
@@ -16,6 +16,7 @@ , getAllParentTypes ) where +import Control.Monad import Control.Monad.State import Control.Monad.Trans.Maybe import Control.Monad.Writer
test/TestBoilerplate.hs view
@@ -6,11 +6,17 @@ import qualified Data.Aeson as A import Data.Aeson.TH as A import Data.Aeson.TypeScript.TH+import Data.Functor.Compose+import Data.Functor.Const import Data.Functor.Identity+import Data.Functor.Product import Data.Kind+import Data.List.NonEmpty import Data.Proxy import Data.String.Interpolate+import Data.Word import Language.Haskell.TH hiding (Type)+import Numeric.Natural (Natural) import Test.Hspec import Util import Util.Aeson@@ -25,7 +31,25 @@ data Complex a = Nullary | Unary Int | Product String Char a | Record { testOne :: Int, testTwo :: Bool, testThree :: Complex a} deriving Eq data Optional = Optional {optionalInt :: Maybe Int} data AesonTypes = AesonTypes { aesonValue :: A.Value, aesonObject :: A.Object }+data Numbers = Numbers {+ natural :: Natural+ , word :: Word+ , word16 :: Word16+ , word32 :: Word32+ , word64 :: Word64+ }+data FancyFunctors = FancyFunctors {+ nonEmpty :: NonEmpty Int+ , const :: Const Int Int+ , product :: Product Identity Identity Int+ , compose :: Compose Identity Identity Int+ } +-- * Values++fancyFunctorsValue :: FancyFunctors+fancyFunctorsValue = FancyFunctors (42 :| []) (Const 42) (Pair 42 42) (Compose 42)+ -- * For testing type families instance TypeScript Identity where getTypeScriptType _ = "any"@@ -65,6 +89,8 @@ deriveInstances ''Complex deriveInstances ''Optional deriveInstances ''AesonTypes+ deriveInstances ''Numbers+ deriveInstances ''FancyFunctors typesAndValues :: Exp <- [e|[(getTypeScriptType (Proxy :: Proxy Unit), A.encode Unit) @@ -94,6 +120,9 @@ aesonValue = A.object [("foo" :: A.Key, A.Number 42)] , aesonObject = aesonFromList [("foo", A.Number 42)] }))++ , (getTypeScriptType (Proxy :: Proxy Numbers), A.encode (Numbers 42 42 42 42 42))+ , (getTypeScriptType (Proxy :: Proxy FancyFunctors), A.encode fancyFunctorsValue) ]|] declarations :: Exp <- [e|getTypeScriptDeclarations (Proxy :: Proxy Unit)@@ -106,6 +135,8 @@ <> getTypeScriptDeclarations (Proxy :: Proxy (Complex T)) <> getTypeScriptDeclarations (Proxy :: Proxy Optional) <> getTypeScriptDeclarations (Proxy :: Proxy AesonTypes)+ <> getTypeScriptDeclarations (Proxy :: Proxy Numbers)+ <> getTypeScriptDeclarations (Proxy :: Proxy FancyFunctors) |] tests <- [d|tests :: SpecWith ()