Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion packages/schema/bind/src/bindings/golang/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ 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":
Expand All @@ -109,6 +118,11 @@ export const makeImports: MustacheFn = () => {
case "*fastjson.Value":
exist["github.com/valyala/fastjson"] = true;
break;
default:
if (pattern.test(t)) {
exist[t] = true;
}
break;
}
}
const imports: Array<string> = [
Expand Down Expand Up @@ -206,6 +220,33 @@ 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 @@ -280,7 +321,7 @@ export const toWasm: MustacheFn = () => {
type = `${type.replace("Enum_", "")}`;
isEnum = true;
} else {
type = `${type}`;
type = type.charAt(0).toUpperCase() + type.slice(1);
}
}

Expand Down
11 changes: 10 additions & 1 deletion packages/schema/bind/src/bindings/golang/wasm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,16 @@ export const generateBinding: GenerateBindingFn = (
type: "Directory",
name: "types",
data: renderTemplates(
templatePath("module-type"),
templatePath("module-type/types"),
{ goImport, ...abi.moduleType },
subTemplates
),
});
output.entries.push({
type: "Directory",
name: "module",
data: renderTemplates(
templatePath("module-type/module"),
{ goImport, ...abi.moduleType },
subTemplates
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
"{{goImport}}/wrap/types"
"{{goImport}}/wrap/module"
"github.com/consideritdone/polywrap-go/polywrap"
)

Expand All @@ -12,7 +12,7 @@ func _wrap_invoke(methodSize, argsSize, envSize uint32) bool {
{{#moduleType}}
{{#methods}}
case "{{name}}":
return polywrap.WrapInvoke(args, envSize, types.{{#toUpper}}{{name}}{{/toUpper}}Wrapped)
return polywrap.WrapInvoke(args, envSize, module.{{#toUpper}}{{name}}{{/toUpper}}Wrapped)
{{/methods}}
{{/moduleType}}
default:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
package types
package module

{{#makeImports}}{{#arguments}}{{#toWasm}}{{toGraphQLType}}{{/toWasm}}{{^last}},{{/last}}{{/arguments}}{{/makeImports}}
{{#makeImports}}{{goImport}}/wrap/types,{{#arguments}}{{#toWasm}}{{toGraphQLType}}{{/toWasm}}{{^last}},{{/last}}{{/arguments}}{{/makeImports}}

{{#methods}}
type Args{{#toUpper}}{{name}}{{/toUpper}} struct {
{{#stuctProps}}
{{#arguments}}
{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}} {{#toWasm}}{{toGraphQLType}}{{/toWasm}}
{{/arguments}}
{{/stuctProps}}
}

func Deserialize{{#toUpper}}{{name}}{{/toUpper}}Args(argsBuf []byte) *Args{{#toUpper}}{{name}}{{/toUpper}} {
func Deserialize{{#toUpper}}{{name}}{{/toUpper}}Args(argsBuf []byte) *types.Args{{#toUpper}}{{name}}{{/toUpper}} {
ctx := msgpack.NewContext("Deserializing module-type: {{#toUpper}}{{name}}{{/toUpper}}")
{{#arguments.length}}
reader := msgpack.NewReadDecoder(ctx, argsBuf)
Expand Down Expand Up @@ -71,7 +63,7 @@ func Deserialize{{#toUpper}}{{name}}{{/toUpper}}Args(argsBuf []byte) *Args{{#toU
{{/arguments}}
{{/arguments.length}}

return &Args{{#toUpper}}{{name}}{{/toUpper}}{
return &types.Args{{#toUpper}}{{name}}{{/toUpper}}{
{{#stuctProps}}
{{#arguments}}
{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}: _{{name}},
Expand All @@ -80,14 +72,14 @@ func Deserialize{{#toUpper}}{{name}}{{/toUpper}}Args(argsBuf []byte) *Args{{#toU
}
}

func Serialize{{#toUpper}}{{name}}{{/toUpper}}Result(value {{#return}}{{#toWasm}}{{toGraphQLType}}{{/toWasm}}{{/return}}) []byte {
func Serialize{{#toUpper}}{{name}}{{/toUpper}}Result(value {{#return}}{{#typeable}}{{#toWasm}}{{toGraphQLType}}{{/toWasm}}{{/typeable}}{{/return}}) []byte {
ctx := msgpack.NewContext("Serializing module-type: {{#toUpper}}{{name}}{{/toUpper}}")
encoder := msgpack.NewWriteEncoder(ctx)
Write{{#toUpper}}{{name}}{{/toUpper}}Result(encoder, value);
return encoder.Buffer()
}

func Write{{#toUpper}}{{name}}{{/toUpper}}Result(writer msgpack.Write, value {{#return}}{{#toWasm}}{{toGraphQLType}}{{/toWasm}}{{/return}}) {
func Write{{#toUpper}}{{name}}{{/toUpper}}Result(writer msgpack.Write, value {{#return}}{{#typeable}}{{#toWasm}}{{toGraphQLType}}{{/toWasm}}{{/typeable}}{{/return}}) {
{{#return}}
writer.Context().Push("{{name}}", "{{#toWasm}}{{toGraphQLType}}{{/toWasm}}", "writing property")
{{#scalar}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package types
package module

{{#methods.length}}
import (
Expand All @@ -9,8 +9,8 @@ import (

{{#methods}}
func {{#toUpper}}{{name}}{{/toUpper}}Wrapped(argsBuf []byte, envSize uint32) []byte {
var env *Env
{{#env}}
var env *Env
{{#required}}
if envSize == 0 {
panic("Environment is not set, and it is required by method 'objectMethod'")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package types

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

{{#methods}}
type Args{{#toUpper}}{{name}}{{/toUpper}} struct {
{{#stuctProps}}
{{#arguments}}
{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}} {{#toWasm}}{{toGraphQLType}}{{/toWasm}}
{{/arguments}}
{{/stuctProps}}
}
{{^last}}

{{/last}}
{{/methods}}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
v := value{{#lastFullIter}}i{{/lastFullIter}}
{{#required}}
{{#toUpper}}{{type}}{{/toUpper}}Write(writer, &v)
types.{{#toUpper}}{{type}}{{/toUpper}}Write(writer, &v)
{{/required}}
{{^required}}
{{#toUpper}}{{type}}{{/toUpper}}Write(writer, v)
types.{{#toUpper}}{{type}}{{/toUpper}}Write(writer, v)
{{/required}}
}
10 changes: 5 additions & 5 deletions packages/test-cases/cases/bind/sanity/output/wasm-go/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
"github.com/testorg/testrepo/wrap/types"
"github.com/testorg/testrepo/wrap/module"
"github.com/consideritdone/polywrap-go/polywrap"
)

Expand All @@ -10,13 +10,13 @@ func _wrap_invoke(methodSize, argsSize, envSize uint32) bool {
args := polywrap.WrapInvokeArgs(methodSize, argsSize)
switch args.Method {
case "moduleMethod":
return polywrap.WrapInvoke(args, envSize, types.ModuleMethodWrapped)
return polywrap.WrapInvoke(args, envSize, module.ModuleMethodWrapped)
case "objectMethod":
return polywrap.WrapInvoke(args, envSize, types.ObjectMethodWrapped)
return polywrap.WrapInvoke(args, envSize, module.ObjectMethodWrapped)
case "optionalEnvMethod":
return polywrap.WrapInvoke(args, envSize, types.OptionalEnvMethodWrapped)
return polywrap.WrapInvoke(args, envSize, module.OptionalEnvMethodWrapped)
case "if":
return polywrap.WrapInvoke(args, envSize, types.IfWrapped)
return polywrap.WrapInvoke(args, envSize, module.IfWrapped)
default:
return polywrap.WrapInvoke(args, envSize, nil)
}
Expand Down
Loading