Skip to content

Commit

Permalink
add update to the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lastmjs committed Sep 20, 2023
1 parent bd73cf0 commit b96bcd8
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 31 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@
# "examples/stable_structures",
# "examples/timers",
# "examples/tuple_types",
# "examples/update"

name: Azle Tests
on:
Expand Down Expand Up @@ -123,7 +122,8 @@ jobs:
run: |
EXAMPLE_DIRECTORIES=$(cat << END
[
"examples/query"
"examples/query",
"examples/update"
]
END
)
Expand Down
17 changes: 7 additions & 10 deletions examples/update/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { query, text, update, Void } from 'azle';
import { query, Service, text, update, Void } from 'azle';

let currentMessage: string = '';

export default class {
@query([], text)
getCurrentMessage(): text {
export default Service({
getCurrentMessage: query([], text, () => {
return currentMessage;
}

@update([text], Void)
simpleUpdate(message: text): Void {
}),
simpleUpdate: update([text], Void, (message) => {
currentMessage = message;
}
}
})
});
27 changes: 19 additions & 8 deletions src/lib_functional/candid/reference/service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { CanisterMethods } from '../../../compiler/utils/types';
import { CanisterMethodInfo, query } from '../../canister_methods/query';

// type CanisterMethodType = 'query';
// type CanisterMethods = (typeof query & {
// canisterMethodType: CanisterMethodType;
// })[];
import { CanisterMethodInfo } from '../../canister_methods';

type ServiceOptions = {
[key: string]: CanisterMethodInfo;
Expand Down Expand Up @@ -37,6 +32,22 @@ export function Service(serviceOptions: ServiceOptions): CanisterMethods {
};
});

const updates = Object.entries(serviceOptions)
.filter((entry) => {
const key = entry[0];
const value = entry[1];

return value.type === 'update';
})
.map((entry) => {
const key = entry[0];
const value = entry[1];

return {
name: key
};
});

// TODO loop through each key and simply grab the candid off
// TODO grab the init/post_upgrade candid as well
return {
Expand All @@ -45,11 +56,11 @@ export function Service(serviceOptions: ServiceOptions): CanisterMethods {
.map((entry) => {
return `${entry[0]}: ${entry[1].candid}`;
})
.join('')}
.join('\n ')}
}
`,
queries,
updates: [],
updates,
callbacks
};
}
9 changes: 8 additions & 1 deletion src/lib_functional/canister_methods/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ import { IDL } from '../../lib_new/index';
import { ic } from '../../lib_new/ic';

export * from './query';
export * from './update';

export type CanisterMethodInfo = {
type: 'query' | 'update';
callback: (...args: any) => any;
candid: string;
};

export function executeMethod(
paramCandid: any,
Expand All @@ -11,7 +18,7 @@ export function executeMethod(
) {
const decoded = IDL.decode(paramCandid[0] as any, args[0]);

const result = callback(decoded);
const result = callback(...decoded);

if (
result !== undefined &&
Expand Down
10 changes: 1 addition & 9 deletions src/lib_functional/canister_methods/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,7 @@ import {
handleRecursiveParams,
handleRecursiveReturn
} from '../../lib_new/method_decorators';
import { IDL } from '../../lib_new/index';
import { ic } from '../../lib_new/ic';
import { executeMethod } from '.';

export type CanisterMethodInfo = {
type: 'query';
callback: (...args: any) => any;
candid: string;
};
import { CanisterMethodInfo, executeMethod } from '.';

// TODO execute the candid stuff and just store it on the function itself?
export function query(
Expand Down
23 changes: 22 additions & 1 deletion src/lib_functional/canister_methods/update.ts
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
export function update() {}
import {
handleRecursiveParams,
handleRecursiveReturn
} from '../../lib_new/method_decorators';
import { CanisterMethodInfo, executeMethod } from '.';

export function update(
paramsIdls: any,
returnIdl: any,
callback: any
): CanisterMethodInfo {
const paramCandid = handleRecursiveParams(paramsIdls);
const returnCandid = handleRecursiveReturn(returnIdl, paramCandid[2]);

return {
type: 'update',
callback: (...args) => {
executeMethod(paramCandid, returnCandid, args, callback);
},
candid: `(${paramCandid[1].join(', ')}) -> (${returnCandid[1]});`
};
}

0 comments on commit b96bcd8

Please sign in to comment.