Skip to content

Commit

Permalink
add simple_user_accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
lastmjs committed Sep 20, 2023
1 parent d50389e commit f993bc6
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
# "examples/rust_type_conversions",
# "examples/service",
# "examples/simple_erc20",
# "examples/simple_user_accounts",
# "examples/stable_memory",
# "examples/stable_structures",
# "examples/timers",
Expand Down Expand Up @@ -123,6 +122,7 @@ jobs:
EXAMPLE_DIRECTORIES=$(cat << END
[
"examples/query",
"examples/simple_user_accounts",
"examples/update"
]
END
Expand Down
53 changes: 21 additions & 32 deletions examples/simple_user_accounts/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
candid,
None,
Opt,
query,
Expand All @@ -13,46 +12,36 @@ import {

type Db = {
users: {
[id: string]: User;
[id: string]: typeof User;
};
};

class User extends Record {
@candid(text)
id: text;

@candid(text)
username: text;
}

export default class extends Service {
db: Db = {
users: {}
};
let db: Db = {
users: {}
};

@query([text], Opt(User))
getUserById(id: text): Opt<User> {
const userOrUndefined = this.db.users[id];
const User = Record({
id: text,
username: text
});

export default Service({
getUserById: query([text], Opt(User), (id) => {
const userOrUndefined = db.users[id];
return userOrUndefined ? Some(userOrUndefined) : None;
}

@query([], Vec(User))
getAllUsers(): Vec<User> {
return Object.values(this.db.users);
}

@update([text], User)
createUser(username: text): User {
const id = Object.keys(this.db.users).length.toString();

const user = {
}),
getAllUsers: query([], Vec(User), () => {
return Object.values(db.users);
}),
createUser: update([text], User, (username) => {
const id = Object.keys(db.users).length.toString();
const user: typeof User = {
id,
username
};

this.db.users[id] = user;
db.users[id] = user;

return user;
}
}
})
});
11 changes: 11 additions & 0 deletions src/compiler/generate_candid_and_canister_methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ export function generateCandidAndCanisterMethods(mainJs: string): {

const sandbox = {
globalThis: {},
crypto: {
getRandomValues: () => {
let array = new Uint8Array(32);

for (let i = 0; i < array.length; i++) {
array[i] = Math.floor(Math.random() * 256);
}

return array;
}
},
exports: {},
console,
TextDecoder,
Expand Down
5 changes: 5 additions & 0 deletions src/lib_functional/candid/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
export * from './reference';
import { IDL } from '@dfinity/candid';
import { AzleVec, AzleOpt } from '../../lib_new';

export type TypeMapping<T> = T extends IDL.TextClass
? string
: T extends never[]
? void
: T extends AzleVec<infer U>
? TypeMapping<U>[]
: T extends AzleOpt<infer U>
? [TypeMapping<U>] | []
: T;
1 change: 1 addition & 0 deletions src/lib_functional/candid/reference/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './record';
export * from './service';
28 changes: 28 additions & 0 deletions src/lib_functional/candid/reference/record.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { TypeMapping } from '..';
import { IDL } from '@dfinity/candid';
import { processMap } from '../../../lib_new/utils';
import { v4 } from 'uuid';

export function Record<T>(obj: T): {
[K in keyof T]: TypeMapping<T[K]>;
} {
const name = v4();

return {
getIDL(parents: any) {
const idl = IDL.Rec();
idl.fill(
IDL.Record(
processMap(obj as any, [
...parents,
{
idl: idl,
name
}
])
)
);
return idl;
}
} as any;
}
11 changes: 10 additions & 1 deletion src/lib_functional/candid/reference/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ export function Service(serviceOptions: ServiceOptions): CanisterMethods {
};
}, {});

const candidTypes = Object.values(serviceOptions).reduce(
(acc: string[], canisterMethodInfo) => {
return [...acc, ...canisterMethodInfo.candidTypes];
},
[]
);

const queries = Object.entries(serviceOptions)
.filter((entry) => {
const key = entry[0];
Expand Down Expand Up @@ -51,7 +58,9 @@ export function Service(serviceOptions: ServiceOptions): CanisterMethods {
// TODO loop through each key and simply grab the candid off
// TODO grab the init/post_upgrade candid as well
return {
candid: `service: () -> {
candid: `${
candidTypes.length === 0 ? '' : candidTypes.join('\n') + '\n'
}service: () -> {
${Object.entries(serviceOptions)
.map((entry) => {
return `${entry[0]}: ${entry[1].candid}`;
Expand Down
1 change: 1 addition & 0 deletions src/lib_functional/canister_methods/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type CanisterMethodInfo = {
type: 'query' | 'update';
callback: (...args: any) => any;
candid: string;
candidTypes: string[];
};

// TODO this doesn't produce a TS error when the user returns a non-void value in a void function
Expand Down
6 changes: 4 additions & 2 deletions src/lib_functional/canister_methods/query.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
handleRecursiveParams,
handleRecursiveReturn
handleRecursiveReturn,
newTypesToStingArr
} from '../../lib_new/method_decorators';
import { Callback, CanisterMethodInfo, executeMethod } from '.';

Expand All @@ -20,6 +21,7 @@ export function query<Params extends any[], Return>(
callback: (...args) => {
executeMethod(paramCandid, returnCandid, args, callback);
},
candid: `(${paramCandid[1].join(', ')}) -> (${returnCandid[1]}) query;`
candid: `(${paramCandid[1].join(', ')}) -> (${returnCandid[1]}) query;`,
candidTypes: newTypesToStingArr(returnCandid[2])
};
}
6 changes: 4 additions & 2 deletions src/lib_functional/canister_methods/update.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
handleRecursiveParams,
handleRecursiveReturn
handleRecursiveReturn,
newTypesToStingArr
} from '../../lib_new/method_decorators';
import { Callback, CanisterMethodInfo, executeMethod } from '.';

Expand All @@ -20,6 +21,7 @@ export function update<Params extends any[], Return>(
callback: (...args) => {
executeMethod(paramCandid, returnCandid, args, callback);
},
candid: `(${paramCandid[1].join(', ')}) -> (${returnCandid[1]});`
candid: `(${paramCandid[1].join(', ')}) -> (${returnCandid[1]});`,
candidTypes: newTypesToStingArr(returnCandid[2])
};
}
4 changes: 2 additions & 2 deletions src/lib_new/method_decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ export function update(
};
}

function newTypesToStingArr(newTypes: CandidTypesDefs): string[] {
export function newTypesToStingArr(newTypes: CandidTypesDefs): string[] {
return Object.entries(newTypes).map(
([name, candid]) => `type ${name} = ${candid}`
([name, candid]) => `type ${name} = ${candid};`
);
}

Expand Down
8 changes: 4 additions & 4 deletions src/lib_new/primitives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ export function Some<T>(value: T): [T] {
export const None: [] = [];

// TODO what happens if we pass something to Opt() that can't be converted to CandidClass?
export function Opt<T>(t: IDL.Type<T> | any): AzleOpt {
export function Opt<T>(t: T): AzleOpt<T> {
// return IDL.Opt(toCandidClass(t));
return new AzleOpt(t);
}

export class AzleOpt {
export class AzleOpt<T> {
constructor(t: any) {
this._azleType = t;
}
Expand All @@ -78,7 +78,7 @@ export class AzleOpt {
}
}

export class AzleVec {
export class AzleVec<T> {
constructor(t: any) {
this._azleType = t;
}
Expand All @@ -101,7 +101,7 @@ export class AzleTuple {
}
}

export function Vec<T>(t: IDL.Type<T> | any): AzleVec {
export function Vec<T>(t: T): AzleVec<T> {
// return IDL.Vec(toCandidClass(t));
return new AzleVec(t);
}
Expand Down

0 comments on commit f993bc6

Please sign in to comment.