Skip to content

Commit

Permalink
number types are now unique types with an azleKind
Browse files Browse the repository at this point in the history
  • Loading branch information
lastmjs committed Nov 8, 2023
1 parent 8f53df8 commit 0252691
Show file tree
Hide file tree
Showing 35 changed files with 169 additions and 41 deletions.
2 changes: 1 addition & 1 deletion examples/ethereum_json_rpc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
managementCanister
} from 'azle/canisters/management';

let stableStorage = StableBTreeMap(text, text, 0);
let stableStorage = StableBTreeMap<text, text>(text, text, 0);

export default Canister({
init: init([text], (ethereumUrl) => {
Expand Down
3 changes: 2 additions & 1 deletion examples/func_types/canisters/func_types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ const Reaction = Variant({
});

const StableFunc = Func([nat64, text], Void, 'query');
type StableFunc = typeof StableFunc;

const NullFunc = Func(
[Opt(Null), Vec(Null), Null, Vec(Vec(Null)), Vec(Opt(Null))],
Null,
'query'
);

let stableStorage = StableBTreeMap(text, StableFunc, 0);
let stableStorage = StableBTreeMap<text, StableFunc>(text, StableFunc, 0);

export default Canister({
init: init([], () => {
Expand Down
2 changes: 1 addition & 1 deletion examples/motoko_examples/http_counter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const HttpRequest = Record({
certificate_version: Opt(nat16)
});

let stableStorage = StableBTreeMap(text, nat, 0);
let stableStorage = StableBTreeMap<text, nat>(text, nat, 0);

export default Canister({
init: init([], () => {
Expand Down
2 changes: 1 addition & 1 deletion examples/motoko_examples/persistent-storage/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
update
} from 'azle';

let stableStorage = StableBTreeMap(text, nat, 0);
let stableStorage = StableBTreeMap<text, nat>(text, nat, 0);
let redeployed = false;

export default Canister({
Expand Down
2 changes: 1 addition & 1 deletion examples/robust_imports/src/import_coverage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export const makeCavernousRecord = dollarSignQuery(
);

export const typeCheck = dollarSignQuery(
[CoveredVec(CoveredOpt(ic.nat16))],
[CoveredVec(CoveredOpt(ic.int16))],
coveredInt16,
(vec) => {
if (vec.length === 1) {
Expand Down
2 changes: 1 addition & 1 deletion examples/stable_structures/src/canister1/stable_map_0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
Vec
} from 'azle';

let stableMap0 = StableBTreeMap(nat8, text, 0);
let stableMap0 = StableBTreeMap<nat8, text>(nat8, text, 0);

export const stableMap0Methods = {
stableMap0ContainsKey: query([nat8], bool, (key) => {
Expand Down
2 changes: 1 addition & 1 deletion examples/stable_structures/src/canister1/stable_map_1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
Vec
} from 'azle';

let stableMap1 = StableBTreeMap(nat16, blob, 1);
let stableMap1 = StableBTreeMap<nat16, blob>(nat16, blob, 1);

export const stableMap1Methods = {
stableMap1ContainsKey: query([nat16], bool, (key) => {
Expand Down
2 changes: 1 addition & 1 deletion examples/stable_structures/src/canister1/stable_map_2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
Vec
} from 'azle';

let stableMap2 = StableBTreeMap(nat32, nat, 2);
let stableMap2 = StableBTreeMap<nat32, nat>(nat32, nat, 2);

export const stableMap2Methods = {
stableMap2ContainsKey: query([nat32], bool, (key) => {
Expand Down
2 changes: 1 addition & 1 deletion examples/stable_structures/src/canister1/stable_map_3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from 'azle';
import { Reaction } from '../types';

let stableMap3 = StableBTreeMap(Reaction, int, 3);
let stableMap3 = StableBTreeMap<Reaction, int>(Reaction, int, 3);

export const stableMap3Methods = {
stableMap3ContainsKey: query([Reaction], bool, (key) => {
Expand Down
2 changes: 1 addition & 1 deletion examples/stable_structures/src/canister1/stable_map_4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from 'azle';
import { User } from '../types';

export let stableMap4 = StableBTreeMap(User, float32, 4);
export let stableMap4 = StableBTreeMap<User, float32>(User, float32, 4);

export const stableMap4Methods = {
stableMap4ContainsKey: query([User], bool, (key) => {
Expand Down
2 changes: 1 addition & 1 deletion examples/stable_structures/src/canister2/stable_map_5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
Tuple
} from 'azle';

let stableMap5 = StableBTreeMap(Opt(text), float64, 5);
let stableMap5 = StableBTreeMap<Opt<text>, float64>(Opt(text), float64, 5);

export const stableMap5Methods = {
stableMap5ContainsKey: query([Opt(text)], bool, (key) => {
Expand Down
2 changes: 1 addition & 1 deletion examples/stable_structures/src/canister2/stable_map_6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
Tuple
} from 'azle';

let stableMap6 = StableBTreeMap(Vec(nat64), bool, 6);
let stableMap6 = StableBTreeMap<Vec<nat64>, bool>(Vec(nat64), bool, 6);

export const stableMap6Methods = {
stableMap6ContainsKey: query([Vec(nat64)], bool, (key) => {
Expand Down
2 changes: 1 addition & 1 deletion examples/stable_structures/src/canister2/stable_map_7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
Vec
} from 'azle';

let stableMap7 = StableBTreeMap(Null, Null, 7);
let stableMap7 = StableBTreeMap<Null, Null>(Null, Null, 7);

export const stableMap7Methods = {
stableMap7ContainsKey: query([Null], bool, (key) => {
Expand Down
2 changes: 1 addition & 1 deletion examples/stable_structures/src/canister2/stable_map_8.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
Tuple
} from 'azle';

let stableMap8 = StableBTreeMap(bool, Null, 8);
let stableMap8 = StableBTreeMap<bool, Null>(bool, Null, 8);

export const stableMap8Methods = {
stableMap8ContainsKey: query([bool], bool, (key) => {
Expand Down
2 changes: 1 addition & 1 deletion examples/stable_structures/src/canister2/stable_map_9.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
Tuple
} from 'azle';

let stableMap9 = StableBTreeMap(float64, Vec(text), 9);
let stableMap9 = StableBTreeMap<float64, Vec<text>>(float64, Vec(text), 9);

export const stableMap9Methods = {
stableMap9ContainsKey: query([float64], bool, (key) => {
Expand Down
2 changes: 1 addition & 1 deletion examples/stable_structures/src/canister3/stable_map_10.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
Vec
} from 'azle';

let stableMap10 = StableBTreeMap(float32, Opt(bool), 10);
let stableMap10 = StableBTreeMap<float32, Opt<bool>>(float32, Opt(bool), 10);

export const stableMap10Methods = {
stableMap10ContainsKey: query([float32], bool, (key) => {
Expand Down
2 changes: 1 addition & 1 deletion examples/stable_structures/src/canister3/stable_map_11.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from 'azle';
import { User } from '../types';

let stableMap11 = StableBTreeMap(nat, User, 11);
let stableMap11 = StableBTreeMap<nat, User>(nat, User, 11);

export const stableMap11Methods = {
stableMap11ContainsKey: query([nat], bool, (key) => {
Expand Down
2 changes: 1 addition & 1 deletion examples/stable_structures/src/canister3/stable_map_12.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from 'azle';
import { Reaction } from '../types';

let stableMap12 = StableBTreeMap(blob, Reaction, 12);
let stableMap12 = StableBTreeMap<blob, Reaction>(blob, Reaction, 12);

export const stableMap12Methods = {
stableMap12ContainsKey: query([blob], bool, (key) => {
Expand Down
2 changes: 1 addition & 1 deletion examples/stable_structures/src/canister3/stable_map_13.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
Vec
} from 'azle';

let stableMap13 = StableBTreeMap(text, Principal, 13);
let stableMap13 = StableBTreeMap<text, Principal>(text, Principal, 13);

export const stableMap13Methods = {
stableMap13ContainsKey: query([text], bool, (key) => {
Expand Down
2 changes: 1 addition & 1 deletion examples/stable_structures/src/canister3/stable_map_14.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from 'azle';
import { Callback } from '../types';

let stableMap14 = StableBTreeMap(text, Callback, 14);
let stableMap14 = StableBTreeMap<text, Callback>(text, Callback, 14);

export const stableMap14Methods = {
stableMap14ContainsKey: query([text], bool, (key) => {
Expand Down
2 changes: 1 addition & 1 deletion examples/stable_structures/src/canister3/stable_map_15.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from 'azle';
import { Callback } from '../types';

let stableMap15 = StableBTreeMap(Callback, text, 15);
let stableMap15 = StableBTreeMap<Callback, text>(Callback, text, 15);

export const stableMap15Methods = {
stableMap15ContainsKey: query([Callback], bool, (key) => {
Expand Down
2 changes: 1 addition & 1 deletion examples/stable_structures/src/canister3/stable_map_16.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
Vec
} from 'azle';

let stableMap16 = StableBTreeMap(text, StableJson(), 16);
let stableMap16 = StableBTreeMap<text, {}>(text, StableJson(), 16);

export const stableMap16Methods = {
stableMap16ContainsKey: query([text], bool, (key) => {
Expand Down
2 changes: 1 addition & 1 deletion examples/stable_structures/src/canister3/stable_map_17.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
Vec
} from 'azle';

let stableMap17 = StableBTreeMap(StableJson(), text, 17);
let stableMap17 = StableBTreeMap<{}, text>(StableJson(), text, 17);

export const stableMap17Methods = {
stableMap17ContainsKey: query([text], bool, (key) => {
Expand Down
3 changes: 3 additions & 0 deletions examples/stable_structures/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ export const Reaction = Variant({
Happy: Null,
Sad: Null
});
export type Reaction = typeof Reaction;

export const User = Record({
username: text,
posts: Vec(BlogPost)
});
export type User = typeof User;

export const Callback = Func([BlogPost], Reaction, 'update');
export type Callback = typeof Callback;
16 changes: 8 additions & 8 deletions src/lib/candid/type_mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,21 @@ export type TypeMapping<T, RecursionLevel = 0> = RecursionLevel extends 10
>;
}
: T extends AzleVec<infer U>
? U extends { _azleKind: 'AzleNat8' }
? U extends { _azleKind?: 'AzleNat8' }
? Uint8Array
: U extends { _azleKind: 'AzleNat16' }
: U extends { _azleKind?: 'AzleNat16' }
? Uint16Array
: U extends { _azleKind: 'AzleNat32' }
: U extends { _azleKind?: 'AzleNat32' }
? Uint32Array
: U extends { _azleKind: 'AzleNat64' }
: U extends { _azleKind?: 'AzleNat64' }
? BigUint64Array
: U extends { _azleKind: 'AzleInt8' }
: U extends { _azleKind?: 'AzleInt8' }
? Int8Array
: U extends { _azleKind: 'AzleInt16' }
: U extends { _azleKind?: 'AzleInt16' }
? Int16Array
: U extends { _azleKind: 'AzleInt32' }
: U extends { _azleKind?: 'AzleInt32' }
? Int32Array
: U extends { _azleKind: 'AzleInt64' }
: U extends { _azleKind?: 'AzleInt64' }
? BigInt64Array
: T extends AzleVec<infer U> // TODO I do not know why we have to do this?
? TypeMapping<U>[]
Expand Down
3 changes: 2 additions & 1 deletion src/lib/candid/types/constructed/vec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Parent, toIdl } from '../../to_idl';
import { IDL } from '@dfinity/candid';
import { encode } from '../../serde/encode';
import { decode } from '../../serde/decode';
import { TypeMapping } from '../../type_mapping';

export class AzleVec<T> {
constructor(t: any) {
Expand Down Expand Up @@ -30,7 +31,7 @@ export class AzleVec<T> {
}
}

export type Vec<T> = T[];
export type Vec<T> = TypeMapping<AzleVec<T>>;
export function Vec<T>(t: T): AzleVec<T> {
return new AzleVec(t);
}
2 changes: 1 addition & 1 deletion src/lib/candid/types/primitive/ints/int16.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ export class AzleInt16 {
}
}
export const int16 = AzleInt16;
export type int16 = number;
export type int16 = number & { _azleKind?: 'AzleInt16' };
2 changes: 1 addition & 1 deletion src/lib/candid/types/primitive/ints/int32.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ export class AzleInt32 {
}
}
export const int32 = AzleInt32;
export type int32 = number;
export type int32 = number & { _azleKind?: 'AzleInt32' };
2 changes: 1 addition & 1 deletion src/lib/candid/types/primitive/ints/int64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ export class AzleInt64 {
}
}
export const int64 = AzleInt64;
export type int64 = bigint;
export type int64 = bigint & { _azleKind?: 'AzleInt64' };
2 changes: 1 addition & 1 deletion src/lib/candid/types/primitive/ints/int8.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ export class AzleInt8 {
}

export const int8 = AzleInt8;
export type int8 = number;
export type int8 = number & { _azleKind?: 'AzleInt8' };
2 changes: 1 addition & 1 deletion src/lib/candid/types/primitive/nats/nat16.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ export class AzleNat16 {
}

export const nat16 = AzleNat16;
export type nat16 = number;
export type nat16 = number & { _azleKind?: 'AzleNat16' };
2 changes: 1 addition & 1 deletion src/lib/candid/types/primitive/nats/nat32.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ export class AzleNat32 {
}
}
export const nat32 = AzleNat32;
export type nat32 = number;
export type nat32 = number & { _azleKind?: 'AzleNat32' };
2 changes: 1 addition & 1 deletion src/lib/candid/types/primitive/nats/nat64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ export class AzleNat64 {
}
}
export const nat64 = AzleNat64;
export type nat64 = bigint;
export type nat64 = bigint & { _azleKind?: 'AzleNat64' };
2 changes: 1 addition & 1 deletion src/lib/candid/types/primitive/nats/nat8.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ export class AzleNat8 {
}

export const nat8 = AzleNat8;
export type nat8 = number;
export type nat8 = number & { _azleKind?: 'AzleNat8' };
Loading

0 comments on commit 0252691

Please sign in to comment.