Skip to content

Commit

Permalink
rename paramIdls to paramCandidTypes etc
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemann committed Oct 4, 2023
1 parent a26e0d9 commit 3451dcf
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 176 deletions.
22 changes: 14 additions & 8 deletions src/lib/candid/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,20 +115,26 @@ export type Parent = {
name: string;
};

export function toIDLType(idl: CandidType, parents: Parent[]): IDL.Type<any> {
if ('_azleName' in idl) {
const parent = parents.find((parent) => parent.name === idl._azleName);
// If we the parent isn't undefined (ie we found one with the same name)
// this is a recursive type and we should return the parent rec idl instead of calling getIDL
export function toIDLType(
candidType: CandidType,
parents: Parent[]
): IDL.Type<any> {
if ('_azleName' in candidType) {
const parent = parents.find(
(parent) => parent.name === candidType._azleName
);
// If the parent isn't undefined (ie we found one with the same name)
// this is a recursive type and we should return the parent rec idl
// instead of calling getIDL
if (parent !== undefined) {
return parent.idl;
}
}
if ('_azleIsCanister' in idl && idl._azleIsCanister) {
return toIDLType((idl as any)(), parents);
if ('_azleIsCanister' in candidType && candidType._azleIsCanister) {
return toIDLType((candidType as any)(), parents);
}
// All CandidTypes ought to have a getIDL function defined for them
return (idl as any).getIDL(parents);
return (candidType as any).getIDL(parents);
}

export function toParamIDLTypes(
Expand Down
10 changes: 5 additions & 5 deletions src/lib/candid/recursive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { v4 } from 'uuid';
import { IDL } from '@dfinity/candid';
import { CandidType, Parent } from './index';

type _AzleRecursiveFunction = {
export type _AzleRecursiveFunction = {
(...args: any[]): CandidType;
_azleName?: string;
_azleIsRecursive?: boolean;
Expand All @@ -13,11 +13,11 @@ export function Recursive(candidTypeCallback: any): any {
const name = v4();

let result: _AzleRecursiveFunction = (...args: any[]) => {
const idl = candidTypeCallback();
if (idl._azleIsCanister) {
return idl(...args);
const candidType = candidTypeCallback();
if (candidType._azleIsCanister) {
return candidType(...args);
}
return idl;
return candidType;
};

result._azleName = name;
Expand Down
8 changes: 4 additions & 4 deletions src/lib/candid/types/reference/func.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ const modeToCandid = {
};

export function Func(
paramsIdls: CandidType[],
returnIdl: CandidType,
paramCandidTypes: CandidType[],
returnCandidTypes: CandidType,
mode: Mode
): [Principal, string] & { _azleCandidType?: '_azleCandidType' } {
return {
getIDL(parents: Parent[]) {
return IDL.Func(
toParamIDLTypes(paramsIdls, parents),
toReturnIDLType(returnIdl, parents),
toParamIDLTypes(paramCandidTypes, parents),
toReturnIDLType(returnCandidTypes, parents),
modeToCandid[mode]
);
}
Expand Down
187 changes: 62 additions & 125 deletions src/lib/candid/types/reference/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
toReturnIDLType,
CandidType
} from '../../index';
import { _AzleRecursiveFunction } from '../../recursive';
import { ic } from '../../../ic';
import { Principal } from './principal';
import { IDL } from '@dfinity/candid';
Expand All @@ -16,7 +17,9 @@ type CanisterOptions = {
};

type _AzleCanisterOptions = {
[key: string]: (parent: Parent | undefined) => CanisterMethodInfo<any, any>;
[key: string]: (
parentOrUndefined: _AzleRecursiveFunction | undefined
) => CanisterMethodInfo<any, any>;
};

type _AzleFunctionReturnType = {
Expand All @@ -33,6 +36,11 @@ type _AzleFunctionReturnType = {
getIDL?: (parents: Parent[]) => IDL.Type<any>;
};

type _AzleCanisterReturnType = {
(parentOrPrincipal: _AzleRecursiveFunction | Principal): void;
_azleIsCanister?: boolean;
};

type CanisterReturn<T extends CanisterOptions> = {
[EndpointName in keyof T]: T[EndpointName] extends CanisterMethodInfo<
infer Params,
Expand All @@ -53,7 +61,7 @@ export function Canister<T extends CanisterOptions>(
): CallableObject<T> & { _azleCandidType?: '_azleCandidType' } {
const _azleCanisterOptions =
serviceOptions as unknown as _AzleCanisterOptions;
let result = (parentOrPrincipal: any) => {
let result: _AzleCanisterReturnType = (parentOrPrincipal: any) => {
const originalPrincipal = parentOrPrincipal;
const parentOrUndefined =
parentOrPrincipal !== undefined && parentOrPrincipal._isPrincipal
Expand All @@ -64,19 +72,6 @@ export function Canister<T extends CanisterOptions>(
const key = entry[0];
const value = entry[1](parentOrUndefined);

// if (principal === undefined) {
// return {
// ...acc,
// [key]: (...args: any[]) => {
// return serviceCall(
// principal as any,
// key,
// value.paramsIdls,
// value.returnIdl
// )(...args);
// }
// };
// } else {
return {
...acc,
[key]: {
Expand All @@ -85,8 +80,8 @@ export function Canister<T extends CanisterOptions>(
return serviceCall(
this.principal as any,
key,
value.paramsIdls,
value.returnIdl
value.paramCandidTypes,
value.returnCandidType
)(...args);
}
}
Expand Down Expand Up @@ -189,19 +184,6 @@ export function Canister<T extends CanisterOptions>(
const key = entry[0];
const value = entry[1](parentOrUndefined);

// if (principal === undefined) {
// return {
// ...acc,
// [key]: (...args: any[]) => {
// return serviceCall(
// principal as any,
// key,
// value.paramsIdls,
// value.returnIdl
// )(...args);
// }
// };
// } else {
return {
...acc,
[key]: {
Expand All @@ -210,8 +192,8 @@ export function Canister<T extends CanisterOptions>(
return serviceCall(
principal as any,
key,
value.paramsIdls,
value.returnIdl
value.paramCandidTypes,
value.returnCandidType
)(...args);
}
}
Expand All @@ -235,11 +217,11 @@ export function Canister<T extends CanisterOptions>(
returnFunction.queries = queries;
returnFunction.updates = updates;
returnFunction.callbacks = callbacks;
(returnFunction.getSystemFunctionIDLs = (
returnFunction.getSystemFunctionIDLs = (
parents: Parent[]
): IDL.FuncClass[] => {
const serviceFunctionInfo: ServiceFunctionInfo =
_azleCanisterOptions;
const serviceFunctionInfo =
_azleCanisterOptions as unknown as ServiceFunctionInfo;

return Object.entries(serviceFunctionInfo).reduce(
(accumulator, [_methodName, functionInfo]) => {
Expand All @@ -250,11 +232,11 @@ export function Canister<T extends CanisterOptions>(
}

const paramRealIdls = toParamIDLTypes(
functionInfo(parentOrUndefined).paramsIdls,
functionInfo(parentOrUndefined).paramCandidTypes,
parents
);
const returnRealIdl = toReturnIDLType(
functionInfo(parentOrUndefined).returnIdl,
functionInfo(parentOrUndefined).returnCandidType,
parents
);
return [
Expand All @@ -264,101 +246,56 @@ export function Canister<T extends CanisterOptions>(
},
[] as IDL.FuncClass[]
);
}),
(returnFunction.getIDL = (parents: Parent[]): IDL.ServiceClass => {
const serviceFunctionInfo: ServiceFunctionInfo =
_azleCanisterOptions;

const record = Object.entries(serviceFunctionInfo).reduce(
(accumulator, [methodName, functionInfo]) => {
const paramRealIdls = toParamIDLTypes(
functionInfo(parentOrUndefined).paramsIdls,
parents
);
const returnRealIdl = toReturnIDLType(
functionInfo(parentOrUndefined).returnIdl,
parents
);

const mode = functionInfo(parentOrUndefined).mode;
let annotations: string[] = [];
if (mode === 'update') {
// do nothing
} else if (mode === 'query') {
annotations = ['query'];
} else {
// We don't want init, post upgrade, etc showing up in the idl
return accumulator;
}
};
returnFunction.getIDL = (parents: Parent[]): IDL.ServiceClass => {
const serviceFunctionInfo =
_azleCanisterOptions as unknown as ServiceFunctionInfo;

return {
...accumulator,
[methodName]: IDL.Func(
paramRealIdls,
returnRealIdl,
annotations
)
};
},
{} as Record<string, IDL.FuncClass>
);
const record = Object.entries(serviceFunctionInfo).reduce(
(accumulator, [methodName, functionInfo]) => {
const paramRealIdls = toParamIDLTypes(
functionInfo(parentOrUndefined).paramCandidTypes,
parents
);
const returnRealIdl = toReturnIDLType(
functionInfo(parentOrUndefined).returnCandidType,
parents
);

return IDL.Service(record);
});
const mode = functionInfo(parentOrUndefined).mode;
let annotations: string[] = [];
if (mode === 'update') {
// do nothing
} else if (mode === 'query') {
annotations = ['query'];
} else {
// We don't want init, post upgrade, etc showing up in the idl
return accumulator;
}

return {
...accumulator,
[methodName]: IDL.Func(
paramRealIdls,
returnRealIdl,
annotations
)
};
},
{} as Record<string, IDL.FuncClass>
);

return IDL.Service(record);
};

if (originalPrincipal !== undefined && originalPrincipal._isPrincipal) {
return returnFunction(originalPrincipal);
}

return returnFunction;

// TODO loop through each key and simply grab the candid off
// TODO grab the init/post_upgrade candid as well
// return {
// candid: `${
// candidTypes.length === 0 ? '' : candidTypes.join('\n') + '\n'
// }service: () -> {
// ${Object.entries(serviceOptions)
// .map((entry) => {
// return `${entry[0]}: ${entry[1].candid}`;
// })
// .join('\n ')}
// }
// `,
// queries,
// updates,
// callbacks,
// principal,
// ...callbacks, // TODO then we can't use any names that could collide in this object
// getIDL(parents: Parent[]): IDL.ServiceClass {
// const serviceFunctionInfo: ServiceFunctionInfo = serviceOptions;

// const record = Object.entries(serviceFunctionInfo).reduce(
// (accumulator, [methodName, functionInfo]) => {
// const paramRealIdls = toParamIDLTypes(functionInfo.paramsIdls);
// const returnRealIdl = toReturnIDLType(functionInfo.returnIdl);

// const annotations =
// functionInfo.mode === 'update' ? [] : ['query'];

// return {
// ...accumulator,
// [methodName]: IDL.Func(
// paramRealIdls,
// returnRealIdl,
// annotations
// )
// };
// },
// {} as Record<string, IDL.FuncClass>
// );

// return IDL.Service(record);
// }
// } as any;
};
result._azleIsCanister = true;
return result;
return result as any;
}

type CallRawFunction = typeof ic.callRaw | typeof ic.callRaw128;
Expand Down Expand Up @@ -422,10 +359,10 @@ function createGlobalGuard(

type FunctionInfo = {
mode: 'query' | 'update';
paramIdls: any[];
returnIdl: any;
paramCandidTypes: CandidType[];
returnCandidType: CandidType;
};

interface ServiceFunctionInfo {
[key: string]: FunctionInfo;
[key: string]: (parent: _AzleRecursiveFunction | undefined) => FunctionInfo;
}
6 changes: 3 additions & 3 deletions src/lib/canister_methods/heartbeat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ export function heartbeat(
return {
mode: 'heartbeat',
callback: finalCallback,
paramsIdls: [],
returnIdl: Void,
paramCandidTypes: [],
returnCandidType: Void,
async: isAsync(callback),
guard: undefined
};
} as CanisterMethodInfo<[], Void>;
}) as any;
}
Loading

0 comments on commit 3451dcf

Please sign in to comment.