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

Update Superheroes example to use ordered map #1039

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
75 changes: 36 additions & 39 deletions motoko/minimal-counter-dapp/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
actor {

var counter : Nat = 0;
stable var counter : Nat = 0;

public func increment() : async Nat {
counter += 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
"vitest": "^0.32.2"
},
"dependencies": {
"@dfinity/agent": "^1.4.0",
"@dfinity/candid": "^1.4.0",
"@dfinity/principal": "^1.4.0",
"@dfinity/agent": "^2.1.3",
"@dfinity/candid": "^2.1.3",
"@dfinity/principal": "^2.1.3",
"lit-html": "^2.8.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default defineConfig({
environment("all", { prefix: "DFX_" }),
],
resolve: {
dedupe: ['@dfinity/agent'],
alias: [
{
find: "declarations",
Expand Down
61 changes: 19 additions & 42 deletions motoko/superheroes/src/superheroes/Main.mo
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import List "mo:base/List";
import Option "mo:base/Option";
import Trie "mo:base/Trie";
import Map "mo:base/OrderedMap";
import Nat32 "mo:base/Nat32";

actor Superheroes {
Expand All @@ -15,18 +15,19 @@ actor Superheroes {
// The type of a superhero.
public type Superhero = {
name : Text;
superpowers : List.List<Text>;
superpowers : List.List<Text>
};

/**
* Application State
*/

// The next available superhero identifier.
private stable var next : SuperheroId = 0;
stable var next : SuperheroId = 0;

// The superhero data store.
private stable var superheroes : Trie.Trie<SuperheroId, Superhero> = Trie.empty();
let Ops = Map.Make<SuperheroId>(Nat32.compare);
stable var map : Map.Map<SuperheroId, Superhero> = Ops.empty();

/**
* High-Level API
Expand All @@ -36,57 +37,33 @@ actor Superheroes {
public func create(superhero : Superhero) : async SuperheroId {
let superheroId = next;
next += 1;
superheroes := Trie.replace(
superheroes,
key(superheroId),
Nat32.equal,
?superhero,
).0;
return superheroId;
map := Ops.put(map, superheroId, superhero);
return superheroId
};

// Read a superhero.
public query func read(superheroId : SuperheroId) : async ?Superhero {
let result = Trie.find(superheroes, key(superheroId), Nat32.equal);
return result;
let result = Ops.get(map, superheroId);
return result
};

// Update a superhero.
public func update(superheroId : SuperheroId, superhero : Superhero) : async Bool {
let result = Trie.find(superheroes, key(superheroId), Nat32.equal);
let exists = Option.isSome(result);
let (result, old_value) = Ops.replace(map, superheroId, superhero);
let exists = Option.isSome(old_value);
if (exists) {
superheroes := Trie.replace(
superheroes,
key(superheroId),
Nat32.equal,
?superhero,
).0;
map := result
};
return exists;
return exists
};

// Delete a superhero.
public func delete(superheroId : SuperheroId) : async Bool {
let result = Trie.find(superheroes, key(superheroId), Nat32.equal);
let exists = Option.isSome(result);
let (result, old_value) = Ops.remove(map, superheroId);
let exists = Option.isSome(old_value);
if (exists) {
superheroes := Trie.replace(
superheroes,
key(superheroId),
Nat32.equal,
null,
).0;
map := result
};
return exists;
};

/**
* Utilities
*/

// Create a trie key from a superhero identifier.
private func key(x : SuperheroId) : Trie.Key<SuperheroId> {
return { hash = x; key = x };
};
};
return exists
}
}
Loading