Skip to content

Commit

Permalink
add CandidType and type checking for it
Browse files Browse the repository at this point in the history
  • Loading branch information
lastmjs committed Sep 28, 2023
1 parent 04d4745 commit aedb2e2
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 26 deletions.
2 changes: 1 addition & 1 deletion examples/audio_recorder/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export default Canister({
createRecording: update(
[blob, text, principal],
Result(Recording, AudioRecorderError),
(audio: blob, name: text, userId: Principal) => {
(audio, name, userId) => {
const userOpt = users.get(userId);

if (userOpt.length === 0) {
Expand Down
8 changes: 6 additions & 2 deletions src/lib_functional/candid/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ import {
AzleResult,
Result,
AzleTuple,
Tuple
AzleText
} from '../../lib_new';

export type TypeMapping<T> = T extends () => any
? ReturnType<T>
: T extends IDL.TextClass
: T extends AzleText
? string
: T extends AzleBool
? bool
Expand Down Expand Up @@ -96,3 +96,7 @@ export type TypeMapping<T> = T extends () => any
: T extends AzleEmpty
? empty
: T;

export type CandidType = {
_azleCandidType?: '_azleCandidType';
};
12 changes: 9 additions & 3 deletions src/lib_functional/candid/reference/record.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { TypeMapping } from '..';
import { CandidType, TypeMapping } from '..';
import { IDL } from '@dfinity/candid';
import { Parent, processMap } from '../../../lib_new/utils';
import { v4 } from 'uuid';

export function Record<T>(obj: T): {
export function Record<
T extends {
[K in keyof T]: CandidType;
}
>(
obj: T
): {
[K in keyof T]: TypeMapping<T[K]>;
} {
} & { _azleCandidType?: '_azleCandidType' } {
const name = v4();

return {
Expand Down
2 changes: 1 addition & 1 deletion src/lib_functional/candid/reference/variant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Null } from '../../../lib_new/primitives';

export function Variant<T>(obj: T): RequireExactlyOne<{
[K in keyof T]: TypeMapping<T[K]>;
}> {
}> & { _azleCandidType?: '_azleCandidType' } {
const name = v4();

return {
Expand Down
4 changes: 2 additions & 2 deletions src/lib_functional/canister_methods/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import {
newTypesToStingArr
} from '../../lib_new/method_decorators';
import { Callback, CanisterMethodInfo, executeMethod } from '.';
import { TypeMapping } from '../candid';
import { CandidType, TypeMapping } from '../candid';
import { Void } from '../../lib_new';

export function init<
const Params extends ReadonlyArray<any>,
const Params extends ReadonlyArray<CandidType>,
GenericCallback extends Callback<Params, Void>
>(
paramsIdls: Params,
Expand Down
4 changes: 2 additions & 2 deletions src/lib_functional/canister_methods/post_upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import {
newTypesToStingArr
} from '../../lib_new/method_decorators';
import { Callback, CanisterMethodInfo, executeMethod } from '.';
import { TypeMapping } from '../candid';
import { CandidType, TypeMapping } from '../candid';
import { Void } from '../../lib_new';

export function postUpgrade<
const Params extends ReadonlyArray<any>,
const Params extends ReadonlyArray<CandidType>,
GenericCallback extends Callback<Params, Void>
>(
paramsIdls: Params,
Expand Down
6 changes: 3 additions & 3 deletions src/lib_functional/canister_methods/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import {
newTypesToStingArr
} from '../../lib_new/method_decorators';
import { Callback, CanisterMethodInfo, executeMethod } from '.';
import { TypeMapping } from '../candid';
import { CandidType, TypeMapping } from '../candid';

export function query<
const Params extends ReadonlyArray<any>,
Return,
const Params extends ReadonlyArray<CandidType>,
Return extends CandidType,
GenericCallback extends Callback<Params, Return>
>(
paramsIdls: Params,
Expand Down
6 changes: 3 additions & 3 deletions src/lib_functional/canister_methods/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import {
newTypesToStingArr
} from '../../lib_new/method_decorators';
import { Callback, CanisterMethodInfo, executeMethod } from '.';
import { TypeMapping } from '../candid';
import { CandidType, TypeMapping } from '../candid';

export function update<
const Params extends ReadonlyArray<any>,
Return,
const Params extends ReadonlyArray<CandidType>,
Return extends CandidType,
GenericCallback extends Callback<Params, Return>
>(
paramsIdls: Params,
Expand Down
54 changes: 53 additions & 1 deletion src/lib_new/primitives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,111 +3,143 @@ import { CandidClass, Parent, toIDLType } from './utils';

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;
}
Expand All @@ -120,8 +152,19 @@ export class AzleBool {
}
}

export class AzleText {
_kind: 'AzleText' = 'AzleText';
_azleCandidType?: '_azleCandidType';

static getIDL() {
return IDL.Text;
}
}

export class AzlePrincipal {
_kind: 'AzlePrincipal' = 'AzlePrincipal';
_azleCandidType?: '_azleCandidType';

static getIDL() {
return IDL.Principal;
}
Expand Down Expand Up @@ -157,7 +200,7 @@ 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 = IDL.Text;
export const text: AzleText = AzleText as any;
export type text = string;
export const float32: AzleFloat32 = AzleFloat32 as any;
export type float32 = number;
Expand Down Expand Up @@ -198,7 +241,10 @@ export class AzleOpt<T> {
constructor(t: any) {
this._azleType = t;
}

_azleType: CandidClass;
_azleCandidType?: '_azleCandidType';

getIDL(parents: Parent[]) {
return IDL.Opt(toIDLType(this._azleType, []));
}
Expand All @@ -208,7 +254,10 @@ export class AzleVec<T> {
constructor(t: any) {
this._azleType = t;
}

_azleType: CandidClass;
_azleCandidType?: '_azleCandidType';

getIDL(parents: Parent[]) {
return IDL.Vec(toIDLType(this._azleType, []));
}
Expand All @@ -218,7 +267,10 @@ export class AzleTuple<T extends any[]> {
constructor(t: CandidClass[]) {
this._azleTypes = t;
}

_azleTypes: CandidClass[];
_azleCandidType?: '_azleCandidType';

getIDL(parents: Parent[]) {
const candidTypes = this._azleTypes.map((value) => {
return toIDLType(value, parents);
Expand Down
7 changes: 5 additions & 2 deletions src/lib_new/result.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { RequireExactlyOne } from '../lib/candid_types/variant';
import { CandidType } from '../lib_functional';
import { IDL } from './index';
import { CandidClass, Parent, toIDLType } from './utils';
import { Parent, toIDLType } from './utils';

export class AzleResult<T, K> {
constructor(ok: any, err: any) {
Expand All @@ -11,6 +12,8 @@ export class AzleResult<T, K> {
_azleOk: any;
_azleErr: any;

_azleCandidType?: '_azleCandidType';

getIDL(parents: Parent[]) {
return IDL.Variant({
Ok: toIDLType(this._azleOk, parents),
Expand All @@ -19,7 +22,7 @@ export class AzleResult<T, K> {
}
}

export function Result<Ok extends CandidClass, Err extends CandidClass>(
export function Result<Ok extends CandidType, Err extends CandidType>(
ok: Ok,
err: Err
) {
Expand Down
11 changes: 5 additions & 6 deletions src/lib_new/stable_b_tree_map.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { TypeMapping } from '../lib_functional';
import { CandidType, TypeMapping } from '../lib_functional';
import { IDL, nat8, nat64, Opt } from './index';
import { toIDLType } from './utils';

export function StableBTreeMap<Key, Value>(
key: Key,
value: Value,
memoryId: nat8
) {
export function StableBTreeMap<
Key extends CandidType,
Value extends CandidType
>(key: Key, value: Value, memoryId: nat8) {
const keyIdl = toIDLType(key, []);
const valueIdl = toIDLType(value, []);

Expand Down

0 comments on commit aedb2e2

Please sign in to comment.