Skip to content

Commit

Permalink
Change keywords metadata from string to array
Browse files Browse the repository at this point in the history
  • Loading branch information
earlbread committed Oct 17, 2017
1 parent bbf9677 commit 443315a
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 16 deletions.
29 changes: 27 additions & 2 deletions src/Nirum/Package/Metadata.hs
Expand Up @@ -78,6 +78,7 @@ import Text.Toml.Types (Node ( VArray
)
, Table
, VTArray
, VArray
)
import Text.URI (URI, parseURI)

Expand All @@ -104,7 +105,7 @@ data Metadata t =
Metadata { version :: SV.Version
, description :: Maybe Text
, license :: Maybe Text
, keywords :: Maybe Text
, keywords :: [Text]
, authors :: [Author]
, target :: (Eq t, Ord t, Show t, Target t) => t
}
Expand Down Expand Up @@ -186,7 +187,7 @@ parseMetadata metadataPath' tomlText = do
authors' <- authorsField "authors" table
description' <- optional $ stringField "description" table
license' <- optional $ stringField "license" table
keywords' <- optional $ stringField "keywords" table
keywords' <- textArrayField "keywords" table
targets <- case tableField "targets" table of
Left (FieldError _) -> Right HM.empty
otherwise' -> otherwise'
Expand Down Expand Up @@ -269,6 +270,20 @@ stringField = typedField "string" $ \ n -> case n of
VString s -> Just s
_ -> Nothing

arrayField :: MetadataField -> Table -> Either MetadataError VArray
arrayField f t =
case arrayF f t of
Right vector -> Right vector
Left (FieldError _) -> Right $ fromList []
Left error' -> Left error'
where
arrayF :: MetadataField -> Table -> Either MetadataError VArray
arrayF = typedField "array" $ \ node ->
case node of
VArray array -> Just array
_ -> Nothing


tableArrayField :: MetadataField -> Table -> Either MetadataError VTArray
tableArrayField f t =
case arrayF f t of
Expand Down Expand Up @@ -306,6 +321,16 @@ versionField field' table = do
Left _ -> Left $ FieldValueError field' $
"expected a semver string (e.g. \"1.2.3\"), not " ++ show s

textArrayField :: MetadataField -> Table -> Either MetadataError [Text]
textArrayField field' table = do
array <- arrayField field' table
textArray' <- mapM parseText array
return $ toList textArray'
where
parseText :: Node -> Either MetadataError Text
parseText (VString s) = Right s
parseText a = Left $ FieldTypeError field' "array" $ fieldType a

authorsField :: MetadataField -> Table -> Either MetadataError [Author]
authorsField field' table = do
array <- tableArrayField field' table
Expand Down
14 changes: 8 additions & 6 deletions src/Nirum/Targets/Python.hs
Expand Up @@ -1209,9 +1209,9 @@ setup(
where
target' :: Python
target' = target metadata'
csStrings :: [T.Text] -> T.Text
csStrings [] = "None"
csStrings s = stringLiteral $ T.intercalate ", " s
csStrings :: T.Text -> [T.Text] -> T.Text
csStrings _ [] = "None"
csStrings d s = stringLiteral $ T.intercalate d s
pName :: Code
pName = packageName $ target metadata'
pVersion :: Code
Expand All @@ -1225,13 +1225,15 @@ setup(
pLicense :: Code
pLicense = fromMaybeToMeta $ license metadata'
pKeywords :: Code
pKeywords = fromMaybeToMeta $ MD.keywords metadata'
pKeywords = csStrings " " $ MD.keywords metadata'
strings :: [Code] -> Code
strings values = T.intercalate ", " $ map stringLiteral (L.sort values)
author :: Code
author = csStrings [aName | Author { name = aName } <- authors metadata']
author = csStrings ", " [aName
| Author { name = aName } <- authors metadata'
]
authorEmail :: Code
authorEmail = csStrings [ decodeUtf8 (E.toByteString e)
authorEmail = csStrings ", " [ decodeUtf8 (E.toByteString e)
| Author { email = Just e } <- authors metadata'
]
pPackages :: Code
Expand Down
2 changes: 1 addition & 1 deletion test/Nirum/CodeBuilderSpec.hs
Expand Up @@ -40,7 +40,7 @@ package = Package { metadata = Metadata { version = SV.version 0 0 1 [] []
, authors = []
, description = Nothing
, license = Nothing
, keywords = Nothing
, keywords = []
, target = DummyTarget
}
, modules = modules'
Expand Down
6 changes: 3 additions & 3 deletions test/Nirum/Package/MetadataSpec.hs
Expand Up @@ -136,11 +136,11 @@ spec =
, "integer (123)"
)
, ( [q|version = "1.2.3"
keywords = 789
keywords = "sample example nirum"
|]
, "keywords"
, "string"
, "integer (789)"
, "array"
, "string (sample example nirum)"
)
] $ \ (toml, field, expected, actual) -> do
let Left e = parse toml
Expand Down
4 changes: 2 additions & 2 deletions test/Nirum/PackageSpec.hs
Expand Up @@ -65,7 +65,7 @@ createValidPackage t = createPackage Metadata { version = SV.initial
, authors = []
, description = Nothing
, license = Nothing
, keywords = Nothing
, keywords = []
, target = t
} validModules

Expand Down Expand Up @@ -115,7 +115,7 @@ testPackage target' = do
, authors = []
, description = Nothing
, license = Nothing
, keywords = Nothing
, keywords = []
, target = target'
}
metadata package `shouldBe` metadata'
Expand Down
2 changes: 1 addition & 1 deletion test/Nirum/Targets/PythonSpec.hs
Expand Up @@ -104,7 +104,7 @@ makeDummySource' pathPrefix m renames =
]
, description = Just "Package description"
, license = Just "MIT"
, keywords = Just "test example examples"
, keywords = ["sample", "example", "nirum"]
, target = Python "sample-package" minimumRuntime renames
}
pkg :: Package Python
Expand Down
2 changes: 1 addition & 1 deletion test/nirum_fixture/package.toml
@@ -1,7 +1,7 @@
version = "0.3.0"
description = "Package description"
license = "MIT"
keywords = "test example examples"
keywords = ["sample", "example", "nirum"]

[[authors]]
name = "nirum"
Expand Down

0 comments on commit 443315a

Please sign in to comment.