mirror of
https://github.com/dhall-lang/dhall-kubernetes.git
synced 2024-09-17 10:27:08 +03:00
Incorporate feedback from @Gabriel439
This commit is contained in:
parent
99f4ecd3b9
commit
a7c595b6c9
@ -26,6 +26,7 @@ executable dhall-kubernetes-generator
|
||||
dhall >= 1.22.0 && < 1.25 ,
|
||||
megaparsec >= 7.0 && < 7.1 ,
|
||||
optparse-applicative >= 0.14.3.0 && < 0.15 ,
|
||||
parser-combinators >= 1.0.3 && < 1.1 ,
|
||||
prettyprinter >= 1.2.0.1 && < 1.3 ,
|
||||
sort >= 1.0 && < 1.1 ,
|
||||
text >= 0.11.1.0 && < 1.3 ,
|
||||
|
@ -64,11 +64,10 @@ pathFromRef (Ref r) = (Text.split (== '/') r) List.!! 2
|
||||
|
||||
-- | Build an import from path components (note: they need to be in reverse order)
|
||||
-- and a filename
|
||||
mkImport :: PrefixMap -> [Text] -> Text -> Dhall.Import
|
||||
mkImport :: Data.Map.Map Prefix Dhall.Import -> [Text] -> Text -> Dhall.Import
|
||||
mkImport prefixMap components file =
|
||||
case Data.Map.toList filteredPrefixMap of
|
||||
[] -> localImport
|
||||
[(_, imp)] -> imp <> localImport
|
||||
xs -> (snd . head $ Sort.sortOn (Text.length . fst) xs) <> localImport
|
||||
where
|
||||
localImport = Dhall.Import{..}
|
||||
@ -98,7 +97,7 @@ toTextLit str = Dhall.TextLit (Dhall.Chunks [] str)
|
||||
-- Note: we cannot do 1-to-1 conversion and we need the whole Map because
|
||||
-- many types reference other types so we need to access them to decide things
|
||||
-- like "should this key be optional"
|
||||
toTypes :: PrefixMap -> Data.Map.Map ModelName Definition -> Data.Map.Map ModelName Expr
|
||||
toTypes :: Data.Map.Map Prefix Dhall.Import -> Data.Map.Map ModelName Definition -> Data.Map.Map ModelName Expr
|
||||
toTypes prefixMap definitions = memo
|
||||
where
|
||||
memo = Data.Map.mapWithKey (\k -> convertToType (Just k)) definitions
|
||||
@ -176,7 +175,7 @@ toTypes prefixMap definitions = memo
|
||||
|
||||
-- | Convert a Dhall Type to its default value
|
||||
toDefault
|
||||
:: PrefixMap -- ^ Mapping of prefixes to import roots
|
||||
:: Data.Map.Map Prefix Dhall.Import -- ^ Mapping of prefixes to import roots
|
||||
-> Data.Map.Map ModelName Definition -- ^ All the Swagger definitions
|
||||
-> Data.Map.Map ModelName Expr -- ^ All the Dhall types generated from them
|
||||
-> ModelName -- ^ The name of the object we're converting
|
||||
@ -242,11 +241,11 @@ toDefault prefixMap definitions types modelName = go
|
||||
|
||||
-- | Get a Dhall.Map filled with imports, for creating giant Records or Unions of types or defaults
|
||||
getImportsMap
|
||||
:: PrefixMap -- ^ Mapping of prefixes to import roots
|
||||
-> DuplicateHandler -- ^ Duplicate name handler
|
||||
-> [ModelName] -- ^ A list of all the object names
|
||||
-> Text -- ^ The folder we should get imports from
|
||||
-> [ModelName] -- ^ List of the object names we want to include in the Map
|
||||
:: Data.Map.Map Prefix Dhall.Import -- ^ Mapping of prefixes to import roots
|
||||
-> DuplicateHandler -- ^ Duplicate name handler
|
||||
-> [ModelName] -- ^ A list of all the object names
|
||||
-> Text -- ^ The folder we should get imports from
|
||||
-> [ModelName] -- ^ List of the object names we want to include in the Map
|
||||
-> Dhall.Map.Map Text Expr
|
||||
getImportsMap prefixMap duplicateNameHandler objectNames folder toInclude
|
||||
= Dhall.Map.fromList
|
||||
|
@ -19,7 +19,7 @@ type Expr = Dhall.Expr Dhall.Src Dhall.Import
|
||||
|
||||
type DuplicateHandler = (Text, [ModelName]) -> Maybe ModelName
|
||||
|
||||
type PrefixMap = Data.Map.Map Text Dhall.Import
|
||||
type Prefix = Text
|
||||
|
||||
{-| Type for the Swagger specification.
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
{-# LANGUAGE OverloadedLists #-}
|
||||
{-# LANGUAGE RecordWildCards #-}
|
||||
|
||||
module Main (main) where
|
||||
|
||||
@ -16,7 +17,8 @@ import Data.Bifunctor (bimap)
|
||||
import Data.Foldable (for_)
|
||||
import Data.Text (Text, pack)
|
||||
import qualified Options.Applicative
|
||||
import Text.Megaparsec (some, optional, parse, (<|>), errorBundlePretty)
|
||||
import Control.Applicative.Combinators (sepBy1)
|
||||
import Text.Megaparsec (some, parse, (<|>), errorBundlePretty)
|
||||
import Text.Megaparsec.Char (char, alphaNumChar)
|
||||
|
||||
import qualified Dhall.Kubernetes.Convert as Convert
|
||||
@ -28,7 +30,7 @@ import Dhall.Kubernetes.Types
|
||||
-- | Top-level program options
|
||||
data Options = Options
|
||||
{ skipDuplicates :: Bool
|
||||
, prefixMap :: PrefixMap
|
||||
, prefixMap :: Data.Map.Map Prefix Dhall.Import
|
||||
, filename :: String
|
||||
}
|
||||
|
||||
@ -62,7 +64,7 @@ parseImport :: String -> Expr -> Dhall.Parser.Parser Dhall.Import
|
||||
parseImport _ (Dhall.Note _ (Dhall.Embed l)) = pure l
|
||||
parseImport prefix e = fail $ "Expected a Dhall import for " <> prefix <> " not:\n" <> show e
|
||||
|
||||
parsePrefixMap :: Options.Applicative.ReadM PrefixMap
|
||||
parsePrefixMap :: Options.Applicative.ReadM (Data.Map.Map Prefix Dhall.Import)
|
||||
parsePrefixMap =
|
||||
Options.Applicative.eitherReader $ \s ->
|
||||
bimap errorBundlePretty Data.Map.fromList $ result (pack s)
|
||||
@ -72,9 +74,8 @@ parsePrefixMap =
|
||||
char '='
|
||||
e <- Dhall.Parser.expr
|
||||
imp <- parseImport prefix e
|
||||
optional $ char ','
|
||||
return (pack prefix, imp)
|
||||
result = parse (some (Dhall.Parser.unParser parser)) "MAPPING"
|
||||
result = parse (Dhall.Parser.unParser parser `sepBy1` char ',') "MAPPING"
|
||||
|
||||
parseOptions :: Options.Applicative.Parser Options
|
||||
parseOptions = Options <$> parseSkip <*> parsePrefixMap' <*> fileArg
|
||||
@ -106,17 +107,17 @@ parserInfoOptions =
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
options <- Options.Applicative.execParser parserInfoOptions
|
||||
let duplicateHandler = if skipDuplicates options then skipDuplicatesHandler else errorOnDuplicateHandler
|
||||
Options{..} <- Options.Applicative.execParser parserInfoOptions
|
||||
let duplicateHandler = if skipDuplicates then skipDuplicatesHandler else errorOnDuplicateHandler
|
||||
-- Get the Swagger spec
|
||||
Swagger{..} <- do
|
||||
swaggerFile <- decodeFileStrict $ filename options
|
||||
swaggerFile <- decodeFileStrict filename
|
||||
case swaggerFile of
|
||||
Nothing -> error "Unable to decode the Swagger file"
|
||||
Just s -> pure s
|
||||
|
||||
-- Convert to Dhall types in a Map
|
||||
let types = Convert.toTypes (prefixMap options)
|
||||
let types = Convert.toTypes prefixMap
|
||||
-- TODO: find a better way to deal with this cyclic import
|
||||
$ Data.Map.adjust patchCyclicImports
|
||||
(ModelName "io.k8s.apiextensions-apiserver.pkg.apis.apiextensions.v1beta1.JSONSchemaProps")
|
||||
@ -129,7 +130,7 @@ main = do
|
||||
writeDhall path expr
|
||||
|
||||
-- Convert from Dhall types to defaults
|
||||
let defaults = Data.Map.mapMaybeWithKey (Convert.toDefault (prefixMap options) definitions types) types
|
||||
let defaults = Data.Map.mapMaybeWithKey (Convert.toDefault prefixMap definitions types) types
|
||||
|
||||
-- Output to defaults
|
||||
Turtle.mktree "defaults"
|
||||
@ -139,8 +140,8 @@ main = do
|
||||
|
||||
let toSchema (ModelName key) _ _ =
|
||||
Dhall.RecordLit
|
||||
[ ("Type", Dhall.Embed (Convert.mkImport (prefixMap options) ["types", ".."] (key <> ".dhall")))
|
||||
, ("default", Dhall.Embed (Convert.mkImport (prefixMap options) ["defaults", ".."] (key <> ".dhall")))
|
||||
[ ("Type", Dhall.Embed (Convert.mkImport prefixMap ["types", ".."] (key <> ".dhall")))
|
||||
, ("default", Dhall.Embed (Convert.mkImport prefixMap ["defaults", ".."] (key <> ".dhall")))
|
||||
]
|
||||
|
||||
let schemas = Data.Map.intersectionWithKey toSchema types defaults
|
||||
@ -152,7 +153,7 @@ main = do
|
||||
writeDhall path expr
|
||||
|
||||
-- Output the types record, the defaults record, and the giant union type
|
||||
let getImportsMap = Convert.getImportsMap (prefixMap options) duplicateHandler objectNames
|
||||
let getImportsMap = Convert.getImportsMap prefixMap duplicateHandler objectNames
|
||||
objectNames = Data.Map.keys types
|
||||
typesMap = getImportsMap "types" $ Data.Map.keys types
|
||||
defaultsMap = getImportsMap "defaults" $ Data.Map.keys defaults
|
||||
|
Loading…
Reference in New Issue
Block a user