Skip to content

Commit

Permalink
json buffer serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
mhmd-azeez committed Sep 16, 2024
1 parent e6fe33b commit ae15d7c
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ jobs:
echo "Running xtp plugin init"
xtp plugin init --schema-file schema.yaml --template ../template-bundle --path exampleplugin --name exampleplugin --feature stub-with-code-samples
echo "Running xtp plugin build"
script -q -c "xtp plugin build --path exampleplugin" /dev/stdout
xtp plugin build --path exampleplugin
echo "Running xtp plugin test"
xtp plugin test exampleplugin/dist/plugin.wasm --with test.wasm --mock-host mock.wasm
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"typescript": "^5.3.2"
},
"dependencies": {
"@dylibso/xtp-bindgen": "1.0.0-rc.4",
"@dylibso/xtp-bindgen": "1.0.0-rc.8",
"ejs": "^3.1.10"
}
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function toCSharpType(property: Property): string {
if (property.format === "double") {
return "Double";
}
return "long";
return "Int64";
case "integer":
return "Int32";
case "boolean":
Expand Down
30 changes: 29 additions & 1 deletion template/Host.cs.ejs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
namespace <%- project.name %>;

using Extism;

using System;
using System.Runtime.InteropServices;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Json.Nodes;
using Extism;
using System.Buffers.Text;
using System.Buffers;

public class Host
{
Expand Down Expand Up @@ -135,6 +139,9 @@ public class Host
{
<% Object.values(s.properties).forEach(p => { %>
[JsonPropertyName("<%- p.name %>")]
<% if (p.type === 'buffer') { %>
[JsonConverter(typeof(Base64JsonConverter))]
<% } %>
public <%- toCSharpType(p) %> <%- toPascalCase(p.name) %> { get; set; }
<% }) %>
}
Expand All @@ -152,6 +159,27 @@ public partial class PdkJsonContext : JsonSerializerContext
{
var options = new JsonSerializerOptions();
options.Converters.Add(new JsonStringEnumConverter());
options.Converters.Add(new Base64JsonConverter());

return options;
}
}

public class Base64JsonConverter : JsonConverter<byte[]>
{
public override byte[] Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.String)
{
throw new JsonException("Expected string value for Base64 encoded data.");
}

string base64String = reader.GetString();
return Convert.FromBase64String(base64String);
}

public override void Write(Utf8JsonWriter writer, byte[] value, JsonSerializerOptions options)
{
writer.WriteStringValue(Convert.ToBase64String(value));
}
}
Empty file added template/dist/.ignore.ejs
Empty file.
4 changes: 2 additions & 2 deletions template/xtp.toml.ejs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
app_id = "<%- project.appId %>"

# This is where 'xtp plugin push' expects to find the DLL file after the build script has run.
bin = "dist/Plugin.wasm"
bin = "dist/plugin.wasm"
extension_point_id = "<%- project.extensionPointId %>"
# This is the 'binding' name used for the plugin.
name = "<%- project.name %>"

[scripts]
# xtp plugin build runs this script to generate the DLL file
build = "mkdir dist 2>nul && dotnet publish -c Release && cp bin/Release/wasi-wasm/AppBundle/<%-project.name%>.wasm dist/plugin.wasm"
build = "dotnet publish -c Release && cp bin/Release/wasi-wasm/AppBundle/<%-project.name%>.wasm dist/plugin.wasm"

# xtp plugin init runs this script to format the code
format = "dotnet format && mv Plugin.csproj <%-project.name%>.csproj 2>nul"
Expand Down
7 changes: 2 additions & 5 deletions tests/schemas/fruit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ imports:
description: the raw byte values at key
kv_write:
description: kvwrite
contentType: application/json
input:
contentType: application/json
"$ref": "#/components/schemas/WriteParams"
Expand Down Expand Up @@ -92,8 +91,6 @@ components:
- inky
- clyde
description: A set of all the enemies of pac-man
AnonymousObject:
type: object
ComplexObject:
properties:
ghost:
Expand All @@ -120,6 +117,6 @@ components:
"$ref": "#/components/schemas/WriteParams"
nullable: true
anon:
"$ref": "#/components/schemas/AnonymousObject"
type: object
nullable: true
description: A complex json object
description: A complex json object

0 comments on commit ae15d7c

Please sign in to comment.