Skip to content

Commit

Permalink
Update optional_types to functional runtime syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
dansteren committed Sep 26, 2023
1 parent 9799943 commit 4229748
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 44 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/motoko_examples/superheroes", # blocked by recursive
# "examples/motoko_examples/whoami", # blocked by postUpgrade
# "examples/optional_types",
# "examples/outgoing_http_requests",
# "examples/rejections",
# "examples/robust_imports",
Expand Down Expand Up @@ -111,6 +110,7 @@ jobs:
"examples/motoko_examples/threshold_ecdsa",
"examples/notify_raw",
"examples/null_example",
"examples/optional_types",
"examples/pre_and_post_upgrade",
"examples/primitive_types",
"examples/principal",
Expand Down
3 changes: 2 additions & 1 deletion examples/optional_types/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"root": "src",
"ts": "src/index.ts",
"candid": "src/index.did",
"wasm": ".azle/optional_types/optional_types.wasm.gz",
"wasm": ".azle/optional_types/optional_types.wasm",
"gzip": true,
"declarations": {
"output": "test/dfx_generated/optional_types",
"node_compatibility": true
Expand Down
74 changes: 32 additions & 42 deletions examples/optional_types/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,62 @@
// TODO let's add more examples here, really test it out

import { Opt, query, Record, Vec, text, bool, candid, Null } from 'azle';
import { bool, Null, Opt, query, Record, Service, text, Vec } from 'azle';

class Element extends Record {
@candid(text)
id: string;
}
const Element = Record({
id: text
});

class Head extends Record {
@candid(Vec(Element))
elements: Vec<Element>;
}
const Head = Record({
elements: Vec(Element)
});

class Html extends Record {
@candid(Opt(Head))
head: Opt<Head>;
}
const Html = Record({
head: Opt(Head)
});

export default class {
@query([], Html)
getHtml(): Html {
return Html.create({
export default Service({
getHtml: query([], Html, () => {
return {
head: []
});
}
};
}),

@query([], Opt(Head))
getHead(): Opt<Head> {
getHead: query([], Opt(Head), () => {
return [
Head.create({
{
elements: []
})
}
];
}
}),

@query([], Opt(Head))
getHeadWithElements(): Opt<Head> {
getHeadWithElements: query([], Opt(Head), () => {
return [
Head.create({
{
elements: [
{
id: '0'
}
]
})
}
];
}
}),

@query([Opt(Opt(Element))], Opt(Opt(Element)))
getElement(element: Opt<Opt<Element>>): Opt<Opt<Element>> {
getElement: query([Opt(Opt(Element))], Opt(Opt(Element)), (element) => {
return element;
}
}),

@query([], Null)
getNull(): Null {
getNull: query([], Null, () => {
return null;
}
}),

@query([], Opt(text))
getOptNull(): Opt<text> {
getOptNull: query([], Opt(text), () => {
return [];
}
}),

@query([Opt(text)], bool)
stringToBoolean(optString: Opt<string>): boolean {
stringToBoolean: query([Opt(text)], bool, (optString) => {
if (optString.length > 0) {
return true;
}
return false;
}
}
})
});

0 comments on commit 4229748

Please sign in to comment.