Skip to content

Commit

Permalink
idl to idlType
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemann committed Jul 12, 2024
1 parent f6c2c8c commit 2f2e30d
Show file tree
Hide file tree
Showing 74 changed files with 340 additions and 316 deletions.
4 changes: 2 additions & 2 deletions examples/composite_queries/src/canister1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ const CompQueryCanister = Canister({
);
const responseJson = await response.json();

ic.reply({ data: responseJson, type: text });
ic.reply({ data: responseJson, candidType: text });
} else {
ic.reply({
data: await ic.call(canister2.manualQuery),
type: text
candidType: text
});
}
},
Expand Down
2 changes: 1 addition & 1 deletion examples/composite_queries/src/canister2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default Canister({
() => {
ic.reply({
data: 'Hello from Canister 2 manual query',
type: text
candidType: text
});
},
{ manual: true }
Expand Down
44 changes: 22 additions & 22 deletions examples/manual_reply/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export default Canister({
return;
}

ic.reply({ data: message, type: text });
ic.reply({ data: message, candidType: text });
},
{ manual: true }
),
Expand All @@ -91,7 +91,7 @@ export default Canister({
data: new Uint8Array([
83, 117, 114, 112, 114, 105, 115, 101, 33
]),
type: blob
candidType: blob
});
},
{ manual: true }
Expand All @@ -100,39 +100,39 @@ export default Canister({
[],
Manual(float32),
() => {
ic.reply({ data: 1245.678, type: float32 });
ic.reply({ data: 1245.678, candidType: float32 });
},
{ manual: true }
),
updateInt8: update(
[],
Manual(int8),
() => {
ic.reply({ data: -100, type: int8 });
ic.reply({ data: -100, candidType: int8 });
},
{ manual: true }
),
updateNat: update(
[],
Manual(nat),
() => {
ic.reply({ data: 184467440737095516150n, type: nat });
ic.reply({ data: 184467440737095516150n, candidType: nat });
},
{ manual: true }
),
updateNull: update(
[],
Manual(Null),
() => {
ic.reply({ data: null, type: Null });
ic.reply({ data: null, candidType: Null });
},
{ manual: true }
),
updateVoid: update(
[],
Manual(Void),
() => {
ic.reply({ data: undefined, type: Void });
ic.reply({ data: undefined, candidType: Void });
},
{ manual: true }
),
Expand All @@ -156,23 +156,23 @@ export default Canister({
Gas: { Elemental: null }
}
};
ic.reply({ data: element, type: Element });
ic.reply({ data: element, candidType: Element });
},
{ manual: true }
),
updateReserved: update(
[],
Manual(reserved),
() => {
ic.reply({ data: undefined, type: reserved });
ic.reply({ data: undefined, candidType: reserved });
},
{ manual: true }
),
updateString: update(
[],
Manual(text),
() => {
ic.reply({ data: 'hello', type: text });
ic.reply({ data: 'hello', candidType: text });
},
{ manual: true }
),
Expand All @@ -181,7 +181,7 @@ export default Canister({
Manual(Gas),
() => {
const gas = { Toxic: null };
ic.reply({ data: gas, type: Gas });
ic.reply({ data: gas, candidType: Gas });
},
{ manual: true }
),
Expand All @@ -207,7 +207,7 @@ export default Canister({
return;
}

ic.reply({ data: message, type: text });
ic.reply({ data: message, candidType: text });
},
{ manual: true }
),
Expand All @@ -219,7 +219,7 @@ export default Canister({
data: new Uint8Array([
83, 117, 114, 112, 114, 105, 115, 101, 33
]),
type: blob
candidType: blob
});
},
{ manual: true }
Expand All @@ -228,39 +228,39 @@ export default Canister({
[],
Manual(float32),
() => {
ic.reply({ data: 1245.678, type: float32 });
ic.reply({ data: 1245.678, candidType: float32 });
},
{ manual: true }
),
queryInt8: query(
[],
Manual(int8),
() => {
ic.reply({ data: -100, type: int8 });
ic.reply({ data: -100, candidType: int8 });
},
{ manual: true }
),
queryNat: query(
[],
Manual(nat),
() => {
ic.reply({ data: 184_467_440_737_095_516_150n, type: nat });
ic.reply({ data: 184_467_440_737_095_516_150n, candidType: nat });
},
{ manual: true }
),
queryNull: query(
[],
Manual(Null),
() => {
ic.reply({ data: null, type: Null });
ic.reply({ data: null, candidType: Null });
},
{ manual: true }
),
queryVoid: query(
[],
Manual(Void),
() => {
ic.reply({ data: undefined, type: Void });
ic.reply({ data: undefined, candidType: Void });
},
{ manual: true }
),
Expand All @@ -284,23 +284,23 @@ export default Canister({
Gas: { Elemental: null }
}
};
ic.reply({ data: element, type: Element });
ic.reply({ data: element, candidType: Element });
},
{ manual: true }
),
queryReserved: query(
[],
Manual(reserved),
() => {
ic.reply({ data: undefined, type: reserved });
ic.reply({ data: undefined, candidType: reserved });
},
{ manual: true }
),
queryString: query(
[],
Manual(text),
() => {
ic.reply({ data: 'hello', type: text });
ic.reply({ data: 'hello', candidType: text });
},
{ manual: true }
),
Expand All @@ -309,7 +309,7 @@ export default Canister({
Manual(Gas),
() => {
const gas = { Toxic: null };
ic.reply({ data: gas, type: Gas });
ic.reply({ data: gas, candidType: Gas });
},
{ manual: true }
)
Expand Down
4 changes: 2 additions & 2 deletions property_tests/arbitraries/canister_arb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export function CanisterArb<
const initArgs = config.initMethod?.params.map((param) => {
const value = param.value.value;
return value.runtimeCandidTypeObject
.getIdl([])
.getIdlType([])
.accept(new CliStringVisitor(), {
value: value.agentArgumentValue
});
Expand All @@ -81,7 +81,7 @@ export function CanisterArb<
(param) => {
const value = param.value.value;
return value.runtimeCandidTypeObject
.getIdl([])
.getIdlType([])
.accept(new CliStringVisitor(), {
value: value.agentArgumentValue
});
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/compile_typescript_code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ export async function compileTypeScriptToJavaScript(
const canisterMethods = CanisterMethods.default !== undefined ? CanisterMethods.default() : Server(() => globalThis._azleNodeServer)();
globalThis.candidInfoFunction = () => {
const candidInfo = canisterMethods.getIdl([]).accept(new DidVisitor(), {
const candidInfo = canisterMethods.getIdlType([]).accept(new DidVisitor(), {
...getDefaultVisitorData(),
isFirstService: true,
systemFuncs: canisterMethods.getSystemFunctionIdls()
systemFuncs: canisterMethods.getSystemFunctionIdlTypes()
});
return JSON.stringify({
Expand Down
4 changes: 2 additions & 2 deletions src/lib/candid/candid_type.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IDL } from '@dfinity/candid';

import { Parent } from './to_idl';
import { Parent } from './to_idl_type';

export interface CandidType {
getIdl(parents: Parent[]): IDL.Type<any>;
getIdlType(parents: Parent[]): IDL.Type<any>;
}
2 changes: 1 addition & 1 deletion src/lib/candid/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export * from './candid_type';
export * from './manual';
export * from './recursive';
export * from './to_idl';
export * from './to_idl_type';
export * from './type_mapping';
export * from './types/constructed';
export * from './types/primitive';
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 @@ -7,7 +7,7 @@ export type _AzleRecursiveFunction = {
(...args: any[]): CandidType;
azleName?: string;
isRecursive?: boolean;
getIdl?: (parents: Parent[]) => IDL.Type<any>;
getIdlType?: (parents: Parent[]) => IDL.Type<any>;
};

export function Recursive(candidTypeCallback: any): any {
Expand All @@ -25,14 +25,14 @@ export function Recursive(candidTypeCallback: any): any {
result.isRecursive = true;
// TODO make this function's return type explicit https://github.com/demergent-labs/azle/issues/1860
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
result.getIdl = (parents: Parent[]) => {
const idl = IDL.Rec();
result.getIdlType = (parents: Parent[]) => {
const idlType = IDL.Rec();
let filler = candidTypeCallback();
if (filler.isCanister) {
filler = filler(result);
}
idl.fill(filler.getIdl([...parents, { idl: idl, name }]));
return idl;
idlType.fill(filler.getIdlType([...parents, { idlType, name }]));
return idlType;
};

return result;
Expand Down
21 changes: 11 additions & 10 deletions src/lib/candid/serde/decode.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IDL } from '@dfinity/candid';

import { CandidType, toIdl, toIdlArray } from '../../candid';
import { CandidType, toIdlType, toIdlTypeArray } from '../../candid';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { AzleOpt, AzleTuple, AzleVec } from '../types/constructed'; // Used for links in comments
import { DecodeVisitor } from './visitors/decode_visitor';
Expand Down Expand Up @@ -32,27 +32,28 @@ function decodeSingle(candidType: CandidType, data: ArrayBuffer): any {
// needs to be aligned so that this isn't an error. Both are representing
// candid IDLs, either from the @dfinity/candid library or the
// Azle-augmented ones
const idl = toIdl(candidType);
const idlType = toIdlType(candidType);

const idlIsAzleVoid = Array.isArray(idl);
// The candid type was AzleVoid if when converted to an Idl Type it is []
const candidTypeIsAzleVoid = Array.isArray(idlType);

if (idlIsAzleVoid) {
if (candidTypeIsAzleVoid) {
return undefined;
}

const candidDecodedValue = IDL.decode([idl], data)[0] as any;
const candidDecodedValue = IDL.decode([idlType], data)[0] as any;

return idl.accept(new DecodeVisitor(), {
return idlType.accept(new DecodeVisitor(), {
candidType: candidType,
js_data: candidDecodedValue
});
}

function decodeMultiple(candidTypes: CandidType[], data: ArrayBuffer): any[] {
const idls = toIdlArray(candidTypes);
const decoded = IDL.decode(idls, data);
return idls.map((idl, index) =>
idl.accept(new DecodeVisitor(), {
const idlTypes = toIdlTypeArray(candidTypes);
const decoded = IDL.decode(idlTypes, data);
return idlTypes.map((idlType, index) =>
idlType.accept(new DecodeVisitor(), {
candidType: candidTypes[index],
js_data: decoded[index]
})
Expand Down
19 changes: 10 additions & 9 deletions src/lib/candid/serde/encode.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IDL } from '@dfinity/candid';

import { CandidType, toIdl, toIdlArray } from '../../candid';
import { CandidType, toIdlType, toIdlTypeArray } from '../../candid';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { AzleOpt, AzleTuple, AzleVec } from '../types/constructed'; // Used for links in comments
import { EncodeVisitor } from './visitors/encode_visitor';
Expand Down Expand Up @@ -33,30 +33,31 @@ export function encode<T = any>(
}

function encodeSingle(candidType: CandidType, data: any): Uint8Array {
const idl = toIdl(candidType);
const idlType = toIdlType(candidType);

const idlIsAzleVoid = Array.isArray(idl);
// The candid type was AzleVoid if when converted to an Idl Type it is []
const candidTypeIsAzleVoid = Array.isArray(idlType);

if (idlIsAzleVoid) {
if (candidTypeIsAzleVoid) {
return new Uint8Array(IDL.encode([], []));
}

const encodeReadyKey = idl.accept(new EncodeVisitor(), {
const encodeReadyKey = idlType.accept(new EncodeVisitor(), {
candidType: candidType,
js_data: data
});

return new Uint8Array(IDL.encode([idl], [encodeReadyKey]));
return new Uint8Array(IDL.encode([idlType], [encodeReadyKey]));
}

function encodeMultiple(candidTypes: CandidType[], data: any[]): Uint8Array {
const idls = toIdlArray(candidTypes);
const idlTypes = toIdlTypeArray(candidTypes);
const values = data.map((datum, index) =>
idls[index].accept(new EncodeVisitor(), {
idlTypes[index].accept(new EncodeVisitor(), {
candidType: candidTypes[index],
js_data: datum
})
);

return new Uint8Array(IDL.encode(idls, values));
return new Uint8Array(IDL.encode(idlTypes, values));
}
Loading

0 comments on commit 2f2e30d

Please sign in to comment.