Skip to content

Commit

Permalink
Merge pull request #42 from joneshf/fix-record-based-set-operations
Browse files Browse the repository at this point in the history
Fix behavior of `Option.set'`/`Option.recordSet`
  • Loading branch information
joneshf committed Jan 26, 2021
2 parents 55bf251 + 6710eda commit ee3090d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
4 changes: 1 addition & 3 deletions src/Option.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2086,9 +2086,7 @@ else instance setOptionConsMaybe ::
Option option
setOption _ record oldOption = case value' of
Data.Maybe.Just value -> insert label value option
Data.Maybe.Nothing -> case get label oldOption of
Data.Maybe.Just value -> insert label value option
Data.Maybe.Nothing -> insertField label option
Data.Maybe.Nothing -> insertField label option
where
label :: Data.Symbol.SProxy label
label = Data.Symbol.SProxy
Expand Down
8 changes: 4 additions & 4 deletions test/Test.Option.purs
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,14 @@ spec_recordSet =
anotherRecord :: Option.Record ( foo :: Boolean ) ( bar :: Int )
anotherRecord = Option.recordSet { bar: Data.Maybe.Just 31 } someRecord
Option.recordToRecord anotherRecord `Test.Spec.Assertions.shouldEqual` { bar: Data.Maybe.Just 31, foo: false }
Test.Spec.it "`Data.Maybe.Nothing` keeps the previous value" do
Test.Spec.it "`Data.Maybe.Nothing` removes the previous value" do
let
someRecord :: Option.Record ( foo :: Boolean ) ( bar :: Int )
someRecord = Option.recordFromRecord { bar: 31, foo: false }

anotherRecord :: Option.Record ( foo :: Boolean ) ( bar :: Int )
anotherRecord = Option.recordSet { bar: Data.Maybe.Nothing } someRecord
Option.recordToRecord anotherRecord `Test.Spec.Assertions.shouldEqual` { bar: Data.Maybe.Just 31, foo: false }
Option.recordToRecord anotherRecord `Test.Spec.Assertions.shouldEqual` { bar: Data.Maybe.Nothing, foo: false }
Test.Spec.it "can set both required and optional values" do
let
someRecord :: Option.Record ( foo :: Boolean ) ( bar :: Int )
Expand Down Expand Up @@ -376,11 +376,11 @@ spec_set' =
anotherOption :: Option.Option ( foo :: Boolean, bar :: Int )
anotherOption = Option.set' { bar: Data.Maybe.Just 31 } someOption
Option.get (Proxy :: _ "bar") anotherOption `Test.Spec.Assertions.shouldEqual` Data.Maybe.Just 31
Test.Spec.it "`Data.Maybe.Nothing` keeps the previous value" do
Test.Spec.it "`Data.Maybe.Nothing` removes the previous value" do
let
someOption :: Option.Option ( foo :: Boolean, bar :: Int )
someOption = Option.fromRecord { bar: 31 }

anotherOption :: Option.Option ( foo :: Boolean, bar :: Int )
anotherOption = Option.set' { bar: Data.Maybe.Nothing } someOption
Option.get (Proxy :: _ "bar") anotherOption `Test.Spec.Assertions.shouldEqual` Data.Maybe.Just 31
Option.get (Proxy :: _ "bar") anotherOption `Test.Spec.Assertions.shouldEqual` Data.Maybe.Nothing

0 comments on commit ee3090d

Please sign in to comment.