Skip to content

Commit

Permalink
fix code style
Browse files Browse the repository at this point in the history
  • Loading branch information
n0cte committed Aug 23, 2022
1 parent f7bb1f6 commit e38f83a
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 98 deletions.
186 changes: 95 additions & 91 deletions packages/schema/bind/src/bindings/golang/wasm-go/functions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isBaseType } from "./baseTypes";
import { reservedWordsAS } from "./reservedWords";
import { MustacheFn } from "../../types";

Expand All @@ -7,131 +6,147 @@ let num = -1;
export const startIter: MustacheFn = () => {
return (): string => {
num = -1;
return ""
}
}
return "";
};
};

export const stopIter: MustacheFn = () => {
return (): string => {
num = -1;
return ""
}
}
return "";
};
};

export const currIter: MustacheFn = () => {
return (text: string, render: (template: string) => string): string => {
const rendered: string = render(text);
return `${rendered}${num}`;
}
}
};
};

export const nextIter: MustacheFn = () => {
return (text: string, render: (template: string) => string): string => {
const rendered: string = render(text);
return `${rendered}${++num}`;
}
}
};
};

export const prevFullIter: MustacheFn = () => {
return (text: string, render: (template: string) => string): string => {
const rendered: string = render(text);
if (rendered == "stop") {
return "";
}
return Array(num).fill(0).map((_, i) => `[${rendered}${i}]`).join("");
}
}
return Array(num)
.fill(0)
.map((_, i) => `[${rendered}${i}]`)
.join("");
};
};

export const lastFullIter: MustacheFn = () => {
return (text: string, render: (template: string) => string): string => {
const rendered: string = render(text);
if (rendered == "stop") {
return "";
}
return Array(num + 1).fill(0).map((_, i) => `[${rendered}${i}]`).join("");
}
}
return Array(num + 1)
.fill(0)
.map((_, i) => `[${rendered}${i}]`)
.join("");
};
};

export const writePointer: MustacheFn = () => {
return (text: string, render: (template: string) => string): string => {
const [type, value] = render(text).split(" - ");
let pointer = "*";
if ({
"BigInt": true,
"Json": true,
"Bytes": true,
}[type] ?? false) {
pointer = "";
switch (type) {
case "BigInt":
case "Json":
case "Bytes":
pointer = "";
break;
}
return `writer.Write${type}(${pointer}${value})`
}
}
return `writer.Write${type}(${pointer}${value})`;
};
};

export const readPointer: MustacheFn = () => {
return (text: string, render: (template: string) => string): string => {
const [type, value] = render(text).split(" - ");
let pointer = "&";
if ({
"BigInt": true,
"Json": true,
"Bytes": true,
}[type] ?? false) {
pointer = "";
switch (type) {
case "BigInt":
case "Json":
case "Bytes":
pointer = "";
break;
}
return `${pointer}${value}`
}
}
return `${pointer}${value}`;
};
};

export const toSnakeCase: MustacheFn = () => {
return (text: string, render: (template: string) => string): string => {
text = render(text).replace(/([A-Z])/g, "_$1");
text = text.startsWith("_") ? text.slice(1) : text;
return text.toLowerCase();
}
}
};
};

export const makeImports: MustacheFn = () => {
return (text: string, render: (template: string) => string): string => {
const types = render(text).split(",")
const exist: {[key:string]: boolean} = {};
for (const t of types){
const types = render(text).split(",");
const exist: { [key: string]: boolean } = {};
for (const t of types) {
switch (t) {
case "*big.Int":
exist["github.com/consideritdone/polywrap-go/polywrap/msgpack/big"] = true;
break
exist[
"github.com/consideritdone/polywrap-go/polywrap/msgpack/big"
] = true;
break;
case "*fastjson.Value":
exist["github.com/valyala/fastjson"] = true;
break;
}
}
let imports: Array<string> = ["github.com/consideritdone/polywrap-go/polywrap/msgpack"];
const imports: Array<string> = [
"github.com/consideritdone/polywrap-go/polywrap/msgpack",
];
imports.push(...Object.keys(exist));
const txt = imports.sort().map(imp => `\t"${imp}"`).join("\n");
return `import (\n${txt}\n)`
}
}
const txt = imports
.sort()
.map((imp) => `\t"${imp}"`)
.join("\n");
return `import (\n${txt}\n)`;
};
};

export const stuctProps: MustacheFn = () => {
return (text: string, render: (template: string) => string): string => {
let props: [string, string][] = render(text).split("\n")
.map(line => line.trimEnd())
.filter(line => line !== "")
.map(line => line.split(" ") as [string, string])
const props: [string, string][] = render(text)
.split("\n")
.map((line) => line.trimEnd())
.filter((line) => line !== "")
.map((line) => line.split(" ") as [string, string]);
let maxPropNameLn = 0;
for (const [propName] of props) {
if (propName.length > maxPropNameLn) {
maxPropNameLn = propName.length
maxPropNameLn = propName.length;
}
}
for (let i = 0; i < props.length; i++) {
if (props[i][0].length < maxPropNameLn) {
props[i][0] += Array(maxPropNameLn - props[i][0].length).fill(" ").join("");
props[i][0] += Array(maxPropNameLn - props[i][0].length)
.fill(" ")
.join("");
}
props[i][0] = "\t" + props[i][0];
}
return props.map(v => v.join(" ")).join("\n") + "\n";
}
}
return props.map((v) => v.join(" ")).join("\n") + "\n";
};
};

