Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: replace and replaceGet map methods & refactor of codegen and testing #941

Merged
merged 25 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
050af18
feat: implement .replace operation and cover it with tests
Gusarich Oct 11, 2024
11e51f8
chore: update serialization snapshots
Gusarich Oct 11, 2024
a10604d
chore: update changelog
Gusarich Oct 11, 2024
93f46a4
feat: `replaceGet`
Gusarich Oct 15, 2024
4cb46c3
chore: update changelog
Gusarich Oct 15, 2024
6a47645
chore: update cspell
Gusarich Oct 15, 2024
8428344
chore: remove `should not happen` comment
Gusarich Oct 18, 2024
cbb8d08
feat(docs): moved from PR #997
novusnota Oct 23, 2024
9b56f2e
feat: refactor maps operations
Gusarich Oct 25, 2024
5e1fd0c
fix: replaceGet codegen fix for structs
Gusarich Oct 26, 2024
d7ca167
feat: cover all maps with tests
Gusarich Oct 26, 2024
95e32c5
feat: rework maps testing contract completely to cover all different …
Gusarich Oct 27, 2024
fa0598d
feat: wip on typescript testing part (.set and .get)
Gusarich Oct 27, 2024
5181ca7
fix: keys in `DelAllMaps` operation
Gusarich Oct 27, 2024
33f5ad6
feat: test .del operation
Gusarich Oct 27, 2024
d8026cb
feat: add multiple sets of key-value pairs for testing
Gusarich Oct 29, 2024
52a330f
feat: add more .set test cases
Gusarich Oct 29, 2024
22f56cc
feat: add more .get test cases
Gusarich Oct 29, 2024
e1113ba
feat: add more .del test cases
Gusarich Oct 29, 2024
def2c16
feat: add .exists test cases
Gusarich Oct 29, 2024
96d2f72
feat: add .isEmpty and .asCell test cases
Gusarich Oct 29, 2024
03cfb3b
feat: .replace and .replaceGet test cases (wip)
Gusarich Oct 29, 2024
a9ee943
feat: add replaceGet getter test case
Gusarich Oct 29, 2024
523b401
feat: null reference exception test case
Gusarich Oct 30, 2024
13cc11a
chore: fix eslint
Gusarich Oct 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Destructuring of structs and messages: PR [#856](https://github.com/tact-lang/tact/pull/856)
- Docs: automatic links to Web IDE from all code blocks: PR [#994](https://github.com/tact-lang/tact/pull/994)
- Docs: initial semi-automated Chinese translation of the documentation: PR [#942](https://github.com/tact-lang/tact/pull/942)
- The `replace` and `replaceGet` methods for the `Map` type: PR [#941](https://github.com/tact-lang/tact/pull/941)

### Changed

Expand Down
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"rangle",
"rawslice",
"renamer",
"replaceget",
"rparen",
"rugpull",
"rugpulled",
Expand Down
84 changes: 84 additions & 0 deletions docs/src/content/docs/book/maps.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,90 @@ if (gotButUnsure != null) {
}
```

### Replace values, `.replace()` {#replace}

<Badge text="Available since Tact 1.6" variant="tip" size="medium"/><p/>

To replace the value under a key, if such a key exists, use the `.replace(){:tact}` [method](/book/functions#extension-function). It returns `true{:tact}` on successful replacement and `false{:tact}` otherwise.

```tact
// Empty map
let fizz: map<Int, Int> = emptyMap();

// Setting a couple of values under different keys
fizz.set(7, 70);
fizz.set(42, 42);

// Overriding one of the existing key-value pairs
let replaced1 = fizz.replace(7, 68); // key 7 now points to value 68
replaced1; // true

// Trying to replace the value in a non-existing key-value pair will do nothing
let replaced2 = fizz.replace(8, 68); // no key 8, so nothing was altered
replaced2; // false
```

If the given value is [`null{:tact}`](/book/optionals) and the key exists, the entry will be deleted from the map.

```tact
// Empty map
let fizz: map<Int, Int> = emptyMap();

// Setting a couple of values under different keys
fizz.set(7, 70);
fizz.set(42, 42);

// Overriding one of the existing key-value pairs
let replaced1 = fizz.replace(7, null); // the entry under key 7 is now deleted
replaced1; // true

// Trying to replace the value in a non-existing key-value pair will do nothing
let replaced2 = fizz.replace(8, null); // no key 8, so nothing was altered
anton-trunov marked this conversation as resolved.
Show resolved Hide resolved
replaced2; // false
```

### Replace and get old value, `.replaceGet()` {#replaceget}

<Badge text="Available since Tact 1.6" variant="tip" size="medium"/><p/>

Like [`.replace()`](#replace), but instead of returning a [`Bool{:tact}`](/book/types#booleans) it returns the old (pre-replacement) value on successful replacement and [`null{:tact}`](/book/optionals) otherwise.

```tact
// Empty map
let fizz: map<Int, Int> = emptyMap();

// Setting a couple of values under different keys
fizz.set(7, 70);
fizz.set(42, 42);

// Overriding one of the existing key-value pairs
let oldVal1 = fizz.replaceGet(7, 68); // key 7 now points to value 68
oldVal1; // 70

// Trying to replace the value in a non-existing key-value pair will do nothing
let oldVal2 = fizz.replaceGet(8, 68); // no key 8, so nothing was altered
oldVal2; // null
```

If the given value is [`null{:tact}`](/book/optionals) and the key exists, the entry will be deleted from the map.

```tact
// Empty map
let fizz: map<Int, Int> = emptyMap();

// Setting a couple of values under different keys
fizz.set(7, 70);
fizz.set(42, 42);

// Overriding one of the existing key-value pairs
let oldVal1 = fizz.replaceGet(7, null); // the entry under key 7 is now deleted
oldVal1; // 70

// Trying to replace the value in a non-existing key-value pair will do nothing
let oldVal2 = fizz.replaceGet(8, null); // no key 8, so nothing was altered
anton-trunov marked this conversation as resolved.
Show resolved Hide resolved
oldVal2; // null
```

### Delete entries, `.del()` {#del}

To delete a single key-value pair (single entry), use the `.del(){:tact}` [method](/book/functions#extension-function). It returns `true{:tact}` in the case of successful deletion and `false{:tact}` otherwise.
Expand Down
Loading
Loading