Skip to content

Commit

Permalink
simplify package url matching (#1184)
Browse files Browse the repository at this point in the history
* simplify package url matching

* remove unused import

* fix template for imported module

* add synonym for a imports

* fix imported modules and remove spaces

* fix import problem for module package in wrapper

* fix import formatting

* remove unused var from generated code

* import types if env used

* fix module deserialization in go-codegen

* add sanitization for imported pkgs names and fix problem with similar struct's names

* fix schema testcase location
  • Loading branch information
n0cte authored Sep 6, 2022
1 parent 2ae2c81 commit 9c5b9d3
Show file tree
Hide file tree
Showing 25 changed files with 282 additions and 253 deletions.
147 changes: 40 additions & 107 deletions packages/schema/bind/src/bindings/golang/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,41 +99,38 @@ export const makeImports: MustacheFn = () => {
return (text: string, render: (template: string) => string): string => {
const types = render(text).split(",");
const exist: { [key: string]: boolean } = {};
const pattern = new RegExp(
"^(https?:\\/\\/)?" + // protocol
"((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|" + // domain name
"((\\d{1,3}\\.){3}\\d{1,3}))" + // OR ip (v4) address
"(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*" + // port and path
"(\\?[;&a-z\\d%_.~+=-]*)?" + // query string
"(\\#[-a-z\\d_]*)?$",
"i"
);
for (const t of types) {
switch (t) {
case "*big.Int":
exist[
"github.com/consideritdone/polywrap-go/polywrap/msgpack/big"
] = true;
break;
case "*fastjson.Value":
exist["github.com/valyala/fastjson"] = true;
break;
default:
if (pattern.test(t)) {
exist[t] = true;
}
break;
for (let t of types) {
t = t.trim();
if (t.endsWith("big.Int")) {
exist[
"github.com/consideritdone/polywrap-go/polywrap/msgpack/big"
] = true;
} else if (t.endsWith("fastjson.Value")) {
exist["github.com/valyala/fastjson"] = true;
} else if (/([^/\s]+\/)(.*)/.test(t)) {
exist[t] = true;
}
}
const imports: Array<string> = [
"github.com/consideritdone/polywrap-go/polywrap/msgpack",
];
const imports: Array<string> = [];
imports.push(...Object.keys(exist));
const txt = imports
.sort()
.map((imp) => `\t"${imp}"`)
.map((imp) => {
let res = "";
if (imp.startsWith(". ")) {
res = `. "${imp.slice(2)}"`;
} else {
const parts = imp.split(" as ");
if (parts.length > 1) {
res = `${parts[1]} "${parts[0]}"`;
} else {
res = `"${imp}"`;
}
}
return `\t${res}`;
})
.join("\n");
return `import (\n${txt}\n)`;
return txt !== "" ? `\nimport (\n${txt}\n)\n\n` : "\n";
};
};

Expand Down Expand Up @@ -220,33 +217,6 @@ export const toMsgPack: MustacheFn = () => {
};
};

export const typeable: MustacheFn = () => {
return (value: string, render: (template: string) => string) => {
const type = render(value);
switch (type) {
case "int8":
case "int16":
case "int32":
case "int64":
case "uint8":
case "uint16":
case "uint32":
case "uint64":
case "string":
case "bool":
case "[]byte":
case "*big.Int":
case "*fastjson.Value":
return type;
default:
if (type.startsWith("*")) {
return `*types.${type.slice(1)}`;
}
return `types.${type}`;
}
};
};

export const toWasm: MustacheFn = () => {
return (value: string, render: (template: string) => string) => {
let type = render(value);
Expand Down Expand Up @@ -376,69 +346,32 @@ const applyOptional = (type: string, optional: boolean, _: boolean): string => {
}
};

function replaceAt(str: string, index: number, replacement: string): string {
return (
str.substr(0, index) + replacement + str.substr(index + replacement.length)
);
}

export const toLower: MustacheFn = () => {
return (value: string, render: (template: string) => string) => {
let type = render(value);

for (let i = 0; i < type.length; ++i) {
const char = type.charAt(i);
const lower = char.toLowerCase();

if (char !== lower) {
// Replace the uppercase char w/ the lowercase version
type = replaceAt(type, i, lower);

// if (i !== 0 && type[i - 1] !== "_") {
// // Make sure all lowercase conversions have an underscore before them
// type = insertAt(type, i, "_");
// }
}
}

return type;
return render(value)
.split("")
.map((v) => v.toLowerCase())
.join("");
};
};

export const toFirstLower: MustacheFn = () => {
return (value: string, render: (template: string) => string) => {
let type = render(value);

// First character must always be upper case
const firstChar = type.charAt(0);
const firstLower = firstChar.toLowerCase();
type = replaceAt(type, 0, firstLower);

return type;
const type = render(value);
return type.charAt(0).toLowerCase() + type.slice(1);
};
};

export const toUpper: MustacheFn = () => {
return (value: string, render: (template: string) => string) => {
let type = render(value);

// First character must always be upper case
const firstChar = type.charAt(0);
const firstUpper = firstChar.toUpperCase();
type = replaceAt(type, 0, firstUpper);

// Look for any underscores, remove them if they exist, and make next letter uppercase
// for (let i = 0; i < type.length; ++i) {
// const char = type.charAt(i);
//
// if (char === "_") {
// const nextChar = type.charAt(i + 1);
// const nextCharUpper = nextChar.toUpperCase();
// type = replaceAt(type, i + 1, nextCharUpper);
// type = removeAt(type, i);
// }
// }
const type = render(value);
return type.charAt(0).toUpperCase() + type.slice(1);
};
};

return type;
export const pkgName: MustacheFn = () => {
return (text: string, render: (template: string) => string): string => {
const name = render(text);
return reservedWordsAS.has(name) ? `pkg${name}` : name;
};
};
25 changes: 25 additions & 0 deletions packages/schema/bind/src/bindings/golang/reservedWords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,29 @@ export const reservedWordsAS: Set<string> = new Set([
"__visit",
"__newBuffer",
"__newArray",
"break",
"default",
"func",
"interface",
"select",
"case",
"defer",
"go",
"map",
"struct",
"chan",
"else",
"goto",
"package",
"switch",
"const",
"fallthrough",
"if",
"range",
"type",
"continue",
"for",
"import",
"return",
"var",
]);
31 changes: 25 additions & 6 deletions packages/schema/bind/src/bindings/golang/wasm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { GenerateBindingFn } from "../..";
import { renderTemplates } from "../..";
import { loadSubTemplates } from "../../utils";
import { BindOptions, BindOutput } from "../../..";
import { reservedWordsAS } from "../reservedWords";

import {
Abi,
Expand All @@ -19,6 +20,10 @@ const subTemplates = loadSubTemplates(templatesDir.entries);
const templatePath = (subpath: string) =>
path.join(__dirname, "./templates", subpath);

function pkgName(str: string): string {
return reservedWordsAS.has(str) ? `pkg${str}` : str;
}

function camel2snake(str: string): string {
str = str.replace(/([A-Z])/g, "_$1");
str = str.startsWith("_") ? str.slice(1) : str;
Expand Down Expand Up @@ -61,7 +66,7 @@ export const generateBinding: GenerateBindingFn = (
for (const importedModuleType of abi.importedModuleTypes) {
importEntries.push({
type: "Directory",
name: `${camel2snake(importedModuleType.namespace)}`,
name: `${pkgName(camel2snake(importedModuleType.namespace))}`,
data: renderTemplates(
templatePath("imported/module-type"),
importedModuleType,
Expand All @@ -76,7 +81,7 @@ export const generateBinding: GenerateBindingFn = (
for (const importedEnvType of abi.importedEnvTypes) {
importEntries.push({
type: "Directory",
name: `${camel2snake(importedEnvType.namespace)}`,
name: `${pkgName(camel2snake(importedEnvType.namespace))}`,
data: renderTemplates(
templatePath("imported/env-type"),
importedEnvType,
Expand All @@ -91,7 +96,7 @@ export const generateBinding: GenerateBindingFn = (
for (const importedEnumType of abi.importedEnumTypes) {
importEntries.push({
type: "Directory",
name: `${camel2snake(importedEnumType.namespace)}`,
name: `${pkgName(camel2snake(importedEnumType.namespace))}`,
data: renderTemplates(
templatePath("imported/enum-type"),
importedEnumType,
Expand All @@ -106,7 +111,7 @@ export const generateBinding: GenerateBindingFn = (
for (const importedObectType of abi.importedObjectTypes) {
importEntries.push({
type: "Directory",
name: `${camel2snake(importedObectType.namespace)}`,
name: `${pkgName(camel2snake(importedObectType.namespace))}`,
data: renderTemplates(
templatePath("imported/object-type"),
importedObectType,
Expand Down Expand Up @@ -144,12 +149,26 @@ export const generateBinding: GenerateBindingFn = (

// Generate module type folders
if (abi.moduleType) {
const imports: { [key: string]: boolean } = {};
abi.moduleType.methods?.forEach(function (method) {
method.arguments?.forEach(function (arg) {
const tp = abi.importedObjectTypes?.find(function (mt) {
return mt.type === arg.type;
});
if (tp) {
imports[tp.namespace] = true;
}
});
});
const importedTypes = Object.keys(imports).map((namespace) => ({
namespace,
}));
output.entries.push({
type: "Directory",
name: "types",
data: renderTemplates(
templatePath("module-type/types"),
{ goImport, ...abi.moduleType },
{ importedTypes, goImport, ...abi.moduleType },
subTemplates
),
});
Expand All @@ -158,7 +177,7 @@ export const generateBinding: GenerateBindingFn = (
name: "module",
data: renderTemplates(
templatePath("module-type/module"),
{ goImport, ...abi.moduleType },
{ importedTypes, goImport, ...abi.moduleType },
subTemplates
),
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package types

{{#makeImports}}{{#properties}}{{#toWasm}}{{toGraphQLType}}{{/toWasm}}{{^last}},{{/last}}{{/properties}}{{/makeImports}}
{{#makeImports}}github.com/consideritdone/polywrap-go/polywrap/msgpack,{{#properties}}{{#toWasm}}{{toGraphQLType}}{{/toWasm}}{{^last}},{{/last}}{{/properties}}{{/makeImports}}

type {{#toUpper}}{{type}}{{/toUpper}} struct {
{{#stuctProps}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package types

{{#makeImports}}{{#properties}}{{#toWasm}}{{toGraphQLType}}{{/toWasm}}{{^last}},{{/last}}{{/properties}}{{/makeImports}}
{{#makeImports}}github.com/consideritdone/polywrap-go/polywrap/msgpack,{{#properties}}{{#toWasm}}{{toGraphQLType}}{{/toWasm}}{{^last}},{{/last}}{{/properties}}{{/makeImports}}

func serialize{{#toUpper}}{{type}}{{/toUpper}}(value *{{#toUpper}}{{type}}{{/toUpper}}) []byte {
ctx := msgpack.NewContext("Serializing (encoding) env-type: {{#toUpper}}{{type}}{{/toUpper}}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package {{#toSnakeCase}}{{namespace}}{{/toSnakeCase}}
package {{#pkgName}}{{#toSnakeCase}}{{namespace}}{{/toSnakeCase}}{{/pkgName}}

type {{#toUpper}}{{type}}{{/toUpper}} int32

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package {{#toSnakeCase}}{{namespace}}{{/toSnakeCase}}

{{#makeImports}}{{#properties}}{{#toWasm}}{{toGraphQLType}}{{/toWasm}}{{^last}},{{/last}}{{/properties}}{{/makeImports}}

package {{#pkgName}}{{#toSnakeCase}}{{namespace}}{{/toSnakeCase}}{{/pkgName}}
{{#makeImports}}
github.com/consideritdone/polywrap-go/polywrap/msgpack,
{{#properties}}
{{#toWasm}}{{toGraphQLType}}{{/toWasm}}{{^last}},{{/last}}
{{/properties}}
{{/makeImports}}
type {{#toUpper}}{{type}}{{/toUpper}} struct {
{{#stuctProps}}
{{#properties}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package {{#toSnakeCase}}{{namespace}}{{/toSnakeCase}}

{{#makeImports}}{{#properties}}{{#toWasm}}{{toGraphQLType}}{{/toWasm}}{{^last}},{{/last}}{{/properties}}{{/makeImports}}

package {{#pkgName}}{{#toSnakeCase}}{{namespace}}{{/toSnakeCase}}{{/pkgName}}
{{#makeImports}}
github.com/consideritdone/polywrap-go/polywrap/msgpack,
{{#properties}}
{{#toWasm}}{{toGraphQLType}}{{/toWasm}}{{^last}},{{/last}}
{{/properties}}
{{/makeImports}}
func serialize{{#toUpper}}{{type}}{{/toUpper}}(value *{{#toUpper}}{{type}}{{/toUpper}}) []byte {
ctx := msgpack.NewContext("Serializing (encoding) env-type: {{#toUpper}}{{type}}{{/toUpper}}")
encoder := msgpack.NewWriteEncoder(ctx)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package {{#toSnakeCase}}{{namespace}}{{/toSnakeCase}}
package {{#pkgName}}{{#toSnakeCase}}{{namespace}}{{/toSnakeCase}}{{/pkgName}}

{{#capabilities}}
{{#getImplementations}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package {{#toSnakeCase}}{{namespace}}{{/toSnakeCase}}

{{#makeImports}}{{#arguments}}{{#toWasm}}{{toGraphQLType}}{{/toWasm}}{{^last}},{{/last}}{{/arguments}}{{/makeImports}}

package {{#pkgName}}{{#toSnakeCase}}{{namespace}}{{/toSnakeCase}}{{/pkgName}}
{{#makeImports}}
github.com/consideritdone/polywrap-go/polywrap/msgpack,
{{#arguments}}
{{#toWasm}}{{toGraphQLType}}{{/toWasm}}{{^last}},{{/last}}
{{/arguments}}
{{/makeImports}}
{{#methods}}
type Args{{#toUpper}}{{name}}{{/toUpper}} struct {
{{#stuctProps}}
Expand All @@ -19,6 +22,7 @@ func Serialize{{#toUpper}}{{name}}{{/toUpper}}Args(value *Args{{#toUpper}}{{name
}

func Write{{#toUpper}}{{name}}{{/toUpper}}Args(writer msgpack.Write, value *Args{{#toUpper}}{{name}}{{/toUpper}}) {
{{#arguments.length}}
writer.WriteMapLength({{arguments.length}})
{{#arguments}}
writer.Context().Push("{{name}}", "{{#toWasm}}{{toGraphQLType}}{{/toWasm}}", "writing property")
Expand All @@ -44,6 +48,7 @@ func Write{{#toUpper}}{{name}}{{/toUpper}}Args(writer msgpack.Write, value *Args
{{/enum}}
writer.Context().Pop()
{{/arguments}}
{{/arguments.length}}
}

func Deserialize{{#toUpper}}{{name}}{{/toUpper}}Result(argsBuf []byte) {{#return}}{{#toWasm}}{{toGraphQLType}}{{/toWasm}}{{/return}} {
Expand Down
Loading

0 comments on commit 9c5b9d3

Please sign in to comment.