Skip to content

Commit

Permalink
Merge pull request #110 from getty-zig/untagged-representation
Browse files Browse the repository at this point in the history
Add support for the untagged representation of unions
  • Loading branch information
Jason Phan authored Jul 15, 2023
2 parents 59d1b3e + 8866e98 commit fa67705
Show file tree
Hide file tree
Showing 6 changed files with 860 additions and 137 deletions.
14 changes: 10 additions & 4 deletions src/attributes.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const std = @import("std");
const comptimePrint = std.fmt.comptimePrint;
const Type = std.builtin.Type;

/// Case conventions for the `rename_all` attribute.
/// Case conventions.
pub const Case = enum {
// foobar
lower,
Expand All @@ -29,6 +29,13 @@ pub const Case = enum {
screaming_kebab,
};

/// Tag representations for union variants.
pub const Tag = enum {
external,
internal,
untagged,
};

/// Returns an attribute map type.
pub fn Attributes(comptime T: type, comptime attributes: anytype) type {
const type_name = @typeName(T);
Expand Down Expand Up @@ -171,9 +178,8 @@ const ContainerAttributes = struct {
// convention.
//rename_all: ?Case = null,

// Use the internally tagged enum representation for this enum, with
// the given tag.
//tag: ?[]const u8 = null,
// Use the specified representation for this union.
tag: Tag = .external,

// Deserialize this type by deserializing into the given type, then
// converting fallibly.
Expand Down
6 changes: 3 additions & 3 deletions src/de/blocks/struct.zig
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ test "deserialize - struct" {

inline for (tests) |t| {
const Want = @TypeOf(t.want);
const got = try testing.deserialize(null, t.name, Self, Want, t.tokens);
const got = try testing.deserialize(std.testing.allocator, t.name, Self, Want, t.tokens);
try testing.expectEqual(t.name, t.want, got);
}
}
Expand Down Expand Up @@ -252,12 +252,12 @@ test "deserialize - struct, attributes" {
try testing.expectError(
t.name,
t.want_err,
testing.deserializeErr(null, Self, Want, t.tokens),
testing.deserializeErr(std.testing.allocator, Self, Want, t.tokens),
);
} else {
const Want = @TypeOf(t.want);

const got = try testing.deserialize(null, t.name, Self, Want, t.tokens);
const got = try testing.deserialize(std.testing.allocator, t.name, Self, Want, t.tokens);
try testing.expectEqual(t.name, t.want, got);
}
}
Expand Down
Loading

0 comments on commit fa67705

Please sign in to comment.