From 7a00140081f04146e48a3f9d4dd96a032deb9b12 Mon Sep 17 00:00:00 2001 From: Benjamin DeMann Date: Mon, 2 Oct 2023 14:50:44 -0600 Subject: [PATCH] reorganize directory structure --- .../generate_candid_and_canister_methods.ts | 6 +- src/lib/candid/constructed/blob.ts | 13 + src/lib/candid/constructed/index.ts | 24 ++ src/lib/candid/constructed/option.ts | 40 +++ .../{reference => constructed}/record.ts | 3 +- src/lib/candid/constructed/tuple.ts | 23 ++ .../variant/index.ts} | 8 +- .../variant}/result.ts | 5 +- src/lib/candid/constructed/vector.ts | 21 ++ src/lib/candid/index.ts | 96 ++++- src/lib/candid/primitive.ts | 0 src/lib/candid/primitive/bool.ts | 13 + src/lib/candid/primitive/empty.ts | 13 + .../primitive/floating_point_numbers.ts | 24 ++ src/lib/candid/primitive/index.ts | 9 + src/lib/candid/primitive/integer_numbers.ts | 57 +++ src/lib/candid/primitive/natural_numbers.ts | 57 +++ src/lib/candid/primitive/null.ts | 13 + src/lib/candid/primitive/reserved.ts | 13 + src/lib/candid/primitive/text.ts | 13 + src/lib/candid/primitive/void.ts | 11 + src/lib/candid/reference/func.ts | 10 +- src/lib/candid/reference/index.ts | 3 +- src/lib/candid/reference/primitives.ts | 329 ------------------ src/lib/candid/reference/principal.ts | 10 + src/lib/candid/reference/recursive.ts | 2 +- src/lib/candid/reference/service.ts | 12 +- src/lib/candid/{serde.ts => serde/index.ts} | 7 +- .../serde/visitors}/decode_visitor.ts | 2 +- .../serde/visitors}/encode_visitor.ts | 0 .../serde/visitors}/index.ts | 2 +- src/lib/canister_methods/heartbeat.ts | 5 +- src/lib/canister_methods/index.ts | 22 +- src/lib/canister_methods/init.ts | 3 +- src/lib/canister_methods/inspect_message.ts | 3 +- src/lib/canister_methods/post_upgrade.ts | 10 +- src/lib/canister_methods/pre_upgrade.ts | 5 +- src/lib/canister_methods/query.ts | 12 +- src/lib/canister_methods/update.ts | 12 +- src/lib/{visitors => }/did_visitor.ts | 77 ++-- src/lib/ic.ts | 7 +- src/lib/index.ts | 4 +- src/lib/stable_b_tree_map.ts | 14 +- src/lib/utils.ts | 127 ------- 44 files changed, 583 insertions(+), 557 deletions(-) create mode 100644 src/lib/candid/constructed/blob.ts create mode 100644 src/lib/candid/constructed/index.ts create mode 100644 src/lib/candid/constructed/option.ts rename src/lib/candid/{reference => constructed}/record.ts (86%) create mode 100644 src/lib/candid/constructed/tuple.ts rename src/lib/candid/{reference/variant.ts => constructed/variant/index.ts} (87%) rename src/lib/candid/{reference => constructed/variant}/result.ts (87%) create mode 100644 src/lib/candid/constructed/vector.ts delete mode 100644 src/lib/candid/primitive.ts create mode 100644 src/lib/candid/primitive/bool.ts create mode 100644 src/lib/candid/primitive/empty.ts create mode 100644 src/lib/candid/primitive/floating_point_numbers.ts create mode 100644 src/lib/candid/primitive/index.ts create mode 100644 src/lib/candid/primitive/integer_numbers.ts create mode 100644 src/lib/candid/primitive/natural_numbers.ts create mode 100644 src/lib/candid/primitive/null.ts create mode 100644 src/lib/candid/primitive/reserved.ts create mode 100644 src/lib/candid/primitive/text.ts create mode 100644 src/lib/candid/primitive/void.ts delete mode 100644 src/lib/candid/reference/primitives.ts create mode 100644 src/lib/candid/reference/principal.ts rename src/lib/candid/{serde.ts => serde/index.ts} (93%) rename src/lib/{visitors/encode_decode => candid/serde/visitors}/decode_visitor.ts (97%) rename src/lib/{visitors/encode_decode => candid/serde/visitors}/encode_visitor.ts (100%) rename src/lib/{visitors/encode_decode => candid/serde/visitors}/index.ts (98%) rename src/lib/{visitors => }/did_visitor.ts (97%) delete mode 100644 src/lib/utils.ts diff --git a/src/compiler/generate_candid_and_canister_methods.ts b/src/compiler/generate_candid_and_canister_methods.ts index 6a2da1984c..74e12c731c 100644 --- a/src/compiler/generate_candid_and_canister_methods.ts +++ b/src/compiler/generate_candid_and_canister_methods.ts @@ -1,9 +1,9 @@ import { CanisterMethods } from './utils/types'; import { DEFAULT_VISITOR_DATA, - DidResultToCandidString, + didResultToCandidString, DidVisitor -} from '../lib/visitors/did_visitor'; +} from '../lib/did_visitor'; export function generateCandidAndCanisterMethods(mainJs: string): { candid: string; @@ -43,7 +43,7 @@ export function generateCandidAndCanisterMethods(mainJs: string): { }); return { - candid: DidResultToCandidString(candidInfo), + candid: didResultToCandidString(candidInfo), canisterMethods: canisterMethods }; } diff --git a/src/lib/candid/constructed/blob.ts b/src/lib/candid/constructed/blob.ts new file mode 100644 index 0000000000..55c8859abb --- /dev/null +++ b/src/lib/candid/constructed/blob.ts @@ -0,0 +1,13 @@ +import { IDL } from '../..'; + +export class AzleBlob { + _kind: 'AzleBlob' = 'AzleBlob'; + _azleCandidType?: '_azleCandidType'; + + static getIDL() { + return IDL.Vec(IDL.Nat8); + } +} + +export const blob: AzleBlob = AzleBlob as any; +export type blob = Uint8Array; diff --git a/src/lib/candid/constructed/index.ts b/src/lib/candid/constructed/index.ts new file mode 100644 index 0000000000..1665105762 --- /dev/null +++ b/src/lib/candid/constructed/index.ts @@ -0,0 +1,24 @@ +import { Parent, toIDLType } from '../../'; + +export * from './blob'; +export * from './option'; +export * from './record'; +export * from './tuple'; +export * from './variant'; +export * from './vector'; + +type CandidMap = { [key: string]: any }; + +export function processMap(targetMap: CandidMap, parent: Parent[]): CandidMap { + const newMap: CandidMap = {}; + + for (const key in targetMap) { + if (targetMap.hasOwnProperty(key)) { + const value = targetMap[key]; + const newValue = toIDLType(value, parent); + newMap[key] = newValue; + } + } + + return newMap; +} diff --git a/src/lib/candid/constructed/option.ts b/src/lib/candid/constructed/option.ts new file mode 100644 index 0000000000..efddd2332b --- /dev/null +++ b/src/lib/candid/constructed/option.ts @@ -0,0 +1,40 @@ +import { CandidType, RequireExactlyOne } from '..'; +import { IDL, Parent, toIDLType } from '../..'; + +/** + * Represents an optional value: every {@link Opt} is either `Some` and contains + * a value, or `None` and does not. + */ +export type Opt = RequireExactlyOne<{ Some: T; None: null }>; + +/** + * Wraps the provided value in a `Some` {@link Opt} + * @param value - the value to be wrapped + * @returns a `Some` {@link Opt} containing the provided value + */ +export function Some(value: T) { + return { Some: value }; +} + +/** An {@link Opt} representing the absence of a value */ +export const None = { None: null }; + +// TODO what happens if we pass something to Opt() that can't be converted to CandidClass? +export function Opt(t: T): AzleOpt { + // return IDL.Opt(toCandidClass(t)); + return new AzleOpt(t); +} + +export class AzleOpt { + constructor(t: any) { + this._azleType = t; + } + + _azleType: CandidType; + _azleCandidType?: '_azleCandidType'; + _kind: 'AzleOpt' = 'AzleOpt'; + + getIDL(parents: Parent[]) { + return IDL.Opt(toIDLType(this._azleType, parents)); + } +} diff --git a/src/lib/candid/reference/record.ts b/src/lib/candid/constructed/record.ts similarity index 86% rename from src/lib/candid/reference/record.ts rename to src/lib/candid/constructed/record.ts index 9ae0de0a13..87610d21cf 100644 --- a/src/lib/candid/reference/record.ts +++ b/src/lib/candid/constructed/record.ts @@ -1,6 +1,7 @@ import { CandidType, TypeMapping } from '..'; import { IDL } from '@dfinity/candid'; -import { Parent, processMap } from '../../utils'; +import { Parent } from '../../to_idl_type'; +import { processMap } from '..'; import { v4 } from 'uuid'; export function Record< diff --git a/src/lib/candid/constructed/tuple.ts b/src/lib/candid/constructed/tuple.ts new file mode 100644 index 0000000000..a7a623f94e --- /dev/null +++ b/src/lib/candid/constructed/tuple.ts @@ -0,0 +1,23 @@ +import { CandidType } from '..'; +import { Parent, toIDLType, IDL } from '../../'; + +export class AzleTuple { + constructor(t: CandidType[]) { + this._azleTypes = t; + } + + _azleTypes: CandidType[]; + _azleCandidType?: '_azleCandidType'; + + getIDL(parents: Parent[]) { + const candidTypes = this._azleTypes.map((value) => { + return toIDLType(value, parents); + }); + return IDL.Tuple(...candidTypes); + } +} + +export function Tuple(...types: T): AzleTuple { + return new AzleTuple(types); +} +export type Tuple = T; diff --git a/src/lib/candid/reference/variant.ts b/src/lib/candid/constructed/variant/index.ts similarity index 87% rename from src/lib/candid/reference/variant.ts rename to src/lib/candid/constructed/variant/index.ts index b312da411d..583722c1f3 100644 --- a/src/lib/candid/reference/variant.ts +++ b/src/lib/candid/constructed/variant/index.ts @@ -1,8 +1,10 @@ -import { CandidType, TypeMapping } from '..'; +import { CandidType, TypeMapping } from '../..'; import { IDL } from '@dfinity/candid'; -import { processMap } from '../../utils'; +import { processMap } from '../'; import { v4 } from 'uuid'; -import { Null } from './primitives'; +import { Null } from '../..'; + +export * from './result'; export function Variant< T extends { diff --git a/src/lib/candid/reference/result.ts b/src/lib/candid/constructed/variant/result.ts similarity index 87% rename from src/lib/candid/reference/result.ts rename to src/lib/candid/constructed/variant/result.ts index b18484b0be..9fac837954 100644 --- a/src/lib/candid/reference/result.ts +++ b/src/lib/candid/constructed/variant/result.ts @@ -1,6 +1,5 @@ -import { RequireExactlyOne } from './variant'; -import { CandidType, IDL } from '../..'; -import { Parent, toIDLType } from '../../utils'; +import { Parent, RequireExactlyOne, toIDLType } from '../..'; +import { CandidType, IDL } from '../../..'; export class AzleResult { constructor(ok: any, err: any) { diff --git a/src/lib/candid/constructed/vector.ts b/src/lib/candid/constructed/vector.ts new file mode 100644 index 0000000000..1664863788 --- /dev/null +++ b/src/lib/candid/constructed/vector.ts @@ -0,0 +1,21 @@ +import { CandidType } from '..'; +import { IDL, Parent, toIDLType } from '../..'; + +export class AzleVec { + constructor(t: any) { + this._azleType = t; + } + + _azleType: CandidType; + _azleCandidType?: '_azleCandidType'; + + getIDL(parents: Parent[]) { + return IDL.Vec(toIDLType(this._azleType, parents)); + } +} + +export type Vec = T[]; +export function Vec(t: T): AzleVec { + // return IDL.Vec(toCandidClass(t)); + return new AzleVec(t); +} diff --git a/src/lib/candid/index.ts b/src/lib/candid/index.ts index ae995ebcbe..250587a77b 100644 --- a/src/lib/candid/index.ts +++ b/src/lib/candid/index.ts @@ -1,4 +1,4 @@ -export * from './reference'; +import { IDL } from '..'; import { AzleBlob, blob, @@ -16,6 +16,7 @@ import { AzleNat8, AzleFloat64, AzleFloat32, + AzleResult, nat, nat64, nat32, @@ -40,9 +41,13 @@ import { AzleTuple, AzleText, AzleVoid, - Opt -} from '../candid/reference/primitives'; -import { AzleResult, Result } from './reference/result'; + Opt, + Result +} from './'; + +export * from './constructed'; +export * from './primitive'; +export * from './reference'; export type TypeMapping = RecursionLevel extends 10 ? T @@ -124,3 +129,86 @@ export type TypeMapping = RecursionLevel extends 10 export type CandidType = { _azleCandidType?: '_azleCandidType'; }; + +export type Parent = { + idl: IDL.RecClass; + name: string; +}; + +export function toIDLType(idl: CandidType, parents: Parent[]): IDL.Type { + if ('getIDL' in idl) { + if ('_azleName' in idl) { + const parent = parents.find( + (parent) => parent.name === idl._azleName + ); + if (parent !== undefined) { + return { + ...parent.idl, + _azleName: idl._azleName, + name: parent.idl.name, + valueToString: (x): string => { + return parent.idl.valueToString(x); + }, + buildTypeTable: (typeTable): void => { + return parent.idl.buildTypeTable(typeTable); + }, + covariant: (x): x is any => { + return parent.idl.covariant(x); + }, + encodeType: (typeTable): ArrayBuffer => { + return parent.idl.encodeType(typeTable); + }, + checkType: (t) => { + return parent.idl.checkType(t); + }, + _buildTypeTableImpl: (typeTable): void => { + return parent.idl._buildTypeTableImpl(typeTable); + }, + // TODO check if this is still being called. maybe by adding a throw here and see if we hit it + display: () => parent.idl.name, + decodeValue: (b, t) => { + return parent.idl.decodeValue(b, t); + }, + encodeValue: (b) => { + return parent.idl.encodeValue(b); + }, + accept: (v, d) => { + return parent.idl.accept(v, d); + }, + _id: parent.idl._id, + _type: parent.idl._type, + fill: (t: any): void => { + parent.idl.fill(t); + }, + getType: () => { + return parent.idl.getType(); + } + }; + } + } + return idl.getIDL(parents); + } + if (idl._azleIsCanister) { + return toIDLType(idl(), parents); + } + // if (idl.display === undefined || idl.getIDL === undefined) { + // throw Error(`${JSON.stringify(idl)} is not a candid type`); + // } + return idl; +} + +export function toParamIDLTypes( + idl: CandidType[], + parents: Parent[] = [] +): IDL.Type[] { + return idl.map((value) => toIDLType(value, parents)); +} + +export function toReturnIDLType( + returnIdl: CandidType, + parents: Parent[] = [] +): IDL.Type[] { + const idlType = toIDLType(returnIdl, parents); + + return Array.isArray(idlType) ? idlType : [idlType]; +} diff --git a/src/lib/candid/primitive.ts b/src/lib/candid/primitive.ts deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/src/lib/candid/primitive/bool.ts b/src/lib/candid/primitive/bool.ts new file mode 100644 index 0000000000..71f3ef3e7f --- /dev/null +++ b/src/lib/candid/primitive/bool.ts @@ -0,0 +1,13 @@ +import { IDL } from '../..'; + +export class AzleBool { + _kind: 'AzleBool' = 'AzleBool'; + _azleCandidType?: '_azleCandidType'; + + static getIDL() { + return IDL.Bool; + } +} + +export const bool: AzleBool = AzleBool as any; +export type bool = boolean; diff --git a/src/lib/candid/primitive/empty.ts b/src/lib/candid/primitive/empty.ts new file mode 100644 index 0000000000..0401fbdb75 --- /dev/null +++ b/src/lib/candid/primitive/empty.ts @@ -0,0 +1,13 @@ +import { IDL } from '../..'; + +export class AzleEmpty { + _kind: 'AzleEmpty' = 'AzleEmpty'; + _azleCandidType?: '_azleCandidType'; + + static getIDL() { + return IDL.Empty; + } +} + +export const empty: AzleEmpty = AzleEmpty as any; +export type empty = never; diff --git a/src/lib/candid/primitive/floating_point_numbers.ts b/src/lib/candid/primitive/floating_point_numbers.ts new file mode 100644 index 0000000000..168ed3c4a2 --- /dev/null +++ b/src/lib/candid/primitive/floating_point_numbers.ts @@ -0,0 +1,24 @@ +import { IDL } from '../..'; + +export class AzleFloat64 { + _kind: 'AzleFloat64' = 'AzleFloat64'; + _azleCandidType?: '_azleCandidType'; + + static getIDL() { + return IDL.Float64; + } +} + +export class AzleFloat32 { + _kind: 'AzleFloat32' = 'AzleFloat32'; + _azleCandidType?: '_azleCandidType'; + + static getIDL() { + return IDL.Float32; + } +} + +export const float32: AzleFloat32 = AzleFloat32 as any; +export type float32 = number; +export const float64: AzleFloat64 = AzleFloat64 as any; +export type float64 = number; diff --git a/src/lib/candid/primitive/index.ts b/src/lib/candid/primitive/index.ts new file mode 100644 index 0000000000..2e34c13fbc --- /dev/null +++ b/src/lib/candid/primitive/index.ts @@ -0,0 +1,9 @@ +export * from './bool'; +export * from './empty'; +export * from './floating_point_numbers'; +export * from './integer_numbers'; +export * from './natural_numbers'; +export * from './null'; +export * from './reserved'; +export * from './text'; +export * from './void'; diff --git a/src/lib/candid/primitive/integer_numbers.ts b/src/lib/candid/primitive/integer_numbers.ts new file mode 100644 index 0000000000..0be2c1fe0f --- /dev/null +++ b/src/lib/candid/primitive/integer_numbers.ts @@ -0,0 +1,57 @@ +import { IDL } from '../..'; + +export class AzleInt { + _kind: 'AzleInt' = 'AzleInt'; + _azleCandidType?: '_azleCandidType'; + + static getIDL() { + return IDL.Int; + } +} + +export class AzleInt64 { + _kind: 'AzleInt64' = 'AzleInt64'; + _azleCandidType?: '_azleCandidType'; + + static getIDL() { + return IDL.Int64; + } +} + +export class AzleInt32 { + _kind: 'AzleInt32' = 'AzleInt32'; + _azleCandidType?: '_azleCandidType'; + + static getIDL() { + return IDL.Int32; + } +} + +export class AzleInt16 { + _kind: 'AzleInt16' = 'AzleInt16'; + _azleCandidType?: '_azleCandidType'; + + static getIDL() { + return IDL.Int16; + } +} + +export class AzleInt8 { + _kind: 'AzleInt8' = 'AzleInt8'; + _azleCandidType?: '_azleCandidType'; + + static getIDL() { + return IDL.Int8; + } +} + +export const int: AzleInt = AzleInt as any; +export type int = bigint; +export const int8: AzleInt8 = AzleInt8 as any; +export type int8 = number; +export const int16: AzleInt16 = AzleInt16 as any; +export type int16 = number; +export const int32: AzleInt32 = AzleInt32 as any; +export type int32 = number; +export const int64: AzleInt64 = AzleInt64 as any; +export type int64 = bigint; diff --git a/src/lib/candid/primitive/natural_numbers.ts b/src/lib/candid/primitive/natural_numbers.ts new file mode 100644 index 0000000000..134ad3cda0 --- /dev/null +++ b/src/lib/candid/primitive/natural_numbers.ts @@ -0,0 +1,57 @@ +import { IDL } from '../..'; + +export class AzleNat { + _kind: 'AzleNat' = 'AzleNat'; + _azleCandidType?: '_azleCandidType'; + + static getIDL() { + return IDL.Nat; + } +} + +export class AzleNat64 { + _kind: 'AzleNat64' = 'AzleNat64'; + _azleCandidType?: '_azleCandidType'; + + static getIDL() { + return IDL.Nat64; + } +} + +export class AzleNat32 { + _kind: 'AzleNat32' = 'AzleNat32'; + _azleCandidType?: '_azleCandidType'; + + static getIDL() { + return IDL.Nat32; + } +} + +export class AzleNat16 { + _kind: 'AzleNat16' = 'AzleNat16'; + _azleCandidType?: '_azleCandidType'; + + static getIDL() { + return IDL.Nat16; + } +} + +export class AzleNat8 { + _kind: 'AzleNat8' = 'AzleNat8'; + _azleCandidType?: '_azleCandidType'; + + static getIDL() { + return IDL.Nat8; + } +} + +export const nat: AzleNat = AzleNat as any; +export type nat = bigint; +export const nat8: AzleNat8 = AzleNat8 as any; +export type nat8 = number; +export const nat16: AzleNat16 = AzleNat16 as any; +export type nat16 = number; +export const nat32: AzleNat32 = AzleNat32 as any; +export type nat32 = number; +export const nat64: AzleNat64 = AzleNat64 as any; +export type nat64 = bigint; diff --git a/src/lib/candid/primitive/null.ts b/src/lib/candid/primitive/null.ts new file mode 100644 index 0000000000..a47f806cfa --- /dev/null +++ b/src/lib/candid/primitive/null.ts @@ -0,0 +1,13 @@ +import { IDL } from '../..'; + +export class AzleNull { + _kind: 'AzleNull' = 'AzleNull'; + _azleCandidType?: '_azleCandidType'; + + static getIDL() { + return IDL.Null; + } +} + +export const Null: AzleNull = AzleNull as any; +export type Null = null; diff --git a/src/lib/candid/primitive/reserved.ts b/src/lib/candid/primitive/reserved.ts new file mode 100644 index 0000000000..260aba48b3 --- /dev/null +++ b/src/lib/candid/primitive/reserved.ts @@ -0,0 +1,13 @@ +import { IDL } from '../..'; + +export class AzleReserved { + _kind: 'AzleReserved' = 'AzleReserved'; + _azleCandidType?: '_azleCandidType'; + + static getIDL() { + return IDL.Reserved; + } +} + +export const reserved: AzleReserved = AzleReserved as any; +export type reserved = any; diff --git a/src/lib/candid/primitive/text.ts b/src/lib/candid/primitive/text.ts new file mode 100644 index 0000000000..742cc4be0b --- /dev/null +++ b/src/lib/candid/primitive/text.ts @@ -0,0 +1,13 @@ +import { IDL } from '../..'; + +export class AzleText { + _kind: 'AzleText' = 'AzleText'; + _azleCandidType?: '_azleCandidType'; + + static getIDL() { + return IDL.Text; + } +} + +export const text: AzleText = AzleText as any; +export type text = string; diff --git a/src/lib/candid/primitive/void.ts b/src/lib/candid/primitive/void.ts new file mode 100644 index 0000000000..1bb3bca7e2 --- /dev/null +++ b/src/lib/candid/primitive/void.ts @@ -0,0 +1,11 @@ +export class AzleVoid { + _kind: 'AzleVoid' = 'AzleVoid'; + _azleCandidType?: '_azleCandidType'; + + static getIDL() { + return []; + } +} + +export const Void: AzleVoid = AzleVoid as any; +export type Void = void; diff --git a/src/lib/candid/reference/func.ts b/src/lib/candid/reference/func.ts index becd0ced06..fc8479f055 100644 --- a/src/lib/candid/reference/func.ts +++ b/src/lib/candid/reference/func.ts @@ -1,8 +1,12 @@ import { CandidType } from '..'; -import { IDL } from '@dfinity/candid'; -import { Parent, toParamIDLTypes, toReturnIDLType } from '../../utils'; import { v4 } from 'uuid'; -import { Principal } from '../../'; +import { + Principal, + Parent, + toParamIDLTypes, + toReturnIDLType, + IDL +} from '../../'; type Mode = 'query' | 'update' | 'oneway'; diff --git a/src/lib/candid/reference/index.ts b/src/lib/candid/reference/index.ts index 21ef2709f3..de79720dc2 100644 --- a/src/lib/candid/reference/index.ts +++ b/src/lib/candid/reference/index.ts @@ -1,5 +1,4 @@ export * from './func'; -export * from './record'; export * from './service'; -export * from './variant'; +export * from './principal'; export * from './recursive'; diff --git a/src/lib/candid/reference/primitives.ts b/src/lib/candid/reference/primitives.ts deleted file mode 100644 index c1fcbaafdf..0000000000 --- a/src/lib/candid/reference/primitives.ts +++ /dev/null @@ -1,329 +0,0 @@ -import { IDL } from '../../'; -import { RequireExactlyOne } from './variant'; -import { Parent, toIDLType } from '../../utils'; -import { Principal as DfinityPrincipal } from '@dfinity/principal'; - -export class AzleNat { - _kind: 'AzleNat' = 'AzleNat'; - _azleCandidType?: '_azleCandidType'; - - static getIDL() { - return IDL.Nat; - } -} - -export class AzleNat64 { - _kind: 'AzleNat64' = 'AzleNat64'; - _azleCandidType?: '_azleCandidType'; - - static getIDL() { - return IDL.Nat64; - } -} - -export class AzleNat32 { - _kind: 'AzleNat32' = 'AzleNat32'; - _azleCandidType?: '_azleCandidType'; - - static getIDL() { - return IDL.Nat32; - } -} - -export class AzleNat16 { - _kind: 'AzleNat16' = 'AzleNat16'; - _azleCandidType?: '_azleCandidType'; - - static getIDL() { - return IDL.Nat16; - } -} - -export class AzleNat8 { - _kind: 'AzleNat8' = 'AzleNat8'; - _azleCandidType?: '_azleCandidType'; - - static getIDL() { - return IDL.Nat8; - } -} - -export class AzleInt { - _kind: 'AzleInt' = 'AzleInt'; - _azleCandidType?: '_azleCandidType'; - - static getIDL() { - return IDL.Int; - } -} - -export class AzleInt64 { - _kind: 'AzleInt64' = 'AzleInt64'; - _azleCandidType?: '_azleCandidType'; - - static getIDL() { - return IDL.Int64; - } -} - -export class AzleInt32 { - _kind: 'AzleInt32' = 'AzleInt32'; - _azleCandidType?: '_azleCandidType'; - - static getIDL() { - return IDL.Int32; - } -} - -export class AzleInt16 { - _kind: 'AzleInt16' = 'AzleInt16'; - _azleCandidType?: '_azleCandidType'; - - static getIDL() { - return IDL.Int16; - } -} - -export class AzleInt8 { - _kind: 'AzleInt8' = 'AzleInt8'; - _azleCandidType?: '_azleCandidType'; - - static getIDL() { - return IDL.Int8; - } -} - -export class AzleFloat64 { - _kind: 'AzleFloat64' = 'AzleFloat64'; - _azleCandidType?: '_azleCandidType'; - - static getIDL() { - return IDL.Float64; - } -} - -export class AzleFloat32 { - _kind: 'AzleFloat32' = 'AzleFloat32'; - _azleCandidType?: '_azleCandidType'; - - static getIDL() { - return IDL.Float32; - } -} - -export class AzleBlob { - _kind: 'AzleBlob' = 'AzleBlob'; - _azleCandidType?: '_azleCandidType'; - - static getIDL() { - return IDL.Vec(IDL.Nat8); - } -} - -export class AzleNull { - _kind: 'AzleNull' = 'AzleNull'; - _azleCandidType?: '_azleCandidType'; - - static getIDL() { - return IDL.Null; - } -} - -export class AzleReserved { - _kind: 'AzleReserved' = 'AzleReserved'; - _azleCandidType?: '_azleCandidType'; - - static getIDL() { - return IDL.Reserved; - } -} - -export class AzleEmpty { - _kind: 'AzleEmpty' = 'AzleEmpty'; - _azleCandidType?: '_azleCandidType'; - - static getIDL() { - return IDL.Empty; - } -} - -export class AzleBool { - _kind: 'AzleBool' = 'AzleBool'; - _azleCandidType?: '_azleCandidType'; - - static getIDL() { - return IDL.Bool; - } -} - -export class AzleText { - _kind: 'AzleText' = 'AzleText'; - _azleCandidType?: '_azleCandidType'; - - static getIDL() { - return IDL.Text; - } -} - -export class AzleVoid { - _kind: 'AzleVoid' = 'AzleVoid'; - _azleCandidType?: '_azleCandidType'; - - static getIDL() { - return []; - } -} - -export const bool: AzleBool = AzleBool as any; -export type bool = boolean; -export const blob: AzleBlob = AzleBlob as any; -export type blob = Uint8Array; -export const empty: AzleEmpty = AzleEmpty as any; -export type empty = never; -export const int: AzleInt = AzleInt as any; -export type int = bigint; -export const int8: AzleInt8 = AzleInt8 as any; -export type int8 = number; -export const int16: AzleInt16 = AzleInt16 as any; -export type int16 = number; -export const int32: AzleInt32 = AzleInt32 as any; -export type int32 = number; -export const int64: AzleInt64 = AzleInt64 as any; -export type int64 = bigint; -export const nat: AzleNat = AzleNat as any; -export type nat = bigint; -export const nat8: AzleNat8 = AzleNat8 as any; -export type nat8 = number; -export const nat16: AzleNat16 = AzleNat16 as any; -export type nat16 = number; -export const nat32: AzleNat32 = AzleNat32 as any; -export type nat32 = number; -export const nat64: AzleNat64 = AzleNat64 as any; -export type nat64 = bigint; -export const Null: AzleNull = AzleNull as any; -export type Null = null; -export const reserved: AzleReserved = AzleReserved as any; -export type reserved = any; -export const text: AzleText = AzleText as any; -export type text = string; -export const float32: AzleFloat32 = AzleFloat32 as any; -export type float32 = number; -export const float64: AzleFloat64 = AzleFloat64 as any; -export type float64 = number; -export type Vec = T[]; -export type Tuple = T; - -export class Principal extends DfinityPrincipal { - static _azleCandidType?: '_azleCandidType'; - - static getIDL?() { - return IDL.Principal; - } -} - -/** - * Represents an optional value: every {@link Opt} is either `Some` and contains - * a value, or `None` and does not. - */ -export type Opt = RequireExactlyOne<{ Some: T; None: null }>; -export const Void: AzleVoid = AzleVoid as any; -export type Void = void; - -/** - * Wraps the provided value in a `Some` {@link Opt} - * @param value - the value to be wrapped - * @returns a `Some` {@link Opt} containing the provided value - */ -export function Some(value: T) { - return { Some: value }; -} - -/** An {@link Opt} representing the absence of a value */ -export const None = { None: null }; - -// TODO what happens if we pass something to Opt() that can't be converted to CandidClass? -export function Opt(t: T): AzleOpt { - // return IDL.Opt(toCandidClass(t)); - return new AzleOpt(t); -} - -export class AzleOpt { - constructor(t: any) { - this._azleType = t; - } - - _azleType: CandidClass; - _azleCandidType?: '_azleCandidType'; - _kind: 'AzleOpt' = 'AzleOpt'; - - getIDL(parents: Parent[]) { - return IDL.Opt(toIDLType(this._azleType, parents)); - } -} - -export class AzleVec { - constructor(t: any) { - this._azleType = t; - } - - _azleType: CandidClass; - _azleCandidType?: '_azleCandidType'; - - getIDL(parents: Parent[]) { - return IDL.Vec(toIDLType(this._azleType, parents)); - } -} - -export class AzleTuple { - constructor(t: CandidClass[]) { - this._azleTypes = t; - } - - _azleTypes: CandidClass[]; - _azleCandidType?: '_azleCandidType'; - - getIDL(parents: Parent[]) { - const candidTypes = this._azleTypes.map((value) => { - return toIDLType(value, parents); - }); - return IDL.Tuple(...candidTypes); - } -} - -export function Vec(t: T): AzleVec { - // return IDL.Vec(toCandidClass(t)); - return new AzleVec(t); -} - -// TODO I am not sure of any of these types... but its working so... -export function Tuple(...types: T): AzleTuple { - // const idlTypes = types.map((value) => { - // return toIDLType(value); - // }); - // return IDL.Tuple(...idlTypes); - return new AzleTuple(types); -} - -export function Manual(t: any): AzleVoid { - return t; -} - -type CandidClass = - | IDL.BoolClass - | IDL.EmptyClass - | IDL.FixedIntClass - | IDL.FixedNatClass - | IDL.FloatClass - | IDL.IntClass - | IDL.NatClass - | IDL.NullClass - | IDL.OptClass - | IDL.PrincipalClass - | IDL.RecClass - | IDL.ReservedClass - | IDL.TextClass - | IDL.TupleClass - | IDL.VecClass - | IDL.VecClass; // blob - -type ReturnCandidClass = CandidClass | never[]; diff --git a/src/lib/candid/reference/principal.ts b/src/lib/candid/reference/principal.ts new file mode 100644 index 0000000000..aa5081732d --- /dev/null +++ b/src/lib/candid/reference/principal.ts @@ -0,0 +1,10 @@ +import { IDL } from '../..'; +import { Principal as DfinityPrincipal } from '@dfinity/principal'; + +export class Principal extends DfinityPrincipal { + static _azleCandidType?: '_azleCandidType'; + + static getIDL?() { + return IDL.Principal; + } +} diff --git a/src/lib/candid/reference/recursive.ts b/src/lib/candid/reference/recursive.ts index 109af485d0..2d5c1da509 100644 --- a/src/lib/candid/reference/recursive.ts +++ b/src/lib/candid/reference/recursive.ts @@ -1,6 +1,6 @@ import { v4 } from 'uuid'; import { IDL } from '@dfinity/candid'; -import { Parent } from '../../utils'; +import { Parent } from '../../to_idl_type'; export function Recursive(idlCallback: any): any { const name = v4(); diff --git a/src/lib/candid/reference/service.ts b/src/lib/candid/reference/service.ts index c58c6062b8..7f8c519b1e 100644 --- a/src/lib/candid/reference/service.ts +++ b/src/lib/candid/reference/service.ts @@ -1,6 +1,12 @@ -import { IDL, Principal, TypeMapping } from '../..'; -import { ic } from '../../'; -import { Parent, toParamIDLTypes, toReturnIDLType } from '../../utils'; +import { + IDL, + Principal, + TypeMapping, + Parent, + toParamIDLTypes, + toReturnIDLType, + ic +} from '../..'; import { CanisterMethodInfo } from '../../canister_methods'; import { decode, encodeMultiple } from '../serde'; diff --git a/src/lib/candid/serde.ts b/src/lib/candid/serde/index.ts similarity index 93% rename from src/lib/candid/serde.ts rename to src/lib/candid/serde/index.ts index a011dfbd4a..dac9d57956 100644 --- a/src/lib/candid/serde.ts +++ b/src/lib/candid/serde/index.ts @@ -1,9 +1,8 @@ import { IDL } from '@dfinity/candid'; -import { AzleVec, AzleOpt, AzleTuple } from '../candid/reference/primitives'; -import { toIDLType } from '../utils'; -import { DecodeVisitor, EncodeVisitor } from '../visitors/encode_decode'; -import { CandidType } from '../'; +import { AzleVec, AzleOpt, AzleTuple } from '..'; +import { DecodeVisitor, EncodeVisitor } from './visitors/'; +import { CandidType, toIDLType } from '../..'; /** * Encodes the provided value as candid blob of the designated type. diff --git a/src/lib/visitors/encode_decode/decode_visitor.ts b/src/lib/candid/serde/visitors/decode_visitor.ts similarity index 97% rename from src/lib/visitors/encode_decode/decode_visitor.ts rename to src/lib/candid/serde/visitors/decode_visitor.ts index bb7bb2e76e..402a79279e 100644 --- a/src/lib/visitors/encode_decode/decode_visitor.ts +++ b/src/lib/candid/serde/visitors/decode_visitor.ts @@ -9,7 +9,7 @@ import { visitVariant, visitVec } from '.'; -import { Opt } from '../../candid/reference/primitives'; +import { Opt } from '../../'; /** * When we decode a Service we are given a principal. We need to use that diff --git a/src/lib/visitors/encode_decode/encode_visitor.ts b/src/lib/candid/serde/visitors/encode_visitor.ts similarity index 100% rename from src/lib/visitors/encode_decode/encode_visitor.ts rename to src/lib/candid/serde/visitors/encode_visitor.ts diff --git a/src/lib/visitors/encode_decode/index.ts b/src/lib/candid/serde/visitors/index.ts similarity index 98% rename from src/lib/visitors/encode_decode/index.ts rename to src/lib/candid/serde/visitors/index.ts index dff9c4fa0a..457fc9c290 100644 --- a/src/lib/visitors/encode_decode/index.ts +++ b/src/lib/candid/serde/visitors/index.ts @@ -1,7 +1,7 @@ import { IDL } from '@dfinity/candid'; import { DecodeVisitor } from './decode_visitor'; import { EncodeVisitor } from './encode_visitor'; -import { AzleResult, Result } from '../../candid/reference/result'; +import { AzleResult, Result } from '../../'; export { EncodeVisitor, DecodeVisitor }; /* diff --git a/src/lib/canister_methods/heartbeat.ts b/src/lib/canister_methods/heartbeat.ts index daad2630ce..15ae3471f1 100644 --- a/src/lib/canister_methods/heartbeat.ts +++ b/src/lib/canister_methods/heartbeat.ts @@ -1,6 +1,5 @@ -import { isAsync } from '../utils'; -import { CanisterMethodInfo, executeMethod } from '.'; -import { Void } from '../candid/reference/primitives'; +import { CanisterMethodInfo, executeMethod, isAsync } from '.'; +import { Void } from '../candid/'; export function heartbeat( callback: () => void | Promise diff --git a/src/lib/canister_methods/index.ts b/src/lib/canister_methods/index.ts index 0c531d74a3..6c4f8c6260 100644 --- a/src/lib/canister_methods/index.ts +++ b/src/lib/canister_methods/index.ts @@ -1,8 +1,8 @@ -import { IDL } from '../'; +import { AzleVoid, IDL } from '../'; import { ic } from '../ic'; import { CandidType, TypeMapping } from '..'; -import { DecodeVisitor, EncodeVisitor } from '../visitors/encode_decode'; -import { Parent } from '../utils'; +import { DecodeVisitor, EncodeVisitor } from '../candid/serde/visitors'; +import { Parent } from '../to_idl_type'; export * from './heartbeat'; export * from './init'; @@ -12,6 +12,10 @@ export * from './pre_upgrade'; export * from './query'; export * from './update'; +export function Manual(t: any): AzleVoid { + return t; +} + export type MethodArgs = { manual?: boolean; guard?: () => void }; export type CanisterMethodInfo, K> = { @@ -142,3 +146,15 @@ export function createParents(parent: any): Parent[] { ? [] : [{ idl: parent, name: parent._azleName }]; } + +export function isAsync(originalFunction: any) { + if (originalFunction[Symbol.toStringTag] === 'AsyncFunction') { + return true; + } else if (originalFunction.constructor.name === 'AsyncFunction') { + return true; + } else if (originalFunction.toString().includes('async ')) { + return true; + } else { + return false; + } +} diff --git a/src/lib/canister_methods/init.ts b/src/lib/canister_methods/init.ts index 62f36f94f2..665dc43162 100644 --- a/src/lib/canister_methods/init.ts +++ b/src/lib/canister_methods/init.ts @@ -1,7 +1,6 @@ import { Callback, CanisterMethodInfo, createParents, executeMethod } from '.'; import { CandidType, TypeMapping } from '../candid'; -import { Void } from '../'; -import { toParamIDLTypes, toReturnIDLType } from '../utils'; +import { Void, toParamIDLTypes, toReturnIDLType } from '../'; export function init< const Params extends ReadonlyArray, diff --git a/src/lib/canister_methods/inspect_message.ts b/src/lib/canister_methods/inspect_message.ts index dd164d84d3..c1c174ae01 100644 --- a/src/lib/canister_methods/inspect_message.ts +++ b/src/lib/canister_methods/inspect_message.ts @@ -1,6 +1,5 @@ import { CanisterMethodInfo, createParents, executeMethod } from '.'; -import { Void } from '../candid/reference/primitives'; -import { toParamIDLTypes, toReturnIDLType } from '../utils'; +import { Void, toParamIDLTypes, toReturnIDLType } from '../candid/'; export function inspectMessage( callback: () => void | Promise diff --git a/src/lib/canister_methods/post_upgrade.ts b/src/lib/canister_methods/post_upgrade.ts index dc401e73f7..d644fd85b3 100644 --- a/src/lib/canister_methods/post_upgrade.ts +++ b/src/lib/canister_methods/post_upgrade.ts @@ -1,7 +1,11 @@ import { Callback, CanisterMethodInfo, createParents, executeMethod } from '.'; -import { CandidType, TypeMapping } from '../candid'; -import { Void } from '../candid/reference/primitives'; -import { toParamIDLTypes, toReturnIDLType } from '../utils'; +import { + CandidType, + TypeMapping, + Void, + toParamIDLTypes, + toReturnIDLType +} from '../candid'; export function postUpgrade< const Params extends ReadonlyArray, diff --git a/src/lib/canister_methods/pre_upgrade.ts b/src/lib/canister_methods/pre_upgrade.ts index 8b29adc5e1..52140811e7 100644 --- a/src/lib/canister_methods/pre_upgrade.ts +++ b/src/lib/canister_methods/pre_upgrade.ts @@ -1,6 +1,5 @@ -import { isAsync } from '../utils'; -import { CanisterMethodInfo, executeMethod } from '.'; -import { Void } from '../candid/reference/primitives'; +import { CanisterMethodInfo, executeMethod, isAsync } from '.'; +import { Void } from '../candid'; export function preUpgrade( callback: () => void | Promise diff --git a/src/lib/canister_methods/query.ts b/src/lib/canister_methods/query.ts index d6b377a731..b598ae089c 100644 --- a/src/lib/canister_methods/query.ts +++ b/src/lib/canister_methods/query.ts @@ -1,13 +1,17 @@ -import { isAsync } from '../utils'; import { Callback, CanisterMethodInfo, MethodArgs, createParents, - executeMethod + executeMethod, + isAsync } from '.'; -import { CandidType, TypeMapping } from '../candid'; -import { toParamIDLTypes, toReturnIDLType } from '../utils'; +import { + CandidType, + TypeMapping, + toParamIDLTypes, + toReturnIDLType +} from '../candid'; export function query< const Params extends ReadonlyArray, diff --git a/src/lib/canister_methods/update.ts b/src/lib/canister_methods/update.ts index c2e57d1da2..ae31238f86 100644 --- a/src/lib/canister_methods/update.ts +++ b/src/lib/canister_methods/update.ts @@ -1,13 +1,17 @@ -import { isAsync } from '../utils'; import { Callback, CanisterMethodInfo, MethodArgs, createParents, - executeMethod + executeMethod, + isAsync } from '.'; -import { CandidType, TypeMapping } from '../candid'; -import { toParamIDLTypes, toReturnIDLType } from '../utils'; +import { + CandidType, + TypeMapping, + toParamIDLTypes, + toReturnIDLType +} from '../candid'; export function update< const Params extends ReadonlyArray, diff --git a/src/lib/visitors/did_visitor.ts b/src/lib/did_visitor.ts similarity index 97% rename from src/lib/visitors/did_visitor.ts rename to src/lib/did_visitor.ts index 3207d3b59e..05e1e2f564 100644 --- a/src/lib/visitors/did_visitor.ts +++ b/src/lib/did_visitor.ts @@ -8,6 +8,10 @@ type VisitorData = { }; type VisitorResult = [CandidDef, CandidTypesDefs]; +type TypeName = string; +type CandidDef = string; +type CandidTypesDefs = { [key: TypeName]: CandidDef }; + // TODO it would be nice to have names for the rec types instead of rec_1, rec_2 etc // TODO Once types have names we should deduplicate the init and post_upgrade param types // TODO maybe even before we have names we should deduplicate all sorts of types @@ -22,49 +26,12 @@ export const DEFAULT_VISITOR_DATA: VisitorData = { systemFuncs: [] }; -export function DidResultToCandidString(result: VisitorResult): string { +export function didResultToCandidString(result: VisitorResult): string { const [candid, candidTypeDefs] = result; const candidTypesString = newTypeToCandidString(candidTypeDefs); return candidTypesString + candid + '\n'; } -function newTypeToCandidString(newTypes: CandidTypesDefs): string { - return Object.entries(newTypes).length > 0 - ? newTypesToStingArr(newTypes).join('\n') + '\n' - : ''; -} - -function newTypesToStingArr(newTypes: CandidTypesDefs): string[] { - return Object.entries(newTypes).map( - ([name, candid]) => `type ${name} = ${candid};` - ); -} - -type TypeName = string; -export type CandidDef = string; -export type CandidTypesDefs = { [key: TypeName]: CandidDef }; -export function extractCandid( - paramInfo: [CandidDef, CandidTypesDefs][] -): [CandidDef[], CandidTypesDefs] { - const paramCandid = paramInfo.map(([candid, _candidTypeDefs]) => { - return candid; - }); - const candidTypeDefs = paramInfo.reduce( - (acc, [_candid, candidTypeDefs]) => { - return { ...acc, ...candidTypeDefs }; - }, - {} - ); - return [paramCandid, candidTypeDefs]; -} - -function hch(value: any) { - if (value._azleIsCanister) { - return value().getIDL(); - } - return value; -} - export class DidVisitor extends IDL.Visitor { visitService(t: IDL.ServiceClass, data: VisitorData): VisitorResult { const canisterMethods = extractCandid( @@ -255,3 +222,37 @@ export class DidVisitor extends IDL.Visitor { return [`variant {${fields_string.join('; ')}}`, candid[1]]; } } + +function newTypeToCandidString(newTypes: CandidTypesDefs): string { + return Object.entries(newTypes).length > 0 + ? newTypesToStingArr(newTypes).join('\n') + '\n' + : ''; +} + +function newTypesToStingArr(newTypes: CandidTypesDefs): string[] { + return Object.entries(newTypes).map( + ([name, candid]) => `type ${name} = ${candid};` + ); +} + +function extractCandid( + paramInfo: [CandidDef, CandidTypesDefs][] +): [CandidDef[], CandidTypesDefs] { + const paramCandid = paramInfo.map(([candid, _candidTypeDefs]) => { + return candid; + }); + const candidTypeDefs = paramInfo.reduce( + (acc, [_candid, candidTypeDefs]) => { + return { ...acc, ...candidTypeDefs }; + }, + {} + ); + return [paramCandid, candidTypeDefs]; +} + +function hch(value: any) { + if (value._azleIsCanister) { + return value().getIDL(); + } + return value; +} diff --git a/src/lib/ic.ts b/src/lib/ic.ts index 79be9cf613..ecc8730df8 100644 --- a/src/lib/ic.ts +++ b/src/lib/ic.ts @@ -11,11 +11,10 @@ import { Opt, Some, Void -} from './candid/reference/primitives'; -import { CandidType, RejectionCode } from '.'; +} from './candid'; +import { CandidType, RejectionCode, toIDLType } from '.'; import { v4 } from 'uuid'; -import { toIDLType } from './utils'; -import { EncodeVisitor } from './visitors/encode_decode'; +import { EncodeVisitor } from './candid/serde/visitors'; // declare var globalThis: { // ic: Ic; diff --git a/src/lib/index.ts b/src/lib/index.ts index 2e956c185f..f2fcdf6e05 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -1,8 +1,6 @@ import './globals'; -export * from './canister_methods'; export * from './candid'; -export * from './candid/reference/primitives'; +export * from './canister_methods'; export * from './ic'; export * from './stable_b_tree_map'; -export * from './candid/reference/result'; export { IDL } from '@dfinity/candid'; diff --git a/src/lib/stable_b_tree_map.ts b/src/lib/stable_b_tree_map.ts index d11321b3e8..7c6ed29369 100644 --- a/src/lib/stable_b_tree_map.ts +++ b/src/lib/stable_b_tree_map.ts @@ -1,6 +1,14 @@ -import { CandidType, None, Opt, Some, TypeMapping, nat64, nat8 } from '.'; -import { IDL } from './index'; -import { toIDLType } from './utils'; +import { + CandidType, + None, + Opt, + Some, + TypeMapping, + nat64, + nat8, + IDL, + toIDLType +} from '.'; import { encode, decode } from './candid/serde'; export function StableBTreeMap< diff --git a/src/lib/utils.ts b/src/lib/utils.ts deleted file mode 100644 index 1fa3bb1f8c..0000000000 --- a/src/lib/utils.ts +++ /dev/null @@ -1,127 +0,0 @@ -import { IDL } from './index'; -// import { GetIDL } from './primitives'; - -/* - * Look at each type, -is it a recursive type -yes - add the name to a global list of names -look at all of the sub types -recur - - */ - -type TypeName = string; -export type CandidDef = string; -export type CandidTypesDefs = { [key: TypeName]: CandidDef }; - -export type Parent = { - idl: IDL.RecClass; - name: string; -}; - -export function toIDLType(idl: any, parents: Parent[]): IDL.Type { - if ('getIDL' in idl) { - if ('_azleName' in idl) { - const parent = parents.find( - (parent) => parent.name === idl._azleName - ); - if (parent !== undefined) { - return { - ...parent.idl, - _azleName: idl._azleName, - name: parent.idl.name, - valueToString: (x): string => { - return parent.idl.valueToString(x); - }, - buildTypeTable: (typeTable): void => { - return parent.idl.buildTypeTable(typeTable); - }, - covariant: (x): x is any => { - return parent.idl.covariant(x); - }, - encodeType: (typeTable): ArrayBuffer => { - return parent.idl.encodeType(typeTable); - }, - checkType: (t) => { - return parent.idl.checkType(t); - }, - _buildTypeTableImpl: (typeTable): void => { - return parent.idl._buildTypeTableImpl(typeTable); - }, - // TODO check if this is still being called. maybe by adding a throw here and see if we hit it - display: () => parent.idl.name, - decodeValue: (b, t) => { - return parent.idl.decodeValue(b, t); - }, - encodeValue: (b) => { - return parent.idl.encodeValue(b); - }, - accept: (v, d) => { - return parent.idl.accept(v, d); - }, - _id: parent.idl._id, - _type: parent.idl._type, - fill: (t: any): void => { - parent.idl.fill(t); - }, - getType: () => { - return parent.idl.getType(); - } - }; - } - } - return idl.getIDL(parents); - } - if (idl._azleIsCanister) { - return toIDLType(idl(), parents); - } - // if (idl.display === undefined || idl.getIDL === undefined) { - // throw Error(`${JSON.stringify(idl)} is not a candid type`); - // } - return idl; -} - -export function toParamIDLTypes( - idl: any[], - parents: Parent[] = [] -): IDL.Type[] { - return idl.map((value) => toIDLType(value, parents)); -} - -export function toReturnIDLType( - returnIdl: any, - parents: Parent[] = [] -): IDL.Type[] { - const idlType = toIDLType(returnIdl, parents); - - return Array.isArray(idlType) ? idlType : [idlType]; -} - -export function isAsync(originalFunction: any) { - if (originalFunction[Symbol.toStringTag] === 'AsyncFunction') { - return true; - } else if (originalFunction.constructor.name === 'AsyncFunction') { - return true; - } else if (originalFunction.toString().includes('async ')) { - return true; - } else { - return false; - } -} - -type CandidMap = { [key: string]: any }; - -export function processMap(targetMap: CandidMap, parent: Parent[]): CandidMap { - const newMap: CandidMap = {}; - - for (const key in targetMap) { - if (targetMap.hasOwnProperty(key)) { - const value = targetMap[key]; - const newValue = toIDLType(value, parent); - newMap[key] = newValue; - } - } - - return newMap; -}