Skip to content

Commit

Permalink
Merge pull request #1273 from demergent-labs/functional_syntax_manual…
Browse files Browse the repository at this point in the history
…_reply

refactor manual_reply
  • Loading branch information
lastmjs authored Sep 25, 2023
2 parents fba135e + b60493b commit 3f89d11
Show file tree
Hide file tree
Showing 9 changed files with 437 additions and 630 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
# "examples/func_types",
# "examples/generics",
# "examples/ledger_canister",
# "examples/manual_reply",
# "examples/motoko_examples/calc",
# "examples/motoko_examples/counter",
# "examples/motoko_examples/echo",
Expand Down Expand Up @@ -113,6 +112,7 @@ jobs:
"examples/key_value_store",
"examples/list_of_lists",
"examples/management_canister",
"examples/manual_reply",
"examples/primitive_types",
"examples/principal",
"examples/query",
Expand Down
4 changes: 2 additions & 2 deletions examples/manual_reply/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"type": "custom",
"build": "npx azle manual_reply",
"root": "src",
"ts": "src/manual_reply.ts",
"candid": "src/manual_reply.did",
"ts": "src/index.ts",
"candid": "src/index.did",
"wasm": ".azle/manual_reply/manual_reply.wasm",
"gzip": true,
"declarations": {
Expand Down
450 changes: 107 additions & 343 deletions examples/manual_reply/package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions examples/manual_reply/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"azle": "0.17.1"
},
"devDependencies": {
"@dfinity/agent": "0.15.4",
"ts-node": "10.7.0",
"typescript": "4.6.3"
"@dfinity/agent": "^0.19.2",
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
}
}
File renamed without changes.
306 changes: 306 additions & 0 deletions examples/manual_reply/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,306 @@
import {
blob,
bool,
float32,
ic,
int,
int8,
Manual,
nat,
nat8,
Null,
query,
Record,
reserved,
Service,
text,
Tuple,
update,
Variant,
Vec,
Void
} from 'azle';

const Options = Variant({
Small: Null,
Medium: Null,
Large: Null
});

const RawReply = Record({
int: int,
text: text,
bool: bool,
myBlob: blob,
myVariant: Options
});

const Orbital = Record({
layer: nat8,
electrons: nat8
});

const Solid = Record({
element: text
});

const Gas = Variant({
Elemental: Null,
Mixed: Null,
Toxic: Null
});

const State = Variant({
Gas: Gas,
Liquid: Null,
Solid: Solid
});

const Element = Record({
id: text,
orbitals: Vec(Orbital),
state: State
});

export default Service({
// Updates
manualUpdate: update(
[text],
Manual(text),
(message) => {
if (message === 'reject') {
ic.reject(message);
return;
}

ic.reply(message, text);
},
{ manual: true }
),
updateBlob: update(
[],
Manual(blob),
() => {
ic.reply(
new Uint8Array([83, 117, 114, 112, 114, 105, 115, 101, 33]),
blob
);
},
{ manual: true }
),
updateFloat32: update(
[],
Manual(float32),
() => {
ic.reply(1245.678, float32);
},
{ manual: true }
),
updateInt8: update(
[],
Manual(int8),
() => {
ic.reply(-100, int8);
},
{ manual: true }
),
updateNat: update(
[],
Manual(nat),
() => {
ic.reply(184467440737095516150n, nat);
},
{ manual: true }
),
updateNull: update(
[],
Manual(Null),
() => {
ic.reply(null, Null);
},
{ manual: true }
),
updateVoid: update(
[],
Void,
() => {
ic.reply(undefined, Void);
},
{ manual: true }
),
updateRecord: update(
[],
Manual(Element),
() => {
const element: typeof Element = {
id: 'b0283eb7-9c0e-41e5-8089-3345e6a8fa6a',
orbitals: [
{
electrons: 2,
layer: 1
},
{
electrons: 8,
layer: 2
}
],
state: {
Gas: { Elemental: null }
}
};
ic.reply(element, Element);
},
{ manual: true }
),
updateReserved: update(
[],
Manual(reserved),
() => {
ic.reply(undefined, reserved);
},
{ manual: true }
),
updateString: update(
[],
Manual(text),
() => {
ic.reply('hello', text);
},
{ manual: true }
),
updateVariant: update(
[],
Manual(Gas),
() => {
const gas = { Toxic: null };
ic.reply(gas, Gas);
},
{ manual: true }
),
replyRaw: update(
[],
Manual(RawReply),
() => {
ic.replyRaw(
ic.candidEncode(
'(record { "int" = 42; "text" = "text"; "bool" = true; "myBlob" = blob "Surprise!"; "myVariant" = variant { Medium } })'
)
);
},
{ manual: true }
),
// Queries
manualQuery: query(
[text],
Manual(text),
(message) => {
if (message === 'reject') {
ic.reject(message);
return;
}

ic.reply(message, text);
},
{ manual: true }
),
queryBlob: query(
[],
Manual(blob),
() => {
ic.reply(
new Uint8Array([83, 117, 114, 112, 114, 105, 115, 101, 33]),
blob
);
},
{ manual: true }
),
queryFloat32: query(
[],
Manual(float32),
() => {
ic.reply(1245.678, float32);
},
{ manual: true }
),
queryInt8: query(
[],
Manual(int8),
() => {
ic.reply(-100, int8);
},
{ manual: true }
),
queryNat: query(
[],
Manual(nat),
() => {
ic.reply(184_467_440_737_095_516_150n, nat);
},
{ manual: true }
),
queryNull: query(
[],
Manual(Null),
() => {
ic.reply(null, Null);
},
{ manual: true }
),
queryVoid: query(
[],
Manual(Void),
() => {
ic.reply(undefined, Void);
},
{ manual: true }
),
queryRecord: query(
[],
Manual(Element),
() => {
const element: typeof Element = {
id: 'b0283eb7-9c0e-41e5-8089-3345e6a8fa6a',
orbitals: [
{
electrons: 2,
layer: 1
},
{
electrons: 8,
layer: 2
}
],
state: {
Gas: { Elemental: null }
}
};
ic.reply(element, Element);
},
{ manual: true }
),
queryReserved: query(
[],
Manual(reserved),
() => {
ic.reply(undefined, reserved);
},
{ manual: true }
),
queryString: query(
[],
Manual(text),
() => {
ic.reply('hello', text);
},
{ manual: true }
),
queryVariant: query(
[],
Manual(Gas),
() => {
const gas = { Toxic: null };
ic.reply(gas, Gas);
},
{ manual: true }
)
});
Loading

0 comments on commit 3f89d11

Please sign in to comment.