From 04daf776d3f84105cbc0c23bf5cb04736edc0e9d Mon Sep 17 00:00:00 2001 From: John David Pressman Date: Tue, 4 Feb 2020 23:08:47 -0800 Subject: [PATCH 1/2] Add rough draft of big map reference page to docs --- gitlab-pages/docs/reference/big_map.md | 184 +++++++++++++++++++++++++ 1 file changed, 184 insertions(+) create mode 100644 gitlab-pages/docs/reference/big_map.md diff --git a/gitlab-pages/docs/reference/big_map.md b/gitlab-pages/docs/reference/big_map.md new file mode 100644 index 000000000..861429c34 --- /dev/null +++ b/gitlab-pages/docs/reference/big_map.md @@ -0,0 +1,184 @@ +--- +id: big-map-reference +title: Big Map +--- + +## Big_map.find_opt(k: a', m: (a',b') big_map) : b' option + +Retrieve the value associated with a particular key. This version returns an option +which can either shift logic in response to a missing value or throw an error. + + + +```pascaligo +const my_balance : option(move) = + moves [("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address)] +``` + + + +```cameligo +let my_balance : move option = + Big_map.find_opt ("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address) moves +``` + + + +```reasonligo +let my_balance : option(move) = + Big_map.find_opt("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address, moves); +``` + + +## Big_map.find(k: a', m: (a', b') big_map) : b' + +Forcefully retrieve the value associated with a particular key. If that value +doesn't exist, this function throws an error. + + + +```pascaligo +const my_balance : move = + get_force (("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address), moves); +``` + + + +```cameligo +let my_balance : move = + Big_map.find ("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address) moves +``` + + + +```reasonligo +let my_balance : move = + Big_map.find ("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address, moves); +``` + + + +## Big_map.update(k: a', v: b', m: (a', b') big_map) : (a', b') big_map + +Change the value associated with a particular key, if that value doesn't already +exist add it. + + + + + +The values of a PascaLIGO big map can be updated using the ordinary +assignment syntax: + +```pascaligo + +function set_ (var m : moveset) : moveset is + block { + m [("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address)] := (4,9); + } with m +``` + + + +```cameligo +let updated_map : moveset = + Big_map.update ("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address) (Some (4,9)) moves +``` + + + +```reasonligo +let updated_map : moveset = + Big_map.update(("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address), Some((4,9)), moves); +``` + + + +## Big_map.add(k: a', v: b', m: (a', b') big_map) : (a', b') big_map + +## Big_map.remove(k: a', m: (a', b') big_map) : (a', b') big_map + +Remove a key and its associated value from the big map. + + + + +```pascaligo +function rm (var m : foo) : foo is block { + remove 42 from map m; +} with m +``` + + +```cameligo +let rm (m : foo) : foo = Big_map.remove 42 m +``` + + +```reasonligo +let rm = (m: foo): foo => Big_map.remove(42, m); +``` + + + +## Big_map.literal(key_value_pair_list: (a', b') list) : (a', b') big_map + +Constructs a big map from a list of key-value pair tuples. + + + + +```pascaligo +const moves: moveset = + big_map + ("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx": address) -> (1,2); + ("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address) -> (0,3); + end +``` + + + +```cameligo +let moves: moveset = + Big_map.literal [ + (("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx": address), (1,2)); + (("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address), (0,3)); + ] +``` + + + +```reasonligo +let moves: moveset = + Big_map.literal ([ + ("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx": address, (1,2)), + ("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address, (0,3)), + ]); +``` + + + + +## Big_map.empty() : (a', b') big_map + +Create an empty big map. + + + + +```pascaligo +const empty_big_map : big_map(int,int) = big_map end +``` + + +```cameligo +let empty_map : foo = Big_map.empty +``` + + +```reasonligo +let empty_map: foo = Big_map.empty; +``` + + + From 39c0f019988cb660af2c71dc9c29a85c5dbf16fb Mon Sep 17 00:00:00 2001 From: John David Pressman Date: Tue, 11 Feb 2020 01:03:46 -0800 Subject: [PATCH 2/2] Add big_map reference page to doc tests and add big_map `add` to test suite --- gitlab-pages/docs/reference/big_map.md | 84 ++++++++++++++++++++++++++ src/test/contracts/big_map.ligo | 2 + src/test/contracts/big_map.mligo | 4 +- src/test/contracts/big_map.religo | 2 + src/test/integration_tests.ml | 5 ++ src/test/md_file_tests.ml | 1 + 6 files changed, 96 insertions(+), 2 deletions(-) diff --git a/gitlab-pages/docs/reference/big_map.md b/gitlab-pages/docs/reference/big_map.md index 861429c34..4dd6ba3af 100644 --- a/gitlab-pages/docs/reference/big_map.md +++ b/gitlab-pages/docs/reference/big_map.md @@ -3,6 +3,67 @@ id: big-map-reference title: Big Map --- +## Defining A Big Map Type + + + +```pascaligo +type move is (int * int) +type moveset is big_map (address, move) +type foo is big_map (int, int) +``` + + +```cameligo +type move = int * int +type moveset = (address, move) big_map +type foo = (int, int) big_map +``` + + +```reasonligo +type move = (int, int); +type moveset = big_map(address, move); +type foo = big_map(int, int); +``` + + + +## Creating A Map + + + + +```pascaligo +const moves: moveset = + big_map + ("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx": address) -> (1,2); + ("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address) -> (0,3); + end +``` + + + +```cameligo +let moves: moveset = + Big_map.literal [ + (("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx": address), (1,2)); + (("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address), (0,3)); + ] +``` + + + +```reasonligo +let moves: moveset = + Big_map.literal ([ + ("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx": address, (1,2)), + ("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address, (0,3)), + ]); +``` + + + ## Big_map.find_opt(k: a', m: (a',b') big_map) : b' option Retrieve the value associated with a particular key. This version returns an option @@ -96,6 +157,29 @@ let updated_map : moveset = ## Big_map.add(k: a', v: b', m: (a', b') big_map) : (a', b') big_map +Add a key and its associated value to the big map. + + + + +```pascaligo +function set_ (var n : int ; var m : foo) : foo is block { + m[23] := n ; +} with m +``` + + +```cameligo +let add (n,m : int * foo) : foo = Big_map.add 23 n m +``` + + +```reasonligo +let add = ((n,m): (int, foo)): foo => Big_map.add(23, n, m); +``` + + + ## Big_map.remove(k: a', m: (a', b') big_map) : (a', b') big_map Remove a key and its associated value from the big map. diff --git a/src/test/contracts/big_map.ligo b/src/test/contracts/big_map.ligo index e6ab78c58..a24e09e97 100644 --- a/src/test/contracts/big_map.ligo +++ b/src/test/contracts/big_map.ligo @@ -13,6 +13,8 @@ function set_ (var n : int ; var m : foo) : foo is block { m[23] := n ; } with m +function add (var n : int ; var m : foo) : foo is set_(n,m) + function rm (var m : foo) : foo is block { remove 42 from map m; } with m diff --git a/src/test/contracts/big_map.mligo b/src/test/contracts/big_map.mligo index eb06bd5f4..35a173f82 100644 --- a/src/test/contracts/big_map.mligo +++ b/src/test/contracts/big_map.mligo @@ -1,8 +1,8 @@ type foo = (int, int) big_map -let set_2 (n : int) (m : foo) : foo = Big_map.update 23 (Some n) m +let set_ (n, m: int * foo) : foo = Big_map.update 23 (Some n) m -let set_ (t: int * foo) : foo = set_2 t.0 t.1 +let add (n,m : int * foo) : foo = Big_map.add 23 n m let rm (m : foo) : foo = Big_map.remove 42 m diff --git a/src/test/contracts/big_map.religo b/src/test/contracts/big_map.religo index 03b13b404..92f5431c8 100644 --- a/src/test/contracts/big_map.religo +++ b/src/test/contracts/big_map.religo @@ -4,6 +4,8 @@ let set2 = (n: int, m: foo): foo => Big_map.update(23, Some(n), m); let set_ = (x: (int, foo)): foo => set2(x[0], x[1]); +let add = ((n,m): (int, foo)): foo => Big_map.add(23, n, m); + let rm = (m: foo): foo => Big_map.remove(42, m); let gf = (m: foo): int => Big_map.find(23, m); diff --git a/src/test/integration_tests.ml b/src/test/integration_tests.ml index 204d2fc55..4128a301f 100644 --- a/src/test/integration_tests.ml +++ b/src/test/integration_tests.ml @@ -1073,6 +1073,11 @@ let big_map_ type_f path : unit result = let make_expected = fun n -> ez [(23 , n) ; (42 , 0)] in expect_eq_n_pos_small program "set_" make_input make_expected in + let%bind () = + let input = (e_pair (e_int 23) (ez [(42, 42)])) in + let expected = ez [(23, 23) ; (42, 42)] in + expect_eq program "add" input expected + in let%bind () = let make_input = fun n -> ez [(23, n) ; (42, 4)] in let make_expected = fun _ -> e_some @@ e_int 4 in diff --git a/src/test/md_file_tests.ml b/src/test/md_file_tests.ml index 86aefeb89..9d1d3cf0a 100644 --- a/src/test/md_file_tests.ml +++ b/src/test/md_file_tests.ml @@ -122,6 +122,7 @@ let md_files = [ "/gitlab-pages/docs/advanced/timestamps-addresses.md"; "/gitlab-pages/docs/api/cli-commands.md"; "/gitlab-pages/docs/api/cheat-sheet.md"; + "/gitlab-pages/docs/reference/big_map.md"; ] let md_root = "../../gitlab-pages/docs/language-basics/"