Skip to content

Commit

Permalink
clean up complex types pr fix
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemann committed Sep 30, 2023
1 parent 6f5cd4d commit ff9653c
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 103 deletions.
18 changes: 9 additions & 9 deletions examples/complex_types/src/index.did
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ type rec_617 = record {id:text; text:text; author:rec_616; thread:record {id:tex
type rec_618 = record {id:text; text:text; author:rec_616; thread:record {id:text; title:text; author:rec_616; posts:vec rec_618}; reactions:vec record {id:text; post:rec_618; author:rec_616; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}};
type rec_616 = record {id:text; username:text; threads:vec record {id:text; title:text; author:rec_616; posts:vec rec_619}; posts:vec rec_617; reactions:vec record {id:text; post:rec_618; author:rec_616; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}};
service: () -> {
createPost: (text, text, text, nat32) -> (rec_440) ;
createReaction: (text, text, variant {Fire; ThumbsDown; ThumbsUp}, nat32) -> (record {id:text; post:rec_484; author:rec_480; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}) ;
createThread: (text, text, nat32) -> (record {id:text; title:text; author:rec_544; posts:vec rec_548}) ;
createUser: (text, nat32) -> (rec_600) ;
getAllPosts: (nat32) -> (vec rec_456) query;
getAllReactions: (nat32) -> (vec record {id:text; post:rec_516; author:rec_512; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}) query;
getAllThreads: (nat32) -> (vec record {id:text; title:text; author:rec_576; posts:vec rec_580}) query;
getAllUsers: (nat32) -> (vec rec_616) query;
}
createPost: (text, text, text, nat32) -> (rec_440);
createReaction: (text, text, variant {Fire; ThumbsDown; ThumbsUp}, nat32) -> (record {id:text; post:rec_484; author:rec_480; reactionType:variant {Fire; ThumbsDown; ThumbsUp}});
createThread: (text, text, nat32) -> (record {id:text; title:text; author:rec_544; posts:vec rec_548});
createUser: (text, nat32) -> (rec_600);
getAllPosts: (nat32) -> (vec rec_456) query;
getAllReactions: (nat32) -> (vec record {id:text; post:rec_516; author:rec_512; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}) query;
getAllThreads: (nat32) -> (vec record {id:text; title:text; author:rec_576; posts:vec rec_580}) query;
getAllUsers: (nat32) -> (vec rec_616) query;
}
46 changes: 8 additions & 38 deletions examples/complex_types/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,16 @@
import { nat32, query, Canister, text, update, Vec } from 'azle';
import { Post, Reaction, ReactionType, Thread, User } from './candid_types';
import { Canister } from 'azle';
import { createPost, getAllPosts } from './posts';
import { createReaction, getAllReactions } from './reactions';
import { createThread, getAllThreads } from './threads';
import { createUser, getAllUsers } from './users';

export default Canister({
createPost,

getAllPosts: query([nat32], Vec(Post), (joinDepth) => {
return getAllPosts(joinDepth);
}),

createReaction: update(
[text, text, ReactionType, nat32],
Reaction,
(authorId, postId, reactionType, joinDepth) => {
return createReaction(authorId, postId, reactionType, joinDepth);
}
),

getAllReactions: query([nat32], Vec(Reaction), (joinDepth) => {
return getAllReactions(joinDepth);
}),

createThread: update(
[text, text, nat32],
Thread,
(title, authorId, joinDepth) => {
return createThread(title, authorId, joinDepth);
}
),

getAllThreads: query([nat32], Vec(Thread), (joinDepth) => {
return getAllThreads(joinDepth);
}),

createUser: update([text, nat32], User, (username, joinDepth) => {
return createUser(username, joinDepth);
}),

getAllUsers: query([nat32], Vec(User), (joinDepth) => {
return getAllUsers(joinDepth);
})
getAllPosts,
createReaction,
getAllReactions,
createThread,
getAllThreads,
createUser,
getAllUsers
});
6 changes: 3 additions & 3 deletions examples/complex_types/src/posts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { nat32, text, update, Vec } from 'azle';
import { nat32, query, text, update, Vec } from 'azle';
import { Post } from './candid_types';
import { getReactionFromStateReaction } from './reactions';
import { state, StatePost, StateThread, StateUser } from './state';
Expand Down Expand Up @@ -37,11 +37,11 @@ export const createPost = update(
}
);

export function getAllPosts(joinDepth: nat32): (typeof Post)[] {
export const getAllPosts = query([nat32], Vec(Post), (joinDepth) => {
return Object.values(state.posts).map((statePost) =>
getPostFromStatePost(statePost, joinDepth)
);
}
});

