Skip to content

Commit

Permalink
add serialization and deserialization for impors, interfaces, modules
Browse files Browse the repository at this point in the history
  • Loading branch information
n0cte committed Aug 18, 2022
1 parent c295d1f commit ad3ca8b
Show file tree
Hide file tree
Showing 30 changed files with 1,060 additions and 175 deletions.
8 changes: 8 additions & 0 deletions packages/schema/bind/src/bindings/golang/wasm-go/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ export const readPointer: MustacheFn = () => {
}
}

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 handleKeywords: MustacheFn = () => {
return (text: string, render: (template: string) => string): string => {
const rendered: string = render(text);
Expand Down
216 changes: 111 additions & 105 deletions packages/schema/bind/src/bindings/golang/wasm-go/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ const subTemplates = loadSubTemplates(templatesDir.entries);
const templatePath = (subpath: string) =>
path.join(__dirname, "./templates", subpath);

function camel2snake(str: string): string {
str = str.replace( /([A-Z])/g, "_$1");
str = str.startsWith("_") ? str.slice(1) : str;
return str.toLowerCase();
}

export const generateBinding: GenerateBindingFn = (
options: BindOptions
): BindOutput => {
Expand All @@ -34,7 +40,7 @@ export const generateBinding: GenerateBindingFn = (
for (const objectType of abi.objectTypes) {
output.entries.push({
type: "Directory",
name: "abc",
name: "types",
data: renderTemplates(
templatePath("object-type"),
objectType,
Expand All @@ -59,114 +65,114 @@ export const generateBinding: GenerateBindingFn = (
const importEntries: OutputEntry[] = [];

// Generate imported module type folders
// for (const importedModuleType of abi.importedModuleTypes) {
// importEntries.push({
// type: "Directory",
// name: importedModuleType.type,
// data: renderTemplates(
// templatePath("imported/module-type"),
// importedModuleType,
// subTemplates
// ),
// });
// }
//
for (const importedModuleType of abi.importedModuleTypes) {
importEntries.push({
type: "Directory",
name: `${camel2snake(importedModuleType.namespace)}`,
data: renderTemplates(
templatePath("imported/module-type"),
importedModuleType,
subTemplates
),
});
}

// // Generate imported env type folders
// for (const importedEnvType of abi.importedEnvTypes) {
// importEntries.push({
// type: "Directory",
// name: importedEnvType.type,
// data: renderTemplates(
// templatePath("imported/env-type"),
// importedEnvType,
// subTemplates
// ),
// });
// }
//
// // Generate imported enum type folders
// for (const importedEnumType of abi.importedEnumTypes) {
// importEntries.push({
// type: "Directory",
// name: importedEnumType.type,
// data: renderTemplates(
// templatePath("imported/enum-type"),
// importedEnumType,
// subTemplates
// ),
// });
// }
//
// // Generate imported object type folders
// for (const importedObectType of abi.importedObjectTypes) {
// importEntries.push({
// type: "Directory",
// name: importedObectType.type,
// data: renderTemplates(
// templatePath("imported/object-type"),
// importedObectType,
// subTemplates
// ),
// });
// }
//
// if (importEntries.length) {
// output.entries.push({
// type: "Directory",
// name: "imported",
// data: [
// ...importEntries,
// ...renderTemplates(templatePath("imported"), abi, subTemplates),
// ],
// });
// }
//
// // Generate interface type folders
// for (const interfaceType of abi.interfaceTypes) {
// output.entries.push({
// type: "Directory",
// name: interfaceType.type,
// data: renderTemplates(
// templatePath("interface-type"),
// interfaceType,
// subTemplates
// ),
// });
// }
//
for (const importedEnvType of abi.importedEnvTypes) {
importEntries.push({
type: "Directory",
name: `${camel2snake(importedEnvType.namespace)}`,
data: renderTemplates(
templatePath("imported/env-type"),
importedEnvType,
subTemplates
),
});
}

// Generate imported enum type folders
for (const importedEnumType of abi.importedEnumTypes) {
importEntries.push({
type: "Directory",
name: `${camel2snake(importedEnumType.namespace)}`,
data: renderTemplates(
templatePath("imported/enum-type"),
importedEnumType,
subTemplates
),
});
}

// Generate imported object type folders
for (const importedObectType of abi.importedObjectTypes) {
importEntries.push({
type: "Directory",
name: `${camel2snake(importedObectType.namespace)}`,
data: renderTemplates(
templatePath("imported/object-type"),
importedObectType,
subTemplates
),
});
}

if (importEntries.length) {
output.entries.push({
type: "Directory",
name: "imported",
data: [
...importEntries,
...renderTemplates(templatePath("imported"), abi, subTemplates),
],
});
}

// Generate interface type folders
for (const interfaceType of abi.interfaceTypes) {
output.entries.push({
type: "Directory",
name: "interfaces",
data: renderTemplates(
templatePath("interface-type"),
interfaceType,
subTemplates
),
});
}

// Generate module type folders
// if (abi.moduleType) {
// output.entries.push({
// type: "Directory",
// name: abi.moduleType.type,
// data: renderTemplates(
// templatePath("module-type"),
// abi.moduleType,
// subTemplates
// ),
// });
// }
//
// // Generate enum type folders
// for (const enumType of abi.enumTypes) {
// output.entries.push({
// type: "Directory",
// name: enumType.type,
// data: renderTemplates(templatePath("enum-type"), enumType, subTemplates),
// });
// }
//
// // Generate env type folders
// if (abi.envType) {
// output.entries.push({
// type: "Directory",
// name: abi.envType.type,
// data: renderTemplates(templatePath("object-type"), abi.envType, subTemplates),
// });
// }
if (abi.moduleType) {
output.entries.push({
type: "Directory",
name: "types",
data: renderTemplates(
templatePath("module-type"),
abi.moduleType,
subTemplates
),
});
}

// Generate enum type folders
for (const enumType of abi.enumTypes) {
output.entries.push({
type: "Directory",
name: "types",
data: renderTemplates(templatePath("enum-type"), enumType, subTemplates),
});
}

// Generate env type folders
if (abi.envType) {
output.entries.push({
type: "Directory",
name: "types",
data: renderTemplates(templatePath("object-type"), abi.envType, subTemplates),
});
}

// Generate root entry file
// output.entries.push(...renderTemplates(templatePath(""), abi, subTemplates));
output.entries.push(...renderTemplates(templatePath(""), abi, subTemplates));

return result;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package {{#toSnakeCase}}{{namespace}}{{/toSnakeCase}}

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

const (
{{#constants}}
{{#toUpper}}{{type}}{{/toUpper}}{{.}} = iota
{{/constants}}
{{#toFirstLower}}{{type}}{{/toFirstLower}}Max = iota
)

func Sanitize{{#toUpper}}{{type}}{{/toUpper}}Value(value int32) {
if !(value >= 0 && value < int32({{#toFirstLower}}{{type}}{{/toFirstLower}}Max)) {
panic("Invalid value for enum '{{#toUpper}}{{type}}{{/toUpper}}'")
}
}

func Get{{#toUpper}}{{type}}{{/toUpper}}Value(key string) {{#toUpper}}{{type}}{{/toUpper}} {
switch key {
{{#constants}}
case "{{.}}":
return {{#toUpper}}{{type}}{{/toUpper}}{{.}}
{{/constants}}
default:
panic("Invalid key for enum '{{#toUpper}}{{type}}{{/toUpper}}'")
}
}

func Get{{#toUpper}}{{type}}{{/toUpper}}Key(value {{#toUpper}}{{type}}{{/toUpper}}) string {
Sanitize{{#toUpper}}{{type}}{{/toUpper}}Value(int32(value))
switch value {
{{#constants}}
case {{#toUpper}}{{type}}{{/toUpper}}{{.}}:
return "{{.}}"
{{/constants}}
default:
panic("Invalid value for enum '{{#toUpper}}{{type}}{{/toUpper}}'")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package {{#toSnakeCase}}{{namespace}}{{/toSnakeCase}}

import (
"github.com/consideritdone/polywrap-go/polywrap/msgpack"
"github.com/consideritdone/polywrap-go/polywrap/msgpack/big"
"github.com/consideritdone/polywrap-go/polywrap/msgpack/container"
"github.com/valyala/fastjson"
)

type {{#toUpper}}{{type}}{{/toUpper}} struct {
{{#properties}}
{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}} {{#toWasm}}{{toGraphQLType}}{{/toWasm}}
{{/properties}}
}

func {{#toUpper}}{{type}}{{/toUpper}}ToBuffer(value *{{#toUpper}}{{type}}{{/toUpper}}) []byte {
return serialize{{#toUpper}}{{type}}{{/toUpper}}(value)
}

func {{#toUpper}}{{type}}{{/toUpper}}FromBuffer(data []byte) *{{#toUpper}}{{type}}{{/toUpper}} {
return deserialize{{#toUpper}}{{type}}{{/toUpper}}(data)
}

func {{#toUpper}}{{type}}{{/toUpper}}Write(writer msgpack.Write, value *{{#toUpper}}{{type}}{{/toUpper}}) {
write{{#toUpper}}{{type}}{{/toUpper}}(writer, value)
}

func {{#toUpper}}{{type}}{{/toUpper}}Read(reader msgpack.Read) *{{#toUpper}}{{type}}{{/toUpper}} {
return read{{#toUpper}}{{type}}{{/toUpper}}(reader)
}

Loading

0 comments on commit ad3ca8b

Please sign in to comment.