From 8b1ff1149f82591b5c3443dd6852338acfc317eb Mon Sep 17 00:00:00 2001 From: jaina heartles Date: Fri, 16 Dec 2022 01:02:44 -0800 Subject: [PATCH] Mirror std.json behavior for unions --- src/http/multipart.zig | 2 +- src/util/serialize.zig | 22 ++-------------------- 2 files changed, 3 insertions(+), 21 deletions(-) diff --git a/src/http/multipart.zig b/src/http/multipart.zig index 176ca7b..854b7c3 100644 --- a/src/http/multipart.zig +++ b/src/http/multipart.zig @@ -153,7 +153,7 @@ pub fn openForm(multipart_stream: anytype) MultipartForm(@TypeOf(multipart_strea fn Deserializer(comptime Result: type) type { return util.DeserializerContext(Result, MultipartFormField, struct { - pub const options = .{ .isScalar = isScalar, .embed_unions = true }; + pub const options = .{ .isScalar = isScalar }; pub fn isScalar(comptime T: type) bool { if (T == FormFile or T == ?FormFile) return true; diff --git a/src/util/serialize.zig b/src/util/serialize.zig index bd2e2a4..0cf2324 100644 --- a/src/util/serialize.zig +++ b/src/util/serialize.zig @@ -40,23 +40,14 @@ pub fn deserializeString(allocator: std.mem.Allocator, comptime T: type, value: fn getStaticFieldList(comptime T: type, comptime prefix: FieldRef, comptime options: SerializationOptions) []const FieldRef { comptime { - if (std.meta.trait.is(.Union)(T) and prefix.len == 0 and options.embed_unions) { - @compileError("Cannot embed a union into nothing"); - } - if (options.isScalar(T)) return &.{prefix}; if (std.meta.trait.is(.Optional)(T)) return getStaticFieldList(std.meta.Child(T), prefix, options); if (std.meta.trait.isSlice(T) and !std.meta.trait.isZigString(T)) return &.{}; - const eff_prefix: FieldRef = if (std.meta.trait.is(.Union)(T) and options.embed_unions) - prefix[0 .. prefix.len - 1] - else - prefix; - var fields: []const FieldRef = &.{}; for (std.meta.fields(T)) |f| { - const new_prefix = eff_prefix ++ &[_][]const u8{f.name}; + const new_prefix = if (std.meta.trait.is(.Union)(T)) prefix else prefix ++ &[_][]const u8{f.name}; const F = f.field_type; fields = fields ++ getStaticFieldList(F, new_prefix, options); } @@ -67,25 +58,16 @@ fn getStaticFieldList(comptime T: type, comptime prefix: FieldRef, comptime opti fn getDynamicFieldList(comptime T: type, comptime prefix: FieldRef, comptime options: SerializationOptions) []const DynamicField { comptime { - if (std.meta.trait.is(.Union)(T) and prefix.len == 0 and options.embed_unions) { - @compileError("Cannot embed a union into nothing"); - } - if (options.isScalar(T)) return &.{}; if (std.meta.trait.is(.Optional)(T)) return getDynamicFieldList(std.meta.Child(T), prefix, options); if (std.meta.trait.isSlice(T) and !std.meta.trait.isZigString(T)) return &.{ .{ .ref = prefix, .child_type = std.meta.Child(T) }, }; - const eff_prefix: FieldRef = if (std.meta.trait.is(.Union)(T) and options.embed_unions) - prefix[0 .. prefix.len - 1] - else - prefix; - var fields: []const DynamicField = &.{}; for (std.meta.fields(T)) |f| { - const new_prefix = eff_prefix ++ &[_][]const u8{f.name}; + const new_prefix = if (std.meta.trait.is(.Union)(T)) prefix else prefix ++ &[_][]const u8{f.name}; const F = f.field_type; fields = fields ++ getDynamicFieldList(F, new_prefix, options); }