Skip to content

Commit cefc572

Browse files
authored
Merge pull request kristoff-it#57 from der-teufel-programming/fix-test
Fix serializer code after skip_fields addition
2 parents 69653d0 + 9a0d43e commit cefc572

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

src/ziggy/serializer.zig

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,21 @@ fn stringifyStructInner(
183183
depth: usize,
184184
opts: StringifyOptions,
185185
) !bool {
186-
const T = @typeInfo(@TypeOf(strct)).@"struct";
186+
const StructType = @TypeOf(strct);
187+
const T = @typeInfo(StructType).@"struct";
188+
const FE = std.meta.FieldEnum(StructType);
189+
const has_skip_fields: bool = @hasDecl(StructType, "ziggy_options") and @hasDecl(StructType.ziggy_options, "skip_fields");
187190
const field_count = blk: {
188191
var c: usize = 0;
189-
if (opts.emit_null_fields) break :blk T.fields.len;
190-
inline for (T.fields) |field| {
192+
outer: inline for (T.fields, 0..) |field, idx| {
193+
if (has_skip_fields) {
194+
const e: FE = @enumFromInt(idx);
195+
inline for (StructType.ziggy_options.skip_fields) |sf| {
196+
if (sf == e) continue :outer;
197+
}
198+
}
191199
switch (@typeInfo(field.type)) {
192-
.optional => if (@field(strct, field.name) != null) {
200+
.optional => if (opts.emit_null_fields or @field(strct, field.name) != null) {
193201
c += 1;
194202
},
195203
else => c += 1,
@@ -198,7 +206,14 @@ fn stringifyStructInner(
198206
break :blk c;
199207
};
200208
if (T.fields.len > 0) {
209+
var print_idx: usize = 1;
201210
blk: {
211+
if (has_skip_fields) {
212+
const z: FE = @enumFromInt(0);
213+
inline for (StructType.ziggy_options.skip_fields) |sf| {
214+
if (sf == z) break :blk;
215+
}
216+
}
202217
switch (@typeInfo(T.fields[0].type)) {
203218
.optional => if (!opts.emit_null_fields and @field(strct, T.fields[0].name) == null) break :blk,
204219
else => {},
@@ -216,21 +231,20 @@ fn stringifyStructInner(
216231
if (opts.whitespace != .minified or 1 != field_count) {
217232
try writer.writeAll(",");
218233
}
234+
print_idx += 1;
219235
}
220236

221237
outer: inline for (T.fields[1..], 2..) |field, idx| {
222238
// Skip fields mentioned under 'ziggy_options.skip_fields'
223-
if (@hasDecl(@TypeOf(strct), "ziggy_options")) {
224-
if (@hasDecl(@TypeOf(strct).ziggy_options, "skip_fields")) {
225-
const skip_fields = @TypeOf(strct).ziggy_options.skip_fields;
226-
if (@TypeOf(skip_fields) != []const std.meta.FieldEnum(@TypeOf(strct))) {
227-
@compileError("ziggy_options.skip_fields must be a []const std.meta.FieldEnum(T)");
228-
}
239+
if (has_skip_fields) {
240+
const skip_fields = StructType.ziggy_options.skip_fields;
241+
if (@TypeOf(skip_fields) != []const FE) {
242+
@compileError("ziggy_options.skip_fields must be a []const std.meta.FieldEnum(T)");
243+
}
229244

230-
const sf_idx: std.meta.FieldEnum(@TypeOf(strct)) = @enumFromInt(idx - 1);
231-
inline for (skip_fields) |sf| { // did you pub *var* skip_fields? (should be pub const)
232-
if (sf == sf_idx) continue :outer;
233-
}
245+
const sf_idx: FE = @enumFromInt(idx - 1);
246+
inline for (skip_fields) |sf| { // did you pub *var* skip_fields? (should be pub const)
247+
if (sf == sf_idx) continue :outer;
234248
}
235249
}
236250

@@ -249,9 +263,10 @@ fn stringifyStructInner(
249263
try writer.writeAll(" = ");
250264
}
251265
try stringifyInner(@field(strct, name), opts, indent_level, depth + 1, writer);
252-
if (opts.whitespace != .minified or idx != field_count) {
266+
if (opts.whitespace != .minified or print_idx != field_count) {
253267
try writer.writeAll(",");
254268
}
269+
print_idx += 1;
255270
}
256271
}
257272
}

0 commit comments

Comments
 (0)