Skip to content

Commit

Permalink
Freeze records on insert/remove/update. (#2131)
Browse files Browse the repository at this point in the history
* Freeze the record before insert/remove/update

* Add tests

* Make the test evaluate
  • Loading branch information
jneem authored Dec 24, 2024
1 parent 026e43b commit 77e355a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
9 changes: 5 additions & 4 deletions core/stdlib/std.ncl
Original file line number Diff line number Diff line change
Expand Up @@ -3187,7 +3187,7 @@
# => error
```
"%%
= fun field content r => %record/insert% field r content,
= fun field content r => %record/insert% field (%record/freeze% r) content,

insert_with_opts
: forall a. String -> a -> { _ : a } -> { _ : a }
Expand Down Expand Up @@ -3221,7 +3221,7 @@
# => error
```
"%%
= fun field content r => %record/insert_with_opts% field r content,
= fun field content r => %record/insert_with_opts% field (%record/freeze% r) content,

remove
: forall a. String -> { _ : a } -> { _ : a }
Expand Down Expand Up @@ -3256,7 +3256,7 @@
# => error
```
"%
= fun field r => %record/remove% field r,
= fun field r => %record/remove% field (%record/freeze% r),

remove_with_opts
: forall a. String -> { _ : a } -> { _ : a }
Expand Down Expand Up @@ -3284,7 +3284,7 @@
# => error
```
"%
= fun field r => %record/remove_with_opts% field r,
= fun field r => %record/remove_with_opts% field (%record/freeze% r),

update
: forall a. String -> a -> { _ : a } -> { _ : a }
Expand Down Expand Up @@ -3327,6 +3327,7 @@
```
"%
= fun field content r =>
let r = %record/freeze% r in
let r =
if %record/has_field% field r then
%record/remove% field r
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# test.type = 'error'
#
# [test.metadata]
# error = 'EvalError::MissingFieldDef'
#
# [test.metadata.expectation]
# field = 'bar'
((std.record.insert "bar" 1 { foo = bar + 1, bar | optional }) & { bar | force = 2 }).foo
15 changes: 15 additions & 0 deletions core/tests/integration/inputs/records/freezing_ops.ncl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# test.type = 'pass'
[
((std.record.insert "baz" 1 { foo = bar + 1, bar = 0 }) & { bar | force = 2 }) == { foo = 1, bar = 2, baz = 1 },
((std.record.insert_with_opts "baz" 1 { foo = bar + 1, bar = 0 }) & { bar | force = 2 }) == { foo = 1, bar = 2, baz = 1 },
(
(std.record.remove "to_remove" { foo = bar + 1, bar = 0, to_remove = false, rev_dep = to_remove })
& { bar | force = 1, to_remove = true }
) == { foo = 1, bar = 1, to_remove = true, rev_dep = false },
(
(std.record.remove_with_opts "to_remove" { foo = bar + 1, bar = 0, to_remove = false, rev_dep = to_remove })
& { bar | force = 1, to_remove = true }
) == { foo = 1, bar = 1, to_remove = true, rev_dep = false },
((std.record.update "bar" 1 { foo = bar + 1, bar = 0 }) & { bar | force = 2 }) == { foo = 1, bar = 2, },
]
|> std.test.assert_all

0 comments on commit 77e355a

Please sign in to comment.