From ad3ca8bc29ab3ef3ea4e9365c082a335daf26ce0 Mon Sep 17 00:00:00 2001 From: n0cte Date: Fri, 19 Aug 2022 02:39:50 +0300 Subject: [PATCH 1/5] add serialization and deserialization for impors, interfaces, modules --- .../src/bindings/golang/wasm-go/functions.ts | 8 + .../bind/src/bindings/golang/wasm-go/index.ts | 216 +++++++++--------- ...ype-go.mustache => Enum%type%-go.mustache} | 0 .../Env%type%-go.mustache} | 0 .../Env%type%Serialization-go.mustache} | 0 .../imported/enum-type/Enum%type%-go.mustache | 39 ++++ .../imported/env-type/Env%type%-go.mustache | 31 +++ .../Env%type%Serialization-go.mustache | 109 +++++++++ .../interface-type/%type%-go.mustache | 19 ++ .../%type%Serialization-go.mustache | 84 +++++++ .../module-type/%type%Wrapped-go.mustache | 39 ++++ .../object-type/Object%type%-go.mustache | 31 +++ .../Object%type%Serialization-go.mustache | 109 +++++++++ .../interface-type/%type%-go.mustache | 19 ++ .../%type%Serialization-go.mustache | 115 ++++++++++ .../module-type/%type%Wrapped-go.mustache | 35 +++ .../module-type/serialization-go.mustache | 69 ------ .../object-type/Object%type%-go.mustache | 31 +++ .../Object%type%Serialization-go.mustache | 109 +++++++++ .../value_deserialize_array.mustache | 23 ++ .../templates/value_deserialize_enum.mustache | 11 + .../templates/value_deserialize_map.mustache | 24 ++ .../value_deserialize_object.mustache | 8 + .../value_deserialize_scalar.mustache | 9 + .../templates/value_serialize_array.mustache | 23 ++ .../templates/value_serialize_enum.mustache | 13 ++ .../templates/value_serialize_map.mustache | 24 ++ .../templates/value_serialize_object.mustache | 9 + .../templates/value_serialize_scalar.mustache | 13 ++ .../bind/src/bindings/utils/templates.ts | 15 +- 30 files changed, 1060 insertions(+), 175 deletions(-) rename packages/schema/bind/src/bindings/golang/wasm-go/templates/enum-type/{type-go.mustache => Enum%type%-go.mustache} (100%) rename packages/schema/bind/src/bindings/golang/wasm-go/templates/{object-type/main-go.mustache => env-type/Env%type%-go.mustache} (100%) rename packages/schema/bind/src/bindings/golang/wasm-go/templates/{object-type/serialization-go.mustache => env-type/Env%type%Serialization-go.mustache} (100%) create mode 100644 packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/enum-type/Enum%type%-go.mustache create mode 100644 packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/env-type/Env%type%-go.mustache create mode 100644 packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/env-type/Env%type%Serialization-go.mustache create mode 100644 packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/interface-type/%type%-go.mustache create mode 100644 packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/module-type/%type%Serialization-go.mustache create mode 100644 packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/module-type/%type%Wrapped-go.mustache create mode 100644 packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/object-type/Object%type%-go.mustache create mode 100644 packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/object-type/Object%type%Serialization-go.mustache create mode 100644 packages/schema/bind/src/bindings/golang/wasm-go/templates/interface-type/%type%-go.mustache create mode 100644 packages/schema/bind/src/bindings/golang/wasm-go/templates/module-type/%type%Serialization-go.mustache create mode 100644 packages/schema/bind/src/bindings/golang/wasm-go/templates/module-type/%type%Wrapped-go.mustache delete mode 100644 packages/schema/bind/src/bindings/golang/wasm-go/templates/module-type/serialization-go.mustache create mode 100644 packages/schema/bind/src/bindings/golang/wasm-go/templates/object-type/Object%type%-go.mustache create mode 100644 packages/schema/bind/src/bindings/golang/wasm-go/templates/object-type/Object%type%Serialization-go.mustache create mode 100644 packages/schema/bind/src/bindings/golang/wasm-go/templates/value_deserialize_array.mustache create mode 100644 packages/schema/bind/src/bindings/golang/wasm-go/templates/value_deserialize_enum.mustache create mode 100644 packages/schema/bind/src/bindings/golang/wasm-go/templates/value_deserialize_map.mustache create mode 100644 packages/schema/bind/src/bindings/golang/wasm-go/templates/value_deserialize_object.mustache create mode 100644 packages/schema/bind/src/bindings/golang/wasm-go/templates/value_deserialize_scalar.mustache create mode 100644 packages/schema/bind/src/bindings/golang/wasm-go/templates/value_serialize_array.mustache create mode 100644 packages/schema/bind/src/bindings/golang/wasm-go/templates/value_serialize_enum.mustache create mode 100644 packages/schema/bind/src/bindings/golang/wasm-go/templates/value_serialize_map.mustache create mode 100644 packages/schema/bind/src/bindings/golang/wasm-go/templates/value_serialize_object.mustache create mode 100644 packages/schema/bind/src/bindings/golang/wasm-go/templates/value_serialize_scalar.mustache diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/functions.ts b/packages/schema/bind/src/bindings/golang/wasm-go/functions.ts index ee1ab96b97..9b6c0d1470 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/functions.ts +++ b/packages/schema/bind/src/bindings/golang/wasm-go/functions.ts @@ -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); diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/index.ts b/packages/schema/bind/src/bindings/golang/wasm-go/index.ts index e577b0da3a..bd4823ffda 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/index.ts +++ b/packages/schema/bind/src/bindings/golang/wasm-go/index.ts @@ -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 => { @@ -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, @@ -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; }; diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/enum-type/type-go.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/enum-type/Enum%type%-go.mustache similarity index 100% rename from packages/schema/bind/src/bindings/golang/wasm-go/templates/enum-type/type-go.mustache rename to packages/schema/bind/src/bindings/golang/wasm-go/templates/enum-type/Enum%type%-go.mustache diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/object-type/main-go.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/env-type/Env%type%-go.mustache similarity index 100% rename from packages/schema/bind/src/bindings/golang/wasm-go/templates/object-type/main-go.mustache rename to packages/schema/bind/src/bindings/golang/wasm-go/templates/env-type/Env%type%-go.mustache diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/object-type/serialization-go.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/env-type/Env%type%Serialization-go.mustache similarity index 100% rename from packages/schema/bind/src/bindings/golang/wasm-go/templates/object-type/serialization-go.mustache rename to packages/schema/bind/src/bindings/golang/wasm-go/templates/env-type/Env%type%Serialization-go.mustache diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/enum-type/Enum%type%-go.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/enum-type/Enum%type%-go.mustache new file mode 100644 index 0000000000..f4713678fc --- /dev/null +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/enum-type/Enum%type%-go.mustache @@ -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}}'") + } +} \ No newline at end of file diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/env-type/Env%type%-go.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/env-type/Env%type%-go.mustache new file mode 100644 index 0000000000..ec612efbee --- /dev/null +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/env-type/Env%type%-go.mustache @@ -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) +} + diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/env-type/Env%type%Serialization-go.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/env-type/Env%type%Serialization-go.mustache new file mode 100644 index 0000000000..c8a2d79e65 --- /dev/null +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/env-type/Env%type%Serialization-go.mustache @@ -0,0 +1,109 @@ +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" +) + +func serialize{{#toUpper}}{{type}}{{/toUpper}}(value *{{#toUpper}}{{type}}{{/toUpper}}) []byte { + ctx := msgpack.NewContext("Serializing (encoding) env-type: {{#toUpper}}{{type}}{{/toUpper}}") + encoder := msgpack.NewWriteEncoder(ctx) + write{{#toUpper}}{{type}}{{/toUpper}}(encoder, value) + return encoder.Buffer() +} + +func write{{#toUpper}}{{type}}{{/toUpper}}(writer msgpack.Write, value *{{#toUpper}}{{type}}{{/toUpper}}) { + writer.WriteMapLength({{properties.length}}) + {{#properties}} + writer.Context().Push("{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}", "{{#toWasm}}{{toGraphQLType}}{{/toWasm}}", "writing property") + writer.WriteString("{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}") + {{#scalar}} + {{> serialize_scalar}} + {{/scalar}} + {{#array}} + {{#startIter}}{{/startIter}} + {{> serialize_array}} + {{#stopIter}}{{/stopIter}} + {{/array}} + {{#map}} + {{#startIter}}{{/startIter}} + {{> serialize_map}} + {{#stopIter}}{{/stopIter}} + {{/map}} + {{#object}} + {{> serialize_object}} + {{/object}} + {{#enum}} + {{> serialize_enum}} + {{/enum}} + writer.Context().Pop() + {{/properties}} +} + +func deserialize{{#toUpper}}{{type}}{{/toUpper}}(data []byte) *{{#toUpper}}{{type}}{{/toUpper}} { + ctx := msgpack.NewContext("Deserializing (decoding) env-type: {{#toUpper}}{{type}}{{/toUpper}}") + reader := msgpack.NewReadDecoder(ctx, data) + return read{{#toUpper}}{{type}}{{/toUpper}}(reader) +} + +func read{{#toUpper}}{{type}}{{/toUpper}}(reader msgpack.Read) *{{#toUpper}}{{type}}{{/toUpper}} { + var ( + {{#properties}} + _{{name}} {{#toWasm}}{{toGraphQLType}}{{/toWasm}} + {{#required}} + _{{name}}Set bool + {{/required}} + {{/properties}} + ) + + for i := int32(reader.ReadMapLength()); i > 0; i-- { + field := reader.ReadString() + reader.Context().Push(field, "unknown", "searching for property type"); + switch field { + {{#properties}} + case "{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}": + reader.Context().Push(field, "{{#toWasm}}{{toGraphQLType}}{{/toWasm}}", "type found, reading property") + {{#scalar}} + {{> deserialize_scalar}} + {{/scalar}} + {{#enum}} + {{> deserialize_enum}} + {{/enum}} + {{#array}} + {{#startIter}}{{/startIter}} + {{> deserialize_array}} + {{#stopIter}}{{/stopIter}} + {{/array}} + {{#map}} + {{#startIter}}{{/startIter}} + {{> deserialize_map}} + {{#stopIter}}{{/stopIter}} + {{/map}} + {{#object}} + {{> deserialize_object}} + {{/object}} + {{#required}} + _{{name}}Set = true; + {{/required}} + reader.Context().Pop() + {{/properties}} + } + reader.Context().Pop() + } + + {{#properties}} + {{#required}} + if (!_{{name}}Set) { + panic(reader.Context().PrintWithContext("Missing required property: '{{name}}: {{type}}'")) + } + {{/required}} + {{/properties}} + + return &{{#toUpper}}{{type}}{{/toUpper}}{ + {{#properties}} + {{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}: _{{name}}, + {{/properties}} + } +} \ No newline at end of file diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/interface-type/%type%-go.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/interface-type/%type%-go.mustache new file mode 100644 index 0000000000..2db03af454 --- /dev/null +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/interface-type/%type%-go.mustache @@ -0,0 +1,19 @@ +package {{#toSnakeCase}}{{namespace}}{{/toSnakeCase}} + +{{#capabilities}} +{{#getImplementations}} +{{#enabled}} +import "github.com/consideritdone/polywrap-go/polywrap" +{{/enabled}} +{{/getImplementations}} +{{/capabilities}} + +{{#capabilities}} +{{#getImplementations}} +{{#enabled}} +func {{#toUpper}}{{namespace}}{{/toUpper}}Implementations() []string { + return polywrap.WrapGetImplementations("{{uri}}") +} +{{/enabled}} +{{/getImplementations}} +{{/capabilities}} \ No newline at end of file diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/module-type/%type%Serialization-go.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/module-type/%type%Serialization-go.mustache new file mode 100644 index 0000000000..45e7b0762d --- /dev/null +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/module-type/%type%Serialization-go.mustache @@ -0,0 +1,84 @@ +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" +) + +{{#methods}} +type Args{{#toUpper}}{{name}}{{/toUpper}} struct { + {{#arguments}} + {{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}} {{#toWasm}}{{toGraphQLType}}{{/toWasm}} + {{/arguments}} +} + +func Serialize{{#toUpper}}{{name}}{{/toUpper}}Args(value *Args{{#toUpper}}{{name}}{{/toUpper}}) []byte { + ctx := msgpack.NewContext("Serializing module-type: {{#toUpper}}{{name}}{{/toUpper}}") + encoder := msgpack.NewWriteEncoder(ctx) + Write{{#toUpper}}{{name}}{{/toUpper}}Args(encoder, value); + return encoder.Buffer() +} + +func Write{{#toUpper}}{{name}}{{/toUpper}}Args(writer msgpack.Write, value *Args{{#toUpper}}{{name}}{{/toUpper}}) { + writer.WriteMapLength({{arguments.length}}) + {{#arguments}} + writer.Context().Push("{{name}}", "{{#toWasm}}{{toGraphQLType}}{{/toWasm}}", "writing property") + writer.WriteString("{{name}}") + {{#scalar}} + {{> value_serialize_scalar}} + {{/scalar}} + {{#array}} + {{#startIter}}{{/startIter}} + {{> value_serialize_array}} + {{#stopIter}}{{/stopIter}} + {{/array}} + {{#map}} + {{#startIter}}{{/startIter}} + {{> value_serialize_map}} + {{#stopIter}}{{/stopIter}} + {{/map}} + {{#object}} + {{> value_serialize_object}} + {{/object}} + {{#enum}} + {{> value_serialize_enum}} + {{/enum}} + writer.Context().Pop() + {{/arguments}} +} + +export function Deserialize{{#toUpper}}{{name}}{{/toUpper}}Result(argsBuf []byte): {{#return}}{{#toWasm}}{{toGraphQLType}}{{/toWasm}}{{/return}} { + ctx := msgpack.NewContext("Deserializing module-type: {{#toUpper}}{{name}}{{/toUpper}}") + reader := msgpack.NewReadDecoder(ctx, argsBuf) + + {{#return}} + reader.Context().Push("{{name}}", "{{#toWasm}}{{toGraphQLType}}{{/toWasm}}", "reading function output"); + var value {{#toWasm}}{{toGraphQLType}}{{/toWasm}} + {{#scalar}} + {{> value_deserialize_scalar}} + {{/scalar}} + {{#array}} + {{#startIter}}{{/startIter}} + {{> value_deserialize_array}} + {{#stopIter}}{{/stopIter}} + {{/array}} + {{#map}} + {{#startIter}}{{/startIter}} + {{> value_deserialize_map}} + {{#stopIter}}{{/stopIter}} + {{/map}} + {{#object}} + {{> value_deserialize_object}} + {{/object}} + {{#enum}} + {{> value_deserialize_enum}} + {{/enum}} + writer.Context().Pop() + return value + {{/return}} +} + +{{/methods}} + diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/module-type/%type%Wrapped-go.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/module-type/%type%Wrapped-go.mustache new file mode 100644 index 0000000000..b7d1e09512 --- /dev/null +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/module-type/%type%Wrapped-go.mustache @@ -0,0 +1,39 @@ +package {{#toSnakeCase}}{{namespace}}{{/toSnakeCase}} + +{{#methods.length}} +import ( + "github.com/consideritdone/polywrap-go/polywrap" + "some/library/to/methods" +) +{{/methods.length}} + +{{^isInterface}} +{{#methods}} +func {{#toUpper}}{{type}}{{/toUpper}}{{#toUpper}}{{name}}{{/toUpper}}(args *Args{{#toUpper}}{{name}}{{/toUpper}}) ({{#return}}{{#toWasm}}{{toGraphQLType}}{{/toWasm}}{{/return}}, error) { + argsBuf := Serialize{{#toUpper}}{{name}}{{/toUpper}}Args(args) + data, err := polywrap.WrapSubinvoke("{{uri}}", "{{name}}", argsBuf) + if err != nil { + return nil, result.Error() + } + return Deserialize{{#toUpper}}{{name}}{{/toUpper}}Result(result.unwrap()), nil +} +{{^last}} + +{{/last}} +{{/methods}} +{{/isInterface}} +{{#isInterface}} +{{#methods}} +func {{#toUpper}}{{type}}{{/toUpper}}{{#toUpper}}{{name}}{{/toUpper}}(uri string, args *Args{{#toUpper}}{{name}}{{/toUpper}}) ({{#return}}{{#toWasm}}{{toGraphQLType}}{{/toWasm}}{{/return}}, error) { + argsBuf := Serialize{{#toUpper}}{{name}}{{/toUpper}}Args(args) + data, err := polywrap.WrapSubinvokeImplementation("{{uri}}", uri, "{{name}}", argsBuf) + if err != nil { + return nil, err + } + return Deserialize{{#toUpper}}{{name}}{{/toUpper}}Result(data), nil +} +{{^last}} + +{{/last}} +{{/methods}} +{{/isInterface}} \ No newline at end of file diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/object-type/Object%type%-go.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/object-type/Object%type%-go.mustache new file mode 100644 index 0000000000..ec612efbee --- /dev/null +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/object-type/Object%type%-go.mustache @@ -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) +} + diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/object-type/Object%type%Serialization-go.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/object-type/Object%type%Serialization-go.mustache new file mode 100644 index 0000000000..c8a2d79e65 --- /dev/null +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/object-type/Object%type%Serialization-go.mustache @@ -0,0 +1,109 @@ +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" +) + +func serialize{{#toUpper}}{{type}}{{/toUpper}}(value *{{#toUpper}}{{type}}{{/toUpper}}) []byte { + ctx := msgpack.NewContext("Serializing (encoding) env-type: {{#toUpper}}{{type}}{{/toUpper}}") + encoder := msgpack.NewWriteEncoder(ctx) + write{{#toUpper}}{{type}}{{/toUpper}}(encoder, value) + return encoder.Buffer() +} + +func write{{#toUpper}}{{type}}{{/toUpper}}(writer msgpack.Write, value *{{#toUpper}}{{type}}{{/toUpper}}) { + writer.WriteMapLength({{properties.length}}) + {{#properties}} + writer.Context().Push("{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}", "{{#toWasm}}{{toGraphQLType}}{{/toWasm}}", "writing property") + writer.WriteString("{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}") + {{#scalar}} + {{> serialize_scalar}} + {{/scalar}} + {{#array}} + {{#startIter}}{{/startIter}} + {{> serialize_array}} + {{#stopIter}}{{/stopIter}} + {{/array}} + {{#map}} + {{#startIter}}{{/startIter}} + {{> serialize_map}} + {{#stopIter}}{{/stopIter}} + {{/map}} + {{#object}} + {{> serialize_object}} + {{/object}} + {{#enum}} + {{> serialize_enum}} + {{/enum}} + writer.Context().Pop() + {{/properties}} +} + +func deserialize{{#toUpper}}{{type}}{{/toUpper}}(data []byte) *{{#toUpper}}{{type}}{{/toUpper}} { + ctx := msgpack.NewContext("Deserializing (decoding) env-type: {{#toUpper}}{{type}}{{/toUpper}}") + reader := msgpack.NewReadDecoder(ctx, data) + return read{{#toUpper}}{{type}}{{/toUpper}}(reader) +} + +func read{{#toUpper}}{{type}}{{/toUpper}}(reader msgpack.Read) *{{#toUpper}}{{type}}{{/toUpper}} { + var ( + {{#properties}} + _{{name}} {{#toWasm}}{{toGraphQLType}}{{/toWasm}} + {{#required}} + _{{name}}Set bool + {{/required}} + {{/properties}} + ) + + for i := int32(reader.ReadMapLength()); i > 0; i-- { + field := reader.ReadString() + reader.Context().Push(field, "unknown", "searching for property type"); + switch field { + {{#properties}} + case "{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}": + reader.Context().Push(field, "{{#toWasm}}{{toGraphQLType}}{{/toWasm}}", "type found, reading property") + {{#scalar}} + {{> deserialize_scalar}} + {{/scalar}} + {{#enum}} + {{> deserialize_enum}} + {{/enum}} + {{#array}} + {{#startIter}}{{/startIter}} + {{> deserialize_array}} + {{#stopIter}}{{/stopIter}} + {{/array}} + {{#map}} + {{#startIter}}{{/startIter}} + {{> deserialize_map}} + {{#stopIter}}{{/stopIter}} + {{/map}} + {{#object}} + {{> deserialize_object}} + {{/object}} + {{#required}} + _{{name}}Set = true; + {{/required}} + reader.Context().Pop() + {{/properties}} + } + reader.Context().Pop() + } + + {{#properties}} + {{#required}} + if (!_{{name}}Set) { + panic(reader.Context().PrintWithContext("Missing required property: '{{name}}: {{type}}'")) + } + {{/required}} + {{/properties}} + + return &{{#toUpper}}{{type}}{{/toUpper}}{ + {{#properties}} + {{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}: _{{name}}, + {{/properties}} + } +} \ No newline at end of file diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/interface-type/%type%-go.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/interface-type/%type%-go.mustache new file mode 100644 index 0000000000..46cc9be8e0 --- /dev/null +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/interface-type/%type%-go.mustache @@ -0,0 +1,19 @@ +package interfaces + +{{#capabilities}} +{{#getImplementations}} +{{#enabled}} +import "github.com/consideritdone/polywrap-go/polywrap" +{{/enabled}} +{{/getImplementations}} +{{/capabilities}} + +{{#capabilities}} +{{#getImplementations}} +{{#enabled}} +func {{#toUpper}}{{namespace}}{{/toUpper}}Implementations() []string { + return polywrap.WrapGetImplementations("{{uri}}") +} +{{/enabled}} +{{/getImplementations}} +{{/capabilities}} \ No newline at end of file diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/module-type/%type%Serialization-go.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/module-type/%type%Serialization-go.mustache new file mode 100644 index 0000000000..9dff688252 --- /dev/null +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/module-type/%type%Serialization-go.mustache @@ -0,0 +1,115 @@ +package types + +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" +) + +{{#methods}} +type Args{{#toUpper}}{{name}}{{/toUpper}} struct { + {{#arguments}} + {{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}} {{#toWasm}}{{toGraphQLType}}{{/toWasm}} + {{/arguments}} +} + +func Deserialize{{#toUpper}}{{name}}{{/toUpper}}Args(argsBuf []byte) *Args{{#toUpper}}{{name}}{{/toUpper}} { + ctx := msgpack.NewContext("Deserializing module-type: {{#toUpper}}{{name}}{{/toUpper}}") + {{#arguments.length}} + reader := msgpack.NewReadDecoder(ctx, argsBuf) + + var ( + {{#arguments}} + _{{name}} {{#toWasm}}{{toGraphQLType}}{{/toWasm}} + {{#required}} + _{{name}}Set bool + {{/required}} + {{/arguments}} + ) + + for i := int32(reader.ReadMapLength()); i > 0; i-- { + field := reader.ReadString() + reader.Context().Push(field, "unknown", "searching for property type"); + reader.Context().Pop() + switch field { + {{#arguments}} + case "{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}": + reader.Context().Push(field, "{{#toWasm}}{{toGraphQLType}}{{/toWasm}}", "type found, reading property") + {{#scalar}} + {{> deserialize_scalar}} + {{/scalar}} + {{#enum}} + {{> deserialize_enum}} + {{/enum}} + {{#array}} + {{#startIter}}{{/startIter}} + {{> deserialize_array}} + {{#stopIter}}{{/stopIter}} + {{/array}} + {{#map}} + {{#startIter}}{{/startIter}} + {{> deserialize_map}} + {{#stopIter}}{{/stopIter}} + {{/map}} + {{#object}} + {{> deserialize_object}} + {{/object}} + {{#required}} + _{{name}}Set = true; + {{/required}} + reader.Context().Pop() + {{/arguments}} + } + } + + {{#arguments}} + {{#required}} + if (!_{{name}}Set) { + panic(reader.Context().PrintWithContext("Missing required property: '{{name}}: {{type}}'")) + } + {{/required}} + {{/arguments}} + {{/arguments.length}} + + return &Args{{#toUpper}}{{name}}{{/toUpper}}{ + {{#arguments}} + {{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}: _{{name}}, + {{/arguments}} + } +} + +func Serialize{{#toUpper}}{{name}}{{/toUpper}}Result(value {{#return}}{{#toWasm}}{{toGraphQLType}}{{/toWasm}}{{/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}}) { + {{#return}} + writer.Context().Push("{{name}}", "{{#toWasm}}{{toGraphQLType}}{{/toWasm}}", "writing property"); + {{#scalar}} + {{> value_serialize_scalar}} + {{/scalar}} + {{#array}} + {{#startIter}}{{/startIter}} + {{> value_serialize_array}} + {{#stopIter}}{{/stopIter}} + {{/array}} + {{#map}} + {{#startIter}}{{/startIter}} + {{> value_serialize_map}} + {{#stopIter}}{{/stopIter}} + {{/map}} + {{#object}} + {{> value_serialize_object}} + {{/object}} + {{#enum}} + {{> value_serialize_enum}} + {{/enum}} + writer.Context().Pop() + {{/return}} +} + +{{/methods}} diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/module-type/%type%Wrapped-go.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/module-type/%type%Wrapped-go.mustache new file mode 100644 index 0000000000..5e24b3b48a --- /dev/null +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/module-type/%type%Wrapped-go.mustache @@ -0,0 +1,35 @@ +package types + +{{#methods.length}} +import ( + "github.com/consideritdone/polywrap-go/polywrap" + "some/library/to/methods" +) +{{/methods.length}} + +{{#methods}} +func {{#toUpper}}{{name}}{{/toUpper}}Wrapped(argsBuf []byte, envSize uint32) []byte { + var env *Env + {{#env}} + {{#required}} + if (envSize == 0) { + panic("Environment is not set, and it is required by method 'objectMethod'") + } + {{/required}} + if (envSize > 0) { + envBuf := polywrap.WrapLoadEnv(envSize); + env = EnvFromBuffer(envBuf); + } + {{/env}} + + {{#arguments.length}} + args: = Deserialize{{#toUpper}}{{name}}{{/toUpper}}Args(argsBuf) + {{/arguments.length}} + + result := methods.{{#toUpper}}{{name}}{{/toUpper}}({{#arguments.length}}args{{/arguments.length}}{{^arguments.length}}nil{{/arguments.length}}{{#env}}, env{{/env}}) + return Serialize{{#toUpper}}{{name}}{{/toUpper}}Result(result); +} +{{^last}} + +{{/last}} +{{/methods}} diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/module-type/serialization-go.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/module-type/serialization-go.mustache deleted file mode 100644 index f3149225b5..0000000000 --- a/packages/schema/bind/src/bindings/golang/wasm-go/templates/module-type/serialization-go.mustache +++ /dev/null @@ -1,69 +0,0 @@ -package module - -import ( - "github.com/consideritdone/polywrap-go/examples/demo1/wrap/moduleTypes" - "github.com/consideritdone/polywrap-go/examples/demo1/wrap/sampleResult" - "github.com/consideritdone/polywrap-go/polywrap/msgpack" -) -{{#methods}} - -export class Args{{#toUpper}}{{name}}{{/toUpper}} { - {{#arguments}} - {{#handleKeywords}}{{name}}{{/handleKeywords}} {{#toWasm}}{{toGraphQLType}}{{/toWasm}}; - {{/arguments}} -} - -export function deserialize{{#toUpper}}{{name}}{{/toUpper}}Args(argsBuf []byte): *moduleTypes.Args{{name}} { - context := msgpack.NewContext("Deserializing module-type: {{name}}") - {{#arguments.length}} - reader := msgpack.NewReadDecoder(context, argsBuf) - numFields := reader.ReadMapLength() - - {{#arguments}} - {{^object}} - let _{{name}} {{#toWasm}}{{toGraphQLType}}{{/toWasm}} = {{#toWasmInit}}{{toGraphQLType}}{{/toWasmInit}}; - {{/object}} - {{#required}} - let _{{name}}Set: bool = false; - {{/required}} - {{/arguments}} - - for i := numFields; i > 0; i-- { - const field = reader.readString(); - - reader.Context().Push(field, "unknown", "searching for property type") - {{#arguments}} - {{^first}}else {{/first}}if (field == "{{name}}") { - reader.Context().Push(field, "{{#toWasm}}{{toGraphQLType}}{{/toWasm}}", "type found, reading property") - - {{#scalar}} - _{{name}} = reader.Read{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}(); - {{/scalar}} - {{#required}} - _{{name}}Set = true; - {{/required}} - reader.Context().Pop() - } - {{/arguments}} - reader.context().pop(); - } - - {{#arguments}} - {{#required}} - {{^object}} - if !_{{name}}Set { - {{/object}} - panic(reader.Context().PrintWithContext("Missing required argument: '{{name}}: {{type}}'")) - } - {{/required}} - {{/arguments}} - {{/arguments.length}} - - return { - {{#arguments}} - {{#handleKeywords}}{{name}}{{/handleKeywords}}: _{{name}}{{^last}},{{/last}} - {{/arguments}} - }; -} - -{{/methods}} diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/object-type/Object%type%-go.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/object-type/Object%type%-go.mustache new file mode 100644 index 0000000000..08d588529e --- /dev/null +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/object-type/Object%type%-go.mustache @@ -0,0 +1,31 @@ +package types + +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) +} + diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/object-type/Object%type%Serialization-go.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/object-type/Object%type%Serialization-go.mustache new file mode 100644 index 0000000000..38c9f04e63 --- /dev/null +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/object-type/Object%type%Serialization-go.mustache @@ -0,0 +1,109 @@ +package types + +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" +) + +func serialize{{#toUpper}}{{type}}{{/toUpper}}(value *{{#toUpper}}{{type}}{{/toUpper}}) []byte { + ctx := msgpack.NewContext("Serializing (encoding) env-type: {{#toUpper}}{{type}}{{/toUpper}}") + encoder := msgpack.NewWriteEncoder(ctx) + write{{#toUpper}}{{type}}{{/toUpper}}(encoder, value) + return encoder.Buffer() +} + +func write{{#toUpper}}{{type}}{{/toUpper}}(writer msgpack.Write, value *{{#toUpper}}{{type}}{{/toUpper}}) { + writer.WriteMapLength({{properties.length}}) + {{#properties}} + writer.Context().Push("{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}", "{{#toWasm}}{{toGraphQLType}}{{/toWasm}}", "writing property") + writer.WriteString("{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}") + {{#scalar}} + {{> serialize_scalar}} + {{/scalar}} + {{#array}} + {{#startIter}}{{/startIter}} + {{> serialize_array}} + {{#stopIter}}{{/stopIter}} + {{/array}} + {{#map}} + {{#startIter}}{{/startIter}} + {{> serialize_map}} + {{#stopIter}}{{/stopIter}} + {{/map}} + {{#object}} + {{> serialize_object}} + {{/object}} + {{#enum}} + {{> serialize_enum}} + {{/enum}} + writer.Context().Pop() + {{/properties}} +} + +func deserialize{{#toUpper}}{{type}}{{/toUpper}}(data []byte) *{{#toUpper}}{{type}}{{/toUpper}} { + ctx := msgpack.NewContext("Deserializing (decoding) env-type: {{#toUpper}}{{type}}{{/toUpper}}") + reader := msgpack.NewReadDecoder(ctx, data) + return read{{#toUpper}}{{type}}{{/toUpper}}(reader) +} + +func read{{#toUpper}}{{type}}{{/toUpper}}(reader msgpack.Read) *{{#toUpper}}{{type}}{{/toUpper}} { + var ( + {{#properties}} + _{{name}} {{#toWasm}}{{toGraphQLType}}{{/toWasm}} + {{#required}} + _{{name}}Set bool + {{/required}} + {{/properties}} + ) + + for i := int32(reader.ReadMapLength()); i > 0; i-- { + field := reader.ReadString() + reader.Context().Push(field, "unknown", "searching for property type"); + switch field { + {{#properties}} + case "{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}": + reader.Context().Push(field, "{{#toWasm}}{{toGraphQLType}}{{/toWasm}}", "type found, reading property") + {{#scalar}} + {{> deserialize_scalar}} + {{/scalar}} + {{#enum}} + {{> deserialize_enum}} + {{/enum}} + {{#array}} + {{#startIter}}{{/startIter}} + {{> deserialize_array}} + {{#stopIter}}{{/stopIter}} + {{/array}} + {{#map}} + {{#startIter}}{{/startIter}} + {{> deserialize_map}} + {{#stopIter}}{{/stopIter}} + {{/map}} + {{#object}} + {{> deserialize_object}} + {{/object}} + {{#required}} + _{{name}}Set = true; + {{/required}} + reader.Context().Pop() + {{/properties}} + } + reader.Context().Pop() + } + + {{#properties}} + {{#required}} + if (!_{{name}}Set) { + panic(reader.Context().PrintWithContext("Missing required property: '{{name}}: {{type}}'")) + } + {{/required}} + {{/properties}} + + return &{{#toUpper}}{{type}}{{/toUpper}}{ + {{#properties}} + {{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}: _{{name}}, + {{/properties}} + } +} \ No newline at end of file diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_deserialize_array.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_deserialize_array.mustache new file mode 100644 index 0000000000..8a852d06e1 --- /dev/null +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_deserialize_array.mustache @@ -0,0 +1,23 @@ +if reader.IsNil() { + value{{#lastFullIter}}i{{/lastFullIter}} = nil +} else { + {{#nextIter}}ln{{/nextIter}} := reader.ReadArrayLength() + value{{#prevFullIter}}i{{/prevFullIter}} = make({{#toWasm}}{{toGraphQLType}}{{/toWasm}}, {{#currIter}}ln{{/currIter}}) + for {{#currIter}}i{{/currIter}} := uint32(0); {{#currIter}}i{{/currIter}} < {{#currIter}}ln{{/currIter}}; {{#currIter}}i{{/currIter}}++ { + {{#scalar}} + {{> deserialize_scalar}} + {{/scalar}} + {{#enum}} + {{> deserialize_enum}} + {{/enum}} + {{#array}} + {{> deserialize_array}} + {{/array}} + {{#map}} + {{> deserialize_map}} + {{/map}} + {{#object}} + {{> deserialize_object}} + {{/object}} + } +} diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_deserialize_enum.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_deserialize_enum.mustache new file mode 100644 index 0000000000..72dcb0b7ed --- /dev/null +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_deserialize_enum.mustache @@ -0,0 +1,11 @@ +{{#required}} +value{{#lastFullIter}}i{{/lastFullIter}} = {{#toUpper}}{{type}}{{/toUpper}}(reader.ReadI32()) +Sanitize{{#toUpper}}{{type}}{{/toUpper}}Value(int32(_{{name}}{{#lastFullIter}}i{{/lastFullIter}})) +{{/required}} +{{^required}} +if !reader.IsNil() { + v := {{#toUpper}}{{type}}{{/toUpper}}(reader.ReadI32()) + Sanitize{{#toUpper}}{{type}}{{/toUpper}}Value(int32(v)) + value{{#lastFullIter}}i{{/lastFullIter}} = &v +} +{{/required}} diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_deserialize_map.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_deserialize_map.mustache new file mode 100644 index 0000000000..5861374a16 --- /dev/null +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_deserialize_map.mustache @@ -0,0 +1,24 @@ +if reader.IsNil() { + value{{#lastFullIter}}i{{/lastFullIter}} = nil +} else { + {{#nextIter}}ln{{/nextIter}} := reader.ReadMapLength() + value{{#prevFullIter}}i{{/prevFullIter}} = make({{#toWasm}}{{toGraphQLType}}{{/toWasm}}) + for {{#currIter}}j{{/currIter}} := uint32(0); {{#currIter}}j{{/currIter}} < {{#currIter}}ln{{/currIter}}; {{#currIter}}j{{/currIter}}++ { + {{#currIter}}i{{/currIter}} := reader.Read{{#key}}{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}{{/key}}() + {{#scalar}} + {{> deserialize_scalar}} + {{/scalar}} + {{#enum}} + {{> deserialize_enum}} + {{/enum}} + {{#array}} + {{> deserialize_array}} + {{/array}} + {{#map}} + {{> deserialize_map}} + {{/map}} + {{#object}} + {{> deserialize_object}} + {{/object}} + } +} diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_deserialize_object.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_deserialize_object.mustache new file mode 100644 index 0000000000..645435c97c --- /dev/null +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_deserialize_object.mustache @@ -0,0 +1,8 @@ +if v := {{#toUpper}}{{type}}{{/toUpper}}Read(reader); v != nil { + {{#required}} + value{{#lastFullIter}}i{{/lastFullIter}} = *v + {{/required}} + {{^required}} + value{{#lastFullIter}}i{{/lastFullIter}} = v + {{/required}} +} diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_deserialize_scalar.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_deserialize_scalar.mustache new file mode 100644 index 0000000000..c917632081 --- /dev/null +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_deserialize_scalar.mustache @@ -0,0 +1,9 @@ +{{#required}} +value{{#lastFullIter}}i{{/lastFullIter}} = reader.Read{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}() +{{/required}} +{{^required}} +if !reader.IsNil() { + v := reader.Read{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}(); + value{{#lastFullIter}}i{{/lastFullIter}} = {{#readPointer}}{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}} - v{{/readPointer}} +} +{{/required}} diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_serialize_array.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_serialize_array.mustache new file mode 100644 index 0000000000..9c759d825b --- /dev/null +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_serialize_array.mustache @@ -0,0 +1,23 @@ +if value{{#lastFullIter}}i{{/lastFullIter}} == nil { + writer.WriteNil() +} else if len(value{{#lastFullIter}}i{{/lastFullIter}}) == 0 { + writer.WriteNil() +} else { + for {{#nextIter}}i{{/nextIter}} := range value{{#prevFullIter}}i{{/prevFullIter}} { + {{#scalar}} + {{> value_serialize_scalar}} + {{/scalar}} + {{#enum}} + {{> value_serialize_enum}} + {{/enum}} + {{#array}} + {{> value_serialize_array}} + {{/array}} + {{#map}} + {{> value_serialize_map}} + {{/map}} + {{#object}} + {{> value_serialize_object}} + {{/object}} + } +} diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_serialize_enum.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_serialize_enum.mustache new file mode 100644 index 0000000000..c6567e8c24 --- /dev/null +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_serialize_enum.mustache @@ -0,0 +1,13 @@ +{ + v := value{{#lastFullIter}}i{{/lastFullIter}} + {{#required}} + writer.WriteI32(int32(v)); + {{/required}} + {{^required}} + if v == nil { + writer.WriteNil() + } else { + writer.WriteI32(int32(*v)); + } + {{/required}} +} diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_serialize_map.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_serialize_map.mustache new file mode 100644 index 0000000000..d60bb55b17 --- /dev/null +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_serialize_map.mustache @@ -0,0 +1,24 @@ +if value == nil { + writer.WriteNil() +} else if len(value) == 0 { + writer.WriteNil() +} else { + for {{#nextIter}}i{{/nextIter}} := range value{{#getPrevIter}}i{{/getPrevIter}} { + writer.Write{{#key}}{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}{{/key}}({{#currIter}}i{{/currIter}}) + {{#scalar}} + {{> value_serialize_scalar}} + {{/scalar}} + {{#enum}} + {{> value_serialize_enum}} + {{/enum}} + {{#array}} + {{> value_serialize_array}} + {{/array}} + {{#map}} + {{> value_serialize_map}} + {{/map}} + {{#object}} + {{> value_serialize_object}} + {{/object}} + } +} diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_serialize_object.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_serialize_object.mustache new file mode 100644 index 0000000000..aa5894e0ba --- /dev/null +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_serialize_object.mustache @@ -0,0 +1,9 @@ +{ + v := value{{#lastFullIter}}i{{/lastFullIter}} + {{#required}} + {{#toUpper}}{{type}}{{/toUpper}}Write(writer, &v) + {{/required}} + {{^required}} + {{#toUpper}}{{type}}{{/toUpper}}Write(writer, v) + {{/required}} +} diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_serialize_scalar.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_serialize_scalar.mustache new file mode 100644 index 0000000000..6aa89267b1 --- /dev/null +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_serialize_scalar.mustache @@ -0,0 +1,13 @@ +{ + v := value{{#lastFullIter}}i{{/lastFullIter}} + {{#required}} + writer.Write{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}(v) + {{/required}} + {{^required}} + if v == nil { + writer.WriteNil() + } else { + {{#writePointer}}{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}} - v{{/writePointer}} + } + {{/required}} +} diff --git a/packages/schema/bind/src/bindings/utils/templates.ts b/packages/schema/bind/src/bindings/utils/templates.ts index f3be1c7846..07e0d96949 100644 --- a/packages/schema/bind/src/bindings/utils/templates.ts +++ b/packages/schema/bind/src/bindings/utils/templates.ts @@ -1,7 +1,20 @@ import { OutputEntry, readDirectorySync } from "@polywrap/os-js"; +import { GenericDefinition } from "@polywrap/schema-parse"; import Mustache from "mustache"; import path from "path"; +function transformName(str: string, view: unknown): string { + str = str.replace("-", "."); + if (!str.endsWith(".go")) { + return str; + } + const def = view as GenericDefinition; + str = str.replace("%type%", def.type) + .replace( /([A-Z])/g, "_$1") + .toLowerCase(); + return str.startsWith("_") ? str.slice(1) : str; +} + export function renderTemplates( templateDirAbs: string, view: unknown, @@ -27,7 +40,7 @@ export function renderTemplates( if (data) { output.push({ type: "File", - name: name.replace("-", ".").replace("%type%", (view as {type: string}).type.toLowerCase()), + name: transformName(name, view), data, }); } From 44a5e937d4a065af85213aefa2e572cbc73741e7 Mon Sep 17 00:00:00 2001 From: n0cte Date: Tue, 23 Aug 2022 00:24:34 +0300 Subject: [PATCH 2/5] prettify generated golang code --- .../src/bindings/golang/wasm-go/functions.ts | 45 +- .../templates/deserialize_array.mustache | 40 +- .../templates/deserialize_enum.mustache | 6 +- .../templates/deserialize_map.mustache | 42 +- .../templates/deserialize_object.mustache | 12 +- .../templates/deserialize_scalar.mustache | 4 +- .../enum-type/Enum%type%-go.mustache | 28 +- .../templates/env-type/Env%type%-go.mustache | 16 +- .../Env%type%Serialization-go.mustache | 159 ++- .../imported/enum-type/Enum%type%-go.mustache | 28 +- .../imported/env-type/Env%type%-go.mustache | 16 +- .../Env%type%Serialization-go.mustache | 155 ++- .../interface-type/%type%-go.mustache | 4 +- .../%type%Serialization-go.mustache | 134 +- .../module-type/%type%Wrapped-go.mustache | 28 +- .../object-type/Object%type%-go.mustache | 16 +- .../Object%type%Serialization-go.mustache | 159 ++- .../interface-type/%type%-go.mustache | 4 +- .../golang/wasm-go/templates/main-go.mustache | 24 +- .../%type%Serialization-go.mustache | 191 +-- .../module-type/%type%Wrapped-go.mustache | 40 +- .../object-type/Object%type%-go.mustache | 16 +- .../Object%type%Serialization-go.mustache | 160 ++- .../templates/serialize_array.mustache | 38 +- .../wasm-go/templates/serialize_enum.mustache | 22 +- .../wasm-go/templates/serialize_map.mustache | 40 +- .../templates/serialize_object.mustache | 15 +- .../templates/serialize_scalar.mustache | 22 +- .../value_deserialize_array.mustache | 40 +- .../templates/value_deserialize_enum.mustache | 6 +- .../templates/value_deserialize_map.mustache | 42 +- .../value_deserialize_object.mustache | 12 +- .../value_deserialize_scalar.mustache | 4 +- .../templates/value_serialize_array.mustache | 38 +- .../templates/value_serialize_enum.mustache | 22 +- .../templates/value_serialize_map.mustache | 40 +- .../templates/value_serialize_object.mustache | 14 +- .../templates/value_serialize_scalar.mustache | 22 +- .../test_import/enum_test_import__enum.go | 38 + .../test_import/env_test_import__env.go | 25 + .../env_test_import__env_serialization.go | 57 + .../object_test_import__another_object.go | 25 + ...st_import__another_object_serialization.go | 57 + .../test_import/object_test_import__object.go | 32 + ...bject_test_import__object_serialization.go | 253 ++++ .../test_import__module_serialization.go | 245 ++++ .../test_import__module_wrapped.go | 24 + .../output/wasm-go/interfaces/test_import.go | 7 + .../cases/bind/sanity/output/wasm-go/main.go | 16 +- .../types/custom_type_serialization.go | 1218 ----------------- .../{custom_enum.go => enum_custom_enum.go} | 0 .../wasm-go/types/module_serialization.go | 436 ++++++ .../output/wasm-go/types/module_wrapped.go | 45 + ...another_type.go => object_another_type.go} | 0 ...o => object_another_type_serialization.go} | 1 - .../{custom_type.go => object_custom_type.go} | 0 .../types/object_custom_type_serialization.go | 1211 ++++++++++++++++ .../wasm-go/types/{env.go => object_env.go} | 0 ...ization.go => object_env_serialization.go} | 1 - 59 files changed, 3328 insertions(+), 2067 deletions(-) create mode 100644 packages/test-cases/cases/bind/sanity/output/wasm-go/imported/test_import/enum_test_import__enum.go create mode 100644 packages/test-cases/cases/bind/sanity/output/wasm-go/imported/test_import/env_test_import__env.go create mode 100644 packages/test-cases/cases/bind/sanity/output/wasm-go/imported/test_import/env_test_import__env_serialization.go create mode 100644 packages/test-cases/cases/bind/sanity/output/wasm-go/imported/test_import/object_test_import__another_object.go create mode 100644 packages/test-cases/cases/bind/sanity/output/wasm-go/imported/test_import/object_test_import__another_object_serialization.go create mode 100644 packages/test-cases/cases/bind/sanity/output/wasm-go/imported/test_import/object_test_import__object.go create mode 100644 packages/test-cases/cases/bind/sanity/output/wasm-go/imported/test_import/object_test_import__object_serialization.go create mode 100644 packages/test-cases/cases/bind/sanity/output/wasm-go/imported/test_import/test_import__module_serialization.go create mode 100644 packages/test-cases/cases/bind/sanity/output/wasm-go/imported/test_import/test_import__module_wrapped.go create mode 100644 packages/test-cases/cases/bind/sanity/output/wasm-go/interfaces/test_import.go delete mode 100644 packages/test-cases/cases/bind/sanity/output/wasm-go/types/custom_type_serialization.go rename packages/test-cases/cases/bind/sanity/output/wasm-go/types/{custom_enum.go => enum_custom_enum.go} (100%) create mode 100644 packages/test-cases/cases/bind/sanity/output/wasm-go/types/module_serialization.go create mode 100644 packages/test-cases/cases/bind/sanity/output/wasm-go/types/module_wrapped.go rename packages/test-cases/cases/bind/sanity/output/wasm-go/types/{another_type.go => object_another_type.go} (100%) rename packages/test-cases/cases/bind/sanity/output/wasm-go/types/{another_type_serialization.go => object_another_type_serialization.go} (99%) rename packages/test-cases/cases/bind/sanity/output/wasm-go/types/{custom_type.go => object_custom_type.go} (100%) create mode 100644 packages/test-cases/cases/bind/sanity/output/wasm-go/types/object_custom_type_serialization.go rename packages/test-cases/cases/bind/sanity/output/wasm-go/types/{env.go => object_env.go} (100%) rename packages/test-cases/cases/bind/sanity/output/wasm-go/types/{env_serialization.go => object_env_serialization.go} (99%) diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/functions.ts b/packages/schema/bind/src/bindings/golang/wasm-go/functions.ts index 9b6c0d1470..1265e1d045 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/functions.ts +++ b/packages/schema/bind/src/bindings/golang/wasm-go/functions.ts @@ -84,12 +84,55 @@ 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 = 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){ + 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; + } + } + let imports: Array = ["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)` + } +} + +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]) + let maxPropNameLn = 0; + for (const [propName] of props) { + if (propName.length > maxPropNameLn) { + 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] = "\t" + props[i][0]; + } + return props.map(v => v.join(" ")).join("\n") + "\n"; + } +} + export const handleKeywords: MustacheFn = () => { return (text: string, render: (template: string) => string): string => { const rendered: string = render(text); diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/deserialize_array.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/deserialize_array.mustache index 59003f2d65..c66ee906d9 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/templates/deserialize_array.mustache +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/deserialize_array.mustache @@ -1,23 +1,23 @@ if reader.IsNil() { - _{{name}}{{#lastFullIter}}i{{/lastFullIter}} = nil + _{{name}}{{#lastFullIter}}i{{/lastFullIter}} = nil } else { - {{#nextIter}}ln{{/nextIter}} := reader.ReadArrayLength() - _{{name}}{{#prevFullIter}}i{{/prevFullIter}} = make({{#toWasm}}{{toGraphQLType}}{{/toWasm}}, {{#currIter}}ln{{/currIter}}) - for {{#currIter}}i{{/currIter}} := uint32(0); {{#currIter}}i{{/currIter}} < {{#currIter}}ln{{/currIter}}; {{#currIter}}i{{/currIter}}++ { - {{#scalar}} - {{> deserialize_scalar}} - {{/scalar}} - {{#enum}} - {{> deserialize_enum}} - {{/enum}} - {{#array}} - {{> deserialize_array}} - {{/array}} - {{#map}} - {{> deserialize_map}} - {{/map}} - {{#object}} - {{> deserialize_object}} - {{/object}} - } + {{#nextIter}}ln{{/nextIter}} := reader.ReadArrayLength() + _{{name}}{{#prevFullIter}}i{{/prevFullIter}} = make({{#toWasm}}{{toGraphQLType}}{{/toWasm}}, {{#currIter}}ln{{/currIter}}) + for {{#currIter}}i{{/currIter}} := uint32(0); {{#currIter}}i{{/currIter}} < {{#currIter}}ln{{/currIter}}; {{#currIter}}i{{/currIter}}++ { + {{#scalar}} + {{> deserialize_scalar}} + {{/scalar}} + {{#enum}} + {{> deserialize_enum}} + {{/enum}} + {{#array}} + {{> deserialize_array}} + {{/array}} + {{#map}} + {{> deserialize_map}} + {{/map}} + {{#object}} + {{> deserialize_object}} + {{/object}} + } } diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/deserialize_enum.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/deserialize_enum.mustache index f92f67096d..b5ac1f35bb 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/templates/deserialize_enum.mustache +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/deserialize_enum.mustache @@ -4,8 +4,8 @@ Sanitize{{#toUpper}}{{type}}{{/toUpper}}Value(int32(_{{name}}{{#lastFullIter}}i{ {{/required}} {{^required}} if !reader.IsNil() { - v := {{#toUpper}}{{type}}{{/toUpper}}(reader.ReadI32()) - Sanitize{{#toUpper}}{{type}}{{/toUpper}}Value(int32(v)) - _{{name}}{{#lastFullIter}}i{{/lastFullIter}} = &v + v := {{#toUpper}}{{type}}{{/toUpper}}(reader.ReadI32()) + Sanitize{{#toUpper}}{{type}}{{/toUpper}}Value(int32(v)) + _{{name}}{{#lastFullIter}}i{{/lastFullIter}} = &v } {{/required}} diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/deserialize_map.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/deserialize_map.mustache index a88ea85f13..409d6c97db 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/templates/deserialize_map.mustache +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/deserialize_map.mustache @@ -1,24 +1,24 @@ if reader.IsNil() { - _{{name}}{{#lastFullIter}}i{{/lastFullIter}} = nil + _{{name}}{{#lastFullIter}}i{{/lastFullIter}} = nil } else { - {{#nextIter}}ln{{/nextIter}} := reader.ReadMapLength() - _{{name}}{{#prevFullIter}}i{{/prevFullIter}} = make({{#toWasm}}{{toGraphQLType}}{{/toWasm}}) - for {{#currIter}}j{{/currIter}} := uint32(0); {{#currIter}}j{{/currIter}} < {{#currIter}}ln{{/currIter}}; {{#currIter}}j{{/currIter}}++ { - {{#currIter}}i{{/currIter}} := reader.Read{{#key}}{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}{{/key}}() - {{#scalar}} - {{> deserialize_scalar}} - {{/scalar}} - {{#enum}} - {{> deserialize_enum}} - {{/enum}} - {{#array}} - {{> deserialize_array}} - {{/array}} - {{#map}} - {{> deserialize_map}} - {{/map}} - {{#object}} - {{> deserialize_object}} - {{/object}} - } + {{#nextIter}}ln{{/nextIter}} := reader.ReadMapLength() + _{{name}}{{#prevFullIter}}i{{/prevFullIter}} = make({{#toWasm}}{{toGraphQLType}}{{/toWasm}}) + for {{#currIter}}j{{/currIter}} := uint32(0); {{#currIter}}j{{/currIter}} < {{#currIter}}ln{{/currIter}}; {{#currIter}}j{{/currIter}}++ { + {{#currIter}}i{{/currIter}} := reader.Read{{#key}}{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}{{/key}}() + {{#scalar}} + {{> deserialize_scalar}} + {{/scalar}} + {{#enum}} + {{> deserialize_enum}} + {{/enum}} + {{#array}} + {{> deserialize_array}} + {{/array}} + {{#map}} + {{> deserialize_map}} + {{/map}} + {{#object}} + {{> deserialize_object}} + {{/object}} + } } diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/deserialize_object.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/deserialize_object.mustache index 86cea2fe71..6fc0c5ba2f 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/templates/deserialize_object.mustache +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/deserialize_object.mustache @@ -1,8 +1,8 @@ if v := {{#toUpper}}{{type}}{{/toUpper}}Read(reader); v != nil { - {{#required}} - _{{name}}{{#lastFullIter}}i{{/lastFullIter}} = *v - {{/required}} - {{^required}} - _{{name}}{{#lastFullIter}}i{{/lastFullIter}} = v - {{/required}} + {{#required}} + _{{name}}{{#lastFullIter}}i{{/lastFullIter}} = *v + {{/required}} + {{^required}} + _{{name}}{{#lastFullIter}}i{{/lastFullIter}} = v + {{/required}} } diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/deserialize_scalar.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/deserialize_scalar.mustache index 1a7849f233..2c65dc5c0e 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/templates/deserialize_scalar.mustache +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/deserialize_scalar.mustache @@ -3,7 +3,7 @@ _{{name}}{{#lastFullIter}}i{{/lastFullIter}} = reader.Read{{#toMsgPack}}{{toGrap {{/required}} {{^required}} if !reader.IsNil() { - v := reader.Read{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}(); - _{{name}}{{#lastFullIter}}i{{/lastFullIter}} = {{#readPointer}}{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}} - v{{/readPointer}} + v := reader.Read{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}() + _{{name}}{{#lastFullIter}}i{{/lastFullIter}} = {{#readPointer}}{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}} - v{{/readPointer}} } {{/required}} diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/enum-type/Enum%type%-go.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/enum-type/Enum%type%-go.mustache index a33f6f9723..f88d287c1b 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/templates/enum-type/Enum%type%-go.mustache +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/enum-type/Enum%type%-go.mustache @@ -3,10 +3,12 @@ package types type {{#toUpper}}{{type}}{{/toUpper}} int32 const ( - {{#constants}} - {{#toUpper}}{{type}}{{/toUpper}}{{.}} = iota - {{/constants}} - {{#toFirstLower}}{{type}}{{/toFirstLower}}Max = iota +{{#stuctProps}} +{{#constants}} +{{#toUpper}}{{type}}{{/toUpper}}{{.}} = iota +{{/constants}} +{{#toFirstLower}}{{type}}{{/toFirstLower}}Max = iota +{{/stuctProps}} ) func Sanitize{{#toUpper}}{{type}}{{/toUpper}}Value(value int32) { @@ -17,10 +19,10 @@ func Sanitize{{#toUpper}}{{type}}{{/toUpper}}Value(value int32) { func Get{{#toUpper}}{{type}}{{/toUpper}}Value(key string) {{#toUpper}}{{type}}{{/toUpper}} { switch key { - {{#constants}} - case "{{.}}": - return {{#toUpper}}{{type}}{{/toUpper}}{{.}} - {{/constants}} + {{#constants}} + case "{{.}}": + return {{#toUpper}}{{type}}{{/toUpper}}{{.}} + {{/constants}} default: panic("Invalid key for enum '{{#toUpper}}{{type}}{{/toUpper}}'") } @@ -29,11 +31,11 @@ func Get{{#toUpper}}{{type}}{{/toUpper}}Value(key string) {{#toUpper}}{{type}}{{ 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}} + {{#constants}} + case {{#toUpper}}{{type}}{{/toUpper}}{{.}}: + return "{{.}}" + {{/constants}} default: panic("Invalid value for enum '{{#toUpper}}{{type}}{{/toUpper}}'") } -} \ No newline at end of file +} diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/env-type/Env%type%-go.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/env-type/Env%type%-go.mustache index 08d588529e..4cc1a1eea6 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/templates/env-type/Env%type%-go.mustache +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/env-type/Env%type%-go.mustache @@ -1,16 +1,13 @@ package types -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" -) +{{#makeImports}}{{#properties}}{{#toWasm}}{{toGraphQLType}}{{/toWasm}}{{^last}},{{/last}}{{/properties}}{{/makeImports}} type {{#toUpper}}{{type}}{{/toUpper}} struct { - {{#properties}} - {{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}} {{#toWasm}}{{toGraphQLType}}{{/toWasm}} - {{/properties}} + {{#stuctProps}} + {{#properties}} + {{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}} {{#toWasm}}{{toGraphQLType}}{{/toWasm}} + {{/properties}} + {{/stuctProps}} } func {{#toUpper}}{{type}}{{/toUpper}}ToBuffer(value *{{#toUpper}}{{type}}{{/toUpper}}) []byte { @@ -28,4 +25,3 @@ func {{#toUpper}}{{type}}{{/toUpper}}Write(writer msgpack.Write, value *{{#toUpp func {{#toUpper}}{{type}}{{/toUpper}}Read(reader msgpack.Read) *{{#toUpper}}{{type}}{{/toUpper}} { return read{{#toUpper}}{{type}}{{/toUpper}}(reader) } - diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/env-type/Env%type%Serialization-go.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/env-type/Env%type%Serialization-go.mustache index 38c9f04e63..0574f0e584 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/templates/env-type/Env%type%Serialization-go.mustache +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/env-type/Env%type%Serialization-go.mustache @@ -1,11 +1,6 @@ package types -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" -) +{{#makeImports}}{{#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}}") @@ -16,30 +11,30 @@ func serialize{{#toUpper}}{{type}}{{/toUpper}}(value *{{#toUpper}}{{type}}{{/toU func write{{#toUpper}}{{type}}{{/toUpper}}(writer msgpack.Write, value *{{#toUpper}}{{type}}{{/toUpper}}) { writer.WriteMapLength({{properties.length}}) - {{#properties}} - writer.Context().Push("{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}", "{{#toWasm}}{{toGraphQLType}}{{/toWasm}}", "writing property") - writer.WriteString("{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}") - {{#scalar}} - {{> serialize_scalar}} - {{/scalar}} - {{#array}} - {{#startIter}}{{/startIter}} - {{> serialize_array}} - {{#stopIter}}{{/stopIter}} - {{/array}} - {{#map}} - {{#startIter}}{{/startIter}} - {{> serialize_map}} - {{#stopIter}}{{/stopIter}} - {{/map}} - {{#object}} - {{> serialize_object}} - {{/object}} - {{#enum}} - {{> serialize_enum}} - {{/enum}} - writer.Context().Pop() - {{/properties}} + {{#properties}} + writer.Context().Push("{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}", "{{#toWasm}}{{toGraphQLType}}{{/toWasm}}", "writing property") + writer.WriteString("{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}") + {{#scalar}} + {{> serialize_scalar}} + {{/scalar}} + {{#array}} + {{#startIter}}{{/startIter}} + {{> serialize_array}} + {{#stopIter}}{{/stopIter}} + {{/array}} + {{#map}} + {{#startIter}}{{/startIter}} + {{> serialize_map}} + {{#stopIter}}{{/stopIter}} + {{/map}} + {{#object}} + {{> serialize_object}} + {{/object}} + {{#enum}} + {{> serialize_enum}} + {{/enum}} + writer.Context().Pop() + {{/properties}} } func deserialize{{#toUpper}}{{type}}{{/toUpper}}(data []byte) *{{#toUpper}}{{type}}{{/toUpper}} { @@ -49,61 +44,65 @@ func deserialize{{#toUpper}}{{type}}{{/toUpper}}(data []byte) *{{#toUpper}}{{typ } func read{{#toUpper}}{{type}}{{/toUpper}}(reader msgpack.Read) *{{#toUpper}}{{type}}{{/toUpper}} { - var ( - {{#properties}} - _{{name}} {{#toWasm}}{{toGraphQLType}}{{/toWasm}} - {{#required}} - _{{name}}Set bool - {{/required}} - {{/properties}} - ) + var ( + {{#stuctProps}} + {{#properties}} + _{{name}} {{#toWasm}}{{toGraphQLType}}{{/toWasm}} + {{#required}} + _{{name}}Set bool + {{/required}} + {{/properties}} + {{/stuctProps}} + ) for i := int32(reader.ReadMapLength()); i > 0; i-- { field := reader.ReadString() - reader.Context().Push(field, "unknown", "searching for property type"); + reader.Context().Push(field, "unknown", "searching for property type") switch field { - {{#properties}} - case "{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}": - reader.Context().Push(field, "{{#toWasm}}{{toGraphQLType}}{{/toWasm}}", "type found, reading property") - {{#scalar}} - {{> deserialize_scalar}} - {{/scalar}} - {{#enum}} - {{> deserialize_enum}} - {{/enum}} - {{#array}} - {{#startIter}}{{/startIter}} - {{> deserialize_array}} - {{#stopIter}}{{/stopIter}} - {{/array}} - {{#map}} - {{#startIter}}{{/startIter}} - {{> deserialize_map}} - {{#stopIter}}{{/stopIter}} - {{/map}} - {{#object}} - {{> deserialize_object}} - {{/object}} - {{#required}} - _{{name}}Set = true; - {{/required}} - reader.Context().Pop() - {{/properties}} + {{#properties}} + case "{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}": + reader.Context().Push(field, "{{#toWasm}}{{toGraphQLType}}{{/toWasm}}", "type found, reading property") + {{#scalar}} + {{> deserialize_scalar}} + {{/scalar}} + {{#enum}} + {{> deserialize_enum}} + {{/enum}} + {{#array}} + {{#startIter}}{{/startIter}} + {{> deserialize_array}} + {{#stopIter}}{{/stopIter}} + {{/array}} + {{#map}} + {{#startIter}}{{/startIter}} + {{> deserialize_map}} + {{#stopIter}}{{/stopIter}} + {{/map}} + {{#object}} + {{> deserialize_object}} + {{/object}} + {{#required}} + _{{name}}Set = true + {{/required}} + reader.Context().Pop() + {{/properties}} } - reader.Context().Pop() + reader.Context().Pop() } - {{#properties}} - {{#required}} - if (!_{{name}}Set) { - panic(reader.Context().PrintWithContext("Missing required property: '{{name}}: {{type}}'")) - } - {{/required}} - {{/properties}} + {{#properties}} + {{#required}} + if !_{{name}}Set { + panic(reader.Context().PrintWithContext("Missing required property: '{{name}}: {{type}}'")) + } + {{/required}} + {{/properties}} - return &{{#toUpper}}{{type}}{{/toUpper}}{ - {{#properties}} - {{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}: _{{name}}, - {{/properties}} - } -} \ No newline at end of file + return &{{#toUpper}}{{type}}{{/toUpper}}{ + {{#stuctProps}} + {{#properties}} + {{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}: _{{name}}, + {{/properties}} + {{/stuctProps}} + } +} diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/enum-type/Enum%type%-go.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/enum-type/Enum%type%-go.mustache index f4713678fc..1bd32360c5 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/enum-type/Enum%type%-go.mustache +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/enum-type/Enum%type%-go.mustache @@ -3,10 +3,12 @@ package {{#toSnakeCase}}{{namespace}}{{/toSnakeCase}} type {{#toUpper}}{{type}}{{/toUpper}} int32 const ( - {{#constants}} - {{#toUpper}}{{type}}{{/toUpper}}{{.}} = iota - {{/constants}} - {{#toFirstLower}}{{type}}{{/toFirstLower}}Max = iota +{{#stuctProps}} +{{#constants}} +{{#toUpper}}{{type}}{{/toUpper}}{{.}} = iota +{{/constants}} +{{#toFirstLower}}{{type}}{{/toFirstLower}}Max = iota +{{/stuctProps}} ) func Sanitize{{#toUpper}}{{type}}{{/toUpper}}Value(value int32) { @@ -17,10 +19,10 @@ func Sanitize{{#toUpper}}{{type}}{{/toUpper}}Value(value int32) { func Get{{#toUpper}}{{type}}{{/toUpper}}Value(key string) {{#toUpper}}{{type}}{{/toUpper}} { switch key { - {{#constants}} - case "{{.}}": - return {{#toUpper}}{{type}}{{/toUpper}}{{.}} - {{/constants}} + {{#constants}} + case "{{.}}": + return {{#toUpper}}{{type}}{{/toUpper}}{{.}} + {{/constants}} default: panic("Invalid key for enum '{{#toUpper}}{{type}}{{/toUpper}}'") } @@ -29,11 +31,11 @@ func Get{{#toUpper}}{{type}}{{/toUpper}}Value(key string) {{#toUpper}}{{type}}{{ 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}} + {{#constants}} + case {{#toUpper}}{{type}}{{/toUpper}}{{.}}: + return "{{.}}" + {{/constants}} default: panic("Invalid value for enum '{{#toUpper}}{{type}}{{/toUpper}}'") } -} \ No newline at end of file +} diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/env-type/Env%type%-go.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/env-type/Env%type%-go.mustache index ec612efbee..7f50e2c6a6 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/env-type/Env%type%-go.mustache +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/env-type/Env%type%-go.mustache @@ -1,16 +1,13 @@ 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" -) +{{#makeImports}}{{#properties}}{{#toWasm}}{{toGraphQLType}}{{/toWasm}}{{^last}},{{/last}}{{/properties}}{{/makeImports}} type {{#toUpper}}{{type}}{{/toUpper}} struct { - {{#properties}} - {{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}} {{#toWasm}}{{toGraphQLType}}{{/toWasm}} - {{/properties}} +{{#stuctProps}} +{{#properties}} +{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}} {{#toWasm}}{{toGraphQLType}}{{/toWasm}} +{{/properties}} +{{/stuctProps}} } func {{#toUpper}}{{type}}{{/toUpper}}ToBuffer(value *{{#toUpper}}{{type}}{{/toUpper}}) []byte { @@ -28,4 +25,3 @@ func {{#toUpper}}{{type}}{{/toUpper}}Write(writer msgpack.Write, value *{{#toUpp func {{#toUpper}}{{type}}{{/toUpper}}Read(reader msgpack.Read) *{{#toUpper}}{{type}}{{/toUpper}} { return read{{#toUpper}}{{type}}{{/toUpper}}(reader) } - diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/env-type/Env%type%Serialization-go.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/env-type/Env%type%Serialization-go.mustache index c8a2d79e65..ac4ea04a6f 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/env-type/Env%type%Serialization-go.mustache +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/env-type/Env%type%Serialization-go.mustache @@ -1,11 +1,6 @@ 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" -) +{{#makeImports}}{{#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}}") @@ -16,30 +11,30 @@ func serialize{{#toUpper}}{{type}}{{/toUpper}}(value *{{#toUpper}}{{type}}{{/toU func write{{#toUpper}}{{type}}{{/toUpper}}(writer msgpack.Write, value *{{#toUpper}}{{type}}{{/toUpper}}) { writer.WriteMapLength({{properties.length}}) - {{#properties}} - writer.Context().Push("{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}", "{{#toWasm}}{{toGraphQLType}}{{/toWasm}}", "writing property") - writer.WriteString("{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}") - {{#scalar}} - {{> serialize_scalar}} - {{/scalar}} - {{#array}} - {{#startIter}}{{/startIter}} - {{> serialize_array}} - {{#stopIter}}{{/stopIter}} - {{/array}} - {{#map}} - {{#startIter}}{{/startIter}} - {{> serialize_map}} - {{#stopIter}}{{/stopIter}} - {{/map}} - {{#object}} - {{> serialize_object}} - {{/object}} - {{#enum}} - {{> serialize_enum}} - {{/enum}} - writer.Context().Pop() - {{/properties}} + {{#properties}} + writer.Context().Push("{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}", "{{#toWasm}}{{toGraphQLType}}{{/toWasm}}", "writing property") + writer.WriteString("{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}") + {{#scalar}} + {{> serialize_scalar}} + {{/scalar}} + {{#array}} + {{#startIter}}{{/startIter}} + {{> serialize_array}} + {{#stopIter}}{{/stopIter}} + {{/array}} + {{#map}} + {{#startIter}}{{/startIter}} + {{> serialize_map}} + {{#stopIter}}{{/stopIter}} + {{/map}} + {{#object}} + {{> serialize_object}} + {{/object}} + {{#enum}} + {{> serialize_enum}} + {{/enum}} + writer.Context().Pop() + {{/properties}} } func deserialize{{#toUpper}}{{type}}{{/toUpper}}(data []byte) *{{#toUpper}}{{type}}{{/toUpper}} { @@ -50,60 +45,64 @@ func deserialize{{#toUpper}}{{type}}{{/toUpper}}(data []byte) *{{#toUpper}}{{typ func read{{#toUpper}}{{type}}{{/toUpper}}(reader msgpack.Read) *{{#toUpper}}{{type}}{{/toUpper}} { var ( - {{#properties}} - _{{name}} {{#toWasm}}{{toGraphQLType}}{{/toWasm}} - {{#required}} - _{{name}}Set bool - {{/required}} - {{/properties}} + {{#stuctProps}} + {{#properties}} + _{{name}} {{#toWasm}}{{toGraphQLType}}{{/toWasm}} + {{#required}} + _{{name}}Set bool + {{/required}} + {{/properties}} + {{/stuctProps}} ) for i := int32(reader.ReadMapLength()); i > 0; i-- { field := reader.ReadString() - reader.Context().Push(field, "unknown", "searching for property type"); + reader.Context().Push(field, "unknown", "searching for property type") switch field { - {{#properties}} - case "{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}": - reader.Context().Push(field, "{{#toWasm}}{{toGraphQLType}}{{/toWasm}}", "type found, reading property") - {{#scalar}} - {{> deserialize_scalar}} - {{/scalar}} - {{#enum}} - {{> deserialize_enum}} - {{/enum}} - {{#array}} - {{#startIter}}{{/startIter}} - {{> deserialize_array}} - {{#stopIter}}{{/stopIter}} - {{/array}} - {{#map}} - {{#startIter}}{{/startIter}} - {{> deserialize_map}} - {{#stopIter}}{{/stopIter}} - {{/map}} - {{#object}} - {{> deserialize_object}} - {{/object}} - {{#required}} - _{{name}}Set = true; - {{/required}} - reader.Context().Pop() - {{/properties}} + {{#properties}} + case "{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}": + reader.Context().Push(field, "{{#toWasm}}{{toGraphQLType}}{{/toWasm}}", "type found, reading property") + {{#scalar}} + {{> deserialize_scalar}} + {{/scalar}} + {{#enum}} + {{> deserialize_enum}} + {{/enum}} + {{#array}} + {{#startIter}}{{/startIter}} + {{> deserialize_array}} + {{#stopIter}}{{/stopIter}} + {{/array}} + {{#map}} + {{#startIter}}{{/startIter}} + {{> deserialize_map}} + {{#stopIter}}{{/stopIter}} + {{/map}} + {{#object}} + {{> deserialize_object}} + {{/object}} + {{#required}} + _{{name}}Set = true + {{/required}} + reader.Context().Pop() + {{/properties}} } - reader.Context().Pop() + reader.Context().Pop() } - {{#properties}} - {{#required}} - if (!_{{name}}Set) { - panic(reader.Context().PrintWithContext("Missing required property: '{{name}}: {{type}}'")) - } - {{/required}} - {{/properties}} + {{#properties}} + {{#required}} + if !_{{name}}Set { + panic(reader.Context().PrintWithContext("Missing required property: '{{name}}: {{type}}'")) + } + {{/required}} + {{/properties}} - return &{{#toUpper}}{{type}}{{/toUpper}}{ - {{#properties}} - {{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}: _{{name}}, - {{/properties}} - } -} \ No newline at end of file + return &{{#toUpper}}{{type}}{{/toUpper}}{ + {{#stuctProps}} + {{#properties}} + {{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}: _{{name}}, + {{/properties}} + {{/stuctProps}} + } +} diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/interface-type/%type%-go.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/interface-type/%type%-go.mustache index 2db03af454..c63551016a 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/interface-type/%type%-go.mustache +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/interface-type/%type%-go.mustache @@ -12,8 +12,8 @@ import "github.com/consideritdone/polywrap-go/polywrap" {{#getImplementations}} {{#enabled}} func {{#toUpper}}{{namespace}}{{/toUpper}}Implementations() []string { - return polywrap.WrapGetImplementations("{{uri}}") + return polywrap.WrapGetImplementations("{{uri}}") } {{/enabled}} {{/getImplementations}} -{{/capabilities}} \ No newline at end of file +{{/capabilities}} diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/module-type/%type%Serialization-go.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/module-type/%type%Serialization-go.mustache index 45e7b0762d..e908e48bfc 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/module-type/%type%Serialization-go.mustache +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/module-type/%type%Serialization-go.mustache @@ -1,84 +1,82 @@ 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" -) +{{#makeImports}}{{#arguments}}{{#toWasm}}{{toGraphQLType}}{{/toWasm}}{{^last}},{{/last}}{{/arguments}}{{/makeImports}} {{#methods}} type Args{{#toUpper}}{{name}}{{/toUpper}} struct { - {{#arguments}} - {{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}} {{#toWasm}}{{toGraphQLType}}{{/toWasm}} - {{/arguments}} +{{#stuctProps}} +{{#arguments}} +{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}} {{#toWasm}}{{toGraphQLType}}{{/toWasm}} +{{/arguments}} +{{/stuctProps}} } func Serialize{{#toUpper}}{{name}}{{/toUpper}}Args(value *Args{{#toUpper}}{{name}}{{/toUpper}}) []byte { - ctx := msgpack.NewContext("Serializing module-type: {{#toUpper}}{{name}}{{/toUpper}}") - encoder := msgpack.NewWriteEncoder(ctx) - Write{{#toUpper}}{{name}}{{/toUpper}}Args(encoder, value); - return encoder.Buffer() + ctx := msgpack.NewContext("Serializing module-type: {{#toUpper}}{{name}}{{/toUpper}}") + encoder := msgpack.NewWriteEncoder(ctx) + Write{{#toUpper}}{{name}}{{/toUpper}}Args(encoder, value) + return encoder.Buffer() } func Write{{#toUpper}}{{name}}{{/toUpper}}Args(writer msgpack.Write, value *Args{{#toUpper}}{{name}}{{/toUpper}}) { - writer.WriteMapLength({{arguments.length}}) - {{#arguments}} - writer.Context().Push("{{name}}", "{{#toWasm}}{{toGraphQLType}}{{/toWasm}}", "writing property") - writer.WriteString("{{name}}") - {{#scalar}} - {{> value_serialize_scalar}} - {{/scalar}} - {{#array}} - {{#startIter}}{{/startIter}} - {{> value_serialize_array}} - {{#stopIter}}{{/stopIter}} - {{/array}} - {{#map}} - {{#startIter}}{{/startIter}} - {{> value_serialize_map}} - {{#stopIter}}{{/stopIter}} - {{/map}} - {{#object}} - {{> value_serialize_object}} - {{/object}} - {{#enum}} - {{> value_serialize_enum}} - {{/enum}} - writer.Context().Pop() - {{/arguments}} + writer.WriteMapLength({{arguments.length}}) + {{#arguments}} + writer.Context().Push("{{name}}", "{{#toWasm}}{{toGraphQLType}}{{/toWasm}}", "writing property") + writer.WriteString("{{name}}") + {{#scalar}} + {{> serialize_scalar}} + {{/scalar}} + {{#array}} + {{#startIter}}{{/startIter}} + {{> serialize_array}} + {{#stopIter}}{{/stopIter}} + {{/array}} + {{#map}} + {{#startIter}}{{/startIter}} + {{> serialize_map}} + {{#stopIter}}{{/stopIter}} + {{/map}} + {{#object}} + {{> serialize_object}} + {{/object}} + {{#enum}} + {{> serialize_enum}} + {{/enum}} + writer.Context().Pop() + {{/arguments}} } -export function Deserialize{{#toUpper}}{{name}}{{/toUpper}}Result(argsBuf []byte): {{#return}}{{#toWasm}}{{toGraphQLType}}{{/toWasm}}{{/return}} { - ctx := msgpack.NewContext("Deserializing module-type: {{#toUpper}}{{name}}{{/toUpper}}") - reader := msgpack.NewReadDecoder(ctx, argsBuf) +func Deserialize{{#toUpper}}{{name}}{{/toUpper}}Result(argsBuf []byte) {{#return}}{{#toWasm}}{{toGraphQLType}}{{/toWasm}}{{/return}} { + ctx := msgpack.NewContext("Deserializing module-type: {{#toUpper}}{{name}}{{/toUpper}}") + reader := msgpack.NewReadDecoder(ctx, argsBuf) - {{#return}} - reader.Context().Push("{{name}}", "{{#toWasm}}{{toGraphQLType}}{{/toWasm}}", "reading function output"); - var value {{#toWasm}}{{toGraphQLType}}{{/toWasm}} - {{#scalar}} - {{> value_deserialize_scalar}} - {{/scalar}} - {{#array}} - {{#startIter}}{{/startIter}} - {{> value_deserialize_array}} - {{#stopIter}}{{/stopIter}} - {{/array}} - {{#map}} - {{#startIter}}{{/startIter}} - {{> value_deserialize_map}} - {{#stopIter}}{{/stopIter}} - {{/map}} - {{#object}} - {{> value_deserialize_object}} - {{/object}} - {{#enum}} - {{> value_deserialize_enum}} - {{/enum}} - writer.Context().Pop() - return value - {{/return}} + {{#return}} + reader.Context().Push("{{name}}", "{{#toWasm}}{{toGraphQLType}}{{/toWasm}}", "reading function output") + var value {{#toWasm}}{{toGraphQLType}}{{/toWasm}} + {{#scalar}} + {{> value_deserialize_scalar}} + {{/scalar}} + {{#array}} + {{#startIter}}{{/startIter}} + {{> value_deserialize_array}} + {{#stopIter}}{{/stopIter}} + {{/array}} + {{#map}} + {{#startIter}}{{/startIter}} + {{> value_deserialize_map}} + {{#stopIter}}{{/stopIter}} + {{/map}} + {{#object}} + {{> value_deserialize_object}} + {{/object}} + {{#enum}} + {{> value_deserialize_enum}} + {{/enum}} + reader.Context().Pop() + return value + {{/return}} } +{{^last}} -{{/methods}} - +{{/last}} +{{/methods}} \ No newline at end of file diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/module-type/%type%Wrapped-go.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/module-type/%type%Wrapped-go.mustache index b7d1e09512..1340695e91 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/module-type/%type%Wrapped-go.mustache +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/module-type/%type%Wrapped-go.mustache @@ -2,20 +2,20 @@ package {{#toSnakeCase}}{{namespace}}{{/toSnakeCase}} {{#methods.length}} import ( - "github.com/consideritdone/polywrap-go/polywrap" - "some/library/to/methods" + "github.com/consideritdone/polywrap-go/polywrap" + "some/library/to/methods" ) {{/methods.length}} {{^isInterface}} {{#methods}} func {{#toUpper}}{{type}}{{/toUpper}}{{#toUpper}}{{name}}{{/toUpper}}(args *Args{{#toUpper}}{{name}}{{/toUpper}}) ({{#return}}{{#toWasm}}{{toGraphQLType}}{{/toWasm}}{{/return}}, error) { - argsBuf := Serialize{{#toUpper}}{{name}}{{/toUpper}}Args(args) - data, err := polywrap.WrapSubinvoke("{{uri}}", "{{name}}", argsBuf) - if err != nil { - return nil, result.Error() - } - return Deserialize{{#toUpper}}{{name}}{{/toUpper}}Result(result.unwrap()), nil + argsBuf := Serialize{{#toUpper}}{{name}}{{/toUpper}}Args(args) + data, err := polywrap.WrapSubinvoke("{{uri}}", "{{name}}", argsBuf) + if err != nil { + return nil, result.Error() + } + return Deserialize{{#toUpper}}{{name}}{{/toUpper}}Result(result.unwrap()), nil } {{^last}} @@ -25,12 +25,12 @@ func {{#toUpper}}{{type}}{{/toUpper}}{{#toUpper}}{{name}}{{/toUpper}}(args *Args {{#isInterface}} {{#methods}} func {{#toUpper}}{{type}}{{/toUpper}}{{#toUpper}}{{name}}{{/toUpper}}(uri string, args *Args{{#toUpper}}{{name}}{{/toUpper}}) ({{#return}}{{#toWasm}}{{toGraphQLType}}{{/toWasm}}{{/return}}, error) { - argsBuf := Serialize{{#toUpper}}{{name}}{{/toUpper}}Args(args) - data, err := polywrap.WrapSubinvokeImplementation("{{uri}}", uri, "{{name}}", argsBuf) - if err != nil { - return nil, err - } - return Deserialize{{#toUpper}}{{name}}{{/toUpper}}Result(data), nil + argsBuf := Serialize{{#toUpper}}{{name}}{{/toUpper}}Args(args) + data, err := polywrap.WrapSubinvokeImplementation("{{uri}}", uri, "{{name}}", argsBuf) + if err != nil { + return nil, err + } + return Deserialize{{#toUpper}}{{name}}{{/toUpper}}Result(data), nil } {{^last}} diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/object-type/Object%type%-go.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/object-type/Object%type%-go.mustache index ec612efbee..7f50e2c6a6 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/object-type/Object%type%-go.mustache +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/object-type/Object%type%-go.mustache @@ -1,16 +1,13 @@ 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" -) +{{#makeImports}}{{#properties}}{{#toWasm}}{{toGraphQLType}}{{/toWasm}}{{^last}},{{/last}}{{/properties}}{{/makeImports}} type {{#toUpper}}{{type}}{{/toUpper}} struct { - {{#properties}} - {{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}} {{#toWasm}}{{toGraphQLType}}{{/toWasm}} - {{/properties}} +{{#stuctProps}} +{{#properties}} +{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}} {{#toWasm}}{{toGraphQLType}}{{/toWasm}} +{{/properties}} +{{/stuctProps}} } func {{#toUpper}}{{type}}{{/toUpper}}ToBuffer(value *{{#toUpper}}{{type}}{{/toUpper}}) []byte { @@ -28,4 +25,3 @@ func {{#toUpper}}{{type}}{{/toUpper}}Write(writer msgpack.Write, value *{{#toUpp func {{#toUpper}}{{type}}{{/toUpper}}Read(reader msgpack.Read) *{{#toUpper}}{{type}}{{/toUpper}} { return read{{#toUpper}}{{type}}{{/toUpper}}(reader) } - diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/object-type/Object%type%Serialization-go.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/object-type/Object%type%Serialization-go.mustache index c8a2d79e65..3274d8b9c4 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/object-type/Object%type%Serialization-go.mustache +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/imported/object-type/Object%type%Serialization-go.mustache @@ -1,11 +1,6 @@ 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" -) +{{#makeImports}}{{#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}}") @@ -16,30 +11,30 @@ func serialize{{#toUpper}}{{type}}{{/toUpper}}(value *{{#toUpper}}{{type}}{{/toU func write{{#toUpper}}{{type}}{{/toUpper}}(writer msgpack.Write, value *{{#toUpper}}{{type}}{{/toUpper}}) { writer.WriteMapLength({{properties.length}}) - {{#properties}} - writer.Context().Push("{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}", "{{#toWasm}}{{toGraphQLType}}{{/toWasm}}", "writing property") - writer.WriteString("{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}") - {{#scalar}} - {{> serialize_scalar}} - {{/scalar}} - {{#array}} - {{#startIter}}{{/startIter}} - {{> serialize_array}} - {{#stopIter}}{{/stopIter}} - {{/array}} - {{#map}} - {{#startIter}}{{/startIter}} - {{> serialize_map}} - {{#stopIter}}{{/stopIter}} - {{/map}} - {{#object}} - {{> serialize_object}} - {{/object}} - {{#enum}} - {{> serialize_enum}} - {{/enum}} - writer.Context().Pop() - {{/properties}} + {{#properties}} + writer.Context().Push("{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}", "{{#toWasm}}{{toGraphQLType}}{{/toWasm}}", "writing property") + writer.WriteString("{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}") + {{#scalar}} + {{> serialize_scalar}} + {{/scalar}} + {{#array}} + {{#startIter}}{{/startIter}} + {{> serialize_array}} + {{#stopIter}}{{/stopIter}} + {{/array}} + {{#map}} + {{#startIter}}{{/startIter}} + {{> serialize_map}} + {{#stopIter}}{{/stopIter}} + {{/map}} + {{#object}} + {{> serialize_object}} + {{/object}} + {{#enum}} + {{> serialize_enum}} + {{/enum}} + writer.Context().Pop() + {{/properties}} } func deserialize{{#toUpper}}{{type}}{{/toUpper}}(data []byte) *{{#toUpper}}{{type}}{{/toUpper}} { @@ -49,61 +44,65 @@ func deserialize{{#toUpper}}{{type}}{{/toUpper}}(data []byte) *{{#toUpper}}{{typ } func read{{#toUpper}}{{type}}{{/toUpper}}(reader msgpack.Read) *{{#toUpper}}{{type}}{{/toUpper}} { - var ( - {{#properties}} - _{{name}} {{#toWasm}}{{toGraphQLType}}{{/toWasm}} - {{#required}} - _{{name}}Set bool - {{/required}} - {{/properties}} - ) + var ( + {{#stuctProps}} + {{#properties}} + _{{name}} {{#toWasm}}{{toGraphQLType}}{{/toWasm}} + {{#required}} + _{{name}}Set bool + {{/required}} + {{/properties}} + {{/stuctProps}} + ) for i := int32(reader.ReadMapLength()); i > 0; i-- { field := reader.ReadString() - reader.Context().Push(field, "unknown", "searching for property type"); + reader.Context().Push(field, "unknown", "searching for property type") switch field { - {{#properties}} - case "{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}": - reader.Context().Push(field, "{{#toWasm}}{{toGraphQLType}}{{/toWasm}}", "type found, reading property") - {{#scalar}} - {{> deserialize_scalar}} - {{/scalar}} - {{#enum}} - {{> deserialize_enum}} - {{/enum}} - {{#array}} - {{#startIter}}{{/startIter}} - {{> deserialize_array}} - {{#stopIter}}{{/stopIter}} - {{/array}} - {{#map}} - {{#startIter}}{{/startIter}} - {{> deserialize_map}} - {{#stopIter}}{{/stopIter}} - {{/map}} - {{#object}} - {{> deserialize_object}} - {{/object}} - {{#required}} - _{{name}}Set = true; - {{/required}} - reader.Context().Pop() - {{/properties}} + {{#properties}} + case "{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}": + reader.Context().Push(field, "{{#toWasm}}{{toGraphQLType}}{{/toWasm}}", "type found, reading property") + {{#scalar}} + {{> deserialize_scalar}} + {{/scalar}} + {{#enum}} + {{> deserialize_enum}} + {{/enum}} + {{#array}} + {{#startIter}}{{/startIter}} + {{> deserialize_array}} + {{#stopIter}}{{/stopIter}} + {{/array}} + {{#map}} + {{#startIter}}{{/startIter}} + {{> deserialize_map}} + {{#stopIter}}{{/stopIter}} + {{/map}} + {{#object}} + {{> deserialize_object}} + {{/object}} + {{#required}} + _{{name}}Set = true + {{/required}} + reader.Context().Pop() + {{/properties}} } - reader.Context().Pop() + reader.Context().Pop() } - {{#properties}} - {{#required}} - if (!_{{name}}Set) { - panic(reader.Context().PrintWithContext("Missing required property: '{{name}}: {{type}}'")) - } - {{/required}} - {{/properties}} + {{#properties}} + {{#required}} + if !_{{name}}Set { + panic(reader.Context().PrintWithContext("Missing required property: '{{name}}: {{type}}'")) + } + {{/required}} + {{/properties}} - return &{{#toUpper}}{{type}}{{/toUpper}}{ - {{#properties}} - {{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}: _{{name}}, - {{/properties}} - } -} \ No newline at end of file + return &{{#toUpper}}{{type}}{{/toUpper}}{ + {{#stuctProps}} + {{#properties}} + {{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}: _{{name}}, + {{/properties}} + {{/stuctProps}} + } +} diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/interface-type/%type%-go.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/interface-type/%type%-go.mustache index 46cc9be8e0..861f5555e7 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/templates/interface-type/%type%-go.mustache +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/interface-type/%type%-go.mustache @@ -12,8 +12,8 @@ import "github.com/consideritdone/polywrap-go/polywrap" {{#getImplementations}} {{#enabled}} func {{#toUpper}}{{namespace}}{{/toUpper}}Implementations() []string { - return polywrap.WrapGetImplementations("{{uri}}") + return polywrap.WrapGetImplementations("{{uri}}") } {{/enabled}} {{/getImplementations}} -{{/capabilities}} \ No newline at end of file +{{/capabilities}} diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/main-go.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/main-go.mustache index 5907c2160f..c4f9dfc4e8 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/templates/main-go.mustache +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/main-go.mustache @@ -1,21 +1,23 @@ package main import ( - "github.com/consideritdone/polywrap-go/examples/demo1/wrap/module" - "github.com/consideritdone/polywrap-go/polywrap" + "github.com/consideritdone/polywrap-go/examples/demo1/wrap/module" + "github.com/consideritdone/polywrap-go/polywrap" ) //export _wrap_invoke func _wrap_invoke(methodSize, argsSize, envSize uint32) bool { - args := polywrap.WrapInvokeArgs(methodSize, argsSize) - - {{#moduleType}} - {{#methods}} - {{^first}}else {{/first}}if args.method == "{{name}}" { - return polywrap.WrapInvoke(args, envSize, module.{{#toUpper}}{{name}}{{/toUpper}}Wrapped) - } {{/methods}}{{/moduleType}}else { - return polywrap.WrapInvoke(args, envSize, nil) - } + args := polywrap.WrapInvokeArgs(methodSize, argsSize) + switch args.method { + {{#moduleType}} + {{#methods}} + case "{{name}}": + return polywrap.WrapInvoke(args, envSize, module.{{#toUpper}}{{name}}{{/toUpper}}Wrapped) + {{/methods}} + {{/moduleType}} + default: + return polywrap.WrapInvoke(args, envSize, nil) + } } func main() { diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/module-type/%type%Serialization-go.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/module-type/%type%Serialization-go.mustache index 9dff688252..292aa1978d 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/templates/module-type/%type%Serialization-go.mustache +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/module-type/%type%Serialization-go.mustache @@ -1,115 +1,118 @@ package types -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" -) +{{#makeImports}}{{#arguments}}{{#toWasm}}{{toGraphQLType}}{{/toWasm}}{{^last}},{{/last}}{{/arguments}}{{/makeImports}} {{#methods}} type Args{{#toUpper}}{{name}}{{/toUpper}} struct { - {{#arguments}} - {{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}} {{#toWasm}}{{toGraphQLType}}{{/toWasm}} - {{/arguments}} +{{#stuctProps}} +{{#arguments}} +{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}} {{#toWasm}}{{toGraphQLType}}{{/toWasm}} +{{/arguments}} +{{/stuctProps}} } func Deserialize{{#toUpper}}{{name}}{{/toUpper}}Args(argsBuf []byte) *Args{{#toUpper}}{{name}}{{/toUpper}} { - ctx := msgpack.NewContext("Deserializing module-type: {{#toUpper}}{{name}}{{/toUpper}}") + ctx := msgpack.NewContext("Deserializing module-type: {{#toUpper}}{{name}}{{/toUpper}}") {{#arguments.length}} - reader := msgpack.NewReadDecoder(ctx, argsBuf) + reader := msgpack.NewReadDecoder(ctx, argsBuf) - var ( - {{#arguments}} - _{{name}} {{#toWasm}}{{toGraphQLType}}{{/toWasm}} - {{#required}} - _{{name}}Set bool - {{/required}} - {{/arguments}} - ) + var ( + {{#stuctProps}} + {{#arguments}} + _{{name}} {{#toWasm}}{{toGraphQLType}}{{/toWasm}} + {{#required}} + _{{name}}Set bool + {{/required}} + {{/arguments}} + {{/stuctProps}} + ) - for i := int32(reader.ReadMapLength()); i > 0; i-- { - field := reader.ReadString() - reader.Context().Push(field, "unknown", "searching for property type"); - reader.Context().Pop() - switch field { - {{#arguments}} - case "{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}": - reader.Context().Push(field, "{{#toWasm}}{{toGraphQLType}}{{/toWasm}}", "type found, reading property") - {{#scalar}} - {{> deserialize_scalar}} - {{/scalar}} - {{#enum}} - {{> deserialize_enum}} - {{/enum}} - {{#array}} - {{#startIter}}{{/startIter}} - {{> deserialize_array}} - {{#stopIter}}{{/stopIter}} - {{/array}} - {{#map}} - {{#startIter}}{{/startIter}} - {{> deserialize_map}} - {{#stopIter}}{{/stopIter}} - {{/map}} - {{#object}} - {{> deserialize_object}} - {{/object}} - {{#required}} - _{{name}}Set = true; - {{/required}} - reader.Context().Pop() - {{/arguments}} - } - } + for i := int32(reader.ReadMapLength()); i > 0; i-- { + field := reader.ReadString() + reader.Context().Push(field, "unknown", "searching for property type") + reader.Context().Pop() + switch field { + {{#arguments}} + case "{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}": + reader.Context().Push(field, "{{#toWasm}}{{toGraphQLType}}{{/toWasm}}", "type found, reading property") + {{#scalar}} + {{> deserialize_scalar}} + {{/scalar}} + {{#enum}} + {{> deserialize_enum}} + {{/enum}} + {{#array}} + {{#startIter}}{{/startIter}} + {{> deserialize_array}} + {{#stopIter}}{{/stopIter}} + {{/array}} + {{#map}} + {{#startIter}}{{/startIter}} + {{> deserialize_map}} + {{#stopIter}}{{/stopIter}} + {{/map}} + {{#object}} + {{> deserialize_object}} + {{/object}} + {{#required}} + _{{name}}Set = true + {{/required}} + reader.Context().Pop() + {{/arguments}} + } + } - {{#arguments}} - {{#required}} - if (!_{{name}}Set) { - panic(reader.Context().PrintWithContext("Missing required property: '{{name}}: {{type}}'")) - } - {{/required}} - {{/arguments}} - {{/arguments.length}} + {{#arguments}} + {{#required}} + if !_{{name}}Set { + panic(reader.Context().PrintWithContext("Missing required property: '{{name}}: {{type}}'")) + } + {{/required}} + {{/arguments}} + {{/arguments.length}} - return &Args{{#toUpper}}{{name}}{{/toUpper}}{ - {{#arguments}} - {{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}: _{{name}}, - {{/arguments}} - } + return &Args{{#toUpper}}{{name}}{{/toUpper}}{ + {{#stuctProps}} + {{#arguments}} + {{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}: _{{name}}, + {{/arguments}} + {{/stuctProps}} + } } func Serialize{{#toUpper}}{{name}}{{/toUpper}}Result(value {{#return}}{{#toWasm}}{{toGraphQLType}}{{/toWasm}}{{/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() + 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}}) { - {{#return}} - writer.Context().Push("{{name}}", "{{#toWasm}}{{toGraphQLType}}{{/toWasm}}", "writing property"); - {{#scalar}} - {{> value_serialize_scalar}} - {{/scalar}} - {{#array}} - {{#startIter}}{{/startIter}} - {{> value_serialize_array}} - {{#stopIter}}{{/stopIter}} - {{/array}} - {{#map}} - {{#startIter}}{{/startIter}} - {{> value_serialize_map}} - {{#stopIter}}{{/stopIter}} - {{/map}} - {{#object}} - {{> value_serialize_object}} - {{/object}} - {{#enum}} - {{> value_serialize_enum}} - {{/enum}} - writer.Context().Pop() - {{/return}} + {{#return}} + writer.Context().Push("{{name}}", "{{#toWasm}}{{toGraphQLType}}{{/toWasm}}", "writing property") + {{#scalar}} + {{> value_serialize_scalar}} + {{/scalar}} + {{#array}} + {{#startIter}}{{/startIter}} + {{> value_serialize_array}} + {{#stopIter}}{{/stopIter}} + {{/array}} + {{#map}} + {{#startIter}}{{/startIter}} + {{> value_serialize_map}} + {{#stopIter}}{{/stopIter}} + {{/map}} + {{#object}} + {{> value_serialize_object}} + {{/object}} + {{#enum}} + {{> value_serialize_enum}} + {{/enum}} + writer.Context().Pop() + {{/return}} } +{{^last}} -{{/methods}} +{{/last}} +{{/methods}} \ No newline at end of file diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/module-type/%type%Wrapped-go.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/module-type/%type%Wrapped-go.mustache index 5e24b3b48a..3941f19b41 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/templates/module-type/%type%Wrapped-go.mustache +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/module-type/%type%Wrapped-go.mustache @@ -2,34 +2,32 @@ package types {{#methods.length}} import ( - "github.com/consideritdone/polywrap-go/polywrap" - "some/library/to/methods" + "github.com/consideritdone/polywrap-go/polywrap" + "some/library/to/methods" ) {{/methods.length}} {{#methods}} func {{#toUpper}}{{name}}{{/toUpper}}Wrapped(argsBuf []byte, envSize uint32) []byte { - var env *Env - {{#env}} - {{#required}} - if (envSize == 0) { - panic("Environment is not set, and it is required by method 'objectMethod'") - } - {{/required}} - if (envSize > 0) { - envBuf := polywrap.WrapLoadEnv(envSize); - env = EnvFromBuffer(envBuf); - } - {{/env}} + var env *Env + {{#env}} + {{#required}} + if envSize == 0 { + panic("Environment is not set, and it is required by method 'objectMethod'") + } + {{/required}} + if envSize > 0 { + envBuf := polywrap.WrapLoadEnv(envSize) + env = EnvFromBuffer(envBuf) + } + {{/env}} - {{#arguments.length}} - args: = Deserialize{{#toUpper}}{{name}}{{/toUpper}}Args(argsBuf) - {{/arguments.length}} + {{#arguments.length}} + args := Deserialize{{#toUpper}}{{name}}{{/toUpper}}Args(argsBuf) + {{/arguments.length}} - result := methods.{{#toUpper}}{{name}}{{/toUpper}}({{#arguments.length}}args{{/arguments.length}}{{^arguments.length}}nil{{/arguments.length}}{{#env}}, env{{/env}}) - return Serialize{{#toUpper}}{{name}}{{/toUpper}}Result(result); + result := methods.{{#toUpper}}{{name}}{{/toUpper}}({{#arguments.length}}args{{/arguments.length}}{{^arguments.length}}nil{{/arguments.length}}{{#env}}, env{{/env}}) + return Serialize{{#toUpper}}{{name}}{{/toUpper}}Result(result) } -{{^last}} -{{/last}} {{/methods}} diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/object-type/Object%type%-go.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/object-type/Object%type%-go.mustache index 08d588529e..b17610926a 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/templates/object-type/Object%type%-go.mustache +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/object-type/Object%type%-go.mustache @@ -1,16 +1,13 @@ package types -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" -) +{{#makeImports}}{{#properties}}{{#toWasm}}{{toGraphQLType}}{{/toWasm}}{{^last}},{{/last}}{{/properties}}{{/makeImports}} type {{#toUpper}}{{type}}{{/toUpper}} struct { - {{#properties}} - {{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}} {{#toWasm}}{{toGraphQLType}}{{/toWasm}} - {{/properties}} +{{#stuctProps}} +{{#properties}} +{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}} {{#toWasm}}{{toGraphQLType}}{{/toWasm}} +{{/properties}} +{{/stuctProps}} } func {{#toUpper}}{{type}}{{/toUpper}}ToBuffer(value *{{#toUpper}}{{type}}{{/toUpper}}) []byte { @@ -28,4 +25,3 @@ func {{#toUpper}}{{type}}{{/toUpper}}Write(writer msgpack.Write, value *{{#toUpp func {{#toUpper}}{{type}}{{/toUpper}}Read(reader msgpack.Read) *{{#toUpper}}{{type}}{{/toUpper}} { return read{{#toUpper}}{{type}}{{/toUpper}}(reader) } - diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/object-type/Object%type%Serialization-go.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/object-type/Object%type%Serialization-go.mustache index 38c9f04e63..93bec1a7d4 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/templates/object-type/Object%type%Serialization-go.mustache +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/object-type/Object%type%Serialization-go.mustache @@ -1,11 +1,6 @@ package types -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" -) +{{#makeImports}}{{#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}}") @@ -16,30 +11,30 @@ func serialize{{#toUpper}}{{type}}{{/toUpper}}(value *{{#toUpper}}{{type}}{{/toU func write{{#toUpper}}{{type}}{{/toUpper}}(writer msgpack.Write, value *{{#toUpper}}{{type}}{{/toUpper}}) { writer.WriteMapLength({{properties.length}}) - {{#properties}} - writer.Context().Push("{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}", "{{#toWasm}}{{toGraphQLType}}{{/toWasm}}", "writing property") - writer.WriteString("{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}") - {{#scalar}} - {{> serialize_scalar}} - {{/scalar}} - {{#array}} - {{#startIter}}{{/startIter}} - {{> serialize_array}} - {{#stopIter}}{{/stopIter}} - {{/array}} - {{#map}} - {{#startIter}}{{/startIter}} - {{> serialize_map}} - {{#stopIter}}{{/stopIter}} - {{/map}} - {{#object}} - {{> serialize_object}} - {{/object}} - {{#enum}} - {{> serialize_enum}} - {{/enum}} - writer.Context().Pop() - {{/properties}} + {{#properties}} + writer.Context().Push("{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}", "{{#toWasm}}{{toGraphQLType}}{{/toWasm}}", "writing property") + writer.WriteString("{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}") + {{#scalar}} + {{> serialize_scalar}} + {{/scalar}} + {{#array}} + {{#startIter}}{{/startIter}} + {{> serialize_array}} + {{#stopIter}}{{/stopIter}} + {{/array}} + {{#map}} + {{#startIter}}{{/startIter}} + {{> serialize_map}} + {{#stopIter}}{{/stopIter}} + {{/map}} + {{#object}} + {{> serialize_object}} + {{/object}} + {{#enum}} + {{> serialize_enum}} + {{/enum}} + writer.Context().Pop() + {{/properties}} } func deserialize{{#toUpper}}{{type}}{{/toUpper}}(data []byte) *{{#toUpper}}{{type}}{{/toUpper}} { @@ -49,61 +44,64 @@ func deserialize{{#toUpper}}{{type}}{{/toUpper}}(data []byte) *{{#toUpper}}{{typ } func read{{#toUpper}}{{type}}{{/toUpper}}(reader msgpack.Read) *{{#toUpper}}{{type}}{{/toUpper}} { - var ( - {{#properties}} - _{{name}} {{#toWasm}}{{toGraphQLType}}{{/toWasm}} - {{#required}} - _{{name}}Set bool - {{/required}} - {{/properties}} - ) + var ( + {{#stuctProps}} + {{#properties}} + _{{name}} {{#toWasm}}{{toGraphQLType}}{{/toWasm}} + {{#required}} + _{{name}}Set bool + {{/required}} + {{/properties}} + {{/stuctProps}} + ) for i := int32(reader.ReadMapLength()); i > 0; i-- { field := reader.ReadString() - reader.Context().Push(field, "unknown", "searching for property type"); + reader.Context().Push(field, "unknown", "searching for property type") switch field { - {{#properties}} - case "{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}": - reader.Context().Push(field, "{{#toWasm}}{{toGraphQLType}}{{/toWasm}}", "type found, reading property") - {{#scalar}} - {{> deserialize_scalar}} - {{/scalar}} - {{#enum}} - {{> deserialize_enum}} - {{/enum}} - {{#array}} - {{#startIter}}{{/startIter}} - {{> deserialize_array}} - {{#stopIter}}{{/stopIter}} - {{/array}} - {{#map}} - {{#startIter}}{{/startIter}} - {{> deserialize_map}} - {{#stopIter}}{{/stopIter}} - {{/map}} - {{#object}} - {{> deserialize_object}} - {{/object}} - {{#required}} - _{{name}}Set = true; - {{/required}} - reader.Context().Pop() - {{/properties}} + {{#properties}} + case "{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}": + reader.Context().Push(field, "{{#toWasm}}{{toGraphQLType}}{{/toWasm}}", "type found, reading property") + {{#scalar}} + {{> deserialize_scalar}} + {{/scalar}} + {{#enum}} + {{> deserialize_enum}} + {{/enum}} + {{#array}} + {{#startIter}}{{/startIter}} + {{> deserialize_array}} + {{#stopIter}}{{/stopIter}} + {{/array}} + {{#map}} + {{#startIter}}{{/startIter}} + {{> deserialize_map}} + {{#stopIter}}{{/stopIter}} + {{/map}} + {{#object}} + {{> deserialize_object}} + {{/object}} + {{#required}} + _{{name}}Set = true + {{/required}} + reader.Context().Pop() + {{/properties}} } - reader.Context().Pop() + reader.Context().Pop() } - {{#properties}} - {{#required}} - if (!_{{name}}Set) { - panic(reader.Context().PrintWithContext("Missing required property: '{{name}}: {{type}}'")) - } - {{/required}} - {{/properties}} - - return &{{#toUpper}}{{type}}{{/toUpper}}{ - {{#properties}} - {{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}: _{{name}}, - {{/properties}} - } -} \ No newline at end of file + {{#properties}} + {{#required}} + if !_{{name}}Set { + panic(reader.Context().PrintWithContext("Missing required property: '{{name}}: {{type}}'")) + } + {{/required}} + {{/properties}} + return &{{#toUpper}}{{type}}{{/toUpper}}{ + {{#stuctProps}} + {{#properties}} + {{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}: _{{name}}, + {{/properties}} + {{/stuctProps}} + } +} diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/serialize_array.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/serialize_array.mustache index 2a807ea5b0..cc83c569de 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/templates/serialize_array.mustache +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/serialize_array.mustache @@ -1,23 +1,23 @@ if value.{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}{{#lastFullIter}}i{{/lastFullIter}} == nil { - writer.WriteNil() + writer.WriteNil() } else if len(value.{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}{{#lastFullIter}}i{{/lastFullIter}}) == 0 { - writer.WriteNil() + writer.WriteNil() } else { - for {{#nextIter}}i{{/nextIter}} := range value.{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}{{#prevFullIter}}i{{/prevFullIter}} { - {{#scalar}} - {{> serialize_scalar}} - {{/scalar}} - {{#enum}} - {{> serialize_enum}} - {{/enum}} - {{#array}} - {{> serialize_array}} - {{/array}} - {{#map}} - {{> serialize_map_value}} - {{/map}} - {{#object}} - {{> serialize_object}} - {{/object}} - } + for {{#nextIter}}i{{/nextIter}} := range value.{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}{{#prevFullIter}}i{{/prevFullIter}} { + {{#scalar}} + {{> serialize_scalar}} + {{/scalar}} + {{#enum}} + {{> serialize_enum}} + {{/enum}} + {{#array}} + {{> serialize_array}} + {{/array}} + {{#map}} + {{> serialize_map_value}} + {{/map}} + {{#object}} + {{> serialize_object}} + {{/object}} + } } diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/serialize_enum.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/serialize_enum.mustache index 3a5f83b057..cf2d5be270 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/templates/serialize_enum.mustache +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/serialize_enum.mustache @@ -1,13 +1,13 @@ { - v := value.{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}{{#lastFullIter}}i{{/lastFullIter}} - {{#required}} - writer.WriteI32(int32(v)); - {{/required}} - {{^required}} - if v == nil { - writer.WriteNil() - } else { - writer.WriteI32(int32(*v)); - } - {{/required}} + v := value.{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}{{#lastFullIter}}i{{/lastFullIter}} + {{#required}} + writer.WriteI32(int32(v)) + {{/required}} + {{^required}} + if v == nil { + writer.WriteNil() + } else { + writer.WriteI32(int32(*v)) + } + {{/required}} } diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/serialize_map.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/serialize_map.mustache index ea96d27d16..3191f3ad83 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/templates/serialize_map.mustache +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/serialize_map.mustache @@ -1,24 +1,24 @@ if value.{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}} == nil { - writer.WriteNil() + writer.WriteNil() } else if len(value.{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}) == 0 { - writer.WriteNil() + writer.WriteNil() } else { - for {{#nextIter}}i{{/nextIter}} := range value.{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}{{#getPrevIter}}i{{/getPrevIter}} { - writer.Write{{#key}}{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}{{/key}}({{#currIter}}i{{/currIter}}) - {{#scalar}} - {{> serialize_scalar}} - {{/scalar}} - {{#enum}} - {{> serialize_enum}} - {{/enum}} - {{#array}} - {{> serialize_array}} - {{/array}} - {{#map}} - {{> serialize_map}} - {{/map}} - {{#object}} - {{> serialize_object}} - {{/object}} - } + for {{#nextIter}}i{{/nextIter}} := range value.{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}{{#getPrevIter}}i{{/getPrevIter}} { + writer.Write{{#key}}{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}{{/key}}({{#currIter}}i{{/currIter}}) + {{#scalar}} + {{> serialize_scalar}} + {{/scalar}} + {{#enum}} + {{> serialize_enum}} + {{/enum}} + {{#array}} + {{> serialize_array}} + {{/array}} + {{#map}} + {{> serialize_map}} + {{/map}} + {{#object}} + {{> serialize_object}} + {{/object}} + } } diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/serialize_object.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/serialize_object.mustache index 95139e29c6..62c6d07eeb 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/templates/serialize_object.mustache +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/serialize_object.mustache @@ -1,10 +1,9 @@ - { - v := value.{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}{{#lastFullIter}}i{{/lastFullIter}} - {{#required}} - {{#toUpper}}{{type}}{{/toUpper}}Write(writer, &v) - {{/required}} - {{^required}} - {{#toUpper}}{{type}}{{/toUpper}}Write(writer, v) - {{/required}} + v := value.{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}{{#lastFullIter}}i{{/lastFullIter}} + {{#required}} + {{#toUpper}}{{type}}{{/toUpper}}Write(writer, &v) + {{/required}} + {{^required}} + {{#toUpper}}{{type}}{{/toUpper}}Write(writer, v) + {{/required}} } diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/serialize_scalar.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/serialize_scalar.mustache index 256dd0ec43..322a1867ce 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/templates/serialize_scalar.mustache +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/serialize_scalar.mustache @@ -1,13 +1,13 @@ { - v := value.{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}{{#lastFullIter}}i{{/lastFullIter}} - {{#required}} - writer.Write{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}(v) - {{/required}} - {{^required}} - if v == nil { - writer.WriteNil() - } else { - {{#writePointer}}{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}} - v{{/writePointer}} - } - {{/required}} + v := value.{{#toUpper}}{{#handleKeywords}}{{name}}{{/handleKeywords}}{{/toUpper}}{{#lastFullIter}}i{{/lastFullIter}} + {{#required}} + writer.Write{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}(v) + {{/required}} + {{^required}} + if v == nil { + writer.WriteNil() + } else { + {{#writePointer}}{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}} - v{{/writePointer}} + } + {{/required}} } diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_deserialize_array.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_deserialize_array.mustache index 8a852d06e1..808965046e 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_deserialize_array.mustache +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_deserialize_array.mustache @@ -1,23 +1,23 @@ if reader.IsNil() { - value{{#lastFullIter}}i{{/lastFullIter}} = nil + value{{#lastFullIter}}i{{/lastFullIter}} = nil } else { - {{#nextIter}}ln{{/nextIter}} := reader.ReadArrayLength() - value{{#prevFullIter}}i{{/prevFullIter}} = make({{#toWasm}}{{toGraphQLType}}{{/toWasm}}, {{#currIter}}ln{{/currIter}}) - for {{#currIter}}i{{/currIter}} := uint32(0); {{#currIter}}i{{/currIter}} < {{#currIter}}ln{{/currIter}}; {{#currIter}}i{{/currIter}}++ { - {{#scalar}} - {{> deserialize_scalar}} - {{/scalar}} - {{#enum}} - {{> deserialize_enum}} - {{/enum}} - {{#array}} - {{> deserialize_array}} - {{/array}} - {{#map}} - {{> deserialize_map}} - {{/map}} - {{#object}} - {{> deserialize_object}} - {{/object}} - } + {{#nextIter}}ln{{/nextIter}} := reader.ReadArrayLength() + value{{#prevFullIter}}i{{/prevFullIter}} = make({{#toWasm}}{{toGraphQLType}}{{/toWasm}}, {{#currIter}}ln{{/currIter}}) + for {{#currIter}}i{{/currIter}} := uint32(0); {{#currIter}}i{{/currIter}} < {{#currIter}}ln{{/currIter}}; {{#currIter}}i{{/currIter}}++ { + {{#scalar}} + {{> deserialize_scalar}} + {{/scalar}} + {{#enum}} + {{> deserialize_enum}} + {{/enum}} + {{#array}} + {{> deserialize_array}} + {{/array}} + {{#map}} + {{> deserialize_map}} + {{/map}} + {{#object}} + {{> deserialize_object}} + {{/object}} + } } diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_deserialize_enum.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_deserialize_enum.mustache index 72dcb0b7ed..b498c3d4f5 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_deserialize_enum.mustache +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_deserialize_enum.mustache @@ -4,8 +4,8 @@ Sanitize{{#toUpper}}{{type}}{{/toUpper}}Value(int32(_{{name}}{{#lastFullIter}}i{ {{/required}} {{^required}} if !reader.IsNil() { - v := {{#toUpper}}{{type}}{{/toUpper}}(reader.ReadI32()) - Sanitize{{#toUpper}}{{type}}{{/toUpper}}Value(int32(v)) - value{{#lastFullIter}}i{{/lastFullIter}} = &v + v := {{#toUpper}}{{type}}{{/toUpper}}(reader.ReadI32()) + Sanitize{{#toUpper}}{{type}}{{/toUpper}}Value(int32(v)) + value{{#lastFullIter}}i{{/lastFullIter}} = &v } {{/required}} diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_deserialize_map.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_deserialize_map.mustache index 5861374a16..c0eeaa66d6 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_deserialize_map.mustache +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_deserialize_map.mustache @@ -1,24 +1,24 @@ if reader.IsNil() { - value{{#lastFullIter}}i{{/lastFullIter}} = nil + value{{#lastFullIter}}i{{/lastFullIter}} = nil } else { - {{#nextIter}}ln{{/nextIter}} := reader.ReadMapLength() - value{{#prevFullIter}}i{{/prevFullIter}} = make({{#toWasm}}{{toGraphQLType}}{{/toWasm}}) - for {{#currIter}}j{{/currIter}} := uint32(0); {{#currIter}}j{{/currIter}} < {{#currIter}}ln{{/currIter}}; {{#currIter}}j{{/currIter}}++ { - {{#currIter}}i{{/currIter}} := reader.Read{{#key}}{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}{{/key}}() - {{#scalar}} - {{> deserialize_scalar}} - {{/scalar}} - {{#enum}} - {{> deserialize_enum}} - {{/enum}} - {{#array}} - {{> deserialize_array}} - {{/array}} - {{#map}} - {{> deserialize_map}} - {{/map}} - {{#object}} - {{> deserialize_object}} - {{/object}} - } + {{#nextIter}}ln{{/nextIter}} := reader.ReadMapLength() + value{{#prevFullIter}}i{{/prevFullIter}} = make({{#toWasm}}{{toGraphQLType}}{{/toWasm}}) + for {{#currIter}}j{{/currIter}} := uint32(0); {{#currIter}}j{{/currIter}} < {{#currIter}}ln{{/currIter}}; {{#currIter}}j{{/currIter}}++ { + {{#currIter}}i{{/currIter}} := reader.Read{{#key}}{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}{{/key}}() + {{#scalar}} + {{> deserialize_scalar}} + {{/scalar}} + {{#enum}} + {{> deserialize_enum}} + {{/enum}} + {{#array}} + {{> deserialize_array}} + {{/array}} + {{#map}} + {{> deserialize_map}} + {{/map}} + {{#object}} + {{> deserialize_object}} + {{/object}} + } } diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_deserialize_object.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_deserialize_object.mustache index 645435c97c..2a7c26cd0c 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_deserialize_object.mustache +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_deserialize_object.mustache @@ -1,8 +1,8 @@ if v := {{#toUpper}}{{type}}{{/toUpper}}Read(reader); v != nil { - {{#required}} - value{{#lastFullIter}}i{{/lastFullIter}} = *v - {{/required}} - {{^required}} - value{{#lastFullIter}}i{{/lastFullIter}} = v - {{/required}} + {{#required}} + value{{#lastFullIter}}i{{/lastFullIter}} = *v + {{/required}} + {{^required}} + value{{#lastFullIter}}i{{/lastFullIter}} = v + {{/required}} } diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_deserialize_scalar.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_deserialize_scalar.mustache index c917632081..09966bd026 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_deserialize_scalar.mustache +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_deserialize_scalar.mustache @@ -3,7 +3,7 @@ value{{#lastFullIter}}i{{/lastFullIter}} = reader.Read{{#toMsgPack}}{{toGraphQLT {{/required}} {{^required}} if !reader.IsNil() { - v := reader.Read{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}(); - value{{#lastFullIter}}i{{/lastFullIter}} = {{#readPointer}}{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}} - v{{/readPointer}} + v := reader.Read{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}() + value{{#lastFullIter}}i{{/lastFullIter}} = {{#readPointer}}{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}} - v{{/readPointer}} } {{/required}} diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_serialize_array.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_serialize_array.mustache index 9c759d825b..8dd12bb4f4 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_serialize_array.mustache +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_serialize_array.mustache @@ -1,23 +1,23 @@ if value{{#lastFullIter}}i{{/lastFullIter}} == nil { - writer.WriteNil() + writer.WriteNil() } else if len(value{{#lastFullIter}}i{{/lastFullIter}}) == 0 { - writer.WriteNil() + writer.WriteNil() } else { - for {{#nextIter}}i{{/nextIter}} := range value{{#prevFullIter}}i{{/prevFullIter}} { - {{#scalar}} - {{> value_serialize_scalar}} - {{/scalar}} - {{#enum}} - {{> value_serialize_enum}} - {{/enum}} - {{#array}} - {{> value_serialize_array}} - {{/array}} - {{#map}} - {{> value_serialize_map}} - {{/map}} - {{#object}} - {{> value_serialize_object}} - {{/object}} - } + for {{#nextIter}}i{{/nextIter}} := range value{{#prevFullIter}}i{{/prevFullIter}} { + {{#scalar}} + {{> value_serialize_scalar}} + {{/scalar}} + {{#enum}} + {{> value_serialize_enum}} + {{/enum}} + {{#array}} + {{> value_serialize_array}} + {{/array}} + {{#map}} + {{> value_serialize_map}} + {{/map}} + {{#object}} + {{> value_serialize_object}} + {{/object}} + } } diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_serialize_enum.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_serialize_enum.mustache index c6567e8c24..590702b638 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_serialize_enum.mustache +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_serialize_enum.mustache @@ -1,13 +1,13 @@ { - v := value{{#lastFullIter}}i{{/lastFullIter}} - {{#required}} - writer.WriteI32(int32(v)); - {{/required}} - {{^required}} - if v == nil { - writer.WriteNil() - } else { - writer.WriteI32(int32(*v)); - } - {{/required}} + v := value{{#lastFullIter}}i{{/lastFullIter}} + {{#required}} + writer.WriteI32(int32(v)) + {{/required}} + {{^required}} + if v == nil { + writer.WriteNil() + } else { + writer.WriteI32(int32(*v)) + } + {{/required}} } diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_serialize_map.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_serialize_map.mustache index d60bb55b17..41b00f5949 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_serialize_map.mustache +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_serialize_map.mustache @@ -1,24 +1,24 @@ if value == nil { - writer.WriteNil() + writer.WriteNil() } else if len(value) == 0 { - writer.WriteNil() + writer.WriteNil() } else { - for {{#nextIter}}i{{/nextIter}} := range value{{#getPrevIter}}i{{/getPrevIter}} { - writer.Write{{#key}}{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}{{/key}}({{#currIter}}i{{/currIter}}) - {{#scalar}} - {{> value_serialize_scalar}} - {{/scalar}} - {{#enum}} - {{> value_serialize_enum}} - {{/enum}} - {{#array}} - {{> value_serialize_array}} - {{/array}} - {{#map}} - {{> value_serialize_map}} - {{/map}} - {{#object}} - {{> value_serialize_object}} - {{/object}} - } + for {{#nextIter}}i{{/nextIter}} := range value{{#getPrevIter}}i{{/getPrevIter}} { + writer.Write{{#key}}{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}{{/key}}({{#currIter}}i{{/currIter}}) + {{#scalar}} + {{> value_serialize_scalar}} + {{/scalar}} + {{#enum}} + {{> value_serialize_enum}} + {{/enum}} + {{#array}} + {{> value_serialize_array}} + {{/array}} + {{#map}} + {{> value_serialize_map}} + {{/map}} + {{#object}} + {{> value_serialize_object}} + {{/object}} + } } diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_serialize_object.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_serialize_object.mustache index aa5894e0ba..3b6ae4d7c6 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_serialize_object.mustache +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_serialize_object.mustache @@ -1,9 +1,9 @@ { - v := value{{#lastFullIter}}i{{/lastFullIter}} - {{#required}} - {{#toUpper}}{{type}}{{/toUpper}}Write(writer, &v) - {{/required}} - {{^required}} - {{#toUpper}}{{type}}{{/toUpper}}Write(writer, v) - {{/required}} + v := value{{#lastFullIter}}i{{/lastFullIter}} + {{#required}} + {{#toUpper}}{{type}}{{/toUpper}}Write(writer, &v) + {{/required}} + {{^required}} + {{#toUpper}}{{type}}{{/toUpper}}Write(writer, v) + {{/required}} } diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_serialize_scalar.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_serialize_scalar.mustache index 6aa89267b1..440c44f87c 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_serialize_scalar.mustache +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/value_serialize_scalar.mustache @@ -1,13 +1,13 @@ { - v := value{{#lastFullIter}}i{{/lastFullIter}} - {{#required}} - writer.Write{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}(v) - {{/required}} - {{^required}} - if v == nil { - writer.WriteNil() - } else { - {{#writePointer}}{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}} - v{{/writePointer}} - } - {{/required}} + v := value{{#lastFullIter}}i{{/lastFullIter}} + {{#required}} + writer.Write{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}(v) + {{/required}} + {{^required}} + if v == nil { + writer.WriteNil() + } else { + {{#writePointer}}{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}} - v{{/writePointer}} + } + {{/required}} } diff --git a/packages/test-cases/cases/bind/sanity/output/wasm-go/imported/test_import/enum_test_import__enum.go b/packages/test-cases/cases/bind/sanity/output/wasm-go/imported/test_import/enum_test_import__enum.go new file mode 100644 index 0000000000..e70a6de407 --- /dev/null +++ b/packages/test-cases/cases/bind/sanity/output/wasm-go/imported/test_import/enum_test_import__enum.go @@ -0,0 +1,38 @@ +package test_import + +type TestImport_Enum int32 + +const ( + TestImport_EnumSTRING = iota + TestImport_EnumBYTES = iota + testImport_EnumMax = iota +) + +func SanitizeTestImport_EnumValue(value int32) { + if !(value >= 0 && value < int32(testImport_EnumMax)) { + panic("Invalid value for enum 'TestImport_Enum'") + } +} + +func GetTestImport_EnumValue(key string) TestImport_Enum { + switch key { + case "STRING": + return TestImport_EnumSTRING + case "BYTES": + return TestImport_EnumBYTES + default: + panic("Invalid key for enum 'TestImport_Enum'") + } +} + +func GetTestImport_EnumKey(value TestImport_Enum) string { + SanitizeTestImport_EnumValue(int32(value)) + switch value { + case TestImport_EnumSTRING: + return "STRING" + case TestImport_EnumBYTES: + return "BYTES" + default: + panic("Invalid value for enum 'TestImport_Enum'") + } +} diff --git a/packages/test-cases/cases/bind/sanity/output/wasm-go/imported/test_import/env_test_import__env.go b/packages/test-cases/cases/bind/sanity/output/wasm-go/imported/test_import/env_test_import__env.go new file mode 100644 index 0000000000..a82fa76d57 --- /dev/null +++ b/packages/test-cases/cases/bind/sanity/output/wasm-go/imported/test_import/env_test_import__env.go @@ -0,0 +1,25 @@ +package test_import + +import ( + "github.com/consideritdone/polywrap-go/polywrap/msgpack" +) + +type TestImport_Env struct { + EnviroProp string +} + +func TestImport_EnvToBuffer(value *TestImport_Env) []byte { + return serializeTestImport_Env(value) +} + +func TestImport_EnvFromBuffer(data []byte) *TestImport_Env { + return deserializeTestImport_Env(data) +} + +func TestImport_EnvWrite(writer msgpack.Write, value *TestImport_Env) { + writeTestImport_Env(writer, value) +} + +func TestImport_EnvRead(reader msgpack.Read) *TestImport_Env { + return readTestImport_Env(reader) +} diff --git a/packages/test-cases/cases/bind/sanity/output/wasm-go/imported/test_import/env_test_import__env_serialization.go b/packages/test-cases/cases/bind/sanity/output/wasm-go/imported/test_import/env_test_import__env_serialization.go new file mode 100644 index 0000000000..f0bca14901 --- /dev/null +++ b/packages/test-cases/cases/bind/sanity/output/wasm-go/imported/test_import/env_test_import__env_serialization.go @@ -0,0 +1,57 @@ +package test_import + +import ( + "github.com/consideritdone/polywrap-go/polywrap/msgpack" +) + +func serializeTestImport_Env(value *TestImport_Env) []byte { + ctx := msgpack.NewContext("Serializing (encoding) env-type: TestImport_Env") + encoder := msgpack.NewWriteEncoder(ctx) + writeTestImport_Env(encoder, value) + return encoder.Buffer() +} + +func writeTestImport_Env(writer msgpack.Write, value *TestImport_Env) { + writer.WriteMapLength(1) + writer.Context().Push("EnviroProp", "string", "writing property") + writer.WriteString("EnviroProp") + { + v := value.EnviroProp + writer.WriteString(v) + } + writer.Context().Pop() +} + +func deserializeTestImport_Env(data []byte) *TestImport_Env { + ctx := msgpack.NewContext("Deserializing (decoding) env-type: TestImport_Env") + reader := msgpack.NewReadDecoder(ctx, data) + return readTestImport_Env(reader) +} + +func readTestImport_Env(reader msgpack.Read) *TestImport_Env { + var ( + _enviroProp string + _enviroPropSet bool + ) + + for i := int32(reader.ReadMapLength()); i > 0; i-- { + field := reader.ReadString() + reader.Context().Push(field, "unknown", "searching for property type") + switch field { + case "EnviroProp": + reader.Context().Push(field, "string", "type found, reading property") + _enviroProp = reader.ReadString() + _enviroPropSet = true + reader.Context().Pop() + } + reader.Context().Pop() + } + + if !_enviroPropSet { + panic(reader.Context().PrintWithContext("Missing required property: 'enviroProp: String'")) + } + + return &TestImport_Env{ + EnviroProp: _enviroProp, + } +} diff --git a/packages/test-cases/cases/bind/sanity/output/wasm-go/imported/test_import/object_test_import__another_object.go b/packages/test-cases/cases/bind/sanity/output/wasm-go/imported/test_import/object_test_import__another_object.go new file mode 100644 index 0000000000..8e38e568f2 --- /dev/null +++ b/packages/test-cases/cases/bind/sanity/output/wasm-go/imported/test_import/object_test_import__another_object.go @@ -0,0 +1,25 @@ +package test_import + +import ( + "github.com/consideritdone/polywrap-go/polywrap/msgpack" +) + +type TestImport_AnotherObject struct { + Prop string +} + +func TestImport_AnotherObjectToBuffer(value *TestImport_AnotherObject) []byte { + return serializeTestImport_AnotherObject(value) +} + +func TestImport_AnotherObjectFromBuffer(data []byte) *TestImport_AnotherObject { + return deserializeTestImport_AnotherObject(data) +} + +func TestImport_AnotherObjectWrite(writer msgpack.Write, value *TestImport_AnotherObject) { + writeTestImport_AnotherObject(writer, value) +} + +func TestImport_AnotherObjectRead(reader msgpack.Read) *TestImport_AnotherObject { + return readTestImport_AnotherObject(reader) +} diff --git a/packages/test-cases/cases/bind/sanity/output/wasm-go/imported/test_import/object_test_import__another_object_serialization.go b/packages/test-cases/cases/bind/sanity/output/wasm-go/imported/test_import/object_test_import__another_object_serialization.go new file mode 100644 index 0000000000..8fd5893842 --- /dev/null +++ b/packages/test-cases/cases/bind/sanity/output/wasm-go/imported/test_import/object_test_import__another_object_serialization.go @@ -0,0 +1,57 @@ +package test_import + +import ( + "github.com/consideritdone/polywrap-go/polywrap/msgpack" +) + +func serializeTestImport_AnotherObject(value *TestImport_AnotherObject) []byte { + ctx := msgpack.NewContext("Serializing (encoding) env-type: TestImport_AnotherObject") + encoder := msgpack.NewWriteEncoder(ctx) + writeTestImport_AnotherObject(encoder, value) + return encoder.Buffer() +} + +func writeTestImport_AnotherObject(writer msgpack.Write, value *TestImport_AnotherObject) { + writer.WriteMapLength(1) + writer.Context().Push("Prop", "string", "writing property") + writer.WriteString("Prop") + { + v := value.Prop + writer.WriteString(v) + } + writer.Context().Pop() +} + +func deserializeTestImport_AnotherObject(data []byte) *TestImport_AnotherObject { + ctx := msgpack.NewContext("Deserializing (decoding) env-type: TestImport_AnotherObject") + reader := msgpack.NewReadDecoder(ctx, data) + return readTestImport_AnotherObject(reader) +} + +func readTestImport_AnotherObject(reader msgpack.Read) *TestImport_AnotherObject { + var ( + _prop string + _propSet bool + ) + + for i := int32(reader.ReadMapLength()); i > 0; i-- { + field := reader.ReadString() + reader.Context().Push(field, "unknown", "searching for property type") + switch field { + case "Prop": + reader.Context().Push(field, "string", "type found, reading property") + _prop = reader.ReadString() + _propSet = true + reader.Context().Pop() + } + reader.Context().Pop() + } + + if !_propSet { + panic(reader.Context().PrintWithContext("Missing required property: 'prop: String'")) + } + + return &TestImport_AnotherObject{ + Prop: _prop, + } +} diff --git a/packages/test-cases/cases/bind/sanity/output/wasm-go/imported/test_import/object_test_import__object.go b/packages/test-cases/cases/bind/sanity/output/wasm-go/imported/test_import/object_test_import__object.go new file mode 100644 index 0000000000..6b60414f31 --- /dev/null +++ b/packages/test-cases/cases/bind/sanity/output/wasm-go/imported/test_import/object_test_import__object.go @@ -0,0 +1,32 @@ +package test_import + +import ( + "github.com/consideritdone/polywrap-go/polywrap/msgpack" +) + +type TestImport_Object struct { + Object TestImport_AnotherObject + OptObject *TestImport_AnotherObject + ObjectArray []TestImport_AnotherObject + OptObjectArray []*TestImport_AnotherObject + En TestImport_Enum + OptEnum *TestImport_Enum + EnumArray []TestImport_Enum + OptEnumArray []*TestImport_Enum +} + +func TestImport_ObjectToBuffer(value *TestImport_Object) []byte { + return serializeTestImport_Object(value) +} + +func TestImport_ObjectFromBuffer(data []byte) *TestImport_Object { + return deserializeTestImport_Object(data) +} + +func TestImport_ObjectWrite(writer msgpack.Write, value *TestImport_Object) { + writeTestImport_Object(writer, value) +} + +func TestImport_ObjectRead(reader msgpack.Read) *TestImport_Object { + return readTestImport_Object(reader) +} diff --git a/packages/test-cases/cases/bind/sanity/output/wasm-go/imported/test_import/object_test_import__object_serialization.go b/packages/test-cases/cases/bind/sanity/output/wasm-go/imported/test_import/object_test_import__object_serialization.go new file mode 100644 index 0000000000..e9e87399a2 --- /dev/null +++ b/packages/test-cases/cases/bind/sanity/output/wasm-go/imported/test_import/object_test_import__object_serialization.go @@ -0,0 +1,253 @@ +package test_import + +import ( + "github.com/consideritdone/polywrap-go/polywrap/msgpack" +) + +func serializeTestImport_Object(value *TestImport_Object) []byte { + ctx := msgpack.NewContext("Serializing (encoding) env-type: TestImport_Object") + encoder := msgpack.NewWriteEncoder(ctx) + writeTestImport_Object(encoder, value) + return encoder.Buffer() +} + +func writeTestImport_Object(writer msgpack.Write, value *TestImport_Object) { + writer.WriteMapLength(8) + writer.Context().Push("Object", "TestImport_AnotherObject", "writing property") + writer.WriteString("Object") + { + v := value.Object + TestImport_AnotherObjectWrite(writer, &v) + } + writer.Context().Pop() + writer.Context().Push("OptObject", "*TestImport_AnotherObject", "writing property") + writer.WriteString("OptObject") + { + v := value.OptObject + TestImport_AnotherObjectWrite(writer, v) + } + writer.Context().Pop() + writer.Context().Push("ObjectArray", "[]TestImport_AnotherObject", "writing property") + writer.WriteString("ObjectArray") + if value.ObjectArray == nil { + writer.WriteNil() + } else if len(value.ObjectArray) == 0 { + writer.WriteNil() + } else { + for i0 := range value.ObjectArray { + { + v := value.ObjectArray[i0] + TestImport_AnotherObjectWrite(writer, &v) + } + } + } + writer.Context().Pop() + writer.Context().Push("OptObjectArray", "[]*TestImport_AnotherObject", "writing property") + writer.WriteString("OptObjectArray") + if value.OptObjectArray == nil { + writer.WriteNil() + } else if len(value.OptObjectArray) == 0 { + writer.WriteNil() + } else { + for i0 := range value.OptObjectArray { + { + v := value.OptObjectArray[i0] + TestImport_AnotherObjectWrite(writer, v) + } + } + } + writer.Context().Pop() + writer.Context().Push("En", "TestImport_Enum", "writing property") + writer.WriteString("En") + { + v := value.En + writer.WriteI32(int32(v)) + } + writer.Context().Pop() + writer.Context().Push("OptEnum", "*TestImport_Enum", "writing property") + writer.WriteString("OptEnum") + { + v := value.OptEnum + if v == nil { + writer.WriteNil() + } else { + writer.WriteI32(int32(*v)) + } + } + writer.Context().Pop() + writer.Context().Push("EnumArray", "[]TestImport_Enum", "writing property") + writer.WriteString("EnumArray") + if value.EnumArray == nil { + writer.WriteNil() + } else if len(value.EnumArray) == 0 { + writer.WriteNil() + } else { + for i0 := range value.EnumArray { + { + v := value.EnumArray[i0] + writer.WriteI32(int32(v)) + } + } + } + writer.Context().Pop() + writer.Context().Push("OptEnumArray", "[]*TestImport_Enum", "writing property") + writer.WriteString("OptEnumArray") + if value.OptEnumArray == nil { + writer.WriteNil() + } else if len(value.OptEnumArray) == 0 { + writer.WriteNil() + } else { + for i0 := range value.OptEnumArray { + { + v := value.OptEnumArray[i0] + if v == nil { + writer.WriteNil() + } else { + writer.WriteI32(int32(*v)) + } + } + } + } + writer.Context().Pop() +} + +func deserializeTestImport_Object(data []byte) *TestImport_Object { + ctx := msgpack.NewContext("Deserializing (decoding) env-type: TestImport_Object") + reader := msgpack.NewReadDecoder(ctx, data) + return readTestImport_Object(reader) +} + +func readTestImport_Object(reader msgpack.Read) *TestImport_Object { + var ( + _object TestImport_AnotherObject + _objectSet bool + _optObject *TestImport_AnotherObject + _objectArray []TestImport_AnotherObject + _objectArraySet bool + _optObjectArray []*TestImport_AnotherObject + _en TestImport_Enum + _enSet bool + _optEnum *TestImport_Enum + _enumArray []TestImport_Enum + _enumArraySet bool + _optEnumArray []*TestImport_Enum + ) + + for i := int32(reader.ReadMapLength()); i > 0; i-- { + field := reader.ReadString() + reader.Context().Push(field, "unknown", "searching for property type") + switch field { + case "Object": + reader.Context().Push(field, "TestImport_AnotherObject", "type found, reading property") + if v := TestImport_AnotherObjectRead(reader); v != nil { + _object = *v + } + _objectSet = true + reader.Context().Pop() + case "OptObject": + reader.Context().Push(field, "*TestImport_AnotherObject", "type found, reading property") + if v := TestImport_AnotherObjectRead(reader); v != nil { + _optObject = v + } + reader.Context().Pop() + case "ObjectArray": + reader.Context().Push(field, "[]TestImport_AnotherObject", "type found, reading property") + if reader.IsNil() { + _objectArray = nil + } else { + ln0 := reader.ReadArrayLength() + _objectArray = make([]TestImport_AnotherObject, ln0) + for i0 := uint32(0); i0 < ln0; i0++ { + if v := TestImport_AnotherObjectRead(reader); v != nil { + _objectArray[i0] = *v + } + } + } + _objectArraySet = true + reader.Context().Pop() + case "OptObjectArray": + reader.Context().Push(field, "[]*TestImport_AnotherObject", "type found, reading property") + if reader.IsNil() { + _optObjectArray = nil + } else { + ln0 := reader.ReadArrayLength() + _optObjectArray = make([]*TestImport_AnotherObject, ln0) + for i0 := uint32(0); i0 < ln0; i0++ { + if v := TestImport_AnotherObjectRead(reader); v != nil { + _optObjectArray[i0] = v + } + } + } + reader.Context().Pop() + case "En": + reader.Context().Push(field, "TestImport_Enum", "type found, reading property") + _en = TestImport_Enum(reader.ReadI32()) + SanitizeTestImport_EnumValue(int32(_en)) + _enSet = true + reader.Context().Pop() + case "OptEnum": + reader.Context().Push(field, "*TestImport_Enum", "type found, reading property") + if !reader.IsNil() { + v := TestImport_Enum(reader.ReadI32()) + SanitizeTestImport_EnumValue(int32(v)) + _optEnum = &v + } + reader.Context().Pop() + case "EnumArray": + reader.Context().Push(field, "[]TestImport_Enum", "type found, reading property") + if reader.IsNil() { + _enumArray = nil + } else { + ln0 := reader.ReadArrayLength() + _enumArray = make([]TestImport_Enum, ln0) + for i0 := uint32(0); i0 < ln0; i0++ { + _enumArray[i0] = TestImport_Enum(reader.ReadI32()) + SanitizeTestImport_EnumValue(int32(_enumArray[i0])) + } + } + _enumArraySet = true + reader.Context().Pop() + case "OptEnumArray": + reader.Context().Push(field, "[]*TestImport_Enum", "type found, reading property") + if reader.IsNil() { + _optEnumArray = nil + } else { + ln0 := reader.ReadArrayLength() + _optEnumArray = make([]*TestImport_Enum, ln0) + for i0 := uint32(0); i0 < ln0; i0++ { + if !reader.IsNil() { + v := TestImport_Enum(reader.ReadI32()) + SanitizeTestImport_EnumValue(int32(v)) + _optEnumArray[i0] = &v + } + } + } + reader.Context().Pop() + } + reader.Context().Pop() + } + + if !_objectSet { + panic(reader.Context().PrintWithContext("Missing required property: 'object: TestImport_AnotherObject'")) + } + if !_objectArraySet { + panic(reader.Context().PrintWithContext("Missing required property: 'objectArray: [TestImport_AnotherObject]'")) + } + if !_enSet { + panic(reader.Context().PrintWithContext("Missing required property: 'en: TestImport_Enum'")) + } + if !_enumArraySet { + panic(reader.Context().PrintWithContext("Missing required property: 'enumArray: [TestImport_Enum]'")) + } + + return &TestImport_Object{ + Object: _object, + OptObject: _optObject, + ObjectArray: _objectArray, + OptObjectArray: _optObjectArray, + En: _en, + OptEnum: _optEnum, + EnumArray: _enumArray, + OptEnumArray: _optEnumArray, + } +} diff --git a/packages/test-cases/cases/bind/sanity/output/wasm-go/imported/test_import/test_import__module_serialization.go b/packages/test-cases/cases/bind/sanity/output/wasm-go/imported/test_import/test_import__module_serialization.go new file mode 100644 index 0000000000..3189323eff --- /dev/null +++ b/packages/test-cases/cases/bind/sanity/output/wasm-go/imported/test_import/test_import__module_serialization.go @@ -0,0 +1,245 @@ +package test_import + +import ( + "github.com/consideritdone/polywrap-go/polywrap/msgpack" +) + +type ArgsImportedMethod struct { + Str string + OptStr *string + U uint32 + OptU *uint32 + UArrayArray [][]*uint32 + Object TestImport_Object + OptObject *TestImport_Object + ObjectArray []TestImport_Object + OptObjectArray []*TestImport_Object + En TestImport_Enum + OptEnum *TestImport_Enum + EnumArray []TestImport_Enum + OptEnumArray []*TestImport_Enum +} + +func SerializeImportedMethodArgs(value *ArgsImportedMethod) []byte { + ctx := msgpack.NewContext("Serializing module-type: ImportedMethod") + encoder := msgpack.NewWriteEncoder(ctx) + WriteImportedMethodArgs(encoder, value) + return encoder.Buffer() +} + +func WriteImportedMethodArgs(writer msgpack.Write, value *ArgsImportedMethod) { + writer.WriteMapLength(13) + writer.Context().Push("str", "string", "writing property") + writer.WriteString("str") + { + v := value.Str + writer.WriteString(v) + } + writer.Context().Pop() + writer.Context().Push("optStr", "*string", "writing property") + writer.WriteString("optStr") + { + v := value.OptStr + if v == nil { + writer.WriteNil() + } else { + writer.WriteString(*v) + } + } + writer.Context().Pop() + writer.Context().Push("u", "uint32", "writing property") + writer.WriteString("u") + { + v := value.U + writer.WriteU32(v) + } + writer.Context().Pop() + writer.Context().Push("optU", "*uint32", "writing property") + writer.WriteString("optU") + { + v := value.OptU + if v == nil { + writer.WriteNil() + } else { + writer.WriteU32(*v) + } + } + writer.Context().Pop() + writer.Context().Push("uArrayArray", "[][]*uint32", "writing property") + writer.WriteString("uArrayArray") + if value.UArrayArray == nil { + writer.WriteNil() + } else if len(value.UArrayArray) == 0 { + writer.WriteNil() + } else { + for i0 := range value.UArrayArray { + if value.UArrayArray[i0] == nil { + writer.WriteNil() + } else if len(value.UArrayArray[i0]) == 0 { + writer.WriteNil() + } else { + for i1 := range value.UArrayArray[i0] { + { + v := value.UArrayArray[i0][i1] + if v == nil { + writer.WriteNil() + } else { + writer.WriteU32(*v) + } + } + } + } + } + } + writer.Context().Pop() + writer.Context().Push("object", "TestImport_Object", "writing property") + writer.WriteString("object") + { + v := value.Object + TestImport_ObjectWrite(writer, &v) + } + writer.Context().Pop() + writer.Context().Push("optObject", "*TestImport_Object", "writing property") + writer.WriteString("optObject") + { + v := value.OptObject + TestImport_ObjectWrite(writer, v) + } + writer.Context().Pop() + writer.Context().Push("objectArray", "[]TestImport_Object", "writing property") + writer.WriteString("objectArray") + if value.ObjectArray == nil { + writer.WriteNil() + } else if len(value.ObjectArray) == 0 { + writer.WriteNil() + } else { + for i0 := range value.ObjectArray { + { + v := value.ObjectArray[i0] + TestImport_ObjectWrite(writer, &v) + } + } + } + writer.Context().Pop() + writer.Context().Push("optObjectArray", "[]*TestImport_Object", "writing property") + writer.WriteString("optObjectArray") + if value.OptObjectArray == nil { + writer.WriteNil() + } else if len(value.OptObjectArray) == 0 { + writer.WriteNil() + } else { + for i0 := range value.OptObjectArray { + { + v := value.OptObjectArray[i0] + TestImport_ObjectWrite(writer, v) + } + } + } + writer.Context().Pop() + writer.Context().Push("en", "TestImport_Enum", "writing property") + writer.WriteString("en") + { + v := value.En + writer.WriteI32(int32(v)) + } + writer.Context().Pop() + writer.Context().Push("optEnum", "*TestImport_Enum", "writing property") + writer.WriteString("optEnum") + { + v := value.OptEnum + if v == nil { + writer.WriteNil() + } else { + writer.WriteI32(int32(*v)) + } + } + writer.Context().Pop() + writer.Context().Push("enumArray", "[]TestImport_Enum", "writing property") + writer.WriteString("enumArray") + if value.EnumArray == nil { + writer.WriteNil() + } else if len(value.EnumArray) == 0 { + writer.WriteNil() + } else { + for i0 := range value.EnumArray { + { + v := value.EnumArray[i0] + writer.WriteI32(int32(v)) + } + } + } + writer.Context().Pop() + writer.Context().Push("optEnumArray", "[]*TestImport_Enum", "writing property") + writer.WriteString("optEnumArray") + if value.OptEnumArray == nil { + writer.WriteNil() + } else if len(value.OptEnumArray) == 0 { + writer.WriteNil() + } else { + for i0 := range value.OptEnumArray { + { + v := value.OptEnumArray[i0] + if v == nil { + writer.WriteNil() + } else { + writer.WriteI32(int32(*v)) + } + } + } + } + writer.Context().Pop() +} + +func DeserializeImportedMethodResult(argsBuf []byte) *TestImport_Object { + ctx := msgpack.NewContext("Deserializing module-type: ImportedMethod") + reader := msgpack.NewReadDecoder(ctx, argsBuf) + + reader.Context().Push("importedMethod", "*TestImport_Object", "reading function output") + var value *TestImport_Object + if v := TestImport_ObjectRead(reader); v != nil { + value = v + } + reader.Context().Pop() + return value +} + +type ArgsAnotherMethod struct { + Arg []string +} + +func SerializeAnotherMethodArgs(value *ArgsAnotherMethod) []byte { + ctx := msgpack.NewContext("Serializing module-type: AnotherMethod") + encoder := msgpack.NewWriteEncoder(ctx) + WriteAnotherMethodArgs(encoder, value) + return encoder.Buffer() +} + +func WriteAnotherMethodArgs(writer msgpack.Write, value *ArgsAnotherMethod) { + writer.WriteMapLength(1) + writer.Context().Push("arg", "[]string", "writing property") + writer.WriteString("arg") + if value.Arg == nil { + writer.WriteNil() + } else if len(value.Arg) == 0 { + writer.WriteNil() + } else { + for i0 := range value.Arg { + { + v := value.Arg[i0] + writer.WriteString(v) + } + } + } + writer.Context().Pop() +} + +func DeserializeAnotherMethodResult(argsBuf []byte) int32 { + ctx := msgpack.NewContext("Deserializing module-type: AnotherMethod") + reader := msgpack.NewReadDecoder(ctx, argsBuf) + + reader.Context().Push("anotherMethod", "int32", "reading function output") + var value int32 + value = reader.ReadI32() + reader.Context().Pop() + return value +} diff --git a/packages/test-cases/cases/bind/sanity/output/wasm-go/imported/test_import/test_import__module_wrapped.go b/packages/test-cases/cases/bind/sanity/output/wasm-go/imported/test_import/test_import__module_wrapped.go new file mode 100644 index 0000000000..9b45b19f55 --- /dev/null +++ b/packages/test-cases/cases/bind/sanity/output/wasm-go/imported/test_import/test_import__module_wrapped.go @@ -0,0 +1,24 @@ +package test_import + +import ( + "github.com/consideritdone/polywrap-go/polywrap" + "some/library/to/methods" +) + +func MethodImportedMethod(uri string, args *ArgsImportedMethod) (*TestImport_Object, error) { + argsBuf := SerializeImportedMethodArgs(args) + data, err := polywrap.WrapSubinvokeImplementation("testimport.uri.eth", uri, "importedMethod", argsBuf) + if err != nil { + return nil, err + } + return DeserializeImportedMethodResult(data), nil +} + +func MethodAnotherMethod(uri string, args *ArgsAnotherMethod) (int32, error) { + argsBuf := SerializeAnotherMethodArgs(args) + data, err := polywrap.WrapSubinvokeImplementation("testimport.uri.eth", uri, "anotherMethod", argsBuf) + if err != nil { + return nil, err + } + return DeserializeAnotherMethodResult(data), nil +} diff --git a/packages/test-cases/cases/bind/sanity/output/wasm-go/interfaces/test_import.go b/packages/test-cases/cases/bind/sanity/output/wasm-go/interfaces/test_import.go new file mode 100644 index 0000000000..c82ec76871 --- /dev/null +++ b/packages/test-cases/cases/bind/sanity/output/wasm-go/interfaces/test_import.go @@ -0,0 +1,7 @@ +package interfaces + +import "github.com/consideritdone/polywrap-go/polywrap" + +func TestImportImplementations() []string { + return polywrap.WrapGetImplementations("testimport.uri.eth") +} 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 fa5dc8d666..5c9d9a022a 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 @@ -7,15 +7,15 @@ import ( //export _wrap_invoke func _wrap_invoke(methodSize, argsSize, envSize uint32) bool { - args := polywrap.WrapInvokeArgs(methodSize, argsSize) - - if args.Method == "moduleMethod" { + args := polywrap.WrapInvokeArgs(methodSize, argsSize) + switch args.method { + case "moduleMethod": return polywrap.WrapInvoke(args, envSize, module.ModuleMethodWrapped) - } else if args.Method == "objectMethod" { - return polywrap.WrapInvoke(args, envSize, module.ObjectMethodWrapped) - } else if args.Method == "optionalEnvMethod" { - return polywrap.WrapInvoke(args, envSize, module.OptionalEnvMethodWrapped) - } else { + case "objectMethod": + return polywrap.WrapInvoke(args, envSize, module.ObjectMethodWrapped) + case "optionalEnvMethod": + return polywrap.WrapInvoke(args, envSize, module.OptionalEnvMethodWrapped) + default: return polywrap.WrapInvoke(args, envSize, nil) } } diff --git a/packages/test-cases/cases/bind/sanity/output/wasm-go/types/custom_type_serialization.go b/packages/test-cases/cases/bind/sanity/output/wasm-go/types/custom_type_serialization.go deleted file mode 100644 index d986dab75f..0000000000 --- a/packages/test-cases/cases/bind/sanity/output/wasm-go/types/custom_type_serialization.go +++ /dev/null @@ -1,1218 +0,0 @@ -package types - -import ( - "github.com/consideritdone/polywrap-go/polywrap/msgpack" - "github.com/consideritdone/polywrap-go/polywrap/msgpack/big" - "github.com/valyala/fastjson" -) - -func serializeCustomType(value *CustomType) []byte { - ctx := msgpack.NewContext("Serializing (encoding) env-type: CustomType") - encoder := msgpack.NewWriteEncoder(ctx) - writeCustomType(encoder, value) - return encoder.Buffer() -} - -func writeCustomType(writer msgpack.Write, value *CustomType) { - writer.WriteMapLength(41) - writer.Context().Push("Str", "string", "writing property") - writer.WriteString("Str") - { - v := value.Str - writer.WriteString(v) - } - writer.Context().Pop() - writer.Context().Push("OptStr", "*string", "writing property") - writer.WriteString("OptStr") - { - v := value.OptStr - if v == nil { - writer.WriteNil() - } else { - writer.WriteString(*v) - } - } - writer.Context().Pop() - writer.Context().Push("U", "uint32", "writing property") - writer.WriteString("U") - { - v := value.U - writer.WriteU32(v) - } - writer.Context().Pop() - writer.Context().Push("OptU", "*uint32", "writing property") - writer.WriteString("OptU") - { - v := value.OptU - if v == nil { - writer.WriteNil() - } else { - writer.WriteU32(*v) - } - } - writer.Context().Pop() - writer.Context().Push("M_u8", "uint8", "writing property") - writer.WriteString("M_u8") - { - v := value.M_u8 - writer.WriteU8(v) - } - writer.Context().Pop() - writer.Context().Push("M_u16", "uint16", "writing property") - writer.WriteString("M_u16") - { - v := value.M_u16 - writer.WriteU16(v) - } - writer.Context().Pop() - writer.Context().Push("M_u32", "uint32", "writing property") - writer.WriteString("M_u32") - { - v := value.M_u32 - writer.WriteU32(v) - } - writer.Context().Pop() - writer.Context().Push("I", "int32", "writing property") - writer.WriteString("I") - { - v := value.I - writer.WriteI32(v) - } - writer.Context().Pop() - writer.Context().Push("M_i8", "int8", "writing property") - writer.WriteString("M_i8") - { - v := value.M_i8 - writer.WriteI8(v) - } - writer.Context().Pop() - writer.Context().Push("M_i16", "int16", "writing property") - writer.WriteString("M_i16") - { - v := value.M_i16 - writer.WriteI16(v) - } - writer.Context().Pop() - writer.Context().Push("M_i32", "int32", "writing property") - writer.WriteString("M_i32") - { - v := value.M_i32 - writer.WriteI32(v) - } - writer.Context().Pop() - writer.Context().Push("Bigint", "*big.Int", "writing property") - writer.WriteString("Bigint") - { - v := value.Bigint - writer.WriteBigInt(v) - } - writer.Context().Pop() - writer.Context().Push("OptBigint", "*big.Int", "writing property") - writer.WriteString("OptBigint") - { - v := value.OptBigint - if v == nil { - writer.WriteNil() - } else { - writer.WriteBigInt(v) - } - } - writer.Context().Pop() - writer.Context().Push("Bignumber", "*big.Int", "writing property") - writer.WriteString("Bignumber") - { - v := value.Bignumber - writer.WriteBigInt(v) - } - writer.Context().Pop() - writer.Context().Push("OptBignumber", "*big.Int", "writing property") - writer.WriteString("OptBignumber") - { - v := value.OptBignumber - if v == nil { - writer.WriteNil() - } else { - writer.WriteBigInt(v) - } - } - writer.Context().Pop() - writer.Context().Push("Json", "*fastjson.Value", "writing property") - writer.WriteString("Json") - { - v := value.Json - writer.WriteJson(v) - } - writer.Context().Pop() - writer.Context().Push("OptJson", "*fastjson.Value", "writing property") - writer.WriteString("OptJson") - { - v := value.OptJson - if v == nil { - writer.WriteNil() - } else { - writer.WriteJson(v) - } - } - writer.Context().Pop() - writer.Context().Push("Bytes", "[]byte", "writing property") - writer.WriteString("Bytes") - { - v := value.Bytes - writer.WriteBytes(v) - } - writer.Context().Pop() - writer.Context().Push("OptBytes", "[]byte", "writing property") - writer.WriteString("OptBytes") - { - v := value.OptBytes - if v == nil { - writer.WriteNil() - } else { - writer.WriteBytes(v) - } - } - writer.Context().Pop() - writer.Context().Push("M_boolean", "bool", "writing property") - writer.WriteString("M_boolean") - { - v := value.M_boolean - writer.WriteBool(v) - } - writer.Context().Pop() - writer.Context().Push("OptBoolean", "*bool", "writing property") - writer.WriteString("OptBoolean") - { - v := value.OptBoolean - if v == nil { - writer.WriteNil() - } else { - writer.WriteBool(*v) - } - } - writer.Context().Pop() - writer.Context().Push("UArray", "[]uint32", "writing property") - writer.WriteString("UArray") - if value.UArray == nil { - writer.WriteNil() - } else if len(value.UArray) == 0 { - writer.WriteNil() - } else { - for i0 := range value.UArray { - { - v := value.UArray[i0] - writer.WriteU32(v) - } - } - } - writer.Context().Pop() - writer.Context().Push("UOptArray", "[]uint32", "writing property") - writer.WriteString("UOptArray") - if value.UOptArray == nil { - writer.WriteNil() - } else if len(value.UOptArray) == 0 { - writer.WriteNil() - } else { - for i0 := range value.UOptArray { - { - v := value.UOptArray[i0] - writer.WriteU32(v) - } - } - } - writer.Context().Pop() - writer.Context().Push("OptUOptArray", "[]*uint32", "writing property") - writer.WriteString("OptUOptArray") - if value.OptUOptArray == nil { - writer.WriteNil() - } else if len(value.OptUOptArray) == 0 { - writer.WriteNil() - } else { - for i0 := range value.OptUOptArray { - { - v := value.OptUOptArray[i0] - if v == nil { - writer.WriteNil() - } else { - writer.WriteU32(*v) - } - } - } - } - writer.Context().Pop() - writer.Context().Push("OptStrOptArray", "[]*string", "writing property") - writer.WriteString("OptStrOptArray") - if value.OptStrOptArray == nil { - writer.WriteNil() - } else if len(value.OptStrOptArray) == 0 { - writer.WriteNil() - } else { - for i0 := range value.OptStrOptArray { - { - v := value.OptStrOptArray[i0] - if v == nil { - writer.WriteNil() - } else { - writer.WriteString(*v) - } - } - } - } - writer.Context().Pop() - writer.Context().Push("UArrayArray", "[][]uint32", "writing property") - writer.WriteString("UArrayArray") - if value.UArrayArray == nil { - writer.WriteNil() - } else if len(value.UArrayArray) == 0 { - writer.WriteNil() - } else { - for i0 := range value.UArrayArray { - if value.UArrayArray[i0] == nil { - writer.WriteNil() - } else if len(value.UArrayArray[i0]) == 0 { - writer.WriteNil() - } else { - for i1 := range value.UArrayArray[i0] { - { - v := value.UArrayArray[i0][i1] - writer.WriteU32(v) - } - } - } - } - } - writer.Context().Pop() - writer.Context().Push("UOptArrayOptArray", "[][]*uint32", "writing property") - writer.WriteString("UOptArrayOptArray") - if value.UOptArrayOptArray == nil { - writer.WriteNil() - } else if len(value.UOptArrayOptArray) == 0 { - writer.WriteNil() - } else { - for i0 := range value.UOptArrayOptArray { - if value.UOptArrayOptArray[i0] == nil { - writer.WriteNil() - } else if len(value.UOptArrayOptArray[i0]) == 0 { - writer.WriteNil() - } else { - for i1 := range value.UOptArrayOptArray[i0] { - { - v := value.UOptArrayOptArray[i0][i1] - if v == nil { - writer.WriteNil() - } else { - writer.WriteU32(*v) - } - } - } - } - } - } - writer.Context().Pop() - writer.Context().Push("UArrayOptArrayArray", "[][][]uint32", "writing property") - writer.WriteString("UArrayOptArrayArray") - if value.UArrayOptArrayArray == nil { - writer.WriteNil() - } else if len(value.UArrayOptArrayArray) == 0 { - writer.WriteNil() - } else { - for i0 := range value.UArrayOptArrayArray { - if value.UArrayOptArrayArray[i0] == nil { - writer.WriteNil() - } else if len(value.UArrayOptArrayArray[i0]) == 0 { - writer.WriteNil() - } else { - for i1 := range value.UArrayOptArrayArray[i0] { - if value.UArrayOptArrayArray[i0][i1] == nil { - writer.WriteNil() - } else if len(value.UArrayOptArrayArray[i0][i1]) == 0 { - writer.WriteNil() - } else { - for i2 := range value.UArrayOptArrayArray[i0][i1] { - { - v := value.UArrayOptArrayArray[i0][i1][i2] - writer.WriteU32(v) - } - } - } - } - } - } - } - writer.Context().Pop() - writer.Context().Push("CrazyArray", "[][][][]uint32", "writing property") - writer.WriteString("CrazyArray") - if value.CrazyArray == nil { - writer.WriteNil() - } else if len(value.CrazyArray) == 0 { - writer.WriteNil() - } else { - for i0 := range value.CrazyArray { - if value.CrazyArray[i0] == nil { - writer.WriteNil() - } else if len(value.CrazyArray[i0]) == 0 { - writer.WriteNil() - } else { - for i1 := range value.CrazyArray[i0] { - if value.CrazyArray[i0][i1] == nil { - writer.WriteNil() - } else if len(value.CrazyArray[i0][i1]) == 0 { - writer.WriteNil() - } else { - for i2 := range value.CrazyArray[i0][i1] { - if value.CrazyArray[i0][i1][i2] == nil { - writer.WriteNil() - } else if len(value.CrazyArray[i0][i1][i2]) == 0 { - writer.WriteNil() - } else { - for i3 := range value.CrazyArray[i0][i1][i2] { - { - v := value.CrazyArray[i0][i1][i2][i3] - writer.WriteU32(v) - } - } - } - } - } - } - } - } - } - writer.Context().Pop() - writer.Context().Push("Object", "AnotherType", "writing property") - writer.WriteString("Object") - - { - v := value.Object - AnotherTypeWrite(writer, &v) - } - writer.Context().Pop() - writer.Context().Push("OptObject", "*AnotherType", "writing property") - writer.WriteString("OptObject") - - { - v := value.OptObject - AnotherTypeWrite(writer, v) - } - writer.Context().Pop() - writer.Context().Push("ObjectArray", "[]AnotherType", "writing property") - writer.WriteString("ObjectArray") - if value.ObjectArray == nil { - writer.WriteNil() - } else if len(value.ObjectArray) == 0 { - writer.WriteNil() - } else { - for i0 := range value.ObjectArray { - - { - v := value.ObjectArray[i0] - AnotherTypeWrite(writer, &v) - } - } - } - writer.Context().Pop() - writer.Context().Push("OptObjectArray", "[]*AnotherType", "writing property") - writer.WriteString("OptObjectArray") - if value.OptObjectArray == nil { - writer.WriteNil() - } else if len(value.OptObjectArray) == 0 { - writer.WriteNil() - } else { - for i0 := range value.OptObjectArray { - - { - v := value.OptObjectArray[i0] - AnotherTypeWrite(writer, v) - } - } - } - writer.Context().Pop() - writer.Context().Push("En", "CustomEnum", "writing property") - writer.WriteString("En") - { - v := value.En - writer.WriteI32(int32(v)); - } - writer.Context().Pop() - writer.Context().Push("OptEnum", "*CustomEnum", "writing property") - writer.WriteString("OptEnum") - { - v := value.OptEnum - if v == nil { - writer.WriteNil() - } else { - writer.WriteI32(int32(*v)); - } - } - writer.Context().Pop() - writer.Context().Push("EnumArray", "[]CustomEnum", "writing property") - writer.WriteString("EnumArray") - if value.EnumArray == nil { - writer.WriteNil() - } else if len(value.EnumArray) == 0 { - writer.WriteNil() - } else { - for i0 := range value.EnumArray { - { - v := value.EnumArray[i0] - writer.WriteI32(int32(v)); - } - } - } - writer.Context().Pop() - writer.Context().Push("OptEnumArray", "[]*CustomEnum", "writing property") - writer.WriteString("OptEnumArray") - if value.OptEnumArray == nil { - writer.WriteNil() - } else if len(value.OptEnumArray) == 0 { - writer.WriteNil() - } else { - for i0 := range value.OptEnumArray { - { - v := value.OptEnumArray[i0] - if v == nil { - writer.WriteNil() - } else { - writer.WriteI32(int32(*v)); - } - } - } - } - writer.Context().Pop() - writer.Context().Push("Map", "map[string]int32", "writing property") - writer.WriteString("Map") - if value.Map == nil { - writer.WriteNil() - } else if len(value.Map) == 0 { - writer.WriteNil() - } else { - for i0 := range value.Map { - writer.WriteString(i0) - { - v := value.Map[i0] - writer.WriteI32(v) - } - } - } - writer.Context().Pop() - writer.Context().Push("MapOfArr", "map[string][]int32", "writing property") - writer.WriteString("MapOfArr") - if value.MapOfArr == nil { - writer.WriteNil() - } else if len(value.MapOfArr) == 0 { - writer.WriteNil() - } else { - for i0 := range value.MapOfArr { - writer.WriteString(i0) - if value.MapOfArr[i0] == nil { - writer.WriteNil() - } else if len(value.MapOfArr[i0]) == 0 { - writer.WriteNil() - } else { - for i1 := range value.MapOfArr[i0] { - { - v := value.MapOfArr[i0][i1] - writer.WriteI32(v) - } - } - } - } - } - writer.Context().Pop() - writer.Context().Push("MapOfObj", "map[string]AnotherType", "writing property") - writer.WriteString("MapOfObj") - if value.MapOfObj == nil { - writer.WriteNil() - } else if len(value.MapOfObj) == 0 { - writer.WriteNil() - } else { - for i0 := range value.MapOfObj { - writer.WriteString(i0) - - { - v := value.MapOfObj[i0] - AnotherTypeWrite(writer, &v) - } - } - } - writer.Context().Pop() - writer.Context().Push("MapOfArrOfObj", "map[string][]AnotherType", "writing property") - writer.WriteString("MapOfArrOfObj") - if value.MapOfArrOfObj == nil { - writer.WriteNil() - } else if len(value.MapOfArrOfObj) == 0 { - writer.WriteNil() - } else { - for i0 := range value.MapOfArrOfObj { - writer.WriteString(i0) - if value.MapOfArrOfObj[i0] == nil { - writer.WriteNil() - } else if len(value.MapOfArrOfObj[i0]) == 0 { - writer.WriteNil() - } else { - for i1 := range value.MapOfArrOfObj[i0] { - - { - v := value.MapOfArrOfObj[i0][i1] - AnotherTypeWrite(writer, &v) - } - } - } - } - } - writer.Context().Pop() -} - -func deserializeCustomType(data []byte) *CustomType { - ctx := msgpack.NewContext("Deserializing (decoding) env-type: CustomType") - reader := msgpack.NewReadDecoder(ctx, data) - return readCustomType(reader) -} - -func readCustomType(reader msgpack.Read) *CustomType { - var ( - _str string - _strSet bool - _optStr *string - _u uint32 - _uSet bool - _optU *uint32 - _u8 uint8 - _u8Set bool - _u16 uint16 - _u16Set bool - _u32 uint32 - _u32Set bool - _i int32 - _iSet bool - _i8 int8 - _i8Set bool - _i16 int16 - _i16Set bool - _i32 int32 - _i32Set bool - _bigint *big.Int - _bigintSet bool - _optBigint *big.Int - _bignumber *big.Int - _bignumberSet bool - _optBignumber *big.Int - _json *fastjson.Value - _jsonSet bool - _optJson *fastjson.Value - _bytes []byte - _bytesSet bool - _optBytes []byte - _boolean bool - _booleanSet bool - _optBoolean *bool - _uArray []uint32 - _uArraySet bool - _uOptArray []uint32 - _optUOptArray []*uint32 - _optStrOptArray []*string - _uArrayArray [][]uint32 - _uArrayArraySet bool - _uOptArrayOptArray [][]*uint32 - _uOptArrayOptArraySet bool - _uArrayOptArrayArray [][][]uint32 - _uArrayOptArrayArraySet bool - _crazyArray [][][][]uint32 - _object AnotherType - _objectSet bool - _optObject *AnotherType - _objectArray []AnotherType - _objectArraySet bool - _optObjectArray []*AnotherType - _en CustomEnum - _enSet bool - _optEnum *CustomEnum - _enumArray []CustomEnum - _enumArraySet bool - _optEnumArray []*CustomEnum - _map map[string]int32 - _mapSet bool - _mapOfArr map[string][]int32 - _mapOfArrSet bool - _mapOfObj map[string]AnotherType - _mapOfObjSet bool - _mapOfArrOfObj map[string][]AnotherType - _mapOfArrOfObjSet bool - ) - - for i := int32(reader.ReadMapLength()); i > 0; i-- { - field := reader.ReadString() - reader.Context().Push(field, "unknown", "searching for property type"); - switch field { - case "Str": - reader.Context().Push(field, "string", "type found, reading property") - _str = reader.ReadString() - _strSet = true; - reader.Context().Pop() - case "OptStr": - reader.Context().Push(field, "*string", "type found, reading property") - if !reader.IsNil() { - v := reader.ReadString(); - _optStr = &v - } - reader.Context().Pop() - case "U": - reader.Context().Push(field, "uint32", "type found, reading property") - _u = reader.ReadU32() - _uSet = true; - reader.Context().Pop() - case "OptU": - reader.Context().Push(field, "*uint32", "type found, reading property") - if !reader.IsNil() { - v := reader.ReadU32(); - _optU = &v - } - reader.Context().Pop() - case "M_u8": - reader.Context().Push(field, "uint8", "type found, reading property") - _u8 = reader.ReadU8() - _u8Set = true; - reader.Context().Pop() - case "M_u16": - reader.Context().Push(field, "uint16", "type found, reading property") - _u16 = reader.ReadU16() - _u16Set = true; - reader.Context().Pop() - case "M_u32": - reader.Context().Push(field, "uint32", "type found, reading property") - _u32 = reader.ReadU32() - _u32Set = true; - reader.Context().Pop() - case "I": - reader.Context().Push(field, "int32", "type found, reading property") - _i = reader.ReadI32() - _iSet = true; - reader.Context().Pop() - case "M_i8": - reader.Context().Push(field, "int8", "type found, reading property") - _i8 = reader.ReadI8() - _i8Set = true; - reader.Context().Pop() - case "M_i16": - reader.Context().Push(field, "int16", "type found, reading property") - _i16 = reader.ReadI16() - _i16Set = true; - reader.Context().Pop() - case "M_i32": - reader.Context().Push(field, "int32", "type found, reading property") - _i32 = reader.ReadI32() - _i32Set = true; - reader.Context().Pop() - case "Bigint": - reader.Context().Push(field, "*big.Int", "type found, reading property") - _bigint = reader.ReadBigInt() - _bigintSet = true; - reader.Context().Pop() - case "OptBigint": - reader.Context().Push(field, "*big.Int", "type found, reading property") - if !reader.IsNil() { - v := reader.ReadBigInt(); - _optBigint = v - } - reader.Context().Pop() - case "Bignumber": - reader.Context().Push(field, "*big.Int", "type found, reading property") - _bignumber = reader.ReadBigInt() - _bignumberSet = true; - reader.Context().Pop() - case "OptBignumber": - reader.Context().Push(field, "*big.Int", "type found, reading property") - if !reader.IsNil() { - v := reader.ReadBigInt(); - _optBignumber = v - } - reader.Context().Pop() - case "Json": - reader.Context().Push(field, "*fastjson.Value", "type found, reading property") - _json = reader.ReadJson() - _jsonSet = true; - reader.Context().Pop() - case "OptJson": - reader.Context().Push(field, "*fastjson.Value", "type found, reading property") - if !reader.IsNil() { - v := reader.ReadJson(); - _optJson = v - } - reader.Context().Pop() - case "Bytes": - reader.Context().Push(field, "[]byte", "type found, reading property") - _bytes = reader.ReadBytes() - _bytesSet = true; - reader.Context().Pop() - case "OptBytes": - reader.Context().Push(field, "[]byte", "type found, reading property") - if !reader.IsNil() { - v := reader.ReadBytes(); - _optBytes = v - } - reader.Context().Pop() - case "M_boolean": - reader.Context().Push(field, "bool", "type found, reading property") - _boolean = reader.ReadBool() - _booleanSet = true; - reader.Context().Pop() - case "OptBoolean": - reader.Context().Push(field, "*bool", "type found, reading property") - if !reader.IsNil() { - v := reader.ReadBool(); - _optBoolean = &v - } - reader.Context().Pop() - case "UArray": - reader.Context().Push(field, "[]uint32", "type found, reading property") - if reader.IsNil() { - _uArray = nil - } else { - ln0 := reader.ReadArrayLength() - _uArray = make([]uint32, ln0) - for i0 := uint32(0); i0 < ln0; i0++ { - _uArray[i0] = reader.ReadU32() - } - } - _uArraySet = true; - reader.Context().Pop() - case "UOptArray": - reader.Context().Push(field, "[]uint32", "type found, reading property") - if reader.IsNil() { - _uOptArray = nil - } else { - ln0 := reader.ReadArrayLength() - _uOptArray = make([]uint32, ln0) - for i0 := uint32(0); i0 < ln0; i0++ { - _uOptArray[i0] = reader.ReadU32() - } - } - reader.Context().Pop() - case "OptUOptArray": - reader.Context().Push(field, "[]*uint32", "type found, reading property") - if reader.IsNil() { - _optUOptArray = nil - } else { - ln0 := reader.ReadArrayLength() - _optUOptArray = make([]*uint32, ln0) - for i0 := uint32(0); i0 < ln0; i0++ { - if !reader.IsNil() { - v := reader.ReadU32(); - _optUOptArray[i0] = &v - } - } - } - reader.Context().Pop() - case "OptStrOptArray": - reader.Context().Push(field, "[]*string", "type found, reading property") - if reader.IsNil() { - _optStrOptArray = nil - } else { - ln0 := reader.ReadArrayLength() - _optStrOptArray = make([]*string, ln0) - for i0 := uint32(0); i0 < ln0; i0++ { - if !reader.IsNil() { - v := reader.ReadString(); - _optStrOptArray[i0] = &v - } - } - } - reader.Context().Pop() - case "UArrayArray": - reader.Context().Push(field, "[][]uint32", "type found, reading property") - if reader.IsNil() { - _uArrayArray = nil - } else { - ln0 := reader.ReadArrayLength() - _uArrayArray = make([][]uint32, ln0) - for i0 := uint32(0); i0 < ln0; i0++ { - if reader.IsNil() { - _uArrayArray[i0] = nil - } else { - ln1 := reader.ReadArrayLength() - _uArrayArray[i0] = make([]uint32, ln1) - for i1 := uint32(0); i1 < ln1; i1++ { - _uArrayArray[i0][i1] = reader.ReadU32() - } - } - } - } - _uArrayArraySet = true; - reader.Context().Pop() - case "UOptArrayOptArray": - reader.Context().Push(field, "[][]*uint32", "type found, reading property") - if reader.IsNil() { - _uOptArrayOptArray = nil - } else { - ln0 := reader.ReadArrayLength() - _uOptArrayOptArray = make([][]*uint32, ln0) - for i0 := uint32(0); i0 < ln0; i0++ { - if reader.IsNil() { - _uOptArrayOptArray[i0] = nil - } else { - ln1 := reader.ReadArrayLength() - _uOptArrayOptArray[i0] = make([]*uint32, ln1) - for i1 := uint32(0); i1 < ln1; i1++ { - if !reader.IsNil() { - v := reader.ReadU32(); - _uOptArrayOptArray[i0][i1] = &v - } - } - } - } - } - _uOptArrayOptArraySet = true; - reader.Context().Pop() - case "UArrayOptArrayArray": - reader.Context().Push(field, "[][][]uint32", "type found, reading property") - if reader.IsNil() { - _uArrayOptArrayArray = nil - } else { - ln0 := reader.ReadArrayLength() - _uArrayOptArrayArray = make([][][]uint32, ln0) - for i0 := uint32(0); i0 < ln0; i0++ { - if reader.IsNil() { - _uArrayOptArrayArray[i0] = nil - } else { - ln1 := reader.ReadArrayLength() - _uArrayOptArrayArray[i0] = make([][]uint32, ln1) - for i1 := uint32(0); i1 < ln1; i1++ { - if reader.IsNil() { - _uArrayOptArrayArray[i0][i1] = nil - } else { - ln2 := reader.ReadArrayLength() - _uArrayOptArrayArray[i0][i1] = make([]uint32, ln2) - for i2 := uint32(0); i2 < ln2; i2++ { - _uArrayOptArrayArray[i0][i1][i2] = reader.ReadU32() - } - } - } - } - } - } - _uArrayOptArrayArraySet = true; - reader.Context().Pop() - case "CrazyArray": - reader.Context().Push(field, "[][][][]uint32", "type found, reading property") - if reader.IsNil() { - _crazyArray = nil - } else { - ln0 := reader.ReadArrayLength() - _crazyArray = make([][][][]uint32, ln0) - for i0 := uint32(0); i0 < ln0; i0++ { - if reader.IsNil() { - _crazyArray[i0] = nil - } else { - ln1 := reader.ReadArrayLength() - _crazyArray[i0] = make([][][]uint32, ln1) - for i1 := uint32(0); i1 < ln1; i1++ { - if reader.IsNil() { - _crazyArray[i0][i1] = nil - } else { - ln2 := reader.ReadArrayLength() - _crazyArray[i0][i1] = make([][]uint32, ln2) - for i2 := uint32(0); i2 < ln2; i2++ { - if reader.IsNil() { - _crazyArray[i0][i1][i2] = nil - } else { - ln3 := reader.ReadArrayLength() - _crazyArray[i0][i1][i2] = make([]uint32, ln3) - for i3 := uint32(0); i3 < ln3; i3++ { - _crazyArray[i0][i1][i2][i3] = reader.ReadU32() - } - } - } - } - } - } - } - } - reader.Context().Pop() - case "Object": - reader.Context().Push(field, "AnotherType", "type found, reading property") - if v := AnotherTypeRead(reader); v != nil { - _object = *v - } - _objectSet = true; - reader.Context().Pop() - case "OptObject": - reader.Context().Push(field, "*AnotherType", "type found, reading property") - if v := AnotherTypeRead(reader); v != nil { - _optObject = v - } - reader.Context().Pop() - case "ObjectArray": - reader.Context().Push(field, "[]AnotherType", "type found, reading property") - if reader.IsNil() { - _objectArray = nil - } else { - ln0 := reader.ReadArrayLength() - _objectArray = make([]AnotherType, ln0) - for i0 := uint32(0); i0 < ln0; i0++ { - if v := AnotherTypeRead(reader); v != nil { - _objectArray[i0] = *v - } - } - } - _objectArraySet = true; - reader.Context().Pop() - case "OptObjectArray": - reader.Context().Push(field, "[]*AnotherType", "type found, reading property") - if reader.IsNil() { - _optObjectArray = nil - } else { - ln0 := reader.ReadArrayLength() - _optObjectArray = make([]*AnotherType, ln0) - for i0 := uint32(0); i0 < ln0; i0++ { - if v := AnotherTypeRead(reader); v != nil { - _optObjectArray[i0] = v - } - } - } - reader.Context().Pop() - case "En": - reader.Context().Push(field, "CustomEnum", "type found, reading property") - _en = CustomEnum(reader.ReadI32()) - SanitizeCustomEnumValue(int32(_en)) - _enSet = true; - reader.Context().Pop() - case "OptEnum": - reader.Context().Push(field, "*CustomEnum", "type found, reading property") - if !reader.IsNil() { - v := CustomEnum(reader.ReadI32()) - SanitizeCustomEnumValue(int32(v)) - _optEnum = &v - } - reader.Context().Pop() - case "EnumArray": - reader.Context().Push(field, "[]CustomEnum", "type found, reading property") - if reader.IsNil() { - _enumArray = nil - } else { - ln0 := reader.ReadArrayLength() - _enumArray = make([]CustomEnum, ln0) - for i0 := uint32(0); i0 < ln0; i0++ { - _enumArray[i0] = CustomEnum(reader.ReadI32()) - SanitizeCustomEnumValue(int32(_enumArray[i0])) - } - } - _enumArraySet = true; - reader.Context().Pop() - case "OptEnumArray": - reader.Context().Push(field, "[]*CustomEnum", "type found, reading property") - if reader.IsNil() { - _optEnumArray = nil - } else { - ln0 := reader.ReadArrayLength() - _optEnumArray = make([]*CustomEnum, ln0) - for i0 := uint32(0); i0 < ln0; i0++ { - if !reader.IsNil() { - v := CustomEnum(reader.ReadI32()) - SanitizeCustomEnumValue(int32(v)) - _optEnumArray[i0] = &v - } - } - } - reader.Context().Pop() - case "Map": - reader.Context().Push(field, "map[string]int32", "type found, reading property") - if reader.IsNil() { - _map = nil - } else { - ln0 := reader.ReadMapLength() - _map = make(map[string]int32) - for j0 := uint32(0); j0 < ln0; j0++ { - i0 := reader.ReadString() - _map[i0] = reader.ReadI32() - } - } - _mapSet = true; - reader.Context().Pop() - case "MapOfArr": - reader.Context().Push(field, "map[string][]int32", "type found, reading property") - if reader.IsNil() { - _mapOfArr = nil - } else { - ln0 := reader.ReadMapLength() - _mapOfArr = make(map[string][]int32) - for j0 := uint32(0); j0 < ln0; j0++ { - i0 := reader.ReadString() - if reader.IsNil() { - _mapOfArr[i0] = nil - } else { - ln1 := reader.ReadArrayLength() - _mapOfArr[i0] = make([]int32, ln1) - for i1 := uint32(0); i1 < ln1; i1++ { - _mapOfArr[i0][i1] = reader.ReadI32() - } - } - } - } - _mapOfArrSet = true; - reader.Context().Pop() - case "MapOfObj": - reader.Context().Push(field, "map[string]AnotherType", "type found, reading property") - if reader.IsNil() { - _mapOfObj = nil - } else { - ln0 := reader.ReadMapLength() - _mapOfObj = make(map[string]AnotherType) - for j0 := uint32(0); j0 < ln0; j0++ { - i0 := reader.ReadString() - if v := AnotherTypeRead(reader); v != nil { - _mapOfObj[i0] = *v - } - } - } - _mapOfObjSet = true; - reader.Context().Pop() - case "MapOfArrOfObj": - reader.Context().Push(field, "map[string][]AnotherType", "type found, reading property") - if reader.IsNil() { - _mapOfArrOfObj = nil - } else { - ln0 := reader.ReadMapLength() - _mapOfArrOfObj = make(map[string][]AnotherType) - for j0 := uint32(0); j0 < ln0; j0++ { - i0 := reader.ReadString() - if reader.IsNil() { - _mapOfArrOfObj[i0] = nil - } else { - ln1 := reader.ReadArrayLength() - _mapOfArrOfObj[i0] = make([]AnotherType, ln1) - for i1 := uint32(0); i1 < ln1; i1++ { - if v := AnotherTypeRead(reader); v != nil { - _mapOfArrOfObj[i0][i1] = *v - } - } - } - } - } - _mapOfArrOfObjSet = true; - reader.Context().Pop() - } - reader.Context().Pop() - } - - if (!_strSet) { - panic(reader.Context().PrintWithContext("Missing required property: 'str: String'")) - } - if (!_uSet) { - panic(reader.Context().PrintWithContext("Missing required property: 'u: UInt'")) - } - if (!_u8Set) { - panic(reader.Context().PrintWithContext("Missing required property: 'u8: UInt8'")) - } - if (!_u16Set) { - panic(reader.Context().PrintWithContext("Missing required property: 'u16: UInt16'")) - } - if (!_u32Set) { - panic(reader.Context().PrintWithContext("Missing required property: 'u32: UInt32'")) - } - if (!_iSet) { - panic(reader.Context().PrintWithContext("Missing required property: 'i: Int'")) - } - if (!_i8Set) { - panic(reader.Context().PrintWithContext("Missing required property: 'i8: Int8'")) - } - if (!_i16Set) { - panic(reader.Context().PrintWithContext("Missing required property: 'i16: Int16'")) - } - if (!_i32Set) { - panic(reader.Context().PrintWithContext("Missing required property: 'i32: Int32'")) - } - if (!_bigintSet) { - panic(reader.Context().PrintWithContext("Missing required property: 'bigint: BigInt'")) - } - if (!_bignumberSet) { - panic(reader.Context().PrintWithContext("Missing required property: 'bignumber: BigNumber'")) - } - if (!_jsonSet) { - panic(reader.Context().PrintWithContext("Missing required property: 'json: JSON'")) - } - if (!_bytesSet) { - panic(reader.Context().PrintWithContext("Missing required property: 'bytes: Bytes'")) - } - if (!_booleanSet) { - panic(reader.Context().PrintWithContext("Missing required property: 'boolean: Boolean'")) - } - if (!_uArraySet) { - panic(reader.Context().PrintWithContext("Missing required property: 'uArray: [UInt]'")) - } - if (!_uArrayArraySet) { - panic(reader.Context().PrintWithContext("Missing required property: 'uArrayArray: [[UInt]]'")) - } - if (!_uOptArrayOptArraySet) { - panic(reader.Context().PrintWithContext("Missing required property: 'uOptArrayOptArray: [[UInt32]]'")) - } - if (!_uArrayOptArrayArraySet) { - panic(reader.Context().PrintWithContext("Missing required property: 'uArrayOptArrayArray: [[[UInt32]]]'")) - } - if (!_objectSet) { - panic(reader.Context().PrintWithContext("Missing required property: 'object: AnotherType'")) - } - if (!_objectArraySet) { - panic(reader.Context().PrintWithContext("Missing required property: 'objectArray: [AnotherType]'")) - } - if (!_enSet) { - panic(reader.Context().PrintWithContext("Missing required property: 'en: CustomEnum'")) - } - if (!_enumArraySet) { - panic(reader.Context().PrintWithContext("Missing required property: 'enumArray: [CustomEnum]'")) - } - if (!_mapSet) { - panic(reader.Context().PrintWithContext("Missing required property: 'map: Map'")) - } - if (!_mapOfArrSet) { - panic(reader.Context().PrintWithContext("Missing required property: 'mapOfArr: Map'")) - } - if (!_mapOfObjSet) { - panic(reader.Context().PrintWithContext("Missing required property: 'mapOfObj: Map'")) - } - if (!_mapOfArrOfObjSet) { - panic(reader.Context().PrintWithContext("Missing required property: 'mapOfArrOfObj: Map'")) - } - - return &CustomType{ - Str: _str, - OptStr: _optStr, - U: _u, - OptU: _optU, - M_u8: _u8, - M_u16: _u16, - M_u32: _u32, - I: _i, - M_i8: _i8, - M_i16: _i16, - M_i32: _i32, - Bigint: _bigint, - OptBigint: _optBigint, - Bignumber: _bignumber, - OptBignumber: _optBignumber, - Json: _json, - OptJson: _optJson, - Bytes: _bytes, - OptBytes: _optBytes, - M_boolean: _boolean, - OptBoolean: _optBoolean, - UArray: _uArray, - UOptArray: _uOptArray, - OptUOptArray: _optUOptArray, - OptStrOptArray: _optStrOptArray, - UArrayArray: _uArrayArray, - UOptArrayOptArray: _uOptArrayOptArray, - UArrayOptArrayArray: _uArrayOptArrayArray, - CrazyArray: _crazyArray, - Object: _object, - OptObject: _optObject, - ObjectArray: _objectArray, - OptObjectArray: _optObjectArray, - En: _en, - OptEnum: _optEnum, - EnumArray: _enumArray, - OptEnumArray: _optEnumArray, - Map: _map, - MapOfArr: _mapOfArr, - MapOfObj: _mapOfObj, - MapOfArrOfObj: _mapOfArrOfObj, - } -} \ No newline at end of file diff --git a/packages/test-cases/cases/bind/sanity/output/wasm-go/types/custom_enum.go b/packages/test-cases/cases/bind/sanity/output/wasm-go/types/enum_custom_enum.go similarity index 100% rename from packages/test-cases/cases/bind/sanity/output/wasm-go/types/custom_enum.go rename to packages/test-cases/cases/bind/sanity/output/wasm-go/types/enum_custom_enum.go 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/types/module_serialization.go new file mode 100644 index 0000000000..8bbc6a75a0 --- /dev/null +++ b/packages/test-cases/cases/bind/sanity/output/wasm-go/types/module_serialization.go @@ -0,0 +1,436 @@ +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 +} + +func DeserializeModuleMethodArgs(argsBuf []byte) *ArgsModuleMethod { + ctx := msgpack.NewContext("Deserializing module-type: ModuleMethod") + reader := msgpack.NewReadDecoder(ctx, argsBuf) + + var ( + _str string + _strSet bool + _optStr *string + _en CustomEnum + _enSet bool + _optEnum *CustomEnum + _enumArray []CustomEnum + _enumArraySet bool + _optEnumArray []*CustomEnum + _map map[string]int32 + _mapSet bool + _mapOfArr map[string][]int32 + _mapOfArrSet bool + _mapOfObj map[string]AnotherType + _mapOfObjSet bool + _mapOfArrOfObj map[string][]AnotherType + _mapOfArrOfObjSet bool + ) + + for i := int32(reader.ReadMapLength()); i > 0; i-- { + field := reader.ReadString() + reader.Context().Push(field, "unknown", "searching for property type") + reader.Context().Pop() + switch field { + case "Str": + reader.Context().Push(field, "string", "type found, reading property") + _str = reader.ReadString() + _strSet = true + reader.Context().Pop() + case "OptStr": + reader.Context().Push(field, "*string", "type found, reading property") + if !reader.IsNil() { + v := reader.ReadString() + _optStr = &v + } + reader.Context().Pop() + case "En": + reader.Context().Push(field, "CustomEnum", "type found, reading property") + _en = CustomEnum(reader.ReadI32()) + SanitizeCustomEnumValue(int32(_en)) + _enSet = true + reader.Context().Pop() + case "OptEnum": + reader.Context().Push(field, "*CustomEnum", "type found, reading property") + if !reader.IsNil() { + v := CustomEnum(reader.ReadI32()) + SanitizeCustomEnumValue(int32(v)) + _optEnum = &v + } + reader.Context().Pop() + case "EnumArray": + reader.Context().Push(field, "[]CustomEnum", "type found, reading property") + if reader.IsNil() { + _enumArray = nil + } else { + ln0 := reader.ReadArrayLength() + _enumArray = make([]CustomEnum, ln0) + for i0 := uint32(0); i0 < ln0; i0++ { + _enumArray[i0] = CustomEnum(reader.ReadI32()) + SanitizeCustomEnumValue(int32(_enumArray[i0])) + } + } + _enumArraySet = true + reader.Context().Pop() + case "OptEnumArray": + reader.Context().Push(field, "[]*CustomEnum", "type found, reading property") + if reader.IsNil() { + _optEnumArray = nil + } else { + ln0 := reader.ReadArrayLength() + _optEnumArray = make([]*CustomEnum, ln0) + for i0 := uint32(0); i0 < ln0; i0++ { + if !reader.IsNil() { + v := CustomEnum(reader.ReadI32()) + SanitizeCustomEnumValue(int32(v)) + _optEnumArray[i0] = &v + } + } + } + reader.Context().Pop() + case "Map": + reader.Context().Push(field, "map[string]int32", "type found, reading property") + if reader.IsNil() { + _map = nil + } else { + ln0 := reader.ReadMapLength() + _map = make(map[string]int32) + for j0 := uint32(0); j0 < ln0; j0++ { + i0 := reader.ReadString() + _map[i0] = reader.ReadI32() + } + } + _mapSet = true + reader.Context().Pop() + case "MapOfArr": + reader.Context().Push(field, "map[string][]int32", "type found, reading property") + if reader.IsNil() { + _mapOfArr = nil + } else { + ln0 := reader.ReadMapLength() + _mapOfArr = make(map[string][]int32) + for j0 := uint32(0); j0 < ln0; j0++ { + i0 := reader.ReadString() + if reader.IsNil() { + _mapOfArr[i0] = nil + } else { + ln1 := reader.ReadArrayLength() + _mapOfArr[i0] = make([]int32, ln1) + for i1 := uint32(0); i1 < ln1; i1++ { + _mapOfArr[i0][i1] = reader.ReadI32() + } + } + } + } + _mapOfArrSet = true + reader.Context().Pop() + case "MapOfObj": + reader.Context().Push(field, "map[string]AnotherType", "type found, reading property") + if reader.IsNil() { + _mapOfObj = nil + } else { + ln0 := reader.ReadMapLength() + _mapOfObj = make(map[string]AnotherType) + for j0 := uint32(0); j0 < ln0; j0++ { + i0 := reader.ReadString() + if v := AnotherTypeRead(reader); v != nil { + _mapOfObj[i0] = *v + } + } + } + _mapOfObjSet = true + reader.Context().Pop() + case "MapOfArrOfObj": + reader.Context().Push(field, "map[string][]AnotherType", "type found, reading property") + if reader.IsNil() { + _mapOfArrOfObj = nil + } else { + ln0 := reader.ReadMapLength() + _mapOfArrOfObj = make(map[string][]AnotherType) + for j0 := uint32(0); j0 < ln0; j0++ { + i0 := reader.ReadString() + if reader.IsNil() { + _mapOfArrOfObj[i0] = nil + } else { + ln1 := reader.ReadArrayLength() + _mapOfArrOfObj[i0] = make([]AnotherType, ln1) + for i1 := uint32(0); i1 < ln1; i1++ { + if v := AnotherTypeRead(reader); v != nil { + _mapOfArrOfObj[i0][i1] = *v + } + } + } + } + } + _mapOfArrOfObjSet = true + reader.Context().Pop() + } + } + + if !_strSet { + panic(reader.Context().PrintWithContext("Missing required property: 'str: String'")) + } + if !_enSet { + panic(reader.Context().PrintWithContext("Missing required property: 'en: CustomEnum'")) + } + if !_enumArraySet { + panic(reader.Context().PrintWithContext("Missing required property: 'enumArray: [CustomEnum]'")) + } + if !_mapSet { + panic(reader.Context().PrintWithContext("Missing required property: 'map: Map'")) + } + if !_mapOfArrSet { + panic(reader.Context().PrintWithContext("Missing required property: 'mapOfArr: Map'")) + } + if !_mapOfObjSet { + panic(reader.Context().PrintWithContext("Missing required property: 'mapOfObj: Map'")) + } + if !_mapOfArrOfObjSet { + panic(reader.Context().PrintWithContext("Missing required property: 'mapOfArrOfObj: Map'")) + } + + return &ArgsModuleMethod{ + Str: _str, + OptStr: _optStr, + En: _en, + OptEnum: _optEnum, + EnumArray: _enumArray, + OptEnumArray: _optEnumArray, + Map: _map, + MapOfArr: _mapOfArr, + MapOfObj: _mapOfObj, + MapOfArrOfObj: _mapOfArrOfObj, + } +} + +func SerializeModuleMethodResult(value int32) []byte { + ctx := msgpack.NewContext("Serializing module-type: ModuleMethod") + encoder := msgpack.NewWriteEncoder(ctx) + WriteModuleMethodResult(encoder, value); + return encoder.Buffer() +} + +func WriteModuleMethodResult(writer msgpack.Write, value int32) { + writer.Context().Push("moduleMethod", "int32", "writing property") + { + v := value + writer.WriteI32(v) + } + writer.Context().Pop() +} + +type ArgsObjectMethod struct { + Object AnotherType + OptObject *AnotherType + ObjectArray []AnotherType + OptObjectArray []*AnotherType +} + +func DeserializeObjectMethodArgs(argsBuf []byte) *ArgsObjectMethod { + ctx := msgpack.NewContext("Deserializing module-type: ObjectMethod") + reader := msgpack.NewReadDecoder(ctx, argsBuf) + + var ( + _object AnotherType + _objectSet bool + _optObject *AnotherType + _objectArray []AnotherType + _objectArraySet bool + _optObjectArray []*AnotherType + ) + + for i := int32(reader.ReadMapLength()); i > 0; i-- { + field := reader.ReadString() + reader.Context().Push(field, "unknown", "searching for property type") + reader.Context().Pop() + switch field { + case "Object": + reader.Context().Push(field, "AnotherType", "type found, reading property") + if v := AnotherTypeRead(reader); v != nil { + _object = *v + } + _objectSet = true + reader.Context().Pop() + case "OptObject": + reader.Context().Push(field, "*AnotherType", "type found, reading property") + if v := AnotherTypeRead(reader); v != nil { + _optObject = v + } + reader.Context().Pop() + case "ObjectArray": + reader.Context().Push(field, "[]AnotherType", "type found, reading property") + if reader.IsNil() { + _objectArray = nil + } else { + ln0 := reader.ReadArrayLength() + _objectArray = make([]AnotherType, ln0) + for i0 := uint32(0); i0 < ln0; i0++ { + if v := AnotherTypeRead(reader); v != nil { + _objectArray[i0] = *v + } + } + } + _objectArraySet = true + reader.Context().Pop() + case "OptObjectArray": + reader.Context().Push(field, "[]*AnotherType", "type found, reading property") + if reader.IsNil() { + _optObjectArray = nil + } else { + ln0 := reader.ReadArrayLength() + _optObjectArray = make([]*AnotherType, ln0) + for i0 := uint32(0); i0 < ln0; i0++ { + if v := AnotherTypeRead(reader); v != nil { + _optObjectArray[i0] = v + } + } + } + reader.Context().Pop() + } + } + + if !_objectSet { + panic(reader.Context().PrintWithContext("Missing required property: 'object: AnotherType'")) + } + if !_objectArraySet { + panic(reader.Context().PrintWithContext("Missing required property: 'objectArray: [AnotherType]'")) + } + + return &ArgsObjectMethod{ + Object: _object, + OptObject: _optObject, + ObjectArray: _objectArray, + OptObjectArray: _optObjectArray, + } +} + +func SerializeObjectMethodResult(value *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) { + writer.Context().Push("objectMethod", "*AnotherType", "writing property") + { + v := value + AnotherTypeWrite(writer, v) + } + writer.Context().Pop() +} + +type ArgsOptionalEnvMethod struct { + Object AnotherType + OptObject *AnotherType + ObjectArray []AnotherType + OptObjectArray []*AnotherType +} + +func DeserializeOptionalEnvMethodArgs(argsBuf []byte) *ArgsOptionalEnvMethod { + ctx := msgpack.NewContext("Deserializing module-type: OptionalEnvMethod") + reader := msgpack.NewReadDecoder(ctx, argsBuf) + + var ( + _object AnotherType + _objectSet bool + _optObject *AnotherType + _objectArray []AnotherType + _objectArraySet bool + _optObjectArray []*AnotherType + ) + + for i := int32(reader.ReadMapLength()); i > 0; i-- { + field := reader.ReadString() + reader.Context().Push(field, "unknown", "searching for property type") + reader.Context().Pop() + switch field { + case "Object": + reader.Context().Push(field, "AnotherType", "type found, reading property") + if v := AnotherTypeRead(reader); v != nil { + _object = *v + } + _objectSet = true + reader.Context().Pop() + case "OptObject": + reader.Context().Push(field, "*AnotherType", "type found, reading property") + if v := AnotherTypeRead(reader); v != nil { + _optObject = v + } + reader.Context().Pop() + case "ObjectArray": + reader.Context().Push(field, "[]AnotherType", "type found, reading property") + if reader.IsNil() { + _objectArray = nil + } else { + ln0 := reader.ReadArrayLength() + _objectArray = make([]AnotherType, ln0) + for i0 := uint32(0); i0 < ln0; i0++ { + if v := AnotherTypeRead(reader); v != nil { + _objectArray[i0] = *v + } + } + } + _objectArraySet = true + reader.Context().Pop() + case "OptObjectArray": + reader.Context().Push(field, "[]*AnotherType", "type found, reading property") + if reader.IsNil() { + _optObjectArray = nil + } else { + ln0 := reader.ReadArrayLength() + _optObjectArray = make([]*AnotherType, ln0) + for i0 := uint32(0); i0 < ln0; i0++ { + if v := AnotherTypeRead(reader); v != nil { + _optObjectArray[i0] = v + } + } + } + reader.Context().Pop() + } + } + + if !_objectSet { + panic(reader.Context().PrintWithContext("Missing required property: 'object: AnotherType'")) + } + if !_objectArraySet { + panic(reader.Context().PrintWithContext("Missing required property: 'objectArray: [AnotherType]'")) + } + + return &ArgsOptionalEnvMethod{ + Object: _object, + OptObject: _optObject, + ObjectArray: _objectArray, + OptObjectArray: _optObjectArray, + } +} + +func SerializeOptionalEnvMethodResult(value *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) { + writer.Context().Push("optionalEnvMethod", "*AnotherType", "writing property") + { + v := value + AnotherTypeWrite(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/types/module_wrapped.go new file mode 100644 index 0000000000..8b77f7a4a8 --- /dev/null +++ b/packages/test-cases/cases/bind/sanity/output/wasm-go/types/module_wrapped.go @@ -0,0 +1,45 @@ +package types + +import ( + "github.com/consideritdone/polywrap-go/polywrap" + "some/library/to/methods" +) + +func ModuleMethodWrapped(argsBuf []byte, envSize uint32) []byte { + var env *Env + + args := DeserializeModuleMethodArgs(argsBuf) + + result := methods.ModuleMethod(args) + return SerializeModuleMethodResult(result) +} + +func ObjectMethodWrapped(argsBuf []byte, envSize uint32) []byte { + var env *Env + if envSize == 0 { + panic("Environment is not set, and it is required by method 'objectMethod'") + } + if envSize > 0 { + envBuf := polywrap.WrapLoadEnv(envSize) + env = EnvFromBuffer(envBuf) + } + + args := DeserializeObjectMethodArgs(argsBuf) + + result := methods.ObjectMethod(args, env) + return SerializeObjectMethodResult(result) +} + +func OptionalEnvMethodWrapped(argsBuf []byte, envSize uint32) []byte { + var env *Env + if envSize > 0 { + envBuf := polywrap.WrapLoadEnv(envSize) + env = EnvFromBuffer(envBuf) + } + + args := DeserializeOptionalEnvMethodArgs(argsBuf) + + result := methods.OptionalEnvMethod(args, env) + return SerializeOptionalEnvMethodResult(result) +} + diff --git a/packages/test-cases/cases/bind/sanity/output/wasm-go/types/another_type.go b/packages/test-cases/cases/bind/sanity/output/wasm-go/types/object_another_type.go similarity index 100% rename from packages/test-cases/cases/bind/sanity/output/wasm-go/types/another_type.go rename to packages/test-cases/cases/bind/sanity/output/wasm-go/types/object_another_type.go diff --git a/packages/test-cases/cases/bind/sanity/output/wasm-go/types/another_type_serialization.go b/packages/test-cases/cases/bind/sanity/output/wasm-go/types/object_another_type_serialization.go similarity index 99% rename from packages/test-cases/cases/bind/sanity/output/wasm-go/types/another_type_serialization.go rename to packages/test-cases/cases/bind/sanity/output/wasm-go/types/object_another_type_serialization.go index 682e6bde21..3bb5d8f434 100644 --- a/packages/test-cases/cases/bind/sanity/output/wasm-go/types/another_type_serialization.go +++ b/packages/test-cases/cases/bind/sanity/output/wasm-go/types/object_another_type_serialization.go @@ -26,7 +26,6 @@ func writeAnotherType(writer msgpack.Write, value *AnotherType) { writer.Context().Pop() writer.Context().Push("Circular", "*CustomType", "writing property") writer.WriteString("Circular") - { v := value.Circular CustomTypeWrite(writer, v) diff --git a/packages/test-cases/cases/bind/sanity/output/wasm-go/types/custom_type.go b/packages/test-cases/cases/bind/sanity/output/wasm-go/types/object_custom_type.go similarity index 100% rename from packages/test-cases/cases/bind/sanity/output/wasm-go/types/custom_type.go rename to packages/test-cases/cases/bind/sanity/output/wasm-go/types/object_custom_type.go diff --git a/packages/test-cases/cases/bind/sanity/output/wasm-go/types/object_custom_type_serialization.go b/packages/test-cases/cases/bind/sanity/output/wasm-go/types/object_custom_type_serialization.go new file mode 100644 index 0000000000..74d75efa54 --- /dev/null +++ b/packages/test-cases/cases/bind/sanity/output/wasm-go/types/object_custom_type_serialization.go @@ -0,0 +1,1211 @@ +package types + +import ( + "github.com/consideritdone/polywrap-go/polywrap/msgpack" + "github.com/consideritdone/polywrap-go/polywrap/msgpack/big" + "github.com/valyala/fastjson" +) + +func serializeCustomType(value *CustomType) []byte { + ctx := msgpack.NewContext("Serializing (encoding) env-type: CustomType") + encoder := msgpack.NewWriteEncoder(ctx) + writeCustomType(encoder, value) + return encoder.Buffer() +} + +func writeCustomType(writer msgpack.Write, value *CustomType) { + writer.WriteMapLength(41) + writer.Context().Push("Str", "string", "writing property") + writer.WriteString("Str") + { + v := value.Str + writer.WriteString(v) + } + writer.Context().Pop() + writer.Context().Push("OptStr", "*string", "writing property") + writer.WriteString("OptStr") + { + v := value.OptStr + if v == nil { + writer.WriteNil() + } else { + writer.WriteString(*v) + } + } + writer.Context().Pop() + writer.Context().Push("U", "uint32", "writing property") + writer.WriteString("U") + { + v := value.U + writer.WriteU32(v) + } + writer.Context().Pop() + writer.Context().Push("OptU", "*uint32", "writing property") + writer.WriteString("OptU") + { + v := value.OptU + if v == nil { + writer.WriteNil() + } else { + writer.WriteU32(*v) + } + } + writer.Context().Pop() + writer.Context().Push("M_u8", "uint8", "writing property") + writer.WriteString("M_u8") + { + v := value.M_u8 + writer.WriteU8(v) + } + writer.Context().Pop() + writer.Context().Push("M_u16", "uint16", "writing property") + writer.WriteString("M_u16") + { + v := value.M_u16 + writer.WriteU16(v) + } + writer.Context().Pop() + writer.Context().Push("M_u32", "uint32", "writing property") + writer.WriteString("M_u32") + { + v := value.M_u32 + writer.WriteU32(v) + } + writer.Context().Pop() + writer.Context().Push("I", "int32", "writing property") + writer.WriteString("I") + { + v := value.I + writer.WriteI32(v) + } + writer.Context().Pop() + writer.Context().Push("M_i8", "int8", "writing property") + writer.WriteString("M_i8") + { + v := value.M_i8 + writer.WriteI8(v) + } + writer.Context().Pop() + writer.Context().Push("M_i16", "int16", "writing property") + writer.WriteString("M_i16") + { + v := value.M_i16 + writer.WriteI16(v) + } + writer.Context().Pop() + writer.Context().Push("M_i32", "int32", "writing property") + writer.WriteString("M_i32") + { + v := value.M_i32 + writer.WriteI32(v) + } + writer.Context().Pop() + writer.Context().Push("Bigint", "*big.Int", "writing property") + writer.WriteString("Bigint") + { + v := value.Bigint + writer.WriteBigInt(v) + } + writer.Context().Pop() + writer.Context().Push("OptBigint", "*big.Int", "writing property") + writer.WriteString("OptBigint") + { + v := value.OptBigint + if v == nil { + writer.WriteNil() + } else { + writer.WriteBigInt(v) + } + } + writer.Context().Pop() + writer.Context().Push("Bignumber", "*big.Int", "writing property") + writer.WriteString("Bignumber") + { + v := value.Bignumber + writer.WriteBigInt(v) + } + writer.Context().Pop() + writer.Context().Push("OptBignumber", "*big.Int", "writing property") + writer.WriteString("OptBignumber") + { + v := value.OptBignumber + if v == nil { + writer.WriteNil() + } else { + writer.WriteBigInt(v) + } + } + writer.Context().Pop() + writer.Context().Push("Json", "*fastjson.Value", "writing property") + writer.WriteString("Json") + { + v := value.Json + writer.WriteJson(v) + } + writer.Context().Pop() + writer.Context().Push("OptJson", "*fastjson.Value", "writing property") + writer.WriteString("OptJson") + { + v := value.OptJson + if v == nil { + writer.WriteNil() + } else { + writer.WriteJson(v) + } + } + writer.Context().Pop() + writer.Context().Push("Bytes", "[]byte", "writing property") + writer.WriteString("Bytes") + { + v := value.Bytes + writer.WriteBytes(v) + } + writer.Context().Pop() + writer.Context().Push("OptBytes", "[]byte", "writing property") + writer.WriteString("OptBytes") + { + v := value.OptBytes + if v == nil { + writer.WriteNil() + } else { + writer.WriteBytes(v) + } + } + writer.Context().Pop() + writer.Context().Push("M_boolean", "bool", "writing property") + writer.WriteString("M_boolean") + { + v := value.M_boolean + writer.WriteBool(v) + } + writer.Context().Pop() + writer.Context().Push("OptBoolean", "*bool", "writing property") + writer.WriteString("OptBoolean") + { + v := value.OptBoolean + if v == nil { + writer.WriteNil() + } else { + writer.WriteBool(*v) + } + } + writer.Context().Pop() + writer.Context().Push("UArray", "[]uint32", "writing property") + writer.WriteString("UArray") + if value.UArray == nil { + writer.WriteNil() + } else if len(value.UArray) == 0 { + writer.WriteNil() + } else { + for i0 := range value.UArray { + { + v := value.UArray[i0] + writer.WriteU32(v) + } + } + } + writer.Context().Pop() + writer.Context().Push("UOptArray", "[]uint32", "writing property") + writer.WriteString("UOptArray") + if value.UOptArray == nil { + writer.WriteNil() + } else if len(value.UOptArray) == 0 { + writer.WriteNil() + } else { + for i0 := range value.UOptArray { + { + v := value.UOptArray[i0] + writer.WriteU32(v) + } + } + } + writer.Context().Pop() + writer.Context().Push("OptUOptArray", "[]*uint32", "writing property") + writer.WriteString("OptUOptArray") + if value.OptUOptArray == nil { + writer.WriteNil() + } else if len(value.OptUOptArray) == 0 { + writer.WriteNil() + } else { + for i0 := range value.OptUOptArray { + { + v := value.OptUOptArray[i0] + if v == nil { + writer.WriteNil() + } else { + writer.WriteU32(*v) + } + } + } + } + writer.Context().Pop() + writer.Context().Push("OptStrOptArray", "[]*string", "writing property") + writer.WriteString("OptStrOptArray") + if value.OptStrOptArray == nil { + writer.WriteNil() + } else if len(value.OptStrOptArray) == 0 { + writer.WriteNil() + } else { + for i0 := range value.OptStrOptArray { + { + v := value.OptStrOptArray[i0] + if v == nil { + writer.WriteNil() + } else { + writer.WriteString(*v) + } + } + } + } + writer.Context().Pop() + writer.Context().Push("UArrayArray", "[][]uint32", "writing property") + writer.WriteString("UArrayArray") + if value.UArrayArray == nil { + writer.WriteNil() + } else if len(value.UArrayArray) == 0 { + writer.WriteNil() + } else { + for i0 := range value.UArrayArray { + if value.UArrayArray[i0] == nil { + writer.WriteNil() + } else if len(value.UArrayArray[i0]) == 0 { + writer.WriteNil() + } else { + for i1 := range value.UArrayArray[i0] { + { + v := value.UArrayArray[i0][i1] + writer.WriteU32(v) + } + } + } + } + } + writer.Context().Pop() + writer.Context().Push("UOptArrayOptArray", "[][]*uint32", "writing property") + writer.WriteString("UOptArrayOptArray") + if value.UOptArrayOptArray == nil { + writer.WriteNil() + } else if len(value.UOptArrayOptArray) == 0 { + writer.WriteNil() + } else { + for i0 := range value.UOptArrayOptArray { + if value.UOptArrayOptArray[i0] == nil { + writer.WriteNil() + } else if len(value.UOptArrayOptArray[i0]) == 0 { + writer.WriteNil() + } else { + for i1 := range value.UOptArrayOptArray[i0] { + { + v := value.UOptArrayOptArray[i0][i1] + if v == nil { + writer.WriteNil() + } else { + writer.WriteU32(*v) + } + } + } + } + } + } + writer.Context().Pop() + writer.Context().Push("UArrayOptArrayArray", "[][][]uint32", "writing property") + writer.WriteString("UArrayOptArrayArray") + if value.UArrayOptArrayArray == nil { + writer.WriteNil() + } else if len(value.UArrayOptArrayArray) == 0 { + writer.WriteNil() + } else { + for i0 := range value.UArrayOptArrayArray { + if value.UArrayOptArrayArray[i0] == nil { + writer.WriteNil() + } else if len(value.UArrayOptArrayArray[i0]) == 0 { + writer.WriteNil() + } else { + for i1 := range value.UArrayOptArrayArray[i0] { + if value.UArrayOptArrayArray[i0][i1] == nil { + writer.WriteNil() + } else if len(value.UArrayOptArrayArray[i0][i1]) == 0 { + writer.WriteNil() + } else { + for i2 := range value.UArrayOptArrayArray[i0][i1] { + { + v := value.UArrayOptArrayArray[i0][i1][i2] + writer.WriteU32(v) + } + } + } + } + } + } + } + writer.Context().Pop() + writer.Context().Push("CrazyArray", "[][][][]uint32", "writing property") + writer.WriteString("CrazyArray") + if value.CrazyArray == nil { + writer.WriteNil() + } else if len(value.CrazyArray) == 0 { + writer.WriteNil() + } else { + for i0 := range value.CrazyArray { + if value.CrazyArray[i0] == nil { + writer.WriteNil() + } else if len(value.CrazyArray[i0]) == 0 { + writer.WriteNil() + } else { + for i1 := range value.CrazyArray[i0] { + if value.CrazyArray[i0][i1] == nil { + writer.WriteNil() + } else if len(value.CrazyArray[i0][i1]) == 0 { + writer.WriteNil() + } else { + for i2 := range value.CrazyArray[i0][i1] { + if value.CrazyArray[i0][i1][i2] == nil { + writer.WriteNil() + } else if len(value.CrazyArray[i0][i1][i2]) == 0 { + writer.WriteNil() + } else { + for i3 := range value.CrazyArray[i0][i1][i2] { + { + v := value.CrazyArray[i0][i1][i2][i3] + writer.WriteU32(v) + } + } + } + } + } + } + } + } + } + writer.Context().Pop() + writer.Context().Push("Object", "AnotherType", "writing property") + writer.WriteString("Object") + { + v := value.Object + AnotherTypeWrite(writer, &v) + } + writer.Context().Pop() + writer.Context().Push("OptObject", "*AnotherType", "writing property") + writer.WriteString("OptObject") + { + v := value.OptObject + AnotherTypeWrite(writer, v) + } + writer.Context().Pop() + writer.Context().Push("ObjectArray", "[]AnotherType", "writing property") + writer.WriteString("ObjectArray") + if value.ObjectArray == nil { + writer.WriteNil() + } else if len(value.ObjectArray) == 0 { + writer.WriteNil() + } else { + for i0 := range value.ObjectArray { + { + v := value.ObjectArray[i0] + AnotherTypeWrite(writer, &v) + } + } + } + writer.Context().Pop() + writer.Context().Push("OptObjectArray", "[]*AnotherType", "writing property") + writer.WriteString("OptObjectArray") + if value.OptObjectArray == nil { + writer.WriteNil() + } else if len(value.OptObjectArray) == 0 { + writer.WriteNil() + } else { + for i0 := range value.OptObjectArray { + { + v := value.OptObjectArray[i0] + AnotherTypeWrite(writer, v) + } + } + } + writer.Context().Pop() + writer.Context().Push("En", "CustomEnum", "writing property") + writer.WriteString("En") + { + v := value.En + writer.WriteI32(int32(v)) + } + writer.Context().Pop() + writer.Context().Push("OptEnum", "*CustomEnum", "writing property") + writer.WriteString("OptEnum") + { + v := value.OptEnum + if v == nil { + writer.WriteNil() + } else { + writer.WriteI32(int32(*v)) + } + } + writer.Context().Pop() + writer.Context().Push("EnumArray", "[]CustomEnum", "writing property") + writer.WriteString("EnumArray") + if value.EnumArray == nil { + writer.WriteNil() + } else if len(value.EnumArray) == 0 { + writer.WriteNil() + } else { + for i0 := range value.EnumArray { + { + v := value.EnumArray[i0] + writer.WriteI32(int32(v)) + } + } + } + writer.Context().Pop() + writer.Context().Push("OptEnumArray", "[]*CustomEnum", "writing property") + writer.WriteString("OptEnumArray") + if value.OptEnumArray == nil { + writer.WriteNil() + } else if len(value.OptEnumArray) == 0 { + writer.WriteNil() + } else { + for i0 := range value.OptEnumArray { + { + v := value.OptEnumArray[i0] + if v == nil { + writer.WriteNil() + } else { + writer.WriteI32(int32(*v)) + } + } + } + } + writer.Context().Pop() + writer.Context().Push("Map", "map[string]int32", "writing property") + writer.WriteString("Map") + if value.Map == nil { + writer.WriteNil() + } else if len(value.Map) == 0 { + writer.WriteNil() + } else { + for i0 := range value.Map { + writer.WriteString(i0) + { + v := value.Map[i0] + writer.WriteI32(v) + } + } + } + writer.Context().Pop() + writer.Context().Push("MapOfArr", "map[string][]int32", "writing property") + writer.WriteString("MapOfArr") + if value.MapOfArr == nil { + writer.WriteNil() + } else if len(value.MapOfArr) == 0 { + writer.WriteNil() + } else { + for i0 := range value.MapOfArr { + writer.WriteString(i0) + if value.MapOfArr[i0] == nil { + writer.WriteNil() + } else if len(value.MapOfArr[i0]) == 0 { + writer.WriteNil() + } else { + for i1 := range value.MapOfArr[i0] { + { + v := value.MapOfArr[i0][i1] + writer.WriteI32(v) + } + } + } + } + } + writer.Context().Pop() + writer.Context().Push("MapOfObj", "map[string]AnotherType", "writing property") + writer.WriteString("MapOfObj") + if value.MapOfObj == nil { + writer.WriteNil() + } else if len(value.MapOfObj) == 0 { + writer.WriteNil() + } else { + for i0 := range value.MapOfObj { + writer.WriteString(i0) + { + v := value.MapOfObj[i0] + AnotherTypeWrite(writer, &v) + } + } + } + writer.Context().Pop() + writer.Context().Push("MapOfArrOfObj", "map[string][]AnotherType", "writing property") + writer.WriteString("MapOfArrOfObj") + if value.MapOfArrOfObj == nil { + writer.WriteNil() + } else if len(value.MapOfArrOfObj) == 0 { + writer.WriteNil() + } else { + for i0 := range value.MapOfArrOfObj { + writer.WriteString(i0) + if value.MapOfArrOfObj[i0] == nil { + writer.WriteNil() + } else if len(value.MapOfArrOfObj[i0]) == 0 { + writer.WriteNil() + } else { + for i1 := range value.MapOfArrOfObj[i0] { + { + v := value.MapOfArrOfObj[i0][i1] + AnotherTypeWrite(writer, &v) + } + } + } + } + } + writer.Context().Pop() +} + +func deserializeCustomType(data []byte) *CustomType { + ctx := msgpack.NewContext("Deserializing (decoding) env-type: CustomType") + reader := msgpack.NewReadDecoder(ctx, data) + return readCustomType(reader) +} + +func readCustomType(reader msgpack.Read) *CustomType { + var ( + _str string + _strSet bool + _optStr *string + _u uint32 + _uSet bool + _optU *uint32 + _u8 uint8 + _u8Set bool + _u16 uint16 + _u16Set bool + _u32 uint32 + _u32Set bool + _i int32 + _iSet bool + _i8 int8 + _i8Set bool + _i16 int16 + _i16Set bool + _i32 int32 + _i32Set bool + _bigint *big.Int + _bigintSet bool + _optBigint *big.Int + _bignumber *big.Int + _bignumberSet bool + _optBignumber *big.Int + _json *fastjson.Value + _jsonSet bool + _optJson *fastjson.Value + _bytes []byte + _bytesSet bool + _optBytes []byte + _boolean bool + _booleanSet bool + _optBoolean *bool + _uArray []uint32 + _uArraySet bool + _uOptArray []uint32 + _optUOptArray []*uint32 + _optStrOptArray []*string + _uArrayArray [][]uint32 + _uArrayArraySet bool + _uOptArrayOptArray [][]*uint32 + _uOptArrayOptArraySet bool + _uArrayOptArrayArray [][][]uint32 + _uArrayOptArrayArraySet bool + _crazyArray [][][][]uint32 + _object AnotherType + _objectSet bool + _optObject *AnotherType + _objectArray []AnotherType + _objectArraySet bool + _optObjectArray []*AnotherType + _en CustomEnum + _enSet bool + _optEnum *CustomEnum + _enumArray []CustomEnum + _enumArraySet bool + _optEnumArray []*CustomEnum + _map map[string]int32 + _mapSet bool + _mapOfArr map[string][]int32 + _mapOfArrSet bool + _mapOfObj map[string]AnotherType + _mapOfObjSet bool + _mapOfArrOfObj map[string][]AnotherType + _mapOfArrOfObjSet bool + ) + + for i := int32(reader.ReadMapLength()); i > 0; i-- { + field := reader.ReadString() + reader.Context().Push(field, "unknown", "searching for property type") + switch field { + case "Str": + reader.Context().Push(field, "string", "type found, reading property") + _str = reader.ReadString() + _strSet = true + reader.Context().Pop() + case "OptStr": + reader.Context().Push(field, "*string", "type found, reading property") + if !reader.IsNil() { + v := reader.ReadString() + _optStr = &v + } + reader.Context().Pop() + case "U": + reader.Context().Push(field, "uint32", "type found, reading property") + _u = reader.ReadU32() + _uSet = true + reader.Context().Pop() + case "OptU": + reader.Context().Push(field, "*uint32", "type found, reading property") + if !reader.IsNil() { + v := reader.ReadU32() + _optU = &v + } + reader.Context().Pop() + case "M_u8": + reader.Context().Push(field, "uint8", "type found, reading property") + _u8 = reader.ReadU8() + _u8Set = true + reader.Context().Pop() + case "M_u16": + reader.Context().Push(field, "uint16", "type found, reading property") + _u16 = reader.ReadU16() + _u16Set = true + reader.Context().Pop() + case "M_u32": + reader.Context().Push(field, "uint32", "type found, reading property") + _u32 = reader.ReadU32() + _u32Set = true + reader.Context().Pop() + case "I": + reader.Context().Push(field, "int32", "type found, reading property") + _i = reader.ReadI32() + _iSet = true + reader.Context().Pop() + case "M_i8": + reader.Context().Push(field, "int8", "type found, reading property") + _i8 = reader.ReadI8() + _i8Set = true + reader.Context().Pop() + case "M_i16": + reader.Context().Push(field, "int16", "type found, reading property") + _i16 = reader.ReadI16() + _i16Set = true + reader.Context().Pop() + case "M_i32": + reader.Context().Push(field, "int32", "type found, reading property") + _i32 = reader.ReadI32() + _i32Set = true + reader.Context().Pop() + case "Bigint": + reader.Context().Push(field, "*big.Int", "type found, reading property") + _bigint = reader.ReadBigInt() + _bigintSet = true + reader.Context().Pop() + case "OptBigint": + reader.Context().Push(field, "*big.Int", "type found, reading property") + if !reader.IsNil() { + v := reader.ReadBigInt() + _optBigint = v + } + reader.Context().Pop() + case "Bignumber": + reader.Context().Push(field, "*big.Int", "type found, reading property") + _bignumber = reader.ReadBigInt() + _bignumberSet = true + reader.Context().Pop() + case "OptBignumber": + reader.Context().Push(field, "*big.Int", "type found, reading property") + if !reader.IsNil() { + v := reader.ReadBigInt() + _optBignumber = v + } + reader.Context().Pop() + case "Json": + reader.Context().Push(field, "*fastjson.Value", "type found, reading property") + _json = reader.ReadJson() + _jsonSet = true + reader.Context().Pop() + case "OptJson": + reader.Context().Push(field, "*fastjson.Value", "type found, reading property") + if !reader.IsNil() { + v := reader.ReadJson() + _optJson = v + } + reader.Context().Pop() + case "Bytes": + reader.Context().Push(field, "[]byte", "type found, reading property") + _bytes = reader.ReadBytes() + _bytesSet = true + reader.Context().Pop() + case "OptBytes": + reader.Context().Push(field, "[]byte", "type found, reading property") + if !reader.IsNil() { + v := reader.ReadBytes() + _optBytes = v + } + reader.Context().Pop() + case "M_boolean": + reader.Context().Push(field, "bool", "type found, reading property") + _boolean = reader.ReadBool() + _booleanSet = true + reader.Context().Pop() + case "OptBoolean": + reader.Context().Push(field, "*bool", "type found, reading property") + if !reader.IsNil() { + v := reader.ReadBool() + _optBoolean = &v + } + reader.Context().Pop() + case "UArray": + reader.Context().Push(field, "[]uint32", "type found, reading property") + if reader.IsNil() { + _uArray = nil + } else { + ln0 := reader.ReadArrayLength() + _uArray = make([]uint32, ln0) + for i0 := uint32(0); i0 < ln0; i0++ { + _uArray[i0] = reader.ReadU32() + } + } + _uArraySet = true + reader.Context().Pop() + case "UOptArray": + reader.Context().Push(field, "[]uint32", "type found, reading property") + if reader.IsNil() { + _uOptArray = nil + } else { + ln0 := reader.ReadArrayLength() + _uOptArray = make([]uint32, ln0) + for i0 := uint32(0); i0 < ln0; i0++ { + _uOptArray[i0] = reader.ReadU32() + } + } + reader.Context().Pop() + case "OptUOptArray": + reader.Context().Push(field, "[]*uint32", "type found, reading property") + if reader.IsNil() { + _optUOptArray = nil + } else { + ln0 := reader.ReadArrayLength() + _optUOptArray = make([]*uint32, ln0) + for i0 := uint32(0); i0 < ln0; i0++ { + if !reader.IsNil() { + v := reader.ReadU32() + _optUOptArray[i0] = &v + } + } + } + reader.Context().Pop() + case "OptStrOptArray": + reader.Context().Push(field, "[]*string", "type found, reading property") + if reader.IsNil() { + _optStrOptArray = nil + } else { + ln0 := reader.ReadArrayLength() + _optStrOptArray = make([]*string, ln0) + for i0 := uint32(0); i0 < ln0; i0++ { + if !reader.IsNil() { + v := reader.ReadString() + _optStrOptArray[i0] = &v + } + } + } + reader.Context().Pop() + case "UArrayArray": + reader.Context().Push(field, "[][]uint32", "type found, reading property") + if reader.IsNil() { + _uArrayArray = nil + } else { + ln0 := reader.ReadArrayLength() + _uArrayArray = make([][]uint32, ln0) + for i0 := uint32(0); i0 < ln0; i0++ { + if reader.IsNil() { + _uArrayArray[i0] = nil + } else { + ln1 := reader.ReadArrayLength() + _uArrayArray[i0] = make([]uint32, ln1) + for i1 := uint32(0); i1 < ln1; i1++ { + _uArrayArray[i0][i1] = reader.ReadU32() + } + } + } + } + _uArrayArraySet = true + reader.Context().Pop() + case "UOptArrayOptArray": + reader.Context().Push(field, "[][]*uint32", "type found, reading property") + if reader.IsNil() { + _uOptArrayOptArray = nil + } else { + ln0 := reader.ReadArrayLength() + _uOptArrayOptArray = make([][]*uint32, ln0) + for i0 := uint32(0); i0 < ln0; i0++ { + if reader.IsNil() { + _uOptArrayOptArray[i0] = nil + } else { + ln1 := reader.ReadArrayLength() + _uOptArrayOptArray[i0] = make([]*uint32, ln1) + for i1 := uint32(0); i1 < ln1; i1++ { + if !reader.IsNil() { + v := reader.ReadU32() + _uOptArrayOptArray[i0][i1] = &v + } + } + } + } + } + _uOptArrayOptArraySet = true + reader.Context().Pop() + case "UArrayOptArrayArray": + reader.Context().Push(field, "[][][]uint32", "type found, reading property") + if reader.IsNil() { + _uArrayOptArrayArray = nil + } else { + ln0 := reader.ReadArrayLength() + _uArrayOptArrayArray = make([][][]uint32, ln0) + for i0 := uint32(0); i0 < ln0; i0++ { + if reader.IsNil() { + _uArrayOptArrayArray[i0] = nil + } else { + ln1 := reader.ReadArrayLength() + _uArrayOptArrayArray[i0] = make([][]uint32, ln1) + for i1 := uint32(0); i1 < ln1; i1++ { + if reader.IsNil() { + _uArrayOptArrayArray[i0][i1] = nil + } else { + ln2 := reader.ReadArrayLength() + _uArrayOptArrayArray[i0][i1] = make([]uint32, ln2) + for i2 := uint32(0); i2 < ln2; i2++ { + _uArrayOptArrayArray[i0][i1][i2] = reader.ReadU32() + } + } + } + } + } + } + _uArrayOptArrayArraySet = true + reader.Context().Pop() + case "CrazyArray": + reader.Context().Push(field, "[][][][]uint32", "type found, reading property") + if reader.IsNil() { + _crazyArray = nil + } else { + ln0 := reader.ReadArrayLength() + _crazyArray = make([][][][]uint32, ln0) + for i0 := uint32(0); i0 < ln0; i0++ { + if reader.IsNil() { + _crazyArray[i0] = nil + } else { + ln1 := reader.ReadArrayLength() + _crazyArray[i0] = make([][][]uint32, ln1) + for i1 := uint32(0); i1 < ln1; i1++ { + if reader.IsNil() { + _crazyArray[i0][i1] = nil + } else { + ln2 := reader.ReadArrayLength() + _crazyArray[i0][i1] = make([][]uint32, ln2) + for i2 := uint32(0); i2 < ln2; i2++ { + if reader.IsNil() { + _crazyArray[i0][i1][i2] = nil + } else { + ln3 := reader.ReadArrayLength() + _crazyArray[i0][i1][i2] = make([]uint32, ln3) + for i3 := uint32(0); i3 < ln3; i3++ { + _crazyArray[i0][i1][i2][i3] = reader.ReadU32() + } + } + } + } + } + } + } + } + reader.Context().Pop() + case "Object": + reader.Context().Push(field, "AnotherType", "type found, reading property") + if v := AnotherTypeRead(reader); v != nil { + _object = *v + } + _objectSet = true + reader.Context().Pop() + case "OptObject": + reader.Context().Push(field, "*AnotherType", "type found, reading property") + if v := AnotherTypeRead(reader); v != nil { + _optObject = v + } + reader.Context().Pop() + case "ObjectArray": + reader.Context().Push(field, "[]AnotherType", "type found, reading property") + if reader.IsNil() { + _objectArray = nil + } else { + ln0 := reader.ReadArrayLength() + _objectArray = make([]AnotherType, ln0) + for i0 := uint32(0); i0 < ln0; i0++ { + if v := AnotherTypeRead(reader); v != nil { + _objectArray[i0] = *v + } + } + } + _objectArraySet = true + reader.Context().Pop() + case "OptObjectArray": + reader.Context().Push(field, "[]*AnotherType", "type found, reading property") + if reader.IsNil() { + _optObjectArray = nil + } else { + ln0 := reader.ReadArrayLength() + _optObjectArray = make([]*AnotherType, ln0) + for i0 := uint32(0); i0 < ln0; i0++ { + if v := AnotherTypeRead(reader); v != nil { + _optObjectArray[i0] = v + } + } + } + reader.Context().Pop() + case "En": + reader.Context().Push(field, "CustomEnum", "type found, reading property") + _en = CustomEnum(reader.ReadI32()) + SanitizeCustomEnumValue(int32(_en)) + _enSet = true + reader.Context().Pop() + case "OptEnum": + reader.Context().Push(field, "*CustomEnum", "type found, reading property") + if !reader.IsNil() { + v := CustomEnum(reader.ReadI32()) + SanitizeCustomEnumValue(int32(v)) + _optEnum = &v + } + reader.Context().Pop() + case "EnumArray": + reader.Context().Push(field, "[]CustomEnum", "type found, reading property") + if reader.IsNil() { + _enumArray = nil + } else { + ln0 := reader.ReadArrayLength() + _enumArray = make([]CustomEnum, ln0) + for i0 := uint32(0); i0 < ln0; i0++ { + _enumArray[i0] = CustomEnum(reader.ReadI32()) + SanitizeCustomEnumValue(int32(_enumArray[i0])) + } + } + _enumArraySet = true + reader.Context().Pop() + case "OptEnumArray": + reader.Context().Push(field, "[]*CustomEnum", "type found, reading property") + if reader.IsNil() { + _optEnumArray = nil + } else { + ln0 := reader.ReadArrayLength() + _optEnumArray = make([]*CustomEnum, ln0) + for i0 := uint32(0); i0 < ln0; i0++ { + if !reader.IsNil() { + v := CustomEnum(reader.ReadI32()) + SanitizeCustomEnumValue(int32(v)) + _optEnumArray[i0] = &v + } + } + } + reader.Context().Pop() + case "Map": + reader.Context().Push(field, "map[string]int32", "type found, reading property") + if reader.IsNil() { + _map = nil + } else { + ln0 := reader.ReadMapLength() + _map = make(map[string]int32) + for j0 := uint32(0); j0 < ln0; j0++ { + i0 := reader.ReadString() + _map[i0] = reader.ReadI32() + } + } + _mapSet = true + reader.Context().Pop() + case "MapOfArr": + reader.Context().Push(field, "map[string][]int32", "type found, reading property") + if reader.IsNil() { + _mapOfArr = nil + } else { + ln0 := reader.ReadMapLength() + _mapOfArr = make(map[string][]int32) + for j0 := uint32(0); j0 < ln0; j0++ { + i0 := reader.ReadString() + if reader.IsNil() { + _mapOfArr[i0] = nil + } else { + ln1 := reader.ReadArrayLength() + _mapOfArr[i0] = make([]int32, ln1) + for i1 := uint32(0); i1 < ln1; i1++ { + _mapOfArr[i0][i1] = reader.ReadI32() + } + } + } + } + _mapOfArrSet = true + reader.Context().Pop() + case "MapOfObj": + reader.Context().Push(field, "map[string]AnotherType", "type found, reading property") + if reader.IsNil() { + _mapOfObj = nil + } else { + ln0 := reader.ReadMapLength() + _mapOfObj = make(map[string]AnotherType) + for j0 := uint32(0); j0 < ln0; j0++ { + i0 := reader.ReadString() + if v := AnotherTypeRead(reader); v != nil { + _mapOfObj[i0] = *v + } + } + } + _mapOfObjSet = true + reader.Context().Pop() + case "MapOfArrOfObj": + reader.Context().Push(field, "map[string][]AnotherType", "type found, reading property") + if reader.IsNil() { + _mapOfArrOfObj = nil + } else { + ln0 := reader.ReadMapLength() + _mapOfArrOfObj = make(map[string][]AnotherType) + for j0 := uint32(0); j0 < ln0; j0++ { + i0 := reader.ReadString() + if reader.IsNil() { + _mapOfArrOfObj[i0] = nil + } else { + ln1 := reader.ReadArrayLength() + _mapOfArrOfObj[i0] = make([]AnotherType, ln1) + for i1 := uint32(0); i1 < ln1; i1++ { + if v := AnotherTypeRead(reader); v != nil { + _mapOfArrOfObj[i0][i1] = *v + } + } + } + } + } + _mapOfArrOfObjSet = true + reader.Context().Pop() + } + reader.Context().Pop() + } + + if !_strSet { + panic(reader.Context().PrintWithContext("Missing required property: 'str: String'")) + } + if !_uSet { + panic(reader.Context().PrintWithContext("Missing required property: 'u: UInt'")) + } + if !_u8Set { + panic(reader.Context().PrintWithContext("Missing required property: 'u8: UInt8'")) + } + if !_u16Set { + panic(reader.Context().PrintWithContext("Missing required property: 'u16: UInt16'")) + } + if !_u32Set { + panic(reader.Context().PrintWithContext("Missing required property: 'u32: UInt32'")) + } + if !_iSet { + panic(reader.Context().PrintWithContext("Missing required property: 'i: Int'")) + } + if !_i8Set { + panic(reader.Context().PrintWithContext("Missing required property: 'i8: Int8'")) + } + if !_i16Set { + panic(reader.Context().PrintWithContext("Missing required property: 'i16: Int16'")) + } + if !_i32Set { + panic(reader.Context().PrintWithContext("Missing required property: 'i32: Int32'")) + } + if !_bigintSet { + panic(reader.Context().PrintWithContext("Missing required property: 'bigint: BigInt'")) + } + if !_bignumberSet { + panic(reader.Context().PrintWithContext("Missing required property: 'bignumber: BigNumber'")) + } + if !_jsonSet { + panic(reader.Context().PrintWithContext("Missing required property: 'json: JSON'")) + } + if !_bytesSet { + panic(reader.Context().PrintWithContext("Missing required property: 'bytes: Bytes'")) + } + if !_booleanSet { + panic(reader.Context().PrintWithContext("Missing required property: 'boolean: Boolean'")) + } + if !_uArraySet { + panic(reader.Context().PrintWithContext("Missing required property: 'uArray: [UInt]'")) + } + if !_uArrayArraySet { + panic(reader.Context().PrintWithContext("Missing required property: 'uArrayArray: [[UInt]]'")) + } + if !_uOptArrayOptArraySet { + panic(reader.Context().PrintWithContext("Missing required property: 'uOptArrayOptArray: [[UInt32]]'")) + } + if !_uArrayOptArrayArraySet { + panic(reader.Context().PrintWithContext("Missing required property: 'uArrayOptArrayArray: [[[UInt32]]]'")) + } + if !_objectSet { + panic(reader.Context().PrintWithContext("Missing required property: 'object: AnotherType'")) + } + if !_objectArraySet { + panic(reader.Context().PrintWithContext("Missing required property: 'objectArray: [AnotherType]'")) + } + if !_enSet { + panic(reader.Context().PrintWithContext("Missing required property: 'en: CustomEnum'")) + } + if !_enumArraySet { + panic(reader.Context().PrintWithContext("Missing required property: 'enumArray: [CustomEnum]'")) + } + if !_mapSet { + panic(reader.Context().PrintWithContext("Missing required property: 'map: Map'")) + } + if !_mapOfArrSet { + panic(reader.Context().PrintWithContext("Missing required property: 'mapOfArr: Map'")) + } + if !_mapOfObjSet { + panic(reader.Context().PrintWithContext("Missing required property: 'mapOfObj: Map'")) + } + if !_mapOfArrOfObjSet { + panic(reader.Context().PrintWithContext("Missing required property: 'mapOfArrOfObj: Map'")) + } + return &CustomType{ + Str: _str, + OptStr: _optStr, + U: _u, + OptU: _optU, + M_u8: _u8, + M_u16: _u16, + M_u32: _u32, + I: _i, + M_i8: _i8, + M_i16: _i16, + M_i32: _i32, + Bigint: _bigint, + OptBigint: _optBigint, + Bignumber: _bignumber, + OptBignumber: _optBignumber, + Json: _json, + OptJson: _optJson, + Bytes: _bytes, + OptBytes: _optBytes, + M_boolean: _boolean, + OptBoolean: _optBoolean, + UArray: _uArray, + UOptArray: _uOptArray, + OptUOptArray: _optUOptArray, + OptStrOptArray: _optStrOptArray, + UArrayArray: _uArrayArray, + UOptArrayOptArray: _uOptArrayOptArray, + UArrayOptArrayArray: _uArrayOptArrayArray, + CrazyArray: _crazyArray, + Object: _object, + OptObject: _optObject, + ObjectArray: _objectArray, + OptObjectArray: _optObjectArray, + En: _en, + OptEnum: _optEnum, + EnumArray: _enumArray, + OptEnumArray: _optEnumArray, + Map: _map, + MapOfArr: _mapOfArr, + MapOfObj: _mapOfObj, + MapOfArrOfObj: _mapOfArrOfObj, + } +} diff --git a/packages/test-cases/cases/bind/sanity/output/wasm-go/types/env.go b/packages/test-cases/cases/bind/sanity/output/wasm-go/types/object_env.go similarity index 100% rename from packages/test-cases/cases/bind/sanity/output/wasm-go/types/env.go rename to packages/test-cases/cases/bind/sanity/output/wasm-go/types/object_env.go diff --git a/packages/test-cases/cases/bind/sanity/output/wasm-go/types/env_serialization.go b/packages/test-cases/cases/bind/sanity/output/wasm-go/types/object_env_serialization.go similarity index 99% rename from packages/test-cases/cases/bind/sanity/output/wasm-go/types/env_serialization.go rename to packages/test-cases/cases/bind/sanity/output/wasm-go/types/object_env_serialization.go index 5b7e8a9345..802539ef32 100644 --- a/packages/test-cases/cases/bind/sanity/output/wasm-go/types/env_serialization.go +++ b/packages/test-cases/cases/bind/sanity/output/wasm-go/types/object_env_serialization.go @@ -106,7 +106,6 @@ func readEnv(reader msgpack.Read) *Env { if !_propSet { panic(reader.Context().PrintWithContext("Missing required property: 'prop: String'")) } - return &Env{ Prop: _prop, OptProp: _optProp, From a0a1151c87f1146290f7a4f80ab7bc60e5e9ddb6 Mon Sep 17 00:00:00 2001 From: n0cte Date: Tue, 23 Aug 2022 00:42:36 +0300 Subject: [PATCH 3/5] fix property name for args --- .../bind/src/bindings/golang/wasm-go/templates/main-go.mustache | 2 +- packages/test-cases/cases/bind/sanity/output/wasm-go/main.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/templates/main-go.mustache b/packages/schema/bind/src/bindings/golang/wasm-go/templates/main-go.mustache index c4f9dfc4e8..9e70cc5fbd 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/templates/main-go.mustache +++ b/packages/schema/bind/src/bindings/golang/wasm-go/templates/main-go.mustache @@ -8,7 +8,7 @@ import ( //export _wrap_invoke func _wrap_invoke(methodSize, argsSize, envSize uint32) bool { args := polywrap.WrapInvokeArgs(methodSize, argsSize) - switch args.method { + switch args.Method { {{#moduleType}} {{#methods}} case "{{name}}": 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 5c9d9a022a..23ef94b6fb 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 @@ -8,7 +8,7 @@ import ( //export _wrap_invoke func _wrap_invoke(methodSize, argsSize, envSize uint32) bool { args := polywrap.WrapInvokeArgs(methodSize, argsSize) - switch args.method { + switch args.Method { case "moduleMethod": return polywrap.WrapInvoke(args, envSize, module.ModuleMethodWrapped) case "objectMethod": From f7bb1f67f5d0e5cd9842c1484fceed3c0f8031e4 Mon Sep 17 00:00:00 2001 From: n0cte Date: Tue, 23 Aug 2022 12:11:05 +0300 Subject: [PATCH 4/5] fix schema output structure --- .../bind/src/bindings/golang/wasm-go/index.ts | 40 +++++++++++++------ 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/index.ts b/packages/schema/bind/src/bindings/golang/wasm-go/index.ts index bd4823ffda..025f9ed4e5 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/index.ts +++ b/packages/schema/bind/src/bindings/golang/wasm-go/index.ts @@ -19,7 +19,7 @@ const templatePath = (subpath: string) => path.join(__dirname, "./templates", subpath); function camel2snake(str: string): string { - str = str.replace( /([A-Z])/g, "_$1"); + str = str.replace(/([A-Z])/g, "_$1"); str = str.startsWith("_") ? str.slice(1) : str; return str.toLowerCase(); } @@ -49,18 +49,6 @@ export const generateBinding: GenerateBindingFn = ( }); } - // if (abi.moduleType) { - // output.entries.push({ - // type: "Directory", - // name: "types", - // data: renderTemplates( - // templatePath("types"), - // abi.moduleType, - // subTemplates - // ), - // }); - // } - // Generate imported folder const importEntries: OutputEntry[] = []; @@ -173,10 +161,36 @@ export const generateBinding: GenerateBindingFn = ( // Generate root entry file output.entries.push(...renderTemplates(templatePath(""), abi, subTemplates)); + output.entries = mergePaths(output.entries); return result; }; +function mergePaths(array: OutputEntry[]): OutputEntry[] { + const tmp: { [key: string]: OutputEntry } = {}; + for (let i = 0; i < array.length; i++) { + switch (array[i].type) { + case "File": + tmp[array[i].name] = array[i] + break; + case "Directory": + if (!tmp[array[i].name]) { + tmp[array[i].name] = array[i] + } else { + (tmp[array[i].name].data as OutputEntry[]).push(...(array[i].data as OutputEntry[])) + } + break; + } + } + array = Object.values(tmp); + for (let i = 0; i < array.length; i++) { + if (array[i].type === "Directory") { + array[i].data = mergePaths(array[i].data as OutputEntry[]); + } + } + return array; +} + function applyTransforms(abi: Abi): Abi { const transforms = [ extendType(Functions), From e38f83a68aae0350db4edd1dd0130909b76521a5 Mon Sep 17 00:00:00 2001 From: n0cte Date: Tue, 23 Aug 2022 15:18:10 +0300 Subject: [PATCH 5/5] fix code style --- .../src/bindings/golang/wasm-go/functions.ts | 186 +++++++++--------- .../bind/src/bindings/golang/wasm-go/index.ts | 14 +- .../bind/src/bindings/utils/templates.ts | 5 +- packages/schema/bind/src/types.ts | 7 +- 4 files changed, 114 insertions(+), 98 deletions(-) diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/functions.ts b/packages/schema/bind/src/bindings/golang/wasm-go/functions.ts index 1265e1d045..d73d73fb4b 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/functions.ts +++ b/packages/schema/bind/src/bindings/golang/wasm-go/functions.ts @@ -1,4 +1,3 @@ -import { isBaseType } from "./baseTypes"; import { reservedWordsAS } from "./reservedWords"; import { MustacheFn } from "../../types"; @@ -7,30 +6,30 @@ 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 => { @@ -38,9 +37,12 @@ export const prevFullIter: MustacheFn = () => { 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 => { @@ -48,90 +50,103 @@ export const lastFullIter: MustacheFn = () => { 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 = ["github.com/consideritdone/polywrap-go/polywrap/msgpack"]; + const imports: Array = [ + "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 => { @@ -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; }; @@ -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; } @@ -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); diff --git a/packages/schema/bind/src/bindings/golang/wasm-go/index.ts b/packages/schema/bind/src/bindings/golang/wasm-go/index.ts index 025f9ed4e5..edb7df1988 100644 --- a/packages/schema/bind/src/bindings/golang/wasm-go/index.ts +++ b/packages/schema/bind/src/bindings/golang/wasm-go/index.ts @@ -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 + ), }); } @@ -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; } diff --git a/packages/schema/bind/src/bindings/utils/templates.ts b/packages/schema/bind/src/bindings/utils/templates.ts index 07e0d96949..6b91c0b3de 100644 --- a/packages/schema/bind/src/bindings/utils/templates.ts +++ b/packages/schema/bind/src/bindings/utils/templates.ts @@ -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; } diff --git a/packages/schema/bind/src/types.ts b/packages/schema/bind/src/types.ts index 5f3da26511..f51800eba4 100644 --- a/packages/schema/bind/src/types.ts +++ b/packages/schema/bind/src/types.ts @@ -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;