Skip to content

Commit

Permalink
Extracted JSON manipulation into Util. (wasp-lang#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
matijaSos authored Jan 15, 2020
1 parent ab70bbc commit cf8ad33
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,4 @@ tests:
- tasty-discover
- QuickCheck
- parsec
- deepseq
6 changes: 2 additions & 4 deletions src/Generator/EntityGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import Data.Aeson ((.=), object, toJSON)
import qualified Data.Aeson as Aeson
import qualified Data.Text as Text
import System.FilePath (FilePath, (</>), (<.>))
import qualified Data.HashMap.Strict as M

import qualified Util
import Wasp
Expand Down Expand Up @@ -98,9 +97,8 @@ generateEntityCreateForm wasp entityForm =
templateSrcPath = entityTemplatesDirPath </> "components" </> "CreateForm.js"
dstPath = "src" </> (entityCreateFormPathInSrc entity entityForm)

-- TODO(matija): extract this into a util function.
Aeson.Object entityData = entityTemplateData wasp entity
templateData = Aeson.Object (M.insert "entityForm" (toJSON entityForm) entityData)
entityTemplateJson = entityTemplateData wasp entity
templateData = Util.jsonSet "entityForm" (toJSON entityForm) entityTemplateJson

-- | Generates creation forms for the given entity.
generateEntityCreateForms :: Wasp -> Entity -> [FileDraft]
Expand Down
7 changes: 7 additions & 0 deletions src/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ module Util
, toLowerFirst
, toUpperFirst
, headSafe
, jsonSet
) where

import Data.Char (isUpper, toLower, toUpper)
import qualified Data.Aeson as Aeson
import qualified Data.Text as Text
import qualified Data.HashMap.Strict as M


camelToKebabCase :: String -> String
Expand Down Expand Up @@ -35,3 +38,7 @@ toUpperFirst = onFirst toUpper
headSafe :: [a] -> Maybe a
headSafe [] = Nothing
headSafe xs = Just (head xs)

jsonSet :: Text.Text -> Aeson.Value -> Aeson.Value -> Aeson.Value
jsonSet key value (Aeson.Object o) = Aeson.Object $ M.insert key value o
jsonSet _ _ _ = error "Input JSON must be an object"
30 changes: 30 additions & 0 deletions test/UtilTest.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
module UtilTest where

import Test.Tasty.Hspec
import Control.Exception (evaluate)
import Control.DeepSeq

import qualified Data.Aeson as Aeson
import Data.Aeson ((.=), object, toJSON)

import Util

Expand Down Expand Up @@ -32,3 +37,28 @@ spec_toUpperFirst :: Spec
spec_toUpperFirst = do
it "Capitalizes first letter of string" $ do
toUpperFirst "fooBar" `shouldBe` "FooBar"

spec_jsonSet :: Spec
spec_jsonSet = do
let inputObj = object
[ "prop1" .= ("first" :: String)
]

it "When input JSON is not an object, throws an error." $ do
(evaluate . force) (jsonSet "someProp" (Aeson.Number 23) (Aeson.Bool True))
`shouldThrow` errorCall "Input JSON must be an object"

it "When a new property is set, result object contains it along with the original ones." $ do
let expectedObj = object
[ "prop1" .= ("first" :: String)
, "newProp" .= (23 :: Int)
]
(jsonSet "newProp" (Aeson.Number 23) inputObj) `shouldBe` expectedObj

it "When an existing property is set, it is overwritten in the result object." $ do
let newStrValue = "newVal" :: String
let expectedObj = object
[ "prop1" .= newStrValue
]
(jsonSet "prop1" (toJSON newStrValue) inputObj) `shouldBe` expectedObj

0 comments on commit cf8ad33

Please sign in to comment.