aeson-flowtyped 0.7.1 → 0.7.2
raw patch · 3 files changed
+48/−10 lines, 3 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
- Data.Aeson.Flow: Map :: !(Vector (Text, a)) -> FlowTypeF a
+ Data.Aeson.Flow: ObjectMap :: !Text -> a -> FlowTypeF a
+ Data.Aeson.Flow: instance Data.Aeson.Flow.FlowTyped a => Data.Aeson.Flow.FlowTyped (Data.HashMap.Base.HashMap Data.Text.Internal.Text a)
Files
- aeson-flowtyped.cabal +8/−1
- src/Data/Aeson/Flow.hs +18/−9
- test/Spec.hs +22/−0
aeson-flowtyped.cabal view
@@ -3,10 +3,12 @@ -- see: https://github.com/sol/hpack name: aeson-flowtyped-version: 0.7.1+version: 0.7.2 synopsis: Create Flow type definitions from Haskell data types. description: Create Flow type definitions from Haskell data types. category: Web+homepage: https://github.com/mikeplus64/aeson-flowtyped#readme+bug-reports: https://github.com/mikeplus64/aeson-flowtyped/issues author: Mike Ledger <mike@quasimal.com> maintainer: mike@quasimal.com license: BSD3@@ -14,6 +16,10 @@ build-type: Simple cabal-version: >= 1.10 +source-repository head+ type: git+ location: https://github.com/mikeplus64/aeson-flowtyped+ library hs-source-dirs: src@@ -50,4 +56,5 @@ , aeson-flowtyped , tasty , tasty-hunit+ , unordered-containers default-language: Haskell2010
src/Data/Aeson/Flow.hs view
@@ -125,7 +125,7 @@ data FlowTypeF a = Object !(HashMap Text a) | ExactObject !(HashMap Text a)- | Map !(Vector (Text, a))+ | ObjectMap !Text a | Array a | Tuple !(Vector a) | Fun !(Vector (Text, a)) a@@ -139,7 +139,8 @@ | Poly !Var !(Vector a) | PolyVar !Var deriving (Show, Eq, Functor, Traversable, Foldable)--- XXX: vector >= 0.12 has Eq1 vector which allows us to derive eq+-- XXX: vector >= 0.12 has Eq1 vector which allows us to use eq for Fix FlowTypeF+-- and related types instance Show1 FlowTypeF where liftShowsPrec sp sl i a =@@ -165,16 +166,12 @@ ppAlts :: [FlowType] -> FlowType -> PP.Doc ppAlts alts (Fix f) = case f of Alt a b -> ppAlts (a:alts) b- x -> PP.align- (sep- (map pp- (reverse (Fix x:alts))))+ x -> PP.align (sep (map pp (reverse (Fix x:alts)))) where sep [x] = x sep (x:xs) = x PP.<+> PP.string "|" PP.<$> sep xs sep _ = PP.empty - braceList :: [PP.Doc] -> PP.Doc braceList = (\s -> PP.lbrace PP.</> s PP.</> PP.rbrace)@@ -208,6 +205,7 @@ Nullable _ -> PP.parens x Omitable _ -> PP.parens x Alt _ _ -> PP.parens x+ Array _ -> PP.parens x _ -> x ppObject :: HashMap Text FlowType -> [PP.Doc]@@ -220,6 +218,11 @@ pp :: FlowType -> PP.Doc pp (Fix ft) = case ft of+ ObjectMap keyName a -> braceList+ [ PP.brackets (text keyName PP.<> PP.text ": string") PP.<>+ PP.colon PP.<+>+ pp a+ ] Object hm -> braceList (ppObject hm) ExactObject hm -> braceBarList (ppObject hm) Array a -> mayWrap a (pp a) PP.<> PP.string "[]"@@ -232,8 +235,8 @@ Void -> PP.text "void" Any -> PP.text "any" Mixed -> PP.text "mixed"- Nullable a -> PP.char '?' PP.<> pp a- Omitable a -> PP.char '?' PP.<> pp a -- hopefully these are caught+ Nullable a -> PP.char '?' PP.<> mayWrap a (pp a)+ Omitable a -> PP.char '?' PP.<> mayWrap a (pp a) -- hopefully these are caught Literal a -> ppJson a Tag t -> PP.squotes (text t) Name (FlowName _ t) -> text t@@ -624,6 +627,12 @@ instance Typeable a => FlowTyped (Fixed a) where isPrim _ = False flowType _ = Fix (Prim Number)+ flowTypeName _ = Nothing++instance FlowTyped a => FlowTyped (HashMap Text a) where+ -- XXX this is getting quite incoherent, what makes something "Prim" or not...+ isPrim _ = True+ flowType _ = Fix (ObjectMap "key" (flowTypePreferName (Proxy :: Proxy a))) flowTypeName _ = Nothing -- monomorphic numeric instances
test/Spec.hs view
@@ -3,6 +3,8 @@ import Data.Aeson (Value) import Data.Aeson.Flow import Data.Functor.Foldable (Fix (..))+import Data.HashMap.Strict (HashMap)+import qualified Data.HashMap.Strict as H import Data.Proxy (Proxy (..)) import Data.Text (Text) import Data.Vector (Vector)@@ -43,6 +45,11 @@ instance FlowTyped Codep +data Hmap = Hmap (HashMap Text User)+ deriving (Generic)++instance FlowTyped Hmap+ main :: IO () main = defaultMain $ testGroup "aeson-flowtyped" [ testCase "nullable" $@@ -92,6 +99,21 @@ \ 'C4' |\n\ \ 'D4';" @=? exportFlowTypeAs "Adt4" (flowType (Proxy :: Proxy Adt4))++ , testCase "map-style object / hashmap instance" $+ "export type Hmap =\n\+ \ { [key: string]: User };" @=?+ exportFlowTypeAs "Hmap" (flowType (Proxy :: Proxy (HashMap Text User)))++ , testCase "parens around nullable array" $+ "export type T =\n\+ \ ?(string[]);" @=?+ exportFlowTypeAs "T" (flowType (Proxy :: Proxy (Maybe [Text])))++ , testCase "parens around nullable array of nullable elements" $+ "export type T =\n\+ \ ?((?string)[]);" @=?+ exportFlowTypeAs "T" (flowType (Proxy :: Proxy (Maybe [Maybe Text]))) , testCase "module export" $ "// @flow\n\