Change HashMap to Map, clean up

This commit is contained in:
Filip Gralinski 2018-11-03 10:56:58 +01:00
parent 1ff7c34714
commit d3e2c06b15
4 changed files with 27 additions and 26 deletions

View File

@ -22,7 +22,7 @@ import System.Exit
import System.Process
import qualified Data.Set as S
import qualified Data.HashMap.Strict as H
import qualified Data.Map.Strict as M
import Handler.Shared (gitPath)
@ -31,7 +31,7 @@ import "Glob" System.FilePath.Glob as G
data ExtractionOptions = ExtractionOptions {
extractionOptionsDescription :: Maybe Text,
extractionOptionsTags :: Maybe Text,
extractionOptionsGeneralParams :: Maybe (H.HashMap Text Text),
extractionOptionsGeneralParams :: Maybe (M.Map Text Text),
extractionOptionsUnwantedParams :: Maybe [Text],
extractionOptionsParamFiles :: Maybe [String],
extractionOptionsMLRunPath :: Maybe FilePath
@ -59,7 +59,7 @@ instance Default ExtractionOptions where
data GonitoMetadata = GonitoMetadata {
gonitoMetadataDescription :: Text,
gonitoMetadataTags :: S.Set Text,
gonitoMetadataGeneralParams :: H.HashMap Text Text
gonitoMetadataGeneralParams :: M.Map Text Text
}
deriving (Eq, Show)
@ -75,9 +75,9 @@ combineExtractionOptions Nothing options = options
combineExtractionOptions (Just otherOptions) options = ExtractionOptions {
extractionOptionsDescription = combineWithT extractionOptionsDescription,
extractionOptionsTags = combineWithT extractionOptionsTags,
extractionOptionsGeneralParams = Just $ (fromMaybe H.empty $ extractionOptionsGeneralParams options)
`H.union`
(fromMaybe H.empty $ extractionOptionsGeneralParams otherOptions),
extractionOptionsGeneralParams = Just $ (fromMaybe M.empty $ extractionOptionsGeneralParams options)
`M.union`
(fromMaybe M.empty $ extractionOptionsGeneralParams otherOptions),
extractionOptionsUnwantedParams = Just $ (fromMaybe [] $ extractionOptionsUnwantedParams options)
++
(fromMaybe [] $ extractionOptionsUnwantedParams otherOptions),
@ -112,17 +112,16 @@ extractMetadataFromRepoDir repoDir formExtractionOptions = do
let tagsParsed = union commitTagsParsed formTagsParsed
paramFiles <- case extractionOptionsParamFiles extractionOptions of
Just paramFilesGlobs -> G.globDir (Import.map G.compile $ traceShowId paramFilesGlobs) repoDir
Just paramFilesGlobs -> G.globDir (Import.map G.compile paramFilesGlobs) repoDir
Nothing -> pure []
params' <- H.unions <$> (mapM parseParamFile
$ traceShowId
params' <- M.unions <$> (mapM parseParamFile
$ Import.filter (/= (repoDir </> gonitoYamlFile))
$ Import.concat paramFiles)
let params =
Import.foldl' (flip H.delete) params' (fromMaybe [] $ extractionOptionsUnwantedParams extractionOptions)
`H.union`
fromMaybe H.empty (extractionOptionsGeneralParams extractionOptions)
Import.foldl' (flip M.delete) params' (fromMaybe [] $ extractionOptionsUnwantedParams extractionOptions)
`M.union`
fromMaybe M.empty (extractionOptionsGeneralParams extractionOptions)
pure $ GonitoMetadata {
gonitoMetadataDescription = description,
@ -131,18 +130,18 @@ extractMetadataFromRepoDir repoDir formExtractionOptions = do
}
parseParamFile :: FilePath -> IO (H.HashMap Text Text)
parseParamFile :: FilePath -> IO (M.Map Text Text)
parseParamFile yamlFile = do
decoded <- Y.decodeFileEither yamlFile
return $ case decoded of
Left _ -> H.empty
Left _ -> M.empty
Right h -> enforceTextHash h
enforceTextHash :: H.HashMap Text Value -> H.HashMap Text Text
enforceTextHash h = H.fromList
enforceTextHash :: M.Map Text Value -> M.Map Text Text
enforceTextHash h = M.fromList
$ Import.map (\(p, pv) -> (p, strip $ DTE.decodeUtf8 $ Y.encode pv))
$ H.toList h
$ M.toList h
getLastCommitMessage :: FilePath -> IO (Maybe Text)
getLastCommitMessage repoDir = do

View File

@ -333,7 +333,7 @@ outsForTest repoDir submissionId testEnt@(Entity _ test) = do
outFiles <- liftIO $ outFilesForTest repoDir test
forM outFiles $ \outFile -> do
theVariant <- getVariant submissionId outFile
theVariant <- getVariant submissionId M.empty outFile
outForTest repoDir outFile theVariant testEnt
-- returns the filename (not file path)
@ -348,8 +348,8 @@ outFilesForTest repoDir test = do
Just outF -> return [takeFileName outF]
Nothing -> return []
getVariant :: SubmissionId -> FilePath -> Handler VariantId
getVariant submissionId outFilePath = runDB $ do
getVariant :: SubmissionId -> M.Map Text Text -> FilePath -> Handler VariantId
getVariant submissionId generalParams outFilePath = runDB $ do
let outFile = takeFileName outFilePath
let name = Data.Text.pack $ dropExtensions outFile
maybeVariant <- getBy $ UniqueVariantSubmissionName submissionId name
@ -358,9 +358,11 @@ getVariant submissionId outFilePath = runDB $ do
Nothing -> do
vid <- insert $ Variant submissionId name
let (OutputFileParsed _ paramMap) = parseParamsFromFilePath outFile
forM_ (M.toList paramMap) $ \(param, val) -> do
forM_ (M.toList (paramMap `M.union` generalParams)) $ \(param, val) -> do
_ <- insert $ Parameter vid param val
return ()
return vid
checkOrInsertOut :: Out -> Handler ()

View File

@ -5,7 +5,7 @@ module Gonito.ExtractMetadataSpec (spec) where
import Import
import qualified Data.Set as S
import qualified Data.HashMap.Strict as H
import qualified Data.Map.Strict as M
import Test.Hspec
import Gonito.ExtractMetadata (extractMetadataFromRepoDir, GonitoMetadata(..), ExtractionOptions(..))
@ -17,7 +17,7 @@ spec = do
extractMetadataFromRepoDir "test/fake-git-repos/simple/" def `shouldReturn` GonitoMetadata {
gonitoMetadataDescription = "Simple solution",
gonitoMetadataTags = S.fromList ["foo", "simple-solution", "baz"],
gonitoMetadataGeneralParams = H.empty
gonitoMetadataGeneralParams = M.empty
}
it "simple with some fields from the form" $ do
extractMetadataFromRepoDir "test/fake-git-repos/simple/" def {
@ -26,13 +26,13 @@ spec = do
} `shouldReturn` GonitoMetadata {
gonitoMetadataDescription = "Other solution",
gonitoMetadataTags = S.fromList ["foo", "simple-solution", "baz", "other-tag"],
gonitoMetadataGeneralParams = H.empty
gonitoMetadataGeneralParams = M.empty
}
it "with gonito.yaml" $ do
extractMetadataFromRepoDir "test/fake-git-repos/with-gonito-yaml/" def `shouldReturn` GonitoMetadata {
gonitoMetadataDescription = "Test solution",
gonitoMetadataTags = S.fromList ["zzz", "baz", "simple", "machine-learning"],
gonitoMetadataGeneralParams = H.fromList [("level", "4"),
gonitoMetadataGeneralParams = M.fromList [("level", "4"),
("altitude", "8900.3"),
("q", "10.4"),
("style", "bold")]

@ -1 +1 @@
Subproject commit e54aa938430ccf03383d84a435183aa8d8f64a8b
Subproject commit a71de45eacb63081b2b39db09c48ad3d848c2541