nats.zig/examples/headers.zig
torque b453ec3d92
all: add missing copyright headers
Tests are CC0/public domain because there's no reason for them not to
be. Examples are also CC0/public domain, but this may be a little bit
weird because they are largely straightforward ports of examples from
nats.c which carry the Apache license. However, I personally wrote
them against the zig bindings and I doubt anyone will end up in a
court of law due to their software containing uselessly trivial
example code.
2023-09-02 15:04:46 -07:00

71 lines
2.1 KiB
Zig

// This file is licensed under the CC0 1.0 license.
// See: https://creativecommons.org/publicdomain/zero/1.0/legalcode
const std = @import("std");
const nats = @import("nats");
pub fn main() !void {
const connection = try nats.Connection.connectTo(nats.default_server_url);
defer connection.destroy();
const message = try nats.Message.create("subject", null, "message");
defer message.destroy();
try message.setHeaderValue("My-Key1", "value1");
try message.setHeaderValue("My-Key2", "value2");
try message.addHeaderValue("My-Key1", "value3");
try message.setHeaderValue("My-Key3", "value4");
try message.deleteHeader("My-Key3");
{
var iter = try message.headerIterator();
defer iter.destroy();
while (iter.next()) |resolv| {
var val_iter = try resolv.getValueIterator();
defer val_iter.destroy();
std.debug.print("key '{s}' got: ", .{resolv.key});
while (val_iter.next()) |value| {
std.debug.print("'{s}', ", .{value});
}
std.debug.print("\n", .{});
}
}
const subscription = try connection.subscribeSync("subject");
defer subscription.destroy();
try connection.publishMessage(message);
const received = try subscription.nextMessage(1000);
defer received.destroy();
{
var iter = try received.getHeaderValueIterator("My-Key1");
defer iter.destroy();
std.debug.print("For key 'My-Key1' got: ", .{});
while (iter.next()) |value| {
std.debug.print("'{s}', ", .{value});
}
std.debug.print("\n", .{});
}
_ = received.getHeaderValue("key-does-not-exist") catch |err| switch (err) {
nats.Error.NotFound => {},
else => {
std.debug.print("Should not have found that key!", .{});
return err;
},
};
received.deleteHeader("key-does-not-exist") catch |err| switch (err) {
nats.Error.NotFound => {},
else => {
std.debug.print("Should not have found that key!", .{});
return err;
},
};
}