json-autotype 0.2.5.12 → 0.2.5.13
raw patch · 5 files changed
+26/−18 lines, 5 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
Files
- Data/Aeson/AutoType/Format.hs +14/−9
- Data/Aeson/AutoType/Test.hs +1/−1
- GenerateTestJSON.hs +5/−7
- changelog.md +5/−0
- json-autotype.cabal +1/−1
Data/Aeson/AutoType/Format.hs view
@@ -65,9 +65,10 @@ header = Text.concat ["data ", identifier, " = ", identifier, " { "] -- | Explanatory type alias for making declarations--- First element of the pair is original JSON identifier,--- second element of the pair is the mapped identifier name in Haskell.-type MappedKey = (Text, Text)+-- First element of the triple is original JSON identifier,+-- second element of the triple is the mapped identifier name in Haskell.+-- third element of the triple shows the type in a formatted way+type MappedKey = (Text, Text, Text) -- | Make ToJSON declaration, given identifier (object name in Haskell) and mapping of its keys -- from JSON to Haskell identifiers *in the same order* as in *data type declaration*.@@ -81,8 +82,9 @@ makeParser identifier [] = Text.unwords ["return ", identifier] makeParser identifier _ = Text.unwords [identifier, "<$>", inner] inner = " <*> " `Text.intercalate`- map (takeValue . fst) contents- takeValue jsonId = Text.concat ["v .: \"", jsonId, "\""]+ map takeValue contents+ takeValue (jsonId, _, ty) | isNullable ty = Text.concat ["v .:? \"", jsonId, "\""]+ takeValue (jsonId, _, _ ) = Text.concat ["v .: \"", jsonId, "\""] -- Contents example for wrapFromJSON: -- " <$> --" v .: "hexValue" <*>@@ -101,7 +103,7 @@ | otherwise = ".." inner = ", " `Text.intercalate` map putValue contents- putValue (jsonId, haskellId) = Text.unwords [escapeText jsonId, ".=", haskellId]+ putValue (jsonId, haskellId, _typeText) = Text.unwords [escapeText jsonId, ".=", haskellId] escapeText = Text.pack . show . Text.unpack -- Contents example for wrapToJSON --"hexValue" .= hexValue@@ -119,12 +121,11 @@ newDecl identifier kvs = do attrs <- forM kvs $ \(k, v) -> do formatted <- formatType v return (k, normalizeFieldName identifier k, formatted)- let fieldMapping = map (\(jn, hn, _) -> (jn, hn)) attrs let decl = Text.unlines [wrapDecl identifier $ fieldDecls attrs ,""- ,makeFromJSON identifier fieldMapping+ ,makeFromJSON identifier attrs ,""- ,makeToJSON identifier fieldMapping]+ ,makeToJSON identifier attrs] addDecl decl return identifier where@@ -133,6 +134,9 @@ fieldDecl (_jsonName, haskellName, fType) = Text.concat [ " ", haskellName, " :: ", fType] +-- | Whether the type is nullable or in other words - represented by Maybe type.+isNullable = ("Maybe" `Text.isPrefixOf`)+ addDecl decl = decls %%= (\ds -> ((), decl:ds)) -- | Add new type alias for Array type@@ -168,6 +172,7 @@ formatType (TUnion u) | uu <- u `Set.difference` emptySetLikes, Set.size uu == 1 = do fmt <- formatType $ head $ Set.toList uu return $ "Maybe " `Text.append` fmt+-- TODO: Use Maybe (...) when nullable? formatType (TUnion u) = do tys <- forM (Set.toList u) formatType return $ mkUnion tys where
Data/Aeson/AutoType/Test.hs view
@@ -4,7 +4,7 @@ arbitraryTopValue ) where -import Data.Functor ((<$>))+import Control.Applicative ((<$>), (<*>)) import Data.Aeson import Data.Function (on) import Data.Generics.Uniplate.Data
GenerateTestJSON.hs view
@@ -38,20 +38,18 @@ import Data.Aeson.AutoType.Test import HFlags -fst3 :: (t, t1, t2) -> t-fst3 (a, _, _) = a- -- * Command line flags defineFlag "z:size" (10 :: Int) "Size of generated elements" defineFlag "s:stem" ("Test" :: FilePath) "Test filename stem" defineFlag "c:count" (100 :: Int) "Number of test cases to generate."-defineFlag "o:outputFilename" defaultOutputFilename "Write output to the given file"-defineFlag "suggest" True "Suggest candidates for unification"+--defineFlag "o:outputFilename" defaultOutputFilename "Write output to the given file"+flags_suggest = True+--defineFlag "suggest" True "Suggest candidates for unification" defineFlag "autounify" True "Automatically unify suggested candidates"-defineFlag "t:test" False "Try to run generated parser after"+defineFlag "t:test" True "Try to run generated parser after" defineFlag "d:debug" False "Set this flag to see more debugging info" defineFlag "keep" False "Keep also the successful tests"-defineFlag "fakeFlag" True "Ignore this flag - it doesn't exist!!! It is workaround to library problem."+defineFlag "fakeFlag" True "Ignore this flag - it doesn't exist!!! It is workaround for a library problem." -- Tracing is switched off: myTrace :: String -> IO ()
changelog.md view
@@ -1,5 +1,10 @@ Changelog =========+ 0.2.5.13 Apr 2015++ * Correctly handling lone option, not yet union with optionality.+ Fixed: #3.+ 0.2.5.12 Apr 2015 * Added typechecking before and after type unification.
json-autotype.cabal view
@@ -1,6 +1,6 @@ -- Build information for the package. name: json-autotype-version: 0.2.5.12+version: 0.2.5.13 synopsis: Automatic type declaration for JSON input data description: Generates datatype declarations with Aeson's "FromJSON" instances from a set of example ".json" files.