Skip to content

Commit

Permalink
chore: zig fmt fixes in Zig 0.12
Browse files Browse the repository at this point in the history
  • Loading branch information
sea-grass committed Feb 23, 2024
1 parent 4b05de2 commit 6564f1c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 26 deletions.
11 changes: 7 additions & 4 deletions examples/zig-kv/build.zig
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
const std = @import("std");

const examples = [1][]const u8{ "worker-kv" };
const examples = [1][]const u8{"worker-kv"};

pub fn build(b: *std.Build) !void {
const target = try std.zig.CrossTarget.parse(.{ .arch_os_abi = "wasm32-wasi" });
const target = b.resolveTargetQuery(.{
.cpu_arch = .wasm32,
.os_tag = .wasi,
});
const optimize = b.standardOptimizeOption(.{});

const worker_module = b.createModule(.{
.source_file = .{ .path = "../../kits/zig/worker/src/worker.zig" },
.root_source_file = .{ .path = "../../kits/zig/worker/src/worker.zig" },
});

inline for (examples) |example| {
Expand All @@ -19,7 +22,7 @@ pub fn build(b: *std.Build) !void {
});

exe.wasi_exec_model = .reactor;
exe.addModule("worker", worker_module);
exe.root_module.addImport("worker", worker_module);

b.installArtifact(exe);
}
Expand Down
10 changes: 5 additions & 5 deletions examples/zig-kv/src/worker-kv.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ fn requestFn(resp: *worker.Response, r: *worker.Request) void {
var cache = r.context.cache;
var counter: i32 = 0;

var v = cache.getOrPut("counter") catch undefined;
const v = cache.getOrPut("counter") catch undefined;

if (!v.found_existing) {
v.value_ptr.* = "0";
} else {
var counterValue = v.value_ptr.*;
var num = std.fmt.parseInt(i32, counterValue, 10) catch undefined;
const counterValue = v.value_ptr.*;
const num = std.fmt.parseInt(i32, counterValue, 10) catch undefined;
counter = num + 1;
var num_s = std.fmt.allocPrint(allocator, "{d}", .{ counter }) catch undefined;
const num_s = std.fmt.allocPrint(allocator, "{d}", .{counter}) catch undefined;
_ = cache.put("counter", num_s) catch undefined;
}

Expand All @@ -35,7 +35,7 @@ fn requestFn(resp: *worker.Response, r: *worker.Request) void {
\\</body>
;

var body = std.fmt.allocPrint(allocator, s, .{ counter }) catch undefined; // add useragent
const body = std.fmt.allocPrint(allocator, s, .{counter}) catch undefined; // add useragent

_ = &resp.headers.append("x-generated-by", "wasm-workers-server");
_ = &resp.writeAll(body);
Expand Down
42 changes: 25 additions & 17 deletions kits/zig/worker/src/worker.zig
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,19 @@ pub const Input = struct {
};

pub const Output = struct {
data: []const u8,
headers: std.StringArrayHashMap([]const u8),
status: u16,
base64: bool,
data: []const u8,
headers: std.StringArrayHashMap([]const u8),
status: u16,
base64: bool,

httpHeader: http.Headers,
httpHeader: http.Headers,

const Self = @This();

pub fn init() Self {
return .{
.data = "",
.headers = std.StringArrayHashMap([]const u8).init(allocator),
.headers = std.StringArrayHashMap([]const u8).init(allocator),
.status = 0,
.base64 = false,
.httpHeader = http.Headers.init(allocator),
Expand All @@ -80,7 +80,7 @@ pub const Output = struct {
self.status = statusCode;
}

pub fn write(self: *Self, response: Response) !u32 {
pub fn write(self: *Self, response: Response) !u32 {
self.base64 = response.base64;
if (response.base64) {
self.data = base64Encode(response.body);
Expand Down Expand Up @@ -124,7 +124,9 @@ pub const Output = struct {
const result = slice_stream.getWritten();

const stdout = std.io.getStdOut().writer();
try stdout.print("{s}", .{ result });
try stdout.print("{s}", .{result});

std.debug.print("{s}\n", .{result});

return self.data.len;
}
Expand All @@ -133,7 +135,7 @@ pub const Output = struct {
fn base64Encode(data: []const u8) []const u8 {
// This initializing Base64Encoder throws weird error if not wrapped in function (maybe Zig bug?)
var enc = std.base64.Base64Encoder.init(std.base64.standard_alphabet_chars, '=');
var data_len = enc.calcSize(data.len);
const data_len = enc.calcSize(data.len);
var buf: [16384]u8 = undefined;
return enc.encode(buf[0..data_len], data);
}
Expand All @@ -143,7 +145,7 @@ fn getHeadersJsonObject(s: std.StringArrayHashMap([]const u8)) !std.json.Value {

var i = s.iterator();
while (i.next()) |kv| {
try value.object.put(kv.key_ptr.*, std.json.Value{ .string = kv.value_ptr.*});
try value.object.put(kv.key_ptr.*, std.json.Value{ .string = kv.value_ptr.* });
}

return value;
Expand All @@ -154,7 +156,7 @@ fn getCacheJsonObject(s: std.StringHashMap([]const u8)) !std.json.Value {

var i = s.iterator();
while (i.next()) |entry| {
try value.object.put(entry.key_ptr.*, std.json.Value{ .string = entry.value_ptr.*});
try value.object.put(entry.key_ptr.*, std.json.Value{ .string = entry.value_ptr.* });
}

return value;
Expand All @@ -165,7 +167,8 @@ pub fn readInput() !Input {
var buf = std.io.bufferedReader(in.reader());
var r = buf.reader();

var msg = try r.readAllAlloc(allocator, std.math.maxInt(u32));
const msg = try r.readAllAlloc(allocator, std.math.maxInt(u32));
std.debug.print("in {s}\n", .{msg});
return getInput(msg);
}

Expand Down Expand Up @@ -224,12 +227,12 @@ pub fn getWriterRequest() !RequestAndOutput {
return std.os.exit(1);
};

var req = createRequest(&in) catch |err| {
const req = createRequest(&in) catch |err| {
std.debug.print("error creating request : {!}\n", .{err});
return std.os.exit(1);
};

var output = Output.init();
const output = Output.init();

return RequestAndOutput{
.request = req,
Expand All @@ -250,17 +253,22 @@ pub const Context = struct {
};

pub fn ServeFunc(requestFn: *const fn (*Response, *Request) void) void {
var r = try getWriterRequest();
const r = try getWriterRequest();
var request = r.request;
var output = r.output;

var response = Response{ .body = "", .base64 = false, .headers = http.Headers.init(allocator), .request = request, };
var response = Response{
.body = "",
.base64 = false,
.headers = http.Headers.init(allocator),
.request = request,
};

requestFn(&response, &request);

output.httpHeader = response.headers;

_ = output.write(response) catch |err| {
std.debug.print("error writing data: {!} \n", .{ err });
std.debug.print("error writing data: {!} \n", .{err});
};
}

0 comments on commit 6564f1c

Please sign in to comment.