Something went wrong. Try again.
fork of gitlab.com/devnw/zig/json for zig 0.17
Something went wrong. Try again.
1.3 kB · 42 lines
Zig
12345678910111213141516171819202122232425262728293031323334353637383940414243//! Zig 0.17 reports container fields as parallel slices (`field_names`,//! `field_types`, ...). `fieldAt` presents one field at a time, the shape the//! encoders and decoders work with.
const std = @import("std");const Type = std.builtin.Type;
pub const Field = struct { name: [:0]const u8, type: type, default_value_ptr: ?*const anyopaque,};
pub const EnumField = struct { name: [:0]const u8, value: comptime_int,};
fn FieldOf(comptime Info: type) type { return switch (Info) { Type.Struct, Type.Union => Field, Type.Enum => EnumField, else => @compileError("reflect.fieldAt: unsupported type info " ++ @typeName(Info)), };}
pub fn fieldAt(comptime info: anytype, comptime i: usize) FieldOf(@TypeOf(info)) { return switch (@TypeOf(info)) { Type.Struct => .{ .name = info.field_names[i], .type = info.field_types[i], .default_value_ptr = info.field_attrs[i].default_value_ptr, }, Type.Union => .{ .name = info.field_names[i], .type = info.field_types[i], .default_value_ptr = null, }, Type.Enum => .{ .name = info.field_names[i], .value = info.field_values[i] }, else => unreachable, };}