export const handleKeywords: MustacheFn = () => {
return (text: string, render: (template: string) => string): string => {
Expand All @@ -146,50 +161,46 @@ export const handleKeywords: MustacheFn = () => {
export const toMsgPack: MustacheFn = () => {
return (value: string, render: (template: string) => string) => {
let type = render(value);
let modifier = "";
if (type[type.length - 1] === "!") {
type = type.substring(0, type.length - 1);
} else {
modifier = "Optional";
}

let t = type;
if (type.startsWith("[")) {
t = "Array";
} else if (type.startsWith("Map")) {
t = "Map";
} else if (type.startsWith("Int8")) {
t = "I8"
t = "I8";
} else if (type.startsWith("Int16")) {
t = "I16"
t = "I16";
} else if (type.startsWith("Int32")) {
t = "I32"
t = "I32";
} else if (type.startsWith("Int64")) {
t = "I64"
t = "I64";
} else if (type.startsWith("Int")) {
t = "I32"
t = "I32";
} else if (type.startsWith("UInt8")) {
t = "U8"
t = "U8";
} else if (type.startsWith("UInt16")) {
t = "U16"
t = "U16";
} else if (type.startsWith("UInt32")) {
t = "U32"
t = "U32";
} else if (type.startsWith("UInt64")) {
t = "U64"
t = "U64";
} else if (type.startsWith("UInt")) {
t = "U32"
t = "U32";
} else if (type.startsWith("String")) {
t = "String"
t = "String";
} else if (type.startsWith("Boolean")) {
t = "Bool"
t = "Bool";
} else if (type.startsWith("Bytes")) {
t = "Bytes"
t = "Bytes";
} else if (type.startsWith("BigInt")) {
t = "BigInt"
t = "BigInt";
} else if (type.startsWith("BigNumber")) {
t = "BigInt"
t = "BigInt";
} else if (type.startsWith("JSON")) {
t = "Json"
t = "Json";
}
return t;
};
Expand Down Expand Up @@ -311,13 +322,14 @@ const toWasmMap = (type: string, optional: boolean): string => {
return applyOptional(`map[${keyType}]${valType}`, optional, false);
};

const applyOptional = (
type: string,
optional: boolean,
isEnum: boolean
): string => {
if (optional && !type.startsWith("*") && !type.startsWith("[]") && !type.startsWith("map")) {
return `*${type}`
const applyOptional = (type: string, optional: boolean, _: boolean): string => {
if (
optional &&
!type.startsWith("*") &&
!type.startsWith("[]") &&
!type.startsWith("map")
) {
return `*${type}`;
} else {
return type;
}
Expand All @@ -329,14 +341,6 @@ function replaceAt(str: string, index: number, replacement: string): string {
);
}

function insertAt(str: string, index: number, insert: string): string {
return str.substr(0, index) + insert + str.substr(index);
}

function removeAt(str: string, index: number): string {
return str.substr(0, index) + str.substr(index + 1);
}

export const toLower: MustacheFn = () => {
return (value: string, render: (template: string) => string) => {
let type = render(value);
Expand Down
14 changes: 10 additions & 4 deletions packages/schema/bind/src/bindings/golang/wasm-go/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,11 @@ export const generateBinding: GenerateBindingFn = (
output.entries.push({
type: "Directory",
name: "types",
data: renderTemplates(templatePath("object-type"), abi.envType, subTemplates),
data: renderTemplates(
templatePath("object-type"),
abi.envType,
subTemplates
),
});
}

Expand All @@ -171,13 +175,15 @@ function mergePaths(array: OutputEntry[]): OutputEntry[] {
for (let i = 0; i < array.length; i++) {
switch (array[i].type) {
case "File":
tmp[array[i].name] = array[i]
tmp[array[i].name] = array[i];
break;
case "Directory":
if (!tmp[array[i].name]) {
tmp[array[i].name] = array[i]
tmp[array[i].name] = array[i];
} else {
(tmp[array[i].name].data as OutputEntry[]).push(...(array[i].data as OutputEntry[]))
(tmp[array[i].name].data as OutputEntry[]).push(
...(array[i].data as OutputEntry[])
);
}
break;
}
Expand Down
5 changes: 3 additions & 2 deletions packages/schema/bind/src/bindings/utils/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ function transformName(str: string, view: unknown): string {
return str;
}
const def = view as GenericDefinition;
str = str.replace("%type%", def.type)
.replace( /([A-Z])/g, "_$1")
str = str
.replace("%type%", def.type)
.replace(/([A-Z])/g, "_$1")
.toLowerCase();
return str.startsWith("_") ? str.slice(1) : str;
}
Expand Down
7 changes: 6 additions & 1 deletion packages/schema/bind/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { Abi } from "@polywrap/schema-parse";
import { OutputDirectory } from "@polywrap/os-js";

export type BindLanguage = "wasm-as" | "wasm-rs" | "wasm-go" | "plugin-ts" | "app-ts";
export type BindLanguage =
| "wasm-as"
| "wasm-rs"
| "wasm-go"
| "plugin-ts"
| "app-ts";

export interface BindOutput {
output: OutputDirectory;
Expand Down

0 comments on commit e38f83a

Please sign in to comment.