jsonschema-gen 0.1.0.0 → 0.2.0.0
raw patch · 8 files changed
+113/−28 lines, 8 files
Files
- changelog.md +9/−0
- jsonschema-gen.cabal +5/−3
- src/Data/JSON/Schema/Generator.hs +2/−2
- src/Data/JSON/Schema/Generator/Class.hs +3/−3
- src/Data/JSON/Schema/Generator/Convert.hs +5/−8
- src/Data/JSON/Schema/Generator/Generic.hs +11/−11
- tests/Main.hs +1/−1
- tests/Values.hs +77/−0
+ changelog.md view
@@ -0,0 +1,9 @@+jsonschema-gen-0.2.0.0++* Rename PropType to FieldType.+* Fix typo in cabal file.+* Fix typo in comment.++jsonschema-gen-0.1.0.0++* initial release.
jsonschema-gen.cabal view
@@ -1,7 +1,7 @@ name: jsonschema-gen-version: 0.1.0.0+version: 0.2.0.0 synopsis: JSON Schema generator from Algebraic data type-description: This library contains a JSON Scheam generator.+description: This library contains a JSON Schema generator. homepage: https://github.com/yuga/jsonschema-gen license: BSD3 license-file: LICENSE@@ -11,8 +11,9 @@ category: Text, Data, JSON build-type: Simple cabal-version: >=1.10--- extra-source-files: +extra-source-files: changelog.md+ flag safe-aeson description: To avoid the issue of prior to aeson-0.7.0.6, set this flag True. default: False@@ -54,6 +55,7 @@ type: exitcode-stdio-1.0 main-is: Main.hs other-modules: Types+ Values build-depends: base >=4.6 && <4.9 , aeson , bytestring
src/Data/JSON/Schema/Generator.hs view
@@ -15,7 +15,7 @@ -- $use -- * Genenerating JSON Schema- Options(..), PropType(..), defaultOptions+ Options(..), FieldType(..), defaultOptions , generate, generate' -- * Type conversion@@ -30,7 +30,7 @@ import Data.ByteString.Lazy.Char8 (ByteString) import Data.JSON.Schema.Generator.Class (JSONSchemaGen(toSchema), JSONSchemaPrim(toSchemaPrim)- , GJSONSchemaGen(gToSchema), Options(..), PropType(..), defaultOptions, genericToSchema)+ , GJSONSchemaGen(gToSchema), Options(..), FieldType(..), defaultOptions, genericToSchema) import Data.JSON.Schema.Generator.Convert (convert) import Data.JSON.Schema.Generator.Generic () import Data.Proxy (Proxy)
src/Data/JSON/Schema/Generator/Class.hs view
@@ -36,13 +36,13 @@ -- from your datatype. -- data Options = Options- { baseUri :: String -- ^ shcema id prefix.+ { baseUri :: String -- ^ schema id prefix. , schemaIdSuffix :: String -- ^ schema id suffix. File extension for example. , typeRefMap :: Map TypeRep String -- ^ a mapping from datatypes to referenced schema ids.- , fieldTypeMap :: Map String PropType -- ^ a mapping to assign a preffered type to a field.+ , fieldTypeMap :: Map String FieldType -- ^ a mapping to assign a preffered type to a field. } -data PropType = forall a. (JSONSchemaPrim a) => PropType (Proxy a)+data FieldType = forall a. (JSONSchemaPrim a) => FieldType (Proxy a) instance Show Options where showsPrec p opts =
src/Data/JSON/Schema/Generator/Convert.hs view
@@ -199,8 +199,8 @@ consAsEnum s = error ("conAsEnum could not handle: " ++ show s) conAsObject :: A.Options -> SchemaChoice -> A.Value-conAsObject opts sc- | isArray opts = object $+conAsObject opts@(A.Options {A.sumEncoding = A.TwoElemArray}) sc =+ object $ if sctTitle sc == "" then [] else [ ("title", string $ sctTitle sc) ] `mappend` [ ("type", "array")@@ -209,7 +209,8 @@ , ("maxItems", number 2) , ("additionalItems", false) ]- | otherwise = object $+conAsObject opts sc =+ object $ if sctTitle sc == "" then [] else [ ("title", string $ sctTitle sc) ] `mappend` [ ("type", "object")@@ -217,10 +218,6 @@ , ("additionalProperties", false) ] -isArray :: A.Options -> Bool-isArray (A.sumEncoding -> A.TwoElemArray) = True-isArray _ = False- conAsObject' :: A.Options -> SchemaChoice -> A.Value conAsObject' opts@(A.Options {A.sumEncoding = A.TaggedObject tFld cFld}) sc = conAsTag opts (pack tFld) (pack cFld) sc conAsObject' opts@(A.Options {A.sumEncoding = A.TwoElemArray }) sc = conAsArray opts sc@@ -262,5 +259,5 @@ required A.Options {A.omitNothingFields = _ } = array . map fst $ toMap opts mp toMap :: A.Options -> [(Text,Schema)] -> [(Text,A.Value)]-toMap opts mp = map (\(n,v) -> (n,convert' False opts v)) mp -- ++ [("additionalProperties", false)]+toMap opts mp = map (\(n,v) -> (n,convert' False opts v)) mp
src/Data/JSON/Schema/Generator/Generic.hs view
@@ -22,7 +22,7 @@ #endif import Data.JSON.Schema.Generator.Class (JSONSchemaGen(..), JSONSchemaPrim(..)- , GJSONSchemaGen(..), Options(..), PropType(..))+ , GJSONSchemaGen(..), Options(..), FieldType(..)) import Data.JSON.Schema.Generator.Types (Schema(..), SchemaChoice(..) , scBoolean, scInteger, scNumber, scString) import Data.HashMap.Strict (HashMap)@@ -242,28 +242,28 @@ #if __GLASGOW_HASKELL__ >= 710 instance {-# OVERLAPPING #-} (JSONSchemaPrim a) => ToJSONSchemaDef (K1 i (Maybe a)) where- toJSONSchemaDef opts env _ = case propType opts env of- Just (PropType p) -> toSchemaPrim opts p+ toJSONSchemaDef opts env _ = case fieldType opts env of+ Just (FieldType p) -> toSchemaPrim opts p Nothing -> toSchemaPrim opts (Proxy :: Proxy a) instance {-# OVERLAPPABLE #-} (JSONSchemaPrim a) => ToJSONSchemaDef (K1 i a) where- toJSONSchemaDef opts env _ = case propType opts env of- Just (PropType p) -> toSchemaPrim opts p+ toJSONSchemaDef opts env _ = case fieldType opts env of+ Just (FieldType p) -> toSchemaPrim opts p Nothing -> toSchemaPrim opts (Proxy :: Proxy a) #else instance (JSONSchemaPrim a) => ToJSONSchemaDef (K1 i (Maybe a)) where- toJSONSchemaDef opts env _ = case propType opts env of- Just (PropType p) -> toSchemaPrim opts p+ toJSONSchemaDef opts env _ = case fieldType opts env of+ Just (FieldType p) -> toSchemaPrim opts p Nothing -> toSchemaPrim opts (Proxy :: Proxy a) instance (JSONSchemaPrim a) => ToJSONSchemaDef (K1 i a) where- toJSONSchemaDef opts env _ = case propType opts env of- Just (PropType p) -> toSchemaPrim opts p+ toJSONSchemaDef opts env _ = case fieldType opts env of+ Just (FieldType p) -> toSchemaPrim opts p Nothing -> toSchemaPrim opts (Proxy :: Proxy a) #endif -propType :: Options -> Env -> Maybe PropType-propType opts env = do+fieldType :: Options -> Env -> Maybe FieldType+fieldType opts env = do selname <- envSelname env Map.lookup selname $ fieldTypeMap opts
tests/Main.hs view
@@ -186,7 +186,7 @@ , (typeOf (undefined :: ProductType2), "https://github.com/yuga/jsonschema-gen/tests/Types.ProductType2.True.False.tag.json") , (typeOf (undefined :: UnitType2), "https://github.com/yuga/jsonschema-gen/tests/Types.UnitType2.True.False.tag.json") ]- , G.fieldTypeMap = fromList [("recordField1A", G.PropType (Proxy :: Proxy UnitType2))]+ , G.fieldTypeMap = fromList [("recordField1A", G.FieldType (Proxy :: Proxy UnitType2))] } printTypeAsSchemaInJson :: FilePath -> IO ()
+ tests/Values.hs view
@@ -0,0 +1,77 @@+{-# LANGUAGE DeriveGeneric #-}+{-# LANGUAGE FlexibleContexts #-}+{-# LANGUAGE TemplateHaskell #-}+{-# OPTIONS_GHC -fno-warn-orphans #-}++module Values where++import qualified Data.Aeson.TH as A+import Types++--+-- Value+--++recordType11 :: RecordType1+recordType11 =+ RecordData11+ { recordField11 = "field11"+ , recordField12 = 12+ , recordField13 = 1.3+ , recordField14 = Nothing+ , recordField15 =+ RecordData21+ { recordField21 = "field21"+ , recordField22 = Nothing+ }+ , recordField16 = ["recordField16-1", "recordField17-2"]+ , recordField17 = [17]+ , recordField18 = []+ , recordField19 = Just 19+ , recordField1A = "UnitData21"+ }++recordType22 :: RecordType2+recordType22 =+ RecordData22+ { recordField21 = "field21"+ , recordField22 = Nothing+ , recordField23 = 23+ }++productData11 :: ProductType1+productData11 = ProductData11 "param11" 12 1.3 Nothing (ProductData21 "param21" Nothing)++productData12 :: ProductType2+productData12 = ProductData22 "param21" Nothing 23++unitData1 :: UnitType1+unitData1 = UnitData1++unitData2 :: UnitType1+unitData2 = UnitData2++unitData3 :: UnitType1+unitData3 = UnitData3++mixData11 :: MixType1+mixData11 = MixData11++mixData12 :: MixType1+mixData12 = MixData12 "param31" Nothing++mixData13 :: MixType1+mixData13 =+ MixData13+ { recordField31 = "field31"+ , recordField32 = Nothing+ , recordField33 = 33+ }++--+-- Test Print+--++$(A.deriveJSON A.defaultOptions ''RecordType2)+$(A.deriveJSON A.defaultOptions ''ProductType2)+