I have a local PostgREST instance on which I’m making HTTP requests in Zig v0.14.1.
I’m following the official documentation, at the end of which a simple todo list API gets created.
The schema of a todo is
{
"id": integer,
"done": boolean,
"task": string,
"due": string or null
},
A POST request can be made by:
curl http://localhost:3000/todos -X POST \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{"task": "learn how to auth"}'
So the id
is determined by the database and is auto-incremented, the default value of done
is true
, and due
is null.
I made a todo struct with the following fields and method:
const Todo = struct {
done: bool,
task: []const u8,
due: ?[]const u8,
pub fn init() Todo {
return .{
.done = false, // better default when a new task is being created
.due = null, // same as the curl request
};
}
};
I’d like to understand:
Is the init
method the right way to deal with default values or should the fields be initialized to those values directly ?
and:
Since task
is the only field required to make a POST request, a method to create a task in Todo
can be along the lines of this one, in which the payload is passed directly (without serialization).
However, if a string was to be passed as a parameter
...
...
pub fn create(task_name: []const u8) !void {
}
how would a POST request look like on serializing the string to the struct?
(I’m not confident of the method’s signature)