Skip to content

Commit

Permalink
fix read and write type error
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhongjia committed Jan 15, 2024
1 parent 183347f commit f2bfc8e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
39 changes: 30 additions & 9 deletions src/msgpack.zig
Original file line number Diff line number Diff line change
Expand Up @@ -684,13 +684,7 @@ pub fn MsgPack(
}
},
.Struct => {
if (field_type == Str) {
try self.write_str(@as(Str, field_value).value());
} else if (field_type == Bin) {
try self.write_bin(@as(Bin, field_value).value());
} else {
try self.write_map(field_type, field_value);
}
try self.write_map(field_type, field_value);
},
else => {
@compileError("type is not supported!");
Expand Down Expand Up @@ -776,6 +770,13 @@ pub fn MsgPack(
if (type_info != .Struct or type_info.Struct.is_tuple) {
@compileError("now only support struct");
}
if (T == EXT) {
return self.write_ext(@as(EXT, val));
} else if (T == Str) {
return self.write_str(@as(Str, val).value());
} else if (T == Bin) {
return self.write_bin(@as(Bin, val).value());
}

const fields_len = type_info.Struct.fields.len;

Expand Down Expand Up @@ -1602,6 +1603,15 @@ pub fn MsgPack(

/// read map
pub fn read_map(self: Self, comptime T: type, allocator: Allocator) !T {
if (T == EXT) {
@compileError("please use read_ext for EXT");
}
if (T == Str) {
@compileError("please use read_str for Str");
}
if (T == Bin) {
@compileError("please use read_bin for Bin");
}
const marker_u8 = try self.read_type_marker_u8();
const marker = try self.marker_u8_to(marker_u8);
var len: usize = 0;
Expand Down Expand Up @@ -1690,6 +1700,9 @@ pub fn MsgPack(
} else if (field_type == Bin) {
const bin = try self.read_bin(allocator);
@field(res, field_name) = wrapBin(bin);
} else if (field_type == EXT) {
const ext = try self.read_ext(allocator);
@field(res, field_name) = ext;
} else {
const val = try self.read_map(field_type, allocator);
@field(res, field_name) = val;
Expand Down Expand Up @@ -1795,7 +1808,15 @@ pub fn MsgPack(
}
},
.Struct => {
return self.read_map(T, allocator);
if (T == Bin) {
return self.read_bin(allocator);
} else if (T == Str) {
return self.read_str(allocator);
} else if (T == EXT) {
return self.read_ext(allocator);
} else {
return self.read_map(T, allocator);
}
},
else => {
@compileError("type is not supported!");
Expand Down Expand Up @@ -1878,7 +1899,7 @@ const PO = struct {
}
};

const EXT = struct {
pub const EXT = struct {
type: u8,
data: []u8,
};
8 changes: 4 additions & 4 deletions src/uint-test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,8 @@ test "arrary write and read" {

const test_val = [5]u8{ 1, 2, 3, 4, 5 };

try p.write_arr(u8, &test_val);
const val = try p.read_arr(allocator, u8);
try p.write(&test_val);
const val = try p.read([]u8, allocator);
defer allocator.free(val);
try expect(std.mem.eql(u8, &test_val, val));
}
Expand All @@ -325,8 +325,8 @@ test "ext write and read" {
var test_data = [5]u8{ 1, 2, 3, 4, 5 };
const test_type: u8 = 1;

try p.write_ext(.{ .type = test_type, .data = &test_data });
const val = try p.read_ext(allocator);
try p.write(msgpack.EXT{ .type = test_type, .data = &test_data });
const val = try p.read(msgpack.EXT, allocator);
defer allocator.free(val.data);
try expect(std.mem.eql(u8, &test_data, val.data));
try expect(test_type == val.type);
Expand Down

0 comments on commit f2bfc8e

Please sign in to comment.