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

Made Opt a Variant #1313

Merged
merged 6 commits into from
Oct 2, 2023
Merged
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
16 changes: 8 additions & 8 deletions examples/audio_recorder/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ export default Canister({
deleteUser: update([Principal], Result(User, AudioRecorderError), (id) => {
const userOpt = users.get(id);

if (userOpt.length === 0) {
if ('None' in userOpt) {
return Err({
UserDoesNotExist: id
});
}

const user = userOpt[0];
const user = userOpt.Some;

user.recordingIds.forEach((recordingId) => {
recordings.remove(recordingId);
Expand All @@ -85,13 +85,13 @@ export default Canister({
(audio, name, userId) => {
const userOpt = users.get(userId);

if (userOpt.length === 0) {
if ('None' in userOpt) {
return Err({
UserDoesNotExist: userId
});
}

const user = userOpt[0];
const user = userOpt.Some;

const id = generateId();
const recording: typeof Recording = {
Expand Down Expand Up @@ -126,21 +126,21 @@ export default Canister({
(id) => {
const recordingOpt = recordings.get(id);

if (recordingOpt.length === 0) {
if ('None' in recordingOpt) {
return Err({ RecordingDoesNotExist: id });
}

const recording = recordingOpt[0];
const recording = recordingOpt.Some;

const userOpt = users.get(recording.userId);

if (userOpt.length === 0) {
if ('None' in userOpt) {
return Err({
UserDoesNotExist: recording.userId
});
}

const user = userOpt[0];
const user = userOpt.Some;

const updatedUser: typeof User = {
...user,
Expand Down
18 changes: 14 additions & 4 deletions examples/complex_init/src/complex_init/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
import { Canister, init, Opt, query, Record, text, Tuple } from 'azle';
import {
Canister,
init,
None,
Opt,
query,
Record,
Some,
text,
Tuple
} from 'azle';

const User = Record({
id: text
});

let greeting: text = 'Hello User';
let user: Opt<typeof User> = [];
let user: Opt<typeof User> = None;

export default Canister({
init: init([Tuple(text, User)], (tuple) => {
greeting = tuple[0];
user = [tuple[1]];
user = Some(tuple[1]);
return undefined;
}),
greetUser: query([], text, () => {
return `${greeting} ${user[0]?.id ?? '??'}`;
return `${greeting} ${user.Some?.id ?? '??'}`;
})
});
48 changes: 26 additions & 22 deletions examples/ethereum_json_rpc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import {
ic,
init,
nat32,
Principal,
query,
Some,
StableBTreeMap,
text,
update
Expand All @@ -23,22 +25,22 @@ export default Canister({
ethGetBalance: update([text], text, async (ethereumAddress) => {
const urlOpt = stableStorage.get('ethereumUrl');

if (urlOpt.length === 0) {
if ('None' in urlOpt) {
throw new Error('ethereumUrl is not defined');
}

const url = urlOpt[0];
const url = urlOpt.Some;

const httpResponse = await ic.call(managementCanister.http_request, {
args: [
{
url,
max_response_bytes: [2_000n],
max_response_bytes: Some(2_000n),
method: {
post: null
},
headers: [],
body: [
body: Some(
Buffer.from(
JSON.stringify({
jsonrpc: '2.0',
Expand All @@ -48,13 +50,14 @@ export default Canister({
}),
'utf-8'
)
],
transform: [
{
function: [ic.id(), 'ethTransform'],
context: Uint8Array.from([])
}
]
),
transform: Some({
function: [ic.id(), 'ethTransform'] as [
Principal,
string
],
context: Uint8Array.from([])
})
}
],
cycles: 50_000_000n
Expand All @@ -65,22 +68,22 @@ export default Canister({
ethGetBlockByNumber: update([nat32], text, async (number) => {
const urlOpt = stableStorage.get('ethereumUrl');

if (urlOpt.length === 0) {
if ('None' in urlOpt) {
throw new Error('ethereumUrl is not defined');
}

const url = urlOpt[0];
const url = urlOpt.Some;

const httpResponse = await ic.call(managementCanister.http_request, {
args: [
{
url,
max_response_bytes: [2_000n],
max_response_bytes: Some(2_000n),
method: {
post: null
},
headers: [],
body: [
body: Some(
Buffer.from(
JSON.stringify({
jsonrpc: '2.0',
Expand All @@ -90,13 +93,14 @@ export default Canister({
}),
'utf-8'
)
],
transform: [
{
function: [ic.id(), 'ethTransform'],
context: Uint8Array.from([])
}
]
),
transform: Some({
function: [ic.id(), 'ethTransform'] as [
Principal,
string
],
context: Uint8Array.from([])
})
}
],
cycles: 50_000_000n
Expand Down
27 changes: 13 additions & 14 deletions examples/func_types/canisters/func_types/index.did
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
type rec_26 = func (record {id:text; complexFunc:rec_26; basicFunc:func (text) -> (text) query}, variant {Bad; ComplexFunc:rec_26; Good; BasicFunc:func (text) -> (text) query}) -> (nat64) ;
type rec_29 = func (record {id:text; complexFunc:rec_29; basicFunc:func (text) -> (text) query}, variant {Bad; ComplexFunc:rec_29; Good; BasicFunc:func (text) -> (text) query}) -> (nat64) ;
type rec_34 = func (record {id:text; complexFunc:rec_34; basicFunc:func (text) -> (text) query}, variant {Bad; ComplexFunc:rec_34; Good; BasicFunc:func (text) -> (text) query}) -> (nat64) ;
type rec_26 = func (record {id:text; complexFunc:rec_26; basicFunc:func (text) -> (text) query}, variant {Bad; ComplexFunc:rec_26; Good; BasicFunc:func (text) -> (text) query}) -> (nat64);
type rec_29 = func (record {id:text; complexFunc:rec_29; basicFunc:func (text) -> (text) query}, variant {Bad; ComplexFunc:rec_29; Good; BasicFunc:func (text) -> (text) query}) -> (nat64);
type rec_34 = func (record {id:text; complexFunc:rec_34; basicFunc:func (text) -> (text) query}, variant {Bad; ComplexFunc:rec_34; Good; BasicFunc:func (text) -> (text) query}) -> (nat64);
service: () -> {
basicFuncParam: (func (text) -> (text) query) -> (func (text) -> (text) query) query;
basicFuncParamArray: (vec func (text) -> (text) query) -> (vec func (text) -> (text) query) query;
basicFuncReturnType: () -> (func (text) -> (text) query) query;
basicFuncReturnTypeArray: () -> (vec func (text) -> (text) query) query;
complexFuncParam: (rec_26) -> (rec_29) query;
complexFuncReturnType: () -> (rec_34) query;
getNotifierFromNotifiersCanister: () -> (func (vec nat8) -> () oneway) ;
getStableFunc: () -> (func (nat64, text) -> () query) query;
init: () -> () query;
nullFuncParam: (func (opt null, vec null, null, vec vec null, vec opt null) -> (null) query) -> (func (opt null, vec null, null, vec vec null, vec opt null) -> (null) query) query;
}
basicFuncParam: (func (text) -> (text) query) -> (func (text) -> (text) query) query;
basicFuncParamArray: (vec func (text) -> (text) query) -> (vec func (text) -> (text) query) query;
basicFuncReturnType: () -> (func (text) -> (text) query) query;
basicFuncReturnTypeArray: () -> (vec func (text) -> (text) query) query;
complexFuncParam: (rec_26) -> (rec_29) query;
complexFuncReturnType: () -> (rec_34) query;
getNotifierFromNotifiersCanister: () -> (func (vec nat8) -> () oneway);
getStableFunc: () -> (func (nat64, text) -> () query) query;
nullFuncParam: (func (opt null, vec null, null, vec vec null, vec opt null) -> (null) query) -> (func (opt null, vec null, null, vec vec null, vec opt null) -> (null) query) query;
}
6 changes: 3 additions & 3 deletions examples/func_types/canisters/func_types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ export default Canister({

getStableFunc: query([], StableFunc, () => {
const stableFuncOpt = stableStorage.get('stableFunc');
if (stableFuncOpt.length === 1) {
return stableFuncOpt[0];
if ('None' in stableFuncOpt) {
return [Principal.from('aaaaa-aa'), 'raw_rand'];
}
return [Principal.from('aaaaa-aa'), 'raw_rand'];
return stableFuncOpt.Some;
}),

basicFuncParam: query([BasicFunc], BasicFunc, (basicFunc) => {
Expand Down
4 changes: 2 additions & 2 deletions examples/func_types/canisters/notifiers/index.did
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
service: () -> {
getNotifier: () -> (func (vec nat8) -> () oneway) query;
}
getNotifier: () -> (func (vec nat8) -> () oneway) query;
}
6 changes: 3 additions & 3 deletions examples/ledger_canister/src/ledger_canister/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ export default Canister({
from_subaccount: None,
to: binaryAddressFromAddress(to),
created_at_time:
createdAtTime.length === 1
? Some({ timestamp_nanos: createdAtTime[0] })
: None
'None' in createdAtTime
? None
: Some({ timestamp_nanos: createdAtTime.Some })
}
]
});
Expand Down
16 changes: 8 additions & 8 deletions examples/motoko_examples/http_counter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ export default Canister({

const counterOpt = stableStorage.get('counter');
const counter =
counterOpt.length === 0
'None' in counterOpt
? ic.trap('counter does not exist')
: counterOpt[0];
: counterOpt.Some;

return {
status_code: 200,
Expand Down Expand Up @@ -144,18 +144,18 @@ export default Canister({
if (req.method === 'POST') {
const counterOpt = stableStorage.get('counter');
const counter =
counterOpt.length === 0
'None' in counterOpt
? ic.trap('counter does not exist')
: counterOpt[0];
: counterOpt.Some;

stableStorage.insert('counter', counter + 1n);

if (req.headers.find(isGzip) === undefined) {
const counterOpt = stableStorage.get('counter');
const counter =
counterOpt.length === 0
'None' in counterOpt
? ic.trap('counter does not exist')
: counterOpt[0];
: counterOpt.Some;

return {
status_code: 201,
Expand Down Expand Up @@ -206,9 +206,9 @@ export default Canister({
case 'next': {
const counterOpt = stableStorage.get('counter');
const counter =
counterOpt.length === 0
'None' in counterOpt
? ic.trap('counter does not exist')
: counterOpt[0];
: counterOpt.Some;

return {
body: encode(`${counter}`),
Expand Down
12 changes: 6 additions & 6 deletions examples/motoko_examples/persistent-storage/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ export default Canister({
increment: update([], nat, () => {
const counterOpt = stableStorage.get('counter');
const counter =
counterOpt.length === 0
'None' in counterOpt
? ic.trap('counter not defined')
: counterOpt[0] + 1n;
: counterOpt.Some + 1n;

stableStorage.insert('counter', counter);

Expand All @@ -38,9 +38,9 @@ export default Canister({
get: query([], nat, () => {
const counterOpt = stableStorage.get('counter');
const counter =
counterOpt.length === 0
'None' in counterOpt
? ic.trap('counter not defined')
: counterOpt[0];
: counterOpt.Some;

return counter;
}),
Expand All @@ -49,9 +49,9 @@ export default Canister({

const counterOpt = stableStorage.get('counter');
const counter =
counterOpt.length === 0
'None' in counterOpt
? ic.trap('counter not defined')
: counterOpt[0];
: counterOpt.Some;

return counter;
})
Expand Down
Loading
Loading