Skip to content

Commit

Permalink
feat: update services
Browse files Browse the repository at this point in the history
  • Loading branch information
sirily11 committed Aug 25, 2022
1 parent 2e7d852 commit 3936771
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 13 deletions.
36 changes: 34 additions & 2 deletions functions/codeblock/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,59 @@
import { handler, Message } from ".";

describe("Given a handler", () => {
test("When generation succeed", async () => {
test("When parsing succeed", async () => {
const code = `
//@codeblock
string a = "a"`;

const message: Message = {
code: code,
mode: "parsing",
language: "sol",
};
const result = handler(message);
const data = JSON.parse(result.body);
expect(data.blocks.length).toBe(2);
});

test("When generation failed", async () => {
test("When generation succeed", async () => {
const code = `
//@codeblock
string a = "a"`;

const message: Message = {
blocks: [],
mode: "generation",
language: "sol",
};
const result = handler(message);
const data = JSON.parse(result.body);
expect(data.code).toBeDefined();
});

test("When failed", async () => {
const code = `
//@codeblock
string a = "a"`;

const message: Message = {
blocks: [],
//@ts-ignore
mode: "a",
language: "sol",
};
const result = handler(message);
expect(result.statusCode).toBe(400);
});

test("When parsing failed", async () => {
const code = `
//@codeblock
string a = "a"`;

const message: Message = {
code: code,
mode: "parsing",
language: "python",
};
const result = handler(message);
Expand Down
52 changes: 41 additions & 11 deletions functions/codeblock/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,59 @@
import { getParserByLanguage } from "@etherdata-blockchain/codeblock";
import {
CodeBlock,
getParserByLanguage,
} from "@etherdata-blockchain/codeblock";

export interface Message {
code: string;
code?: string;
blocks?: CodeBlock<any>[];
language: string;
mode: "generation" | "parsing";
}

interface HandlerResponse {
statusCode: number;
body: string;
}

export function handler({ code, language }: Message): HandlerResponse {
export function handler({
code,
language,
mode,
blocks,
}: Message): HandlerResponse {
try {
const parser = getParserByLanguage(language);
const blocks = parser.parse(code);
const generatedCode = parser.generate(blocks);
if (mode === "parsing") {
const blocks = parser.parse(code!);

const data = {
blocks,
code: generatedCode,
};
const data = {
blocks,
};

return {
statusCode: 200,
body: JSON.stringify(data),
};
}

if (mode === "generation") {
const code = parser.generate(blocks!);

const data = {
code,
};

return {
statusCode: 200,
body: JSON.stringify(data),
};
}

return {
statusCode: 200,
body: JSON.stringify(data),
statusCode: 400,
body: JSON.stringify({
message: "Error: mode not supported",
}),
};
} catch (err) {
return {
Expand Down

0 comments on commit 3936771

Please sign in to comment.