Skip to content

Commit

Permalink
Add support for JSON literals
Browse files Browse the repository at this point in the history
To support usages like `mkjson o=$(mkjson x=10)`
  • Loading branch information
mfussenegger committed Mar 13, 2019
1 parent ec3e3a6 commit 8458e90
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# mkjson

[![Build status](https://dev.azure.com/mfussenegger/mkjson/_apis/build/status/mkjson-CI?branchName=master)](https://dev.azure.com/mfussenegger/mkjson/_build/latest?definitionId=2)
[![Build status](https://dev.azure.com/mfussenegger/mkjson/_apis/build/status/mkjson%20CI?branchName=master)](https://dev.azure.com/mfussenegger/mkjson/_build/latest?definitionId=7)

`mkjson` is a CLI to generate JSON records.

Expand Down
60 changes: 48 additions & 12 deletions src/Expr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,26 @@

module Expr where

import Data.Scientific (Scientific)
import Data.String (IsString (..))
import Data.Text (Text)
import qualified Data.Text as T
import Text.Parsec (many, many1, optionMaybe, parse, sepBy,
(<|>), between)
import Text.Parsec.Char (char, digit, letter, noneOf, spaces)
import Text.Parsec.Error (ParseError)
import Text.Parsec.Text (Parser)
import Data.Aeson (Value (..), decode', encode)
import Data.Scientific (Scientific)
import Data.String (IsString (..))
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Encoding as TL
import Text.Parsec (between, many, many1, optionMaybe,
parse, sepBy, (<|>))
import Text.Parsec.Char (char, digit, letter, noneOf, spaces)
import Text.Parsec.Error (ParseError)
import Text.Parsec.Text (Parser)

-- $setup
-- >>> :set -XOverloadedStrings

data Expr = IntLiteral !Integer
| DoubleLiteral !Scientific
| StringLiteral !Text
| JsonLiteral !Value
| FunctionCall !Function
deriving (Show, Eq)

Expand All @@ -40,7 +44,33 @@ expr = literal <|> functionCall


literal :: Parser Expr
literal = number <|> stringLiteral
literal = number <|> stringLiteral <|> jsonLiteral


showExpr :: Expr -> Text
showExpr (StringLiteral s) = "\"" <> s <> "\""
showExpr (IntLiteral n) = T.pack . show $ n
showExpr (DoubleLiteral n) = T.pack . show $ n
showExpr (JsonLiteral s) = TL.toStrict . TL.decodeUtf8 $ encode s
showExpr (FunctionCall _) = error "Can only convert literals to text representation"


jsonLiteral :: Parser Expr
jsonLiteral = do
assignments <- between (char '{') (char '}') (assignment `sepBy` comma)
let
objStr = "{" <> T.intercalate ", " assignments <> "}"
case decode' (TL.encodeUtf8 . TL.fromStrict $ objStr) of
Nothing -> error $ "Invalid JSON string: " <> T.unpack objStr
Just v -> pure $ JsonLiteral v
where
assignment = do
key <- stringLiteral
_ <- colon
value <- literal
pure $ showExpr key <> ": " <> showExpr value
colon = char ':' >> spaces
comma = char ',' >> spaces


number :: Parser Expr
Expand All @@ -59,8 +89,8 @@ number = do
stringLiteral :: Parser Expr
stringLiteral = StringLiteral . T.pack <$> string
where
singleQuote = char '\''
string = between singleQuote singleQuote (many (noneOf "\'"))
quote = char '\'' <|> char '"'
string = between quote quote (many (noneOf "\'\""))


functionCall :: Parser Expr
Expand Down Expand Up @@ -106,5 +136,11 @@ ident = do
--
-- >>> parseExpr "''"
-- Right (StringLiteral "")
--
-- >>> parseExpr "{}"
-- Right (JsonLiteral (Object (fromList [])))
--
-- >>> parseExpr "{\"x\": 10, \"y\": {}}"
-- Right (JsonLiteral (Object (fromList [("x",Number 10.0),("y",Object (fromList []))])))
parseExpr :: Text -> Either ParseError Expr
parseExpr = parse expr "(unknown)"
1 change: 1 addition & 0 deletions src/Fake.hs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ eval :: Expr -> Fake Value
eval (IntLiteral x) = pure $ Number $ fromInteger x
eval (StringLiteral x) = pure $ String x
eval (DoubleLiteral x) = pure $ Number x
eval (JsonLiteral s) = pure $ s
eval (FunctionCall (Function "uuid4" [])) = String . UUID.toText <$> State.state random
eval (FunctionCall (Function "uuid1" [])) = String . UUID.toText <$> liftIO uuid1
eval (FunctionCall (Function "ulid" [])) = getUlid
Expand Down

0 comments on commit 8458e90

Please sign in to comment.