Skip to content

Commit

Permalink
Added InputValue Aeson.Value instance for JSON input fields. Fixes #594
Browse files Browse the repository at this point in the history
  • Loading branch information
mpscholten committed Dec 11, 2020
1 parent 8908fd6 commit 01410d2
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
3 changes: 3 additions & 0 deletions IHP/ModelSupport.hs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ instance InputValue fieldType => InputValue (Maybe fieldType) where
instance InputValue value => InputValue [value] where
inputValue list = list |> map inputValue |> intercalate ","

instance InputValue Aeson.Value where
inputValue json = json |> Aeson.encode |> cs

instance Default Text where
{-# INLINE def #-}
def = ""
Expand Down
4 changes: 3 additions & 1 deletion Test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import qualified Test.View.CSSFrameworkSpec
import qualified Test.Controller.ContextSpec
import qualified Test.Controller.ParamSpec
import qualified Test.SchemaMigrationSpec
import qualified Test.ModelSupportSpec

main :: IO ()
main = hspec do
Expand All @@ -48,4 +49,5 @@ main = hspec do
Test.View.CSSFrameworkSpec.tests
Test.Controller.ContextSpec.tests
Test.Controller.ParamSpec.tests
Test.SchemaMigrationSpec.tests
Test.SchemaMigrationSpec.tests
Test.ModelSupportSpec.tests
79 changes: 79 additions & 0 deletions Test/ModelSupportSpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{-|
Module: Test.HaskellSupportSpec
Copyright: (c) digitally induced GmbH, 2020
-}
module Test.ModelSupportSpec where

import Test.Hspec
import IHP.Prelude
import IHP.ModelSupport
import qualified Data.Aeson as Aeson

tests = do
describe "ModelSupport" do
describe "InputValue" do
describe "Text" do
it "should return the text" do
let text :: Text = "Lorem Ipsum"
(inputValue text) `shouldBe` text

describe "Int" do
it "should return numeric representation" do
(inputValue (1 :: Int)) `shouldBe` "1"

describe "Integer" do
it "should return numeric representation" do
(inputValue (1 :: Int)) `shouldBe` "1"

describe "Double" do
it "should return numeric representation" do
(inputValue (1.337 :: Double)) `shouldBe` "1.337"

describe "Float" do
it "should return numeric representation" do
(inputValue (1.337 :: Float)) `shouldBe` "1.337"

describe "Bool" do
it "should deal with True and False" do
(inputValue True) `shouldBe` "on"
(inputValue False) `shouldBe` "off"

describe "UUID" do
it "should return text representation" do
let uuid :: UUID = "b9a1129e-e53a-4370-b778-0d1c28dc6ecc"
(inputValue uuid) `shouldBe` "b9a1129e-e53a-4370-b778-0d1c28dc6ecc"

describe "UTCTime" do
it "should return text representation" do
let (Just utctime) :: Maybe UTCTime = parseTimeM True defaultTimeLocale "%Y-%m-%dT%H:%M:%S%QZ" "2020-11-08T12:03:35Z"
(inputValue utctime) `shouldBe` "2020-11-08T12:03:35Z"

describe "Day" do
it "should return text representation" do
let (Just utctime) :: Maybe UTCTime = parseTimeM True defaultTimeLocale "%Y-%m-%dT%H:%M:%S%QZ" "2020-11-08T12:03:35Z"
let day :: Day = utctDay utctime
(inputValue day) `shouldBe` "2020-11-08"

describe "Maybe" do
it "should return empty string on Nothing" do
let value :: Maybe Int = Nothing
(inputValue value) `shouldBe` ""

it "should return the value if Just" do
let value :: Maybe Int = Just 1
(inputValue value) `shouldBe` "1"

describe "[a]" do
it "should return CSV" do
let value :: [Int] = [1, 2, 3]
(inputValue value) `shouldBe` "1,2,3"

it "should return empty string on empty list" do
let value :: [Int] = []
(inputValue value) `shouldBe` ""

describe "JSON" do
it "should return a json string for a Aeson.Value" do
let (Just value) :: Maybe Aeson.Value = Aeson.decode "{\"hello\":true}"
(inputValue value) `shouldBe` "{\"hello\":true}"

0 comments on commit 01410d2

Please sign in to comment.