Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: avoid needless html-escaping template values #1

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion template/build.zig.zon.ejs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.{
.name = "<%= project.name %>",
.name = "<%- project.name %>",
// 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
8 changes: 4 additions & 4 deletions template/src/main.zig.ejs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
const schema = @import("schema.zig");
<%= schema.imports && schema.imports.length > 0 ? "const Host = schema.Host;" : null %>
<%- schema.imports && schema.imports.length > 0 ? "const Host = schema.Host;" : null %>

<% schema.exports.forEach(ex => { %>
<% if (hasComment(ex)) -%>
/// <%- formatCommentBlock(ex.description, "/// ") %>
<% if (ex.input && hasComment(ex.input)) { -%>
/// It takes <%= toZigType(ex.input) %> as input (<%- formatCommentLine(ex.input.description) %>)
/// It takes <%- toZigType(ex.input) %> as input (<%- formatCommentLine(ex.input.description) %>)
<% } -%>
<% if (ex.output && hasComment(ex.output)) { -%>
/// And returns <%= toZigType(ex.output) %> (<%- formatCommentLine(ex.output.description) %>)
/// And returns <%- toZigType(ex.output) %> (<%- formatCommentLine(ex.output.description) %>)
<% } -%>
<% -%>
pub fn <%= ex.name %>(<%- ex.input ? `input: ${toZigType(ex.input, "schema")}` : null %>) !<%- ex.output ? `${toZigType(ex.output, "schema")}` : "void" %> {
pub fn <%- ex.name %>(<%- ex.input ? `input: ${toZigType(ex.input, "schema")}` : null %>) !<%- ex.output ? `${toZigType(ex.output, "schema")}` : "void" %> {
<% if (featureFlags['stub-with-code-samples'] && codeSamples(ex, 'zig').length > 0) { -%>
<%- codeSamples(ex, 'zig')[0].source %>
<% } else { -%>
Expand Down
10 changes: 5 additions & 5 deletions template/src/pdk.zig.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const _plugin = extism.Plugin.init(std.heap.wasm_allocator);
const ERR_PRINTING_MSG: []const u8 = "std.fmt.allocPrint failed when formatting plugin error";

<% schema.exports.forEach(ex => { -%>
export fn <%= ex.name %>() i32 {
export fn <%- ex.name %>() i32 {
<% if (ex.input) { -%>
// Get the input data
<% if (ex.input.contentType === 'application/json') { -%>
Expand Down Expand Up @@ -50,27 +50,27 @@ export fn <%= ex.name %>() i32 {

// Call the implementation function
<% if (ex.output) { -%>
const output = user.<%= ex.name %>(input) catch |err| {
const output = user.<%- ex.name %>(input) catch |err| {
const msg = std.fmt.allocPrint(_plugin.allocator, "{}", .{err}) catch ERR_PRINTING_MSG;
_plugin.setError(msg);
return -1;
};
<% } else { -%>
user.<%= ex.name %>(input) catch |err| {
user.<%- ex.name %>(input) catch |err| {
const msg = std.fmt.allocPrint(_plugin.allocator, "{}", .{err}) catch ERR_PRINTING_MSG;
_plugin.setError(msg);
return -1;
};
<% } -%>
<% } else { -%>
<% if (ex.output) { -%>
const output = user.<%= ex.name %>() catch |err| {
const output = user.<%- ex.name %>() catch |err| {
const msg = std.fmt.allocPrint(_plugin.allocator, "{}", .{err}) catch ERR_PRINTING_MSG;
_plugin.setError(msg);
return -1;
};
<% } else { -%>
user.<%= ex.name %>() catch |err| {
user.<%- ex.name %>() catch |err| {
const msg = std.fmt.allocPrint(_plugin.allocator, "{}", .{err}) catch ERR_PRINTING_MSG;
_plugin.setError(msg);
return -1;
Expand Down
22 changes: 11 additions & 11 deletions template/src/schema.zig.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const DateTime = @import("datetime").DateTime;
const _host = struct {
<% if (schema.imports.length > 0) { -%>
<% Object.values(schema.imports).forEach(imp => { -%>
extern "extism:host/user" fn <%= imp.name %>(u64) <%= imp.output ? "u64" : "void" %>;
extern "extism:host/user" fn <%- imp.name %>(u64) <%- imp.output ? "u64" : "void" %>;
<% }) -%>
<% } -%>
};
Expand All @@ -23,12 +23,12 @@ pub const Host = struct {
<% if (hasComment(imp)) -%>
/// <%- formatCommentBlock(imp.description, "/// ") %>
<% if (imp.input && hasComment(imp.input)) { -%>
/// It takes input of <%= toZigType(imp.input) %> (<%- formatCommentLine(imp.input.description) %>)
/// It takes input of <%- toZigType(imp.input) %> (<%- formatCommentLine(imp.input.description) %>)
<% } -%>
<% if (imp.output && hasComment(imp.output)) { -%>
/// And it returns an output <%= toZigType(imp.output) %> (<%- formatCommentLine(imp.output.description) %>)
/// And it returns an output <%- toZigType(imp.output) %> (<%- formatCommentLine(imp.output.description) %>)
<% } -%>
pub fn <%= imp.name %>(<%- imp.input ? `input: ${toZigType(imp.input)}` : null %>) !<%- imp.output ? `${toZigType(imp.output)}` : "void" %> {
pub fn <%- imp.name %>(<%- imp.input ? `input: ${toZigType(imp.input)}` : null %>) !<%- imp.output ? `${toZigType(imp.output)}` : "void" %> {
<% if (imp.input) { -%>
<% if (imp.input.contentType === 'application/json') { -%>
const b = try std.json.stringifyAlloc(_plugin.allocator, input, .{});
Expand All @@ -50,9 +50,9 @@ pub const Host = struct {
}
defer inMem.free();

<% if (imp.output) { -%>const ptr =<% } -%> _host.<%= imp.name %>(inMem.offset);
<% if (imp.output) { -%>const ptr =<% } -%> _host.<%- imp.name %>(inMem.offset);
<% } else { -%>
<% if (imp.output) { -%>const ptr =<% } -%> _host.<%= imp.name %>();
<% if (imp.output) { -%>const ptr =<% } -%> _host.<%- imp.name %>();
<% } -%>
<% if (imp.output) { -%>
if (ptr == 0) {
Expand All @@ -63,7 +63,7 @@ pub const Host = struct {
<% if (imp.output.contentType === 'application/json') { -%>
const buffer = try _plugin.allocator.alloc(u8, @intCast(outMem.length));
outMem.load(buffer);
const out = try std.json.parseFromSlice(<%= toZigType(imp.output) %>, _plugin.allocator, buffer, .{ .allocate = .alloc_always });
const out = try std.json.parseFromSlice(<%- toZigType(imp.output) %>, _plugin.allocator, buffer, .{ .allocate = .alloc_always });
return out.value;
<% } else if (imp.output.contentType === 'text/plain; charset=UTF-8') { -%>
const buffer = try _plugin.allocator.alloc(u8, @intCast(outMem.length));
Expand All @@ -90,21 +90,21 @@ pub const Host = struct {
<% Object.values(schema.schemas).forEach(schema => { %>
<% if (schema.properties.length > 0) { -%>
/// <%- formatCommentBlock(schema.description, "/// ") %>
pub const <%= makeStructName(schema.name) %> = struct {
pub const <%- makeStructName(schema.name) %> = struct {
<% schema.properties.forEach(p => { -%>
<% if (p.description) { -%>
/// <%- formatCommentBlock(p.description, "/// ") %>
<% } -%>
<%= p.name %>: <%= p.nullable ? "?" : null %><%= toZigType(p) %>,
<%- p.name %>: <%- p.nullable ? "?" : null %><%- toZigType(p) %>,
<% }) %>
};
<% } else if (schema.enum) { %>
<% if (schema.description) { -%>
/// <%- formatCommentBlock(schema.description, "/// ") %>
<% } -%>
pub const <%= makeStructName(schema.name) %> = enum {
pub const <%- makeStructName(schema.name) %> = enum {
<% schema.enum.forEach((variant) => { -%>
<%= variant %>,
<%- variant %>,
<% }) -%>
};
<% } -%>
Expand Down
6 changes: 3 additions & 3 deletions template/xtp.toml.ejs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
app_id = "<%= project.appId %>"
app_id = "<%- project.appId %>"

# 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 = "<%= project.extensionPointId %>"
extension_point_id = "<%- project.extensionPointId %>"

# This is the 'binding' name used for the plugin.
name = "<%= project.name %>"
name = "<%- project.name %>"

[scripts]
# xtp plugin build runs this script to generate the wasm file
Expand Down
Loading