Skip to content

Commit

Permalink
Update phone-book to functional runtime syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
dansteren committed Sep 25, 2023
1 parent d193396 commit 9adec96
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
# "examples/generics",
# "examples/ledger_canister",
# "examples/manual_reply",
# "examples/motoko_examples/phone-book",
# "examples/motoko_examples/quicksort",
# "examples/motoko_examples/simple-to-do",
# "examples/motoko_examples/superheroes",
Expand Down Expand Up @@ -113,6 +112,7 @@ jobs:
"examples/motoko_examples/http_counter",
"examples/motoko_examples/minimal-counter-dapp",
"examples/motoko_examples/persistent-storage",
"examples/motoko_examples/phone-book",
"examples/primitive_types",
"examples/principal",
"examples/query",
Expand Down
3 changes: 2 additions & 1 deletion examples/motoko_examples/phone-book/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"root": "src/phone_book",
"ts": "src/phone_book/index.ts",
"candid": "src/phone_book/index.did",
"wasm": ".azle/phone_book/phone_book.wasm.gz",
"wasm": ".azle/phone_book/phone_book.wasm",
"gzip": true,
"declarations": {
"output": "src/declarations/phone_book",
"node_compatibility": false
Expand Down
32 changes: 13 additions & 19 deletions examples/motoko_examples/phone-book/src/phone_book/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
candid,
None,
Opt,
query,
Expand All @@ -11,26 +10,21 @@ import {
Void
} from 'azle';

export class Entry extends Record {
@candid(text)
desc: text;
export const Entry = Record({
desc: text,
phone: text
});

@candid(text)
phone: text;
}
let phoneBook = new Map<string, typeof Entry>();

export default class extends Service {
phoneBook = new Map<string, Entry>();
export default Service({
insert: update([text, Entry], Void, (name, entry) => {
phoneBook.set(name, entry);
}),

@update([text, Entry], Void)
insert(name: text, entry: Entry): void {
this.phoneBook.set(name, entry);
}

@query([text], Opt(Entry))
lookup(name: text): Opt<Entry> {
const entryOrUndefined = this.phoneBook.get(name);
lookup: query([text], Opt(Entry), (name) => {
const entryOrUndefined = phoneBook.get(name);

return entryOrUndefined ? Some(entryOrUndefined) : None;
}
}
})
});
6 changes: 4 additions & 2 deletions examples/motoko_examples/phone-book/test/tests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Test } from 'azle/test';
import { Entry } from '../src/phone_book/index.ts.dont';
import { _SERVICE } from '../src/declarations/phone_book/phone_book.did';
import {
_SERVICE,
rec_0 as Entry
} from '../src/declarations/phone_book/phone_book.did';
import { ActorSubclass } from '@dfinity/agent';

const TEST_PHONE_BOOK_RECORD: Entry = {
Expand Down

0 comments on commit 9adec96

Please sign in to comment.