Skip to content

Commit

Permalink
Merge pull request #7 from dylibso/fix-zig-example
Browse files Browse the repository at this point in the history
fix(example): zig plugin bindgen schema update
  • Loading branch information
nilslice authored Oct 25, 2024
2 parents 756d05e + 6bc86df commit 3f3a0e8
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 23 deletions.
2 changes: 1 addition & 1 deletion examples/ziggity/build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.{
.name = "ziggity",
.name = "sandshrew",
// This is a [Semantic Version](https://semver.org/).
// In a future version of Zig it will be used for package deduplication.
.version = "0.0.0",
Expand Down
16 changes: 13 additions & 3 deletions examples/ziggity/src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,23 @@ pub fn handle(input: schema.IncomingEvent) !void {
};

plugin.log(.Debug, message.id);
_ = try Host.react(schema.OutgoingReaction{
var result = try Host.react(schema.OutgoingReaction{
.messageId = message.id,
.with = "🦎",
});
if (result.errorCode != 0) {
const code = result.errorCode;
const id = result.id;
plugin.log(.Debug, try std.fmt.allocPrint(plugin.allocator, "first reaction failed: {any} ({d})", .{ id, code }));
}

_ = try Host.react(schema.OutgoingReaction{
result = try Host.react(schema.OutgoingReaction{
.messageId = message.id,
.with = "⚡️",
.with = "🚀",
});
if (result.errorCode != 0) {
const code = result.errorCode;
const id = result.id;
plugin.log(.Debug, try std.fmt.allocPrint(plugin.allocator, "second reaction failed: {any} ({d})", .{ id, code }));
}
}
4 changes: 2 additions & 2 deletions examples/ziggity/src/pdk.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
const std = @import("std");
const extism = @import("extism-pdk");

const _plugin = extism.Plugin.init(std.heap.wasm_allocator);

const user = @import("main.zig");
const schema = @import("schema.zig");

const _plugin = extism.Plugin.init(std.heap.wasm_allocator);

const ERR_PRINTING_MSG: []const u8 = "std.fmt.allocPrint failed when formatting plugin error";

export fn handle() i32 {
Expand Down
66 changes: 51 additions & 15 deletions examples/ziggity/src/schema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub const Host = struct {
defer outMem.free();
const buffer = try _plugin.allocator.alloc(u8, @intCast(outMem.length));
outMem.load(buffer);
const out = try std.json.parseFromSlice(HandlerResult, _plugin.allocator, buffer, .{ .allocate = .alloc_always });
const out = try std.json.parseFromSlice(HandlerResult, _plugin.allocator, buffer, .{ .allocate = .alloc_always, .ignore_unknown_fields = true });
return out.value;
}

Expand All @@ -56,7 +56,7 @@ pub const Host = struct {
defer outMem.free();
const buffer = try _plugin.allocator.alloc(u8, @intCast(outMem.length));
outMem.load(buffer);
const out = try std.json.parseFromSlice(HandlerResult, _plugin.allocator, buffer, .{ .allocate = .alloc_always });
const out = try std.json.parseFromSlice(HandlerResult, _plugin.allocator, buffer, .{ .allocate = .alloc_always, .ignore_unknown_fields = true });
return out.value;
}

Expand All @@ -79,7 +79,7 @@ pub const Host = struct {
defer outMem.free();
const buffer = try _plugin.allocator.alloc(u8, @intCast(outMem.length));
outMem.load(buffer);
const out = try std.json.parseFromSlice(HandlerResult, _plugin.allocator, buffer, .{ .allocate = .alloc_always });
const out = try std.json.parseFromSlice(HandlerResult, _plugin.allocator, buffer, .{ .allocate = .alloc_always, .ignore_unknown_fields = true });
return out.value;
}

Expand All @@ -101,17 +101,37 @@ pub const Host = struct {
defer outMem.free();
const buffer = try _plugin.allocator.alloc(u8, @intCast(outMem.length));
outMem.load(buffer);
const out = try std.json.parseFromSlice(HandlerResult, _plugin.allocator, buffer, .{ .allocate = .alloc_always });
const out = try std.json.parseFromSlice(HandlerResult, _plugin.allocator, buffer, .{ .allocate = .alloc_always, .ignore_unknown_fields = true });
return out.value;
}
};

/// An emoji used to react
pub const Emoji = struct {
/// whether or not the emoji is animated
animated: bool,
/// The id of the reaction (if custom); null if a built-in emoji
id: ?[]const u8 = null,
/// the name used for the reactji; built-in emoji will be the literal character, otherwise the text name appears here
name: []const u8,

/// Internally used function, should not be called by plugin authors.
pub fn XXX__decodeBase64Fields(self: *Emoji) !*Emoji {
return self;
}

/// Internally used function, should not be called by plugin authors.
pub fn XXX__encodeBase64Fields(self: *Emoji) !*Emoji {
return self;
}
};

/// A result.
pub const HandlerResult = struct {
/// An error code. Zero indicates success. Negative numbers indicate failure.
errorCode: i64,
/// An id for the result
id: ?[]const u8,
id: ?[]const u8 = null,

/// Internally used function, should not be called by plugin authors.
pub fn XXX__decodeBase64Fields(self: *HandlerResult) !*HandlerResult {
Expand Down Expand Up @@ -141,22 +161,34 @@ pub const IncomingEvent = struct {

/// Internally used function, should not be called by plugin authors.
pub fn XXX__decodeBase64Fields(self: *IncomingEvent) !*IncomingEvent {
self.message = (try self.message.?.XXX__decodeBase64Fields()).*;
if (self.message != null) {
self.message = (try self.message.?.XXX__decodeBase64Fields()).*;
}

self.reaction = (try self.reaction.?.XXX__decodeBase64Fields()).*;
if (self.reaction != null) {
self.reaction = (try self.reaction.?.XXX__decodeBase64Fields()).*;
}

self.response = (try self.response.?.XXX__decodeBase64Fields()).*;
if (self.response != null) {
self.response = (try self.response.?.XXX__decodeBase64Fields()).*;
}

return self;
}

/// Internally used function, should not be called by plugin authors.
pub fn XXX__encodeBase64Fields(self: *IncomingEvent) !*IncomingEvent {
self.message = (try self.message.?.XXX__encodeBase64Fields()).*;
if (self.message != null) {
self.message = (try self.message.?.XXX__encodeBase64Fields()).*;
}

self.reaction = (try self.reaction.?.XXX__encodeBase64Fields()).*;
if (self.reaction != null) {
self.reaction = (try self.reaction.?.XXX__encodeBase64Fields()).*;
}

self.response = (try self.response.?.XXX__encodeBase64Fields()).*;
if (self.response != null) {
self.response = (try self.response.?.XXX__encodeBase64Fields()).*;
}

return self;
}
Expand Down Expand Up @@ -190,20 +222,24 @@ pub const IncomingReaction = struct {
from: []const u8,
/// An incoming message
message: IncomingMessage,
/// The emoji reaction
with: []const u8,
/// An emoji used to react
with: Emoji,

/// Internally used function, should not be called by plugin authors.
pub fn XXX__decodeBase64Fields(self: *IncomingReaction) !*IncomingReaction {
self.message = (try self.message.XXX__decodeBase64Fields()).*;

self.with = (try self.with.XXX__decodeBase64Fields()).*;

return self;
}

/// Internally used function, should not be called by plugin authors.
pub fn XXX__encodeBase64Fields(self: *IncomingReaction) !*IncomingReaction {
self.message = (try self.message.XXX__encodeBase64Fields()).*;

self.with = (try self.with.XXX__encodeBase64Fields()).*;

return self;
}
};
Expand Down Expand Up @@ -233,11 +269,11 @@ pub const IncomingResponse = struct {
/// An outgoing message
pub const OutgoingMessage = struct {
/// The channel the message was received in
channel: ?[]const u8,
channel: ?[]const u8 = null,
/// The message text
message: []const u8,
/// A message ID to reply to
reply: ?[]const u8,
reply: ?[]const u8 = null,

/// Internally used function, should not be called by plugin authors.
pub fn XXX__decodeBase64Fields(self: *OutgoingMessage) !*OutgoingMessage {
Expand Down
4 changes: 2 additions & 2 deletions examples/ziggity/xtp.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
app_01j8r463d3f6c91hdevkctb7cw = "app_01j8r463d3f6c91hdevkctb7cw"
app_id = "app_01j8r463d3f6c91hdevkctb7cw"

# This is where 'xtp plugin push' expects to find the wasm file after the build script has run.
bin = "zig-out/bin/plugin.wasm"
extension_point_id = "ext_01j8r4jqawfd6tjxh85t3988kc"
name = "ziggity"
name = "sandshrew"

[scripts]

Expand Down

0 comments on commit 3f3a0e8

Please sign in to comment.