export function getPostFromStatePost(
statePost: StatePost,
Expand Down
53 changes: 26 additions & 27 deletions examples/complex_types/src/reactions.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,42 @@
import { nat32, Vec } from 'azle';
import { nat32, query, text, update, Vec } from 'azle';
import { Reaction, ReactionType } from './candid_types';
import { getPostFromStatePost } from './posts';
import { state, StatePost, StateReaction, StateUser } from './state';
import { getUserFromStateUser } from './users';

export function createReaction(
authorId: string,
postId: string,
reactionType: typeof ReactionType,
joinDepth: nat32
): typeof Reaction {
const id = Object.keys(state.reactions).length.toString();
export const createReaction = update(
[text, text, ReactionType, nat32],
Reaction,
(authorId, postId, reactionType, joinDepth) => {
const id = Object.keys(state.reactions).length.toString();

const stateReaction: StateReaction = {
id,
authorId,
postId,
reactionType
};
const updatedStateAuthor = getUpdatedStateAuthor(
authorId,
stateReaction.id
);
const updatedStatePost = getUpdatedStatePost(postId, stateReaction.id);
const stateReaction: StateReaction = {
id,
authorId,
postId,
reactionType
};
const updatedStateAuthor = getUpdatedStateAuthor(
authorId,
stateReaction.id
);
const updatedStatePost = getUpdatedStatePost(postId, stateReaction.id);

state.reactions[id] = stateReaction;
state.users[authorId] = updatedStateAuthor;
state.posts[postId] = updatedStatePost;
state.reactions[id] = stateReaction;
state.users[authorId] = updatedStateAuthor;
state.posts[postId] = updatedStatePost;

const reaction = getReactionFromStateReaction(stateReaction, joinDepth);
const reaction = getReactionFromStateReaction(stateReaction, joinDepth);

return reaction;
}
return reaction;
}
);

export function getAllReactions(joinDepth: nat32): (typeof Reaction)[] {
export const getAllReactions = query([nat32], Vec(Reaction), (joinDepth) => {
return Object.values(state.reactions).map((stateReaction) =>
getReactionFromStateReaction(stateReaction, joinDepth)
);
}
});

export function getReactionFromStateReaction(
stateReaction: StateReaction,
Expand Down
45 changes: 24 additions & 21 deletions examples/complex_types/src/threads.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
import { nat32, Vec } from 'azle';
import { nat32, query, text, update, Vec } from 'azle';
import { Thread } from './candid_types';
import { getPostFromStatePost } from './posts';
import { state, StateThread, StateUser } from './state';
import { getUserFromStateUser } from './users';

export function createThread(
title: string,
authorId: string,
joinDepth: nat32
): typeof Thread {
const id = Object.keys(state.threads).length.toString();
export const createThread = update(
[text, text, nat32],
Thread,
(title, authorId, joinDepth) => {
const id = Object.keys(state.threads).length.toString();

const stateThread: StateThread = {
id,
authorId,
postIds: [],
title
};
const updatedStateAuthor = getUpdatedStateAuthor(authorId, stateThread.id);
const stateThread: StateThread = {
id,
authorId,
postIds: [],
title
};
const updatedStateAuthor = getUpdatedStateAuthor(
authorId,
stateThread.id
);

state.threads[id] = stateThread;
state.users[authorId] = updatedStateAuthor;
state.threads[id] = stateThread;
state.users[authorId] = updatedStateAuthor;

const thread = getThreadFromStateThread(stateThread, joinDepth);
const thread = getThreadFromStateThread(stateThread, joinDepth);

return thread;
}
return thread;
}
);

export function getAllThreads(joinDepth: nat32): (typeof Thread)[] {
export const getAllThreads = query([nat32], Vec(Thread), (joinDepth) => {
return Object.values(state.threads).map((stateThread) =>
getThreadFromStateThread(stateThread, joinDepth)
);
}
});

export function getThreadFromStateThread(
stateThread: StateThread,
Expand Down
10 changes: 5 additions & 5 deletions examples/complex_types/src/users.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { nat32, Vec } from 'azle';
import { nat32, query, text, update, Vec } from 'azle';
import { User } from './candid_types';
import { getPostFromStatePost } from './posts';
import { getReactionFromStateReaction } from './reactions';
import { state, StateUser } from './state';
import { getThreadFromStateThread } from './threads';

export function createUser(username: string, joinDepth: nat32): typeof User {
export const createUser = update([text, nat32], User, (username, joinDepth) => {
const id = Object.keys(state.users).length.toString();

const stateUser: StateUser = {
Expand All @@ -21,13 +21,13 @@ export function createUser(username: string, joinDepth: nat32): typeof User {
const user = getUserFromStateUser(stateUser, joinDepth);

return user;
}
});

export function getAllUsers(joinDepth: nat32): (typeof User)[] {
export const getAllUsers = query([nat32], Vec(User), (joinDepth) => {
return Object.values(state.users).map((stateUser) =>
getUserFromStateUser(stateUser, joinDepth)
);
}
});

export function getUserFromStateUser(
stateUser: StateUser,
Expand Down

0 comments on commit ff9653c

Please sign in to comment.