Skip to content

Commit

Permalink
reorganize directory structure
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemann committed Oct 2, 2023
1 parent 68e69eb commit 7a00140
Show file tree
Hide file tree
Showing 44 changed files with 583 additions and 557 deletions.
6 changes: 3 additions & 3 deletions src/compiler/generate_candid_and_canister_methods.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -43,7 +43,7 @@ export function generateCandidAndCanisterMethods(mainJs: string): {
});

return {
candid: DidResultToCandidString(candidInfo),
candid: didResultToCandidString(candidInfo),
canisterMethods: canisterMethods
};
}
13 changes: 13 additions & 0 deletions src/lib/candid/constructed/blob.ts
Original file line number Diff line number Diff line change
@@ -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;
24 changes: 24 additions & 0 deletions src/lib/candid/constructed/index.ts
Original file line number Diff line number Diff line change
@@ -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;
}
40 changes: 40 additions & 0 deletions src/lib/candid/constructed/option.ts
Original file line number Diff line number Diff line change
@@ -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<T> = 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<T>(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: T): AzleOpt<T> {
// return IDL.Opt(toCandidClass(t));
return new AzleOpt(t);
}

export class AzleOpt<T> {
constructor(t: any) {
this._azleType = t;
}

_azleType: CandidType;
_azleCandidType?: '_azleCandidType';
_kind: 'AzleOpt' = 'AzleOpt';

getIDL(parents: Parent[]) {
return IDL.Opt(toIDLType(this._azleType, parents));
}
}
Original file line number Diff line number Diff line change
@@ -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<
Expand Down
23 changes: 23 additions & 0 deletions src/lib/candid/constructed/tuple.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { CandidType } from '..';
import { Parent, toIDLType, IDL } from '../../';

export class AzleTuple<T extends any[]> {
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<T extends any[]>(...types: T): AzleTuple<T> {
return new AzleTuple(types);
}
export type Tuple<T> = T;
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<T, K> {
constructor(ok: any, err: any) {
Expand Down
21 changes: 21 additions & 0 deletions src/lib/candid/constructed/vector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { CandidType } from '..';
import { IDL, Parent, toIDLType } from '../..';

export class AzleVec<T> {
constructor(t: any) {
this._azleType = t;
}

_azleType: CandidType;
_azleCandidType?: '_azleCandidType';

getIDL(parents: Parent[]) {
return IDL.Vec(toIDLType(this._azleType, parents));
}
}

export type Vec<T> = T[];
export function Vec<T>(t: T): AzleVec<T> {
// return IDL.Vec(toCandidClass(t));
return new AzleVec(t);
}
96 changes: 92 additions & 4 deletions src/lib/candid/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './reference';
import { IDL } from '..';
import {
AzleBlob,
blob,
Expand All @@ -16,6 +16,7 @@ import {
AzleNat8,
AzleFloat64,
AzleFloat32,
AzleResult,
nat,
nat64,
nat32,
Expand All @@ -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<T, RecursionLevel = 0> = RecursionLevel extends 10
? T
Expand Down Expand Up @@ -124,3 +129,86 @@ export type TypeMapping<T, RecursionLevel = 0> = 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<any> {
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<any>[] {
return idl.map((value) => toIDLType(value, parents));
}

export function toReturnIDLType(
returnIdl: CandidType,
parents: Parent[] = []
): IDL.Type<any>[] {
const idlType = toIDLType(returnIdl, parents);

return Array.isArray(idlType) ? idlType : [idlType];
}
Empty file removed src/lib/candid/primitive.ts
Empty file.
13 changes: 13 additions & 0 deletions src/lib/candid/primitive/bool.ts
Original file line number Diff line number Diff line change
@@ -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;
13 changes: 13 additions & 0 deletions src/lib/candid/primitive/empty.ts
Original file line number Diff line number Diff line change
@@ -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;
24 changes: 24 additions & 0 deletions src/lib/candid/primitive/floating_point_numbers.ts
Original file line number Diff line number Diff line change
@@ -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;
9 changes: 9 additions & 0 deletions src/lib/candid/primitive/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Loading

0 comments on commit 7a00140

Please sign in to comment.