Skip to content

Commit

Permalink
chore: add example and test
Browse files Browse the repository at this point in the history
  • Loading branch information
nilslice committed Jan 4, 2024
1 parent f681b56 commit 6ff6fe6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,10 @@ jobs:
echo $TEST | grep 'A debug log!'
echo $TEST | grep 'A warning log!'
echo $TEST | grep 'An error log!'
TEST=$(extism call zig-out/bin/basic-example.wasm json_input --input '{"name": "Yoda", "age": 900}')
echo $TEST | grep 'Hello, Yoda. You are 900 years old!'
TEST=$(extism call zig-out/bin/basic-example.wasm json_output)
echo $TEST | grep '["first thing","second thing","third thing"]'
36 changes: 36 additions & 0 deletions examples/basic.zig
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,42 @@ export fn count_vowels() i32 {
return 0;
}

const Input = struct {
name: []const u8,
age: u16,
};

export fn json_input() i32 {
const plugin = Plugin.init(allocator);
const json = plugin.getJson(Input, .{}) catch unreachable; // you must call .deinit() when done
defer json.deinit();

const input: Input = json.value();
const out = std.fmt.allocPrint(allocator, "Hello, {s}. You are {d} years old!\n", .{ input.name, input.age }) catch unreachable;

plugin.output(out);
return 0;
}

const Result = struct {
things: [3][]const u8,
};

export fn json_output() i32 {
const plugin = Plugin.init(allocator);
const data = [_][]const u8{
"first thing",
"second thing",
"third thing",
};

const result = Result{ .things = data };

plugin.outputJson(result, .{}) catch unreachable;

return 0;
}

export fn http_get() i32 {
const plugin = Plugin.init(allocator);
// create an HTTP request via Extism built-in function (doesn't require WASI)
Expand Down

0 comments on commit 6ff6fe6

Please sign in to comment.