parser.value.convertTo: add field converter concept

It is convenient to be able to have custom logic for a specific field
on a given struct without having to write a function to manually reify
the whole thing from scratch.
This commit is contained in:
torque 2024-06-18 18:32:16 -07:00
parent c74d615131
commit 8aaceba484
Signed by: torque
SSH Key Fingerprint: SHA256:nCrXefBNo6EbjNSQhv0nXmEg/VuNq3sMF5b8zETw3Tk

View File

@ -68,6 +68,10 @@ pub const Value = union(enum) {
map: Map,
inline_map: Map,
pub fn FieldConverter(comptime T: type) type {
return *const fn (Value, std.mem.Allocator, Options) error{BadValue}!T;
}
pub fn convertTo(self: Value, comptime T: type, allocator: std.mem.Allocator, options: Options) !T {
switch (@typeInfo(T)) {
.Void => {
@ -215,7 +219,11 @@ pub const Value = union(enum) {
var use_count: usize = 0;
inline for (stt.fields) |field| {
if (map.get(field.name)) |val| {
@field(result, field.name) = try val.convertTo(field.type, allocator, options);
if (comptime hasFn(T, "niceFieldConverter") and T.niceFieldConverter(field.name) != null) {
@field(result, field.name) = try T.niceFieldConverter(field.name).?(val, allocator, options);
} else {
@field(result, field.name) = try val.convertTo(field.type, allocator, options);
}
use_count += 1;
} else if (options.allow_omitting_default_values) {
if (comptime field.default_value) |def|