Skip to content

Commit

Permalink
refactored guard_functions
Browse files Browse the repository at this point in the history
  • Loading branch information
lastmjs committed Sep 25, 2023
1 parent 1660520 commit 1587ba4
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 76 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
# "examples/complex_types",
# "examples/func_types",
# "examples/generics",
# "examples/guard_functions",
# "examples/heartbeat",
# "examples/ic_api",
# "examples/imports",
Expand Down Expand Up @@ -114,6 +113,7 @@ jobs:
"examples/cycles",
"examples/date",
"examples/ethereum_json_rpc",
"examples/guard_functions",
"examples/primitive_types",
"examples/principal",
"examples/query",
Expand Down
3 changes: 2 additions & 1 deletion examples/guard_functions/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/guard_functions/guard_functions.wasm.gz",
"wasm": ".azle/guard_functions/guard_functions.wasm",
"gzip": true,
"declarations": {
"output": "test/dfx_generated/guard_functions",
"node_compatibility": true
Expand Down
157 changes: 89 additions & 68 deletions examples/guard_functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,76 +13,97 @@ export let state = {
heartbeatTick: 0
};

export default class extends Service {
@query([], nat32)
getCounter(): nat32 {
export default Service({
getCounter: query([], nat32, () => {
return state.counter;
}

@query([], bool)
identifierAnnotation(): bool {
}),
identifierAnnotation: query([], bool, () => {
console.log('identifierAnnotation called');
return true;
}

@query([], bool)
callExpressionWithoutOptionsObject(): bool {
}),
callExpressionWithoutOptionsObject: query([], bool, () => {
console.log('callExpressionWithoutOptionsObject called');
return true;
}

@query([], bool, {})
callExpressionWithEmptyOptionsObject(): bool {
console.log('callExpressionWithEmptyOptionsObject called');
return true;
}

@query([], bool, { guard: allowAll })
looselyGuarded(): bool {
console.log('looselyGuarded called');
return true;
}

@query([], bool, { manual: true, guard: allowAll })
looselyGuardedManual(): Manual<bool> {
console.log('looselyGuardedManual called');
ic.reply(true, bool);
}

// prettier-ignore
@query([], bool, { "guard": allowAll })
looselyGuardedWithGuardOptionKeyAsString(): bool {
console.log('looselyGuardedWithGuardOptionKeyAsString called');
return true;
}

@update([], bool, { guard: incrementCounterAndAllowAll })
modifyStateGuarded(): bool {
console.log('modifyStateGuarded called');
return true;
}

@query([], bool, { guard: unpassable })
tightlyGuarded(): bool {
console.log('tightlyGuarded called');
return true;
}

@query([], bool, { guard: throwString })
errorStringGuarded(): bool {
console.log('errorStringGuarded called');
return true;
}

@query([], bool, { guard: throwCustomError })
customErrorGuarded(): bool {
console.log('customErrorGuarded called');
return true;
}

@query([], bool, { guard: returnNonStringErrValue })
nonStringErrValueGuarded(): bool {
console.log('nonStringErrValueGuarded called');
return true;
}
}
}),
callExpressionWithEmptyOptionsObject: query(
[],
bool,
() => {
console.log('callExpressionWithEmptyOptionsObject called');
return true;
},
{}
),
looselyGuarded: query(
[],
bool,
() => {
console.log('looselyGuarded called');
return true;
},
{ guard: allowAll }
),
looselyGuardedManual: query(
[],
Manual(bool),
() => {
console.log('looselyGuardedManual called');
ic.reply(true, bool);
},
{ manual: true, guard: allowAll }
),
looselyGuardedWithGuardOptionKeyAsString: query(
[],
bool,
() => {
console.log('looselyGuardedWithGuardOptionKeyAsString called');
return true;
},
{ guard: allowAll }
),
modifyStateGuarded: update(
[],
bool,
() => {
console.log('modifyStateGuarded called');
return true;
},
{ guard: incrementCounterAndAllowAll }
),
tightlyGuarded: query(
[],
bool,
() => {
console.log('tightlyGuarded called');
return true;
},
{ guard: unpassable }
),
errorStringGuarded: query(
[],
bool,
() => {
console.log('errorStringGuarded called');
return true;
},
{ guard: throwString }
),
customErrorGuarded: query(
[],
bool,
() => {
console.log('customErrorGuarded called');
return true;
},
{ guard: throwCustomError }
),
nonStringErrValueGuarded: query(
[],
bool,
() => {
console.log('nonStringErrValueGuarded called');
return true;
},
{ guard: returnNonStringErrValue }
)
});
13 changes: 10 additions & 3 deletions src/lib_functional/candid/reference/service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { Principal, TypeMapping } from '../../';
import { IDL, ServiceFunctionInfo, serviceCall } from '../../../lib_new';
import {
IDL,
ServiceFunctionInfo,
createGlobalGuard,
serviceCall
} from '../../../lib_new';
import {
Parent,
toParamIDLTypes,
Expand Down Expand Up @@ -93,7 +98,8 @@ export function Service<T extends ServiceOptions>(

return {
name: key,
composite: value.async
composite: value.async,
guard_name: createGlobalGuard(value.guard, key)
};
});

Expand All @@ -109,7 +115,8 @@ export function Service<T extends ServiceOptions>(
const value = entry[1];

return {
name: key
name: key,
guard_name: createGlobalGuard(value.guard, key)
};
});

Expand Down
1 change: 1 addition & 0 deletions src/lib_functional/canister_methods/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type CanisterMethodInfo<T extends ReadonlyArray<any>, K> = {
candidTypes: string[];
paramsIdls: any[];
returnIdl: any;
guard: (() => any) | undefined;
};

export type Callback<Params extends ReadonlyArray<any>, Return> = (
Expand Down
3 changes: 2 additions & 1 deletion src/lib_functional/canister_methods/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export function query<
candidTypes: newTypesToStingArr(returnCandid[2]),
paramsIdls: paramsIdls as any,
returnIdl,
async: callback === undefined ? false : isAsync(callback)
async: callback === undefined ? false : isAsync(callback),
guard: methodArgs?.guard
};
}
3 changes: 2 additions & 1 deletion src/lib_functional/canister_methods/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export function update<
candidTypes: newTypesToStingArr(returnCandid[2]),
paramsIdls: paramsIdls as any,
returnIdl,
async: callback === undefined ? false : isAsync(callback)
async: callback === undefined ? false : isAsync(callback),
guard: methodArgs?.guard
};
}
2 changes: 1 addition & 1 deletion src/lib_new/method_decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ export function isAsync(originalFunction: any) {
}
}

function createGlobalGuard(
export function createGlobalGuard(
guard: (() => GuardResult) | undefined,
functionName: string
): string | undefined {
Expand Down

0 comments on commit 1587ba4

Please sign in to comment.