dhall-openapi 1.0.4 → 1.0.5
raw patch · 4 files changed
+48/−21 lines, 4 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
Files
- dhall-openapi.cabal +4/−4
- openapi-to-dhall/Main.hs +5/−2
- src/Dhall/Kubernetes/Convert.hs +35/−11
- src/Dhall/Kubernetes/Types.hs +4/−4
dhall-openapi.cabal view
@@ -1,6 +1,6 @@-Cabal-Version: >=1.11+Cabal-Version: 1.11 Name: dhall-openapi-Version: 1.0.4+Version: 1.0.5 Homepage: https://github.com/dhall-lang/dhall-haskell/tree/master/dhall-openapi#dhall-openapi Author: Fabrizio Ferrai Maintainer: Gabriel439@gmail.com@@ -77,12 +77,12 @@ Ghc-Options: -Wall Build-Depends: base >= 4.11.0.0 && < 5 ,- aeson >= 1.0.0.0 && < 2.1 ,+ aeson >= 1.0.0.0 && < 2.2 , containers >= 0.5.8.0 && < 0.7 , dhall >= 1.38.0 && < 1.42 , prettyprinter >= 1.7.0 && < 1.8 , scientific >= 0.3.0.0 && < 0.4 , sort >= 1.0 && < 1.1 , text >= 0.11.1.0 && < 2.1 ,- vector >= 0.11.0.0 && < 0.13+ vector >= 0.11.0.0 && < 0.14 Default-Language: Haskell2010
openapi-to-dhall/Main.hs view
@@ -229,12 +229,12 @@ parseNaturalInt = Options.Applicative.switch ( Options.Applicative.long "preferNaturalInt"- <> Options.Applicative.help "Render Swagger Integer as Dhall Natural unless negative numbers included in range"+ <> Options.Applicative.help "Render Swagger Integer as Dhall Natural unless negative numbers included in range, and Swagger IntOrString as Dhall NatOrString" ) parseNatIntExceptions' = option [] $ Options.Applicative.option parseNatIntExceptions ( Options.Applicative.long "natIntExceptions"- <> Options.Applicative.help "List of Type.field that should be treated the opposite way to preferNaturalInt; e.g.: ContainerStateTerminated.exitCode,ContainerStateTerminated.signal"+ <> Options.Applicative.help "List of Integer and/or IntOrString Type.field that should be treated the opposite way to preferNaturalInt; e.g.: ContainerStateTerminated.exitCode,ContainerStateTerminated.signal" <> Options.Applicative.metavar "EXCEPTIONS" ) parsePrefixMap' =@@ -348,6 +348,9 @@ (RecordLit [ ( "IntOrString" , Dhall.makeRecordField $ Field (Embed (Convert.mkImport prefixMap [ ] "types.dhall")) $ Dhall.makeFieldSelection "IntOrString"+ )+ , ( "NatOrString"+ , Dhall.makeRecordField $ Field (Embed (Convert.mkImport prefixMap [ ] "types.dhall")) $ Dhall.makeFieldSelection "NatOrString" ) , ( "Resource", mkEmbedField (Convert.mkImport prefixMap [ ] "typesUnion.dhall")) ]
src/Dhall/Kubernetes/Convert.hs view
@@ -33,6 +33,7 @@ import qualified Dhall.Core as Dhall import qualified Dhall.Map import qualified Dhall.Optics+import qualified Data.Map as Map modelsToText :: ModelHierarchy -> [Text] modelsToText = List.map (\ (ModelName unModelName) -> unModelName)@@ -79,11 +80,32 @@ ) ] +-- Special types: the first is a union found in the k8s swagger;+-- the second is a parallel union we construct for distinguishing+-- signed and unsigned intergers on the dhall side+intOrStringK8sType :: Text+intOrStringK8sType = "io.k8s.apimachinery.pkg.util.intstr.IntOrString"+natOrStringK8sType :: Text+natOrStringK8sType = "io.k8s.apimachinery.pkg.util.intstr.NatOrString" --- | Get a filename from a Swagger ref-pathFromRef :: Ref -> Text-pathFromRef (Ref r) = (Text.split (== '/') r) List.!! 2+intOrStringDhallType :: Dhall.Expr s a+intOrStringDhallType = Dhall.Union $ Dhall.Map.fromList $ fmap (second Just)+ [ ("Int", Dhall.Integer), ("String", Dhall.Text) ]+natOrStringDhallType :: Dhall.Expr s a+natOrStringDhallType = Dhall.Union $ Dhall.Map.fromList $ fmap (second Just)+ [ ("Nat", Dhall.Natural), ("String", Dhall.Text) ] +-- | Get a filename from a Swagger ref, also handling when we need to split+-- | NatOrString from IntOrString+pathFromRef :: Bool -> Ref -> Text+pathFromRef prefNatInt (Ref r) =+ if prefNatInt && qualType == intOrStringK8sType then+ natOrStringK8sType+ else+ qualType+ where+ qualType = (Text.split (== '/') r) List.!! 2+ -- | Build an import from path components (note: they need to be in reverse order) -- and a filename mkImport :: Data.Map.Map Prefix Dhall.Import -> [Text] -> Text -> Dhall.Import@@ -163,7 +185,10 @@ like "should this key be optional" -} toTypes :: Data.Map.Map Prefix Dhall.Import -> ([ModelName] -> Definition -> Maybe ModelName) -> Bool -> [(String,String)] -> Data.Map.Map ModelName Definition -> Data.Map.Map ModelName Expr-toTypes prefixMap typeSplitter preferNaturalInt natIntExceptions definitions = toTypes' prefixMap typeSplitter preferNaturalInt natIntExceptions definitions Data.Map.empty+toTypes prefixMap typeSplitter preferNaturalInt natIntExceptions definitions =+ Map.insert (ModelName natOrStringK8sType) natOrStringDhallType types+ where+ types = toTypes' prefixMap typeSplitter preferNaturalInt natIntExceptions definitions Data.Map.empty toTypes' :: Data.Map.Map Prefix Dhall.Import -> ([ModelName] -> Definition -> Maybe ModelName) -> Bool -> [(String,String)] -> Data.Map.Map ModelName Definition -> Data.Map.Map ModelName Expr -> Data.Map.Map ModelName Expr toTypes' prefixMap typeSplitter preferNaturalInt natIntExceptions definitions toMerge@@ -193,8 +218,6 @@ kvList = Dhall.App Dhall.List $ Dhall.Record $ Dhall.Map.fromList [ ("mapKey", Dhall.makeRecordField Dhall.Text), ("mapValue", Dhall.makeRecordField Dhall.Text) ]- intOrStringType = Dhall.Union $ Dhall.Map.fromList $ fmap (second Just)- [ ("Int", Dhall.Integer), ("String", Dhall.Text) ] -- | Convert a single Definition to a Dhall Type, yielding any definitions to be split -- Note: model hierarchy contains the modelName of of the current definition as the last entry@@ -203,7 +226,7 @@ | Just splitModelName <- typeSplitter modelHierarchy definition = ( Dhall.Embed $ mkImport prefixMap [] ((unModelName splitModelName) <> ".dhall"), Data.Map.singleton splitModelName definition) -- If we point to a ref we just reference it via Import- | Just r <- ref definition = ( Dhall.Embed $ mkImport prefixMap [] (pathFromRef r <> ".dhall"), Data.Map.empty)+ | Just r <- ref definition = ( Dhall.Embed $ mkImport prefixMap [] (pathFromRef prefNatInt r <> ".dhall"), Data.Map.empty) | Just props <- properties definition = let shouldBeRequired :: ModelHierarchy -> FieldName -> Bool@@ -229,14 +252,15 @@ , let (mapValue, rest) = convertToType modelHierarchy props prefNatInt = (Dhall.App Dhall.List (Dhall.Record (Dhall.Map.fromList [ ("mapKey", Dhall.makeRecordField Dhall.Text), ("mapValue", Dhall.makeRecordField mapValue) ])), rest) -- This is another way to declare an intOrString- | Maybe.isJust $ intOrString definition = (intOrStringType, Data.Map.empty)+ | Maybe.isJust $ intOrString definition =+ (if prefNatInt then natOrStringDhallType else intOrStringDhallType, Data.Map.empty) -- Otherwise - if we have a 'type' - it's a basic type | Just basic <- typ definition = case basic of "object" -> (kvList, Data.Map.empty) "array" | Just item <- items definition -> let (e, tm) = convertToType (modelHierarchy) item prefNatInt in (Dhall.App Dhall.List e, tm)- "string" | format definition == Just "int-or-string" -> (intOrStringType, Data.Map.empty)+ "string" | format definition == Just "int-or-string" -> (intOrStringDhallType, Data.Map.empty) "string" -> (Dhall.Text, Data.Map.empty) "boolean" -> (Dhall.Bool, Data.Map.empty) "integer" -> if prefNatInt then case (minimum_ definition, exclusiveMinimum definition,@@ -548,8 +572,8 @@ , format = v1JSONSchemaPropsFormat , minimum_ = v1JSONSchemaPropsMinimum , exclusiveMinimum = v1JSONSchemaPropsExclusiveMinimum- , maximum_ = v1JSONSchemaPropsMaximum- , exclusiveMaximum = v1JSONSchemaPropsExclusiveMaximum+ , maximum_ = v1JSONSchemaPropsMaximum+ , exclusiveMaximum = v1JSONSchemaPropsExclusiveMaximum , description = v1JSONSchemaPropsDescription , items = v1JSONSchemaPropsItems >>= parseMaybe parseJSON , properties = fmap toProperties v1JSONSchemaPropsProperties
src/Dhall/Kubernetes/Types.hs view
@@ -44,8 +44,8 @@ , format :: Maybe Text , minimum_ :: Maybe Scientific -- Avoid shadowing with Prelude.minimum , exclusiveMinimum :: Maybe Bool- , maximum_ :: Maybe Scientific -- Avoid shadowing with Prelude.maximum- , exclusiveMaximum :: Maybe Bool+ , maximum_ :: Maybe Scientific -- Avoid shadowing with Prelude.maximum+ , exclusiveMaximum :: Maybe Bool , description :: Maybe Text , items :: Maybe Definition , properties :: Maybe (Map ModelName Definition)@@ -62,8 +62,8 @@ format <- o .:? "format" minimum_ <- o .:? "minimum" exclusiveMinimum <- o .:? "exclusiveMinimum"- maximum_ <- o .:? "maximum"- exclusiveMaximum <- o .:? "exclusiveMaximum"+ maximum_ <- o .:? "maximum"+ exclusiveMaximum <- o .:? "exclusiveMaximum" properties <- o .:? "properties" additionalProperties <- o .:? "additionalProperties" required <- o .:? "required"