diff --git a/packages/schema/bind/src/bindings/golang/functions.ts b/packages/schema/bind/src/bindings/golang/functions.ts index 2590d0b782..132a5962c7 100644 --- a/packages/schema/bind/src/bindings/golang/functions.ts +++ b/packages/schema/bind/src/bindings/golang/functions.ts @@ -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": @@ -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 = [ @@ -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); @@ -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); } } diff --git a/packages/schema/bind/src/bindings/golang/wasm/index.ts b/packages/schema/bind/src/bindings/golang/wasm/index.ts index 78f3307e0a..3102d038b3 100644 --- a/packages/schema/bind/src/bindings/golang/wasm/index.ts +++ b/packages/schema/bind/src/bindings/golang/wasm/index.ts @@ -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 ), diff --git a/packages/schema/bind/src/bindings/golang/wasm/templates/main-go.mustache b/packages/schema/bind/src/bindings/golang/wasm/templates/main-go.mustache index a9a4eeeecd..6a3450710c 100644 --- a/packages/schema/bind/src/bindings/golang/wasm/templates/main-go.mustache +++ b/packages/schema/bind/src/bindings/golang/wasm/templates/main-go.mustache @@ -1,7 +1,7 @@ package main import ( - "{{goImport}}/wrap/types" + "{{goImport}}/wrap/module" "github.com/consideritdone/polywrap-go/polywrap" ) @@ -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: diff --git a/packages/schema/bind/src/bindings/golang/wasm/templates/module-type/%type%Serialization-go.mustache b/packages/schema/bind/src/bindings/golang/wasm/templates/module-type/module/%type%Serialization-go.mustache similarity index 81% rename from packages/schema/bind/src/bindings/golang/wasm/templates/module-type/%type%Serialization-go.mustache rename to packages/schema/bind/src/bindings/golang/wasm/templates/module-type/module/%type%Serialization-go.mustache index 292aa1978d..2da6b2f512 100644 --- a/packages/schema/bind/src/bindings/golang/wasm/templates/module-type/%type%Serialization-go.mustache +++ b/packages/schema/bind/src/bindings/golang/wasm/templates/module-type/module/%type%Serialization-go.mustache @@ -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) @@ -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}}, @@ -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}} diff --git a/packages/schema/bind/src/bindings/golang/wasm/templates/module-type/%type%Wrapped-go.mustache b/packages/schema/bind/src/bindings/golang/wasm/templates/module-type/module/%type%Wrapped-go.mustache similarity index 98% rename from packages/schema/bind/src/bindings/golang/wasm/templates/module-type/%type%Wrapped-go.mustache rename to packages/schema/bind/src/bindings/golang/wasm/templates/module-type/module/%type%Wrapped-go.mustache index 1d85289bdc..737b7ea2ab 100644 --- a/packages/schema/bind/src/bindings/golang/wasm/templates/module-type/%type%Wrapped-go.mustache +++ b/packages/schema/bind/src/bindings/golang/wasm/templates/module-type/module/%type%Wrapped-go.mustache @@ -1,4 +1,4 @@ -package types +package module {{#methods.length}} import ( @@ -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'") diff --git a/packages/schema/bind/src/bindings/golang/wasm/templates/module-type/types/%type%Args-go.mustache b/packages/schema/bind/src/bindings/golang/wasm/templates/module-type/types/%type%Args-go.mustache new file mode 100644 index 0000000000..8e777aaa0e --- /dev/null +++ b/packages/schema/bind/src/bindings/golang/wasm/templates/module-type/types/%type%Args-go.mustache @@ -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}} \ No newline at end of file diff --git a/packages/schema/bind/src/bindings/golang/wasm/templates/value_serialize_object.mustache b/packages/schema/bind/src/bindings/golang/wasm/templates/value_serialize_object.mustache index 3b6ae4d7c6..b01650869d 100644 --- a/packages/schema/bind/src/bindings/golang/wasm/templates/value_serialize_object.mustache +++ b/packages/schema/bind/src/bindings/golang/wasm/templates/value_serialize_object.mustache @@ -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}} } diff --git a/packages/test-cases/cases/bind/sanity/output/wasm-go/main.go b/packages/test-cases/cases/bind/sanity/output/wasm-go/main.go index c58ac39081..605398790b 100644 --- a/packages/test-cases/cases/bind/sanity/output/wasm-go/main.go +++ b/packages/test-cases/cases/bind/sanity/output/wasm-go/main.go @@ -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" ) @@ -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) } diff --git a/packages/test-cases/cases/bind/sanity/output/wasm-go/types/module_serialization.go b/packages/test-cases/cases/bind/sanity/output/wasm-go/module/module_serialization.go similarity index 87% rename from packages/test-cases/cases/bind/sanity/output/wasm-go/types/module_serialization.go rename to packages/test-cases/cases/bind/sanity/output/wasm-go/module/module_serialization.go index c59b6436ef..b076f507c3 100644 --- a/packages/test-cases/cases/bind/sanity/output/wasm-go/types/module_serialization.go +++ b/packages/test-cases/cases/bind/sanity/output/wasm-go/module/module_serialization.go @@ -1,23 +1,11 @@ -package types +package module import ( "github.com/consideritdone/polywrap-go/polywrap/msgpack" + "github.com/testorg/testrepo/wrap/types" ) -type ArgsModuleMethod struct { - Str string - OptStr *string - En CustomEnum - OptEnum *CustomEnum - EnumArray []CustomEnum - OptEnumArray []*CustomEnum - Map map[string]int32 - MapOfArr map[string][]int32 - MapOfObj map[string]AnotherType - MapOfArrOfObj map[string][]AnotherType -} - -func DeserializeModuleMethodArgs(argsBuf []byte) *ArgsModuleMethod { +func DeserializeModuleMethodArgs(argsBuf []byte) *types.ArgsModuleMethod { ctx := msgpack.NewContext("Deserializing module-type: ModuleMethod") reader := msgpack.NewReadDecoder(ctx, argsBuf) @@ -203,7 +191,7 @@ func DeserializeModuleMethodArgs(argsBuf []byte) *ArgsModuleMethod { panic(reader.Context().PrintWithContext("Missing required property: 'mapOfArrOfObj: Map'")) } - return &ArgsModuleMethod{ + return &types.ArgsModuleMethod{ Str: _str, OptStr: _optStr, En: _en, @@ -233,14 +221,7 @@ func WriteModuleMethodResult(writer msgpack.Write, value int32) { writer.Context().Pop() } -type ArgsObjectMethod struct { - Object AnotherType - OptObject *AnotherType - ObjectArray []AnotherType - OptObjectArray []*AnotherType -} - -func DeserializeObjectMethodArgs(argsBuf []byte) *ArgsObjectMethod { +func DeserializeObjectMethodArgs(argsBuf []byte) *types.ArgsObjectMethod { ctx := msgpack.NewContext("Deserializing module-type: ObjectMethod") reader := msgpack.NewReadDecoder(ctx, argsBuf) @@ -310,7 +291,7 @@ func DeserializeObjectMethodArgs(argsBuf []byte) *ArgsObjectMethod { panic(reader.Context().PrintWithContext("Missing required property: 'objectArray: [AnotherType]'")) } - return &ArgsObjectMethod{ + return &types.ArgsObjectMethod{ Object: _object, OptObject: _optObject, ObjectArray: _objectArray, @@ -318,30 +299,23 @@ func DeserializeObjectMethodArgs(argsBuf []byte) *ArgsObjectMethod { } } -func SerializeObjectMethodResult(value *AnotherType) []byte { +func SerializeObjectMethodResult(value *types.AnotherType) []byte { ctx := msgpack.NewContext("Serializing module-type: ObjectMethod") encoder := msgpack.NewWriteEncoder(ctx) WriteObjectMethodResult(encoder, value); return encoder.Buffer() } -func WriteObjectMethodResult(writer msgpack.Write, value *AnotherType) { +func WriteObjectMethodResult(writer msgpack.Write, value *types.AnotherType) { writer.Context().Push("objectMethod", "*AnotherType", "writing property") { v := value - AnotherTypeWrite(writer, v) + types.AnotherTypeWrite(writer, v) } writer.Context().Pop() } -type ArgsOptionalEnvMethod struct { - Object AnotherType - OptObject *AnotherType - ObjectArray []AnotherType - OptObjectArray []*AnotherType -} - -func DeserializeOptionalEnvMethodArgs(argsBuf []byte) *ArgsOptionalEnvMethod { +func DeserializeOptionalEnvMethodArgs(argsBuf []byte) *types.ArgsOptionalEnvMethod { ctx := msgpack.NewContext("Deserializing module-type: OptionalEnvMethod") reader := msgpack.NewReadDecoder(ctx, argsBuf) @@ -411,7 +385,7 @@ func DeserializeOptionalEnvMethodArgs(argsBuf []byte) *ArgsOptionalEnvMethod { panic(reader.Context().PrintWithContext("Missing required property: 'objectArray: [AnotherType]'")) } - return &ArgsOptionalEnvMethod{ + return &types.ArgsOptionalEnvMethod{ Object: _object, OptObject: _optObject, ObjectArray: _objectArray, @@ -419,32 +393,28 @@ func DeserializeOptionalEnvMethodArgs(argsBuf []byte) *ArgsOptionalEnvMethod { } } -func SerializeOptionalEnvMethodResult(value *AnotherType) []byte { +func SerializeOptionalEnvMethodResult(value *types.AnotherType) []byte { ctx := msgpack.NewContext("Serializing module-type: OptionalEnvMethod") encoder := msgpack.NewWriteEncoder(ctx) WriteOptionalEnvMethodResult(encoder, value); return encoder.Buffer() } -func WriteOptionalEnvMethodResult(writer msgpack.Write, value *AnotherType) { +func WriteOptionalEnvMethodResult(writer msgpack.Write, value *types.AnotherType) { writer.Context().Push("optionalEnvMethod", "*AnotherType", "writing property") { v := value - AnotherTypeWrite(writer, v) + types.AnotherTypeWrite(writer, v) } writer.Context().Pop() } -type ArgsIf struct { - M_if else -} - -func DeserializeIfArgs(argsBuf []byte) *ArgsIf { +func DeserializeIfArgs(argsBuf []byte) *types.ArgsIf { ctx := msgpack.NewContext("Deserializing module-type: If") reader := msgpack.NewReadDecoder(ctx, argsBuf) var ( - _if else + _if Else _ifSet bool ) @@ -454,7 +424,7 @@ func DeserializeIfArgs(argsBuf []byte) *ArgsIf { reader.Context().Pop() switch field { case "M_if": - reader.Context().Push(field, "else", "type found, reading property") + reader.Context().Push(field, "Else", "type found, reading property") if v := ElseRead(reader); v != nil { _if = *v } @@ -467,23 +437,23 @@ func DeserializeIfArgs(argsBuf []byte) *ArgsIf { panic(reader.Context().PrintWithContext("Missing required property: 'if: else'")) } - return &ArgsIf{ + return &types.ArgsIf{ M_if: _if, } } -func SerializeIfResult(value else) []byte { +func SerializeIfResult(value types.Else) []byte { ctx := msgpack.NewContext("Serializing module-type: If") encoder := msgpack.NewWriteEncoder(ctx) WriteIfResult(encoder, value); return encoder.Buffer() } -func WriteIfResult(writer msgpack.Write, value else) { - writer.Context().Push("if", "else", "writing property") +func WriteIfResult(writer msgpack.Write, value types.Else) { + writer.Context().Push("if", "Else", "writing property") { v := value - ElseWrite(writer, &v) + types.ElseWrite(writer, &v) } writer.Context().Pop() } diff --git a/packages/test-cases/cases/bind/sanity/output/wasm-go/types/module_wrapped.go b/packages/test-cases/cases/bind/sanity/output/wasm-go/module/module_wrapped.go similarity index 96% rename from packages/test-cases/cases/bind/sanity/output/wasm-go/types/module_wrapped.go rename to packages/test-cases/cases/bind/sanity/output/wasm-go/module/module_wrapped.go index 4a0fd678b3..3e003cad4b 100644 --- a/packages/test-cases/cases/bind/sanity/output/wasm-go/types/module_wrapped.go +++ b/packages/test-cases/cases/bind/sanity/output/wasm-go/module/module_wrapped.go @@ -1,4 +1,4 @@ -package types +package module import ( "github.com/consideritdone/polywrap-go/polywrap" @@ -6,7 +6,6 @@ import ( ) func ModuleMethodWrapped(argsBuf []byte, envSize uint32) []byte { - var env *Env args := DeserializeModuleMethodArgs(argsBuf) @@ -44,7 +43,6 @@ func OptionalEnvMethodWrapped(argsBuf []byte, envSize uint32) []byte { } func IfWrapped(argsBuf []byte, envSize uint32) []byte { - var env *Env args := DeserializeIfArgs(argsBuf) diff --git a/packages/test-cases/cases/bind/sanity/output/wasm-go/types/module_args.go b/packages/test-cases/cases/bind/sanity/output/wasm-go/types/module_args.go new file mode 100644 index 0000000000..fba2f3ead7 --- /dev/null +++ b/packages/test-cases/cases/bind/sanity/output/wasm-go/types/module_args.go @@ -0,0 +1,36 @@ +package types + +import ( + "github.com/consideritdone/polywrap-go/polywrap/msgpack" +) + +type ArgsModuleMethod struct { + Str string + OptStr *string + En CustomEnum + OptEnum *CustomEnum + EnumArray []CustomEnum + OptEnumArray []*CustomEnum + Map map[string]int32 + MapOfArr map[string][]int32 + MapOfObj map[string]AnotherType + MapOfArrOfObj map[string][]AnotherType +} + +type ArgsObjectMethod struct { + Object AnotherType + OptObject *AnotherType + ObjectArray []AnotherType + OptObjectArray []*AnotherType +} + +type ArgsOptionalEnvMethod struct { + Object AnotherType + OptObject *AnotherType + ObjectArray []AnotherType + OptObjectArray []*AnotherType +} + +type ArgsIf struct { + M_if Else +}