Skip to content

Commit

Permalink
Merge pull request #1485 from demergent-labs/use_stable_json_everywhere
Browse files Browse the repository at this point in the history
use stableJson everywhere
  • Loading branch information
lastmjs authored Dec 8, 2023
2 parents c7fa900 + 05aede4 commit 01a4005
Show file tree
Hide file tree
Showing 30 changed files with 36 additions and 50 deletions.
4 changes: 2 additions & 2 deletions examples/audio_recorder/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ const AudioRecorderError = Variant({
});
type AudioRecorderError = typeof AudioRecorderError.tsType;

let users = StableBTreeMap<Principal, User>(Principal, User, 0);
let recordings = StableBTreeMap<Principal, Recording>(Principal, Recording, 1);
let users = StableBTreeMap<Principal, User>(0);
let recordings = StableBTreeMap<Principal, Recording>(1);

export default Canister({
createUser: update([text], User, (username) => {
Expand Down
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>(text, text, 0);
let stableStorage = StableBTreeMap<text, text>(0);

export default Canister({
init: init([text], (ethereumUrl) => {
Expand Down
2 changes: 1 addition & 1 deletion examples/func_types/canisters/func_types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const NullFunc = Func(
'query'
);

let stableStorage = StableBTreeMap<text, StableFunc>(text, StableFunc, 0);
let stableStorage = StableBTreeMap<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>(text, nat, 0);
let stableStorage = StableBTreeMap<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>(text, nat, 0);
let stableStorage = StableBTreeMap<text, nat>(0);
let redeployed = false;

export default Canister({
Expand Down
2 changes: 1 addition & 1 deletion examples/pre_and_post_upgrade/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const Entry = Record({
});
type Entry = typeof Entry;

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

let entries: {
[key: string]: nat64;
Expand Down
2 changes: 1 addition & 1 deletion examples/robust_imports/src/azle_coverage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export const Watermelon = Voavanga({
Seedless: Nutmeg
});

let soncoya = Soncoya(nectarine8, PreparedFruit, 0);
let soncoya = Soncoya<nectarine8, PreparedFruit>(0);

function gatherGrapes() {
const opt = soncoya.get(0);
Expand Down
2 changes: 1 addition & 1 deletion examples/robust_imports/src/type_alias_decls/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ export const returnFuncAlias = azle.query(
}
);

let stableMap = types.DeepStableBTreeMapAlias(azle.nat16, azle.text, 1);
let stableMap = types.DeepStableBTreeMapAlias<azle.nat16, azle.text>(1);

export const setStable = azle.update(
[azle.nat16, azle.text],
Expand Down
19 changes: 3 additions & 16 deletions examples/stable_b_tree_map_instruction_threshold/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
query,
Record,
StableBTreeMap,
stableJson,
text,
update,
Vec,
Expand All @@ -20,11 +19,7 @@ const SmallRecord = Record({
});
type SmallRecord = typeof SmallRecord.tsType;

let smallRecordMap = StableBTreeMap<string, SmallRecord>(
stableJson,
stableJson,
0
);
let smallRecordMap = StableBTreeMap<string, SmallRecord>(0);

const MediumRecord = Record({
id: text,
Expand All @@ -34,11 +29,7 @@ const MediumRecord = Record({
});
type MediumRecord = typeof MediumRecord.tsType;

let mediumRecordMap = StableBTreeMap<string, MediumRecord>(
stableJson,
stableJson,
1
);
let mediumRecordMap = StableBTreeMap<string, MediumRecord>(1);

const LargeRecord = Record({
id: text,
Expand All @@ -51,11 +42,7 @@ const LargeRecord = Record({
});
type LargeRecord = typeof LargeRecord.tsType;

let largeRecordMap = StableBTreeMap<string, LargeRecord>(
stableJson,
stableJson,
2
);
let largeRecordMap = StableBTreeMap<string, LargeRecord>(2);

export default Canister({
insertSmallRecord: update([nat32], Void, (numToInsert) => {
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>(nat8, text, 0);
let stableMap0 = StableBTreeMap<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>(nat16, blob, 1);
let stableMap1 = StableBTreeMap<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>(nat32, nat, 2);
let stableMap2 = StableBTreeMap<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>(Reaction, int, 3);
let stableMap3 = StableBTreeMap<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>(User, float32, 4);
export let stableMap4 = StableBTreeMap<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>(Opt(text), float64, 5);
let stableMap5 = StableBTreeMap<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>(Vec(nat64), bool, 6);
let stableMap6 = StableBTreeMap<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>(Null, Null, 7);
let stableMap7 = StableBTreeMap<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>(bool, Null, 8);
let stableMap8 = StableBTreeMap<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>>(float64, Vec(text), 9);
let stableMap9 = StableBTreeMap<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>>(float32, Opt(bool), 10);
let stableMap10 = StableBTreeMap<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>(nat, User, 11);
let stableMap11 = StableBTreeMap<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>(blob, Reaction, 12);
let stableMap12 = StableBTreeMap<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>(text, Principal, 13);
let stableMap13 = StableBTreeMap<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>(text, Callback, 14);
let stableMap14 = StableBTreeMap<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>(Callback, text, 15);
let stableMap15 = StableBTreeMap<Callback, text>(15);

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

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

export const stableMap16Methods = {
stableMap16ContainsKey: query([text], bool, (key) => {
Expand Down
3 changes: 1 addition & 2 deletions examples/stable_structures/src/canister3/stable_map_17.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import {
Opt,
query,
StableBTreeMap,
StableJson,
text,
Tuple,
update,
Vec
} from 'azle';

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

export const stableMap17Methods = {
stableMap17ContainsKey: query([text], bool, (key) => {
Expand Down
2 changes: 1 addition & 1 deletion property_tests/arbitraries/stable_b_tree_map_arb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const StableBTreeMapArb = fc
return {
name,
imports,
definition: `let ${name} = StableBTreeMap<${keySample.src.candidTypeAnnotation}, ${valueSample.src.candidTypeAnnotation}>(stableJson, stableJson, ${memoryId});`,
definition: `let ${name} = StableBTreeMap<${keySample.src.candidTypeAnnotation}, ${valueSample.src.candidTypeAnnotation}>(${memoryId});`,
keySample,
valueSample
};
Expand Down
7 changes: 4 additions & 3 deletions src/lib/stable_structures/stable_b_tree_map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ import { None, Opt, Some } from '../candid/types/constructed/opt';
import { nat64 } from '../candid/types/primitive/nats/nat64';
import { nat8 } from '../candid/types/primitive/nats/nat8';
import { decode } from '../candid/serde';
import { stableJson } from './stable_json';

export interface Serializable {
toBytes: (data: any) => Uint8Array;
fromBytes: (bytes: Uint8Array) => any;
}

export function StableBTreeMap<Key = any, Value = any>(
keySerializable: Serializable,
valueSerializable: Serializable,
memoryIdNumber: nat8
memoryIdNumber: nat8,
keySerializable: Serializable = stableJson,
valueSerializable: Serializable = stableJson
) {
const memoryId = memoryIdNumber.toString();

Expand Down
2 changes: 1 addition & 1 deletion type_tests/stable_structures/stable_b_tree_map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { AssertType, NotAnyAndExact } from '../assert_type';
// TODO these tests just make sure that the basic methods are there
// TODO we want to maybe test that the type arguments work correctly as well

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

export type TestContainsKey = AssertType<
NotAnyAndExact<typeof mapText.containsKey, (key: any) => boolean>
Expand Down

0 comments on commit 01a4005

Please sign in to comment.