Skip to content

Commit

Permalink
AText,AInt -> Text,Integer
Browse files Browse the repository at this point in the history
  • Loading branch information
kanghyojun committed May 12, 2018
1 parent 9a5a452 commit e01f299
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 38 deletions.
6 changes: 3 additions & 3 deletions src/Nirum/Constructs/Annotation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import Nirum.Constructs.Identifier (Identifier)
docs :: Docs -> Annotation
docs (Docs d) = Annotation { name = docsAnnotationName
, arguments =
M.singleton docsAnnotationParameter $ AText d
M.singleton docsAnnotationParameter $ Text d
}

newtype NameDuplication = AnnotationNameDuplication Identifier
Expand Down Expand Up @@ -80,7 +80,7 @@ lookupDocs annotationSet = do
Annotation _ args <- lookup docsAnnotationName annotationSet
d <- M.lookup docsAnnotationParameter args
case d of
AText d' -> Just $ Docs d'
Text d' -> Just $ Docs d'
_ -> Nothing

insertDocs :: (Monad m) => Docs -> AnnotationSet -> m AnnotationSet
Expand All @@ -92,4 +92,4 @@ insertDocs docs' (AnnotationSet anno) =
insertLookup :: Ord k => k -> a -> M.Map k a -> (Maybe a, M.Map k a)
insertLookup = M.insertLookupWithKey $ \ _ a _ -> a
args :: AnnotationArgumentSet
args = M.singleton docsAnnotationParameter $ AText $ toText docs'
args = M.singleton docsAnnotationParameter $ Text $ toText docs'
6 changes: 3 additions & 3 deletions src/Nirum/Constructs/Annotation/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Nirum.Constructs.Annotation.Internal
, name
)
, AnnotationArgument ( Text
, Int
, Integer
)
, AnnotationArgumentSet
, AnnotationSet ( AnnotationSet
Expand All @@ -24,7 +24,7 @@ import Nirum.Constructs.Docs
import Nirum.Constructs.Identifier (Identifier)

data AnnotationArgument = Text T.Text
| Int Integer
| Integer Integer
deriving (Eq, Ord, Show)

type AnnotationArgumentSet = M.Map Identifier AnnotationArgument
Expand All @@ -45,7 +45,7 @@ instance Construct Annotation where
showArg (key, value) = [qq|{toCode key} = {argToText value}|]
argToText :: AnnotationArgument -> T.Text
argToText (Text t) = literal t
argToText (Int i) = T.pack $ show i
argToText (Integer i) = T.pack $ show i
literal :: T.Text -> T.Text
literal s = [qq|"{(showLitString $ T.unpack s) ""}"|]
showLitString :: String -> ShowS
Expand Down
22 changes: 16 additions & 6 deletions src/Nirum/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ import Text.Megaparsec.Char.Lexer (charLiteral)
import Text.Read hiding (choice)

import qualified Nirum.Constructs.Annotation as A
import Nirum.Constructs.Annotation.Internal hiding (annotations, name)
import Nirum.Constructs.Annotation.Internal hiding ( Text
, annotations
, name
)
import qualified Nirum.Constructs.Annotation.Internal as AI
import Nirum.Constructs.Declaration (Declaration)
import qualified Nirum.Constructs.Declaration as D
import Nirum.Constructs.Docs (Docs (Docs))
Expand Down Expand Up @@ -165,18 +169,24 @@ uniqueName forwardNames label' = try $ do
nameP :: Parser Name
nameP = name <?> label'

integer :: Parser Integer
integer = do
v <- many digitChar
case readMaybe v of
Just i -> return i
Nothing -> fail "digit expected." -- never happened


annotationArgumentValue :: Parser AnnotationArgument
annotationArgumentValue = do
startQuote <- optional $ try $ char '"'
case startQuote of
Just _ -> do
v <- manyTill charLiteral (char '"')
return $ Text $ T.pack v
return $ AI.Text $ T.pack v
Nothing -> do
v <- many digitChar
case readMaybe v of
Just i -> return $ Int i
Nothing -> fail "digit expected"
v <- integer
return $ Integer v

annotationArgument :: Parser (Identifier, AnnotationArgument)
annotationArgument = do
Expand Down
7 changes: 4 additions & 3 deletions src/Nirum/Targets/Python.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ import Text.Heterocephalus (compileText)
import Text.InterpolatedString.Perl6 (q, qq)

import qualified Nirum.Constructs.Annotation as A
import Nirum.Constructs.Annotation.Internal hiding (annotations, name)
import Nirum.Constructs.Annotation.Internal hiding (Text, annotations, name)
import qualified Nirum.Constructs.Annotation.Internal as AI
import qualified Nirum.Constructs.DeclarationSet as DS
import qualified Nirum.Constructs.Identifier as I
import Nirum.Constructs.Declaration (Documented (docsBlock))
Expand Down Expand Up @@ -1205,8 +1206,8 @@ if hasattr({className}.Client, '__qualname__'):
escapeSingle :: T.Text -> T.Text
escapeSingle = T.strip . T.replace "'" "\\'"
annoArgToText :: AnnotationArgument -> T.Text
annoArgToText (Text t) = [qq|u'''{escapeSingle t}'''|]
annoArgToText (Int i) = T.pack $ show i
annoArgToText (AI.Text t) = [qq|u'''{escapeSingle t}'''|]
annoArgToText (Integer i) = T.pack $ show i
compileMethodAnnotation :: Method -> T.Text
compileMethodAnnotation Method { methodName = mName
, methodAnnotations = annoSet
Expand Down
22 changes: 11 additions & 11 deletions test/Nirum/Constructs/AnnotationSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import Nirum.Constructs.Annotation.Internal
spec :: Spec
spec = do
let annotation = Annotation "foo" M.empty
loremAnno = Annotation "lorem" [("arg", AText "ipsum")]
escapeCharAnno = Annotation "quote" [("arg", AText "\"")]
loremAnno = Annotation "lorem" [("arg", Text "ipsum")]
escapeCharAnno = Annotation "quote" [("arg", Text "\"")]
longNameAnno =
Annotation "long-cat-is-long" [("long", AText "nyancat")]
Annotation "long-cat-is-long" [("long", Text "nyancat")]
docsAnno = docs "Description"
describe "Annotation" $ do
describe "toCode Annotation" $
Expand All @@ -25,15 +25,15 @@ spec = do
toCode escapeCharAnno `shouldBe` "@quote(arg = \"\\\"\")"
specify "docs" $
docsAnno `shouldBe`
Annotation "docs" [("docs", AText "Description\n")]
Annotation "docs" [("docs", Text "Description\n")]
describe "AnnotationSet" $ do
specify "empty" $
empty `shouldSatisfy` null
specify "singleton" $ do
singleton (Annotation "foo" []) `shouldBe`
AnnotationSet [("foo", [])]
singleton (Annotation "bar" [("arg", AText "baz")]) `shouldBe`
AnnotationSet [("bar", [("arg", AText "baz")])]
singleton (Annotation "bar" [("arg", Text "baz")]) `shouldBe`
AnnotationSet [("bar", [("arg", Text "baz")])]
describe "fromList" $ do
it "success" $ do
let Right empty' = fromList []
Expand All @@ -50,12 +50,12 @@ spec = do
specify "union" $ do
let Right a = fromList [annotation, loremAnno]
let Right b = fromList [docsAnno, escapeCharAnno]
let c = AnnotationSet [("foo", [("arg", AText "bar")])]
let c = AnnotationSet [("foo", [("arg", Text "bar")])]
A.union a b `shouldBe`
AnnotationSet [ ("foo", [])
, ("lorem", [("arg", AText "ipsum")])
, ("quote", [("arg", AText "\"")])
, ("docs", [("docs", AText "Description\n")])
, ("lorem", [("arg", Text "ipsum")])
, ("quote", [("arg", Text "\"")])
, ("docs", [("docs", Text "Description\n")])
]
A.union a c `shouldBe` a
let Right annotationSet = fromList [ annotation
Expand Down Expand Up @@ -86,6 +86,6 @@ spec = do
describe "insertDocs" $ do
it "should insert the doc comment as an annotation" $
A.insertDocs "yay" empty `shouldReturn`
AnnotationSet [("docs", [("docs", AText "yay\n")])]
AnnotationSet [("docs", [("docs", Text "yay\n")])]
it "should fail on the annotation that already have a doc" $
A.insertDocs "yay" annotationSet `shouldThrow` anyException
4 changes: 2 additions & 2 deletions test/Nirum/Constructs/ServiceSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import Util (singleDocs)
spec :: Spec
spec = do
let methodAnno = singleton $ Annotation "http" $ Map.fromList
[ ("method", AText "GET")
, ("path", AText "/ping/")
[ ("method", Text "GET")
, ("path", Text "/ping/")
]
let docsAnno = singleDocs "docs..."
describe "Parameter" $
Expand Down
4 changes: 2 additions & 2 deletions test/Nirum/Constructs/TypeDeclarationSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Test.Hspec.Meta

import Nirum.Constructs (Construct (toCode))
import Nirum.Constructs.Annotation hiding (docs, fromList, name)
import Nirum.Constructs.Annotation.Internal hiding (name)
import qualified Nirum.Constructs.Annotation.Internal as AI
import Nirum.Constructs.Declaration (Declaration (name), docs)
import Nirum.Constructs.DeclarationSet hiding (empty)
import Nirum.Constructs.Service (Method (Method), Service (Service))
Expand All @@ -24,7 +24,7 @@ import Nirum.Constructs.TypeDeclaration ( EnumMember (EnumMember)
import Util (singleDocs)

barAnnotationSet :: AnnotationSet
barAnnotationSet = singleton $ Annotation "bar" [("val", AText "baz")]
barAnnotationSet = singleton $ Annotation "bar" [("val", AI.Text "baz")]

spec :: Spec
spec = do
Expand Down
17 changes: 9 additions & 8 deletions test/Nirum/ParserSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import qualified Nirum.Parser as P
import Nirum.Parser (Parser, ParseError)
import Nirum.Constructs (Construct (toCode))
import Nirum.Constructs.Annotation as A
import Nirum.Constructs.Annotation.Internal
import Nirum.Constructs.Annotation.Internal hiding (Text)
import qualified Nirum.Constructs.Annotation.Internal as AI
import Nirum.Constructs.Docs (Docs (Docs))
import Nirum.Constructs.DeclarationSet (DeclarationSet)
import Nirum.Constructs.DeclarationSetSpec (SampleDecl (..))
Expand Down Expand Up @@ -81,7 +82,7 @@ helperFuncs parser =


fooAnnotationSet :: AnnotationSet
fooAnnotationSet = A.singleton $ Annotation "foo" [("v", Text "bar")]
fooAnnotationSet = A.singleton $ Annotation "foo" [("v", AI.Text "bar")]

bazAnnotationSet :: AnnotationSet
bazAnnotationSet = A.singleton $ Annotation "baz" []
Expand Down Expand Up @@ -179,9 +180,9 @@ spec = do
let (parse', expectError) = helperFuncs P.annotation
context "with single argument" $ do
let rightAnnotaiton =
Annotation "name-abc" [("foo", Text "wo\"rld")]
Annotation "name-abc" [("foo", AI.Text "wo\"rld")]
let rightIntAnnotation =
Annotation "name-abc" [("foo", Int 1)]
Annotation "name-abc" [("foo", Integer 1)]
it "success" $ do
parse' "@name-abc(foo=\"wo\\\"rld\")"
`shouldBeRight` rightAnnotaiton
Expand All @@ -196,7 +197,7 @@ spec = do
parse' "@name-abc ( foo=\"wo\\\"rld\")"
`shouldBeRight` rightAnnotaiton
parse' "@name-abc(foo=\"wo\\\"rld\\n\")" `shouldBeRight`
Annotation "name-abc" [("foo", Text "wo\"rld\n")]
Annotation "name-abc" [("foo", AI.Text "wo\"rld\n")]
parse' "@name-abc(foo=1)" `shouldBeRight` rightIntAnnotation
parse' "@name-abc( foo=1)" `shouldBeRight` rightIntAnnotation
parse' "@name-abc(foo=1 )" `shouldBeRight` rightIntAnnotation
Expand Down Expand Up @@ -226,7 +227,7 @@ spec = do
describe "annotationSet" $ do
let (parse', expectError) = helperFuncs P.annotationSet
Right annotationSet = fromList
[ Annotation "a" [("arg", Text "b")]
[ Annotation "a" [("arg", AI.Text "b")]
, Annotation "c" []
]
it "success" $ do
Expand Down Expand Up @@ -830,8 +831,8 @@ union shape
describe "method" $ do
let (parse', expectError) = helperFuncs P.method
httpGetAnnotation = singleton $ Annotation "http"
[ ("method", Text "GET")
, ("path", Text "/get-name/")
[ ("method", AI.Text "GET")
, ("path", AI.Text "/get-name/")
]
it "emits Method if succeeded to parse" $ do
parse' "text get-name()" `shouldBeRight`
Expand Down

0 comments on commit e01f299

Please sign in to comment.