-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathunion.zig
220 lines (184 loc) · 6.69 KB
/
union.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
const std = @import("std");
const hdrs = @import("headers.zig");
const NonOptional = @import("utils.zig").NonOptional;
const Optional = @import("utils.zig").Optional;
const isOptional = @import("utils.zig").isOptional;
const NoAllocator = @import("utils.zig").NoAllocator;
const maybePackNull = @import("null.zig").maybePackNull;
const maybeUnpackNull = @import("null.zig").maybeUnpackNull;
const packMapHeader = @import("map.zig").packMapHeader;
const unpackMapHeader = @import("map.zig").unpackMapHeader;
const packInt = @import("int.zig").packInt;
const unpackInt = @import("int.zig").unpackInt;
const packString = @import("string.zig").packString;
const unpackStringInto = @import("string.zig").unpackStringInto;
const packArrayHeader = @import("array.zig").packArrayHeader;
const unpackArrayHeader = @import("array.zig").unpackArrayHeader;
const packAny = @import("any.zig").packAny;
const unpackAny = @import("any.zig").unpackAny;
pub const UnionAsMapOptions = struct {
key: union(enum) {
field_name,
field_name_prefix: u8,
field_index,
},
omit_nulls: bool = true,
omit_defaults: bool = false,
};
pub const UnionFormat = union(enum) {
as_map: UnionAsMapOptions,
};
pub const default_union_format = UnionFormat{
.as_map = .{
.key = .field_name,
},
};
fn strPrefix(src: []const u8, len: usize) []const u8 {
return src[0..@min(src.len, len)];
}
pub fn packUnionAsMap(writer: anytype, comptime T: type, value: T, opts: UnionAsMapOptions) !void {
const type_info = @typeInfo(T);
const fields = type_info.Union.fields;
const TagType = @typeInfo(T).Union.tag_type.?;
try packMapHeader(writer, 1);
inline for (fields, 0..) |field, i| {
if (value == @field(TagType, field.name)) {
switch (opts.key) {
.field_index => {
try packInt(writer, u16, i);
},
.field_name => {
try packString(writer, field.name);
},
.field_name_prefix => |prefix| {
try packString(writer, strPrefix(field.name, prefix));
},
}
try packAny(writer, field.type, @field(value, field.name));
}
}
}
pub fn packUnion(writer: anytype, comptime T: type, value_or_maybe_null: T) !void {
const value = try maybePackNull(writer, T, value_or_maybe_null) orelse return;
const Type = @TypeOf(value);
const type_info = @typeInfo(Type);
if (type_info != .Union) {
@compileError("Expected union type");
}
const format = if (std.meta.hasFn(Type, "msgpackFormat")) T.msgpackFormat() else default_union_format;
switch (format) {
.as_map => |opts| {
return packUnionAsMap(writer, Type, value, opts);
},
}
}
pub fn unpackUnionAsMap(reader: anytype, allocator: std.mem.Allocator, comptime T: type, opts: UnionAsMapOptions) !T {
const len = if (@typeInfo(T) == .Optional)
try unpackMapHeader(reader, ?u16) orelse return null
else
try unpackMapHeader(reader, u16);
if (len != 1) {
return error.InvalidUnionFieldCount;
}
const Type = NonOptional(T);
const type_info = @typeInfo(Type);
const fields = type_info.Union.fields;
var field_name_buffer: [256]u8 = undefined;
var result: Type = undefined;
switch (opts.key) {
.field_index => {
const field_index = try unpackInt(reader, u16);
inline for (fields, 0..) |field, i| {
if (field_index == i) {
const value = try unpackAny(reader, allocator, field.type);
result = @unionInit(Type, field.name, value);
break;
}
} else {
return error.UnknownUnionField;
}
},
.field_name => {
const field_name = try unpackStringInto(reader, &field_name_buffer);
inline for (fields) |field| {
if (std.mem.eql(u8, field.name, field_name)) {
const value = try unpackAny(reader, allocator, field.type);
result = @unionInit(Type, field.name, value);
break;
}
} else {
return error.UnknownUnionField;
}
},
.field_name_prefix => |prefix| {
const field_name = try unpackStringInto(reader, &field_name_buffer);
inline for (fields) |field| {
if (std.mem.startsWith(u8, field.name, strPrefix(field_name, prefix))) {
const value = try unpackAny(reader, allocator, field.type);
result = @unionInit(Type, field.name, value);
break;
}
} else {
return error.UnknownUnionField;
}
},
}
return result;
}
pub fn unpackUnion(reader: anytype, allocator: std.mem.Allocator, comptime T: type) !T {
const Type = NonOptional(T);
const format = if (std.meta.hasFn(Type, "msgpackFormat")) T.msgpackFormat() else default_union_format;
switch (format) {
.as_map => |opts| {
return try unpackUnionAsMap(reader, allocator, T, opts);
},
}
}
const Msg1 = union(enum) {
a: u32,
b: u64,
pub fn msgpackFormat() UnionFormat {
return .{ .as_map = .{ .key = .field_index } };
}
};
const msg1 = Msg1{ .a = 1 };
const msg1_packed = [_]u8{
0x81, // map with 1 elements
0x00, // key: fixint 0
0x01, // value: u32(1)
};
const Msg2 = union(enum) {
a,
b: u64,
pub fn msgpackFormat() UnionFormat {
return .{ .as_map = .{ .key = .field_index } };
}
};
const msg2 = Msg2{ .a = {} };
const msg2_packed = [_]u8{
0x81, // map with 1 elements
0x00, // key: fixint 0
0xc0, // value: nil
};
test "writeUnion: int field" {
var buffer: [100]u8 = undefined;
var stream = std.io.fixedBufferStream(&buffer);
try packUnion(stream.writer(), Msg1, msg1);
try std.testing.expectEqualSlices(u8, &msg1_packed, stream.getWritten());
}
test "writeUnion: void field" {
var buffer: [100]u8 = undefined;
var stream = std.io.fixedBufferStream(&buffer);
try packUnion(stream.writer(), Msg2, msg2);
try std.testing.expectEqualSlices(u8, &msg2_packed, stream.getWritten());
}
test "readUnion: int field" {
var stream = std.io.fixedBufferStream(&msg1_packed);
const value = try unpackUnion(stream.reader(), NoAllocator.allocator(), Msg1);
try std.testing.expectEqual(msg1, value);
}
test "readUnion: void field" {
var stream = std.io.fixedBufferStream(&msg2_packed);
const value = try unpackUnion(stream.reader(), NoAllocator.allocator(), Msg2);
try std.testing.expectEqual(msg2, value);
}