fix map pattern in zig 0.11
This commit is contained in:
parent
edf931f827
commit
c39fc940b9
60
src/json.zig
60
src/json.zig
|
@ -1817,36 +1817,37 @@ fn parseInternal(comptime T: type, token: Token, tokens: *TokenStream, options:
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
.ObjectBegin => {
|
.ObjectBegin => {
|
||||||
// TODO: Fix this, or better yet, try to switch
|
|
||||||
// back to standard json parse
|
|
||||||
return error.NotConvertedToZig11;
|
|
||||||
// We are parsing into a slice, but we have an
|
// We are parsing into a slice, but we have an
|
||||||
// ObjectBegin. This might be ok, iff the type
|
// ObjectBegin. This might be ok, iff the type
|
||||||
// follows this pattern: []struct { key: []const u8, value: anytype }
|
// follows this pattern: []struct { key: []const u8, value: anytype }
|
||||||
// (could key be anytype?).
|
// (could key be anytype?).
|
||||||
// if (!isMapPattern(T))
|
if (!isMapPattern(T))
|
||||||
// return error.UnexpectedToken;
|
return error.UnexpectedToken;
|
||||||
// var arraylist = std.ArrayList(ptrInfo.child).init(allocator);
|
const key_type = typeForField(ptrInfo.child, "key");
|
||||||
// errdefer {
|
if (key_type == null) return error.UnexpectedToken;
|
||||||
// while (arraylist.popOrNull()) |v| {
|
const value_type = typeForField(ptrInfo.child, "value");
|
||||||
// parseFree(ptrInfo.child, v, options);
|
if (value_type == null) return error.UnexpectedToken;
|
||||||
// }
|
var arraylist = std.ArrayList(ptrInfo.child).init(allocator);
|
||||||
// arraylist.deinit();
|
errdefer {
|
||||||
// }
|
while (arraylist.popOrNull()) |v| {
|
||||||
// while (true) {
|
parseFree(ptrInfo.child, v, options);
|
||||||
// const key = (try tokens.next()) orelse return error.UnexpectedEndOfJson;
|
}
|
||||||
// switch (key) {
|
arraylist.deinit();
|
||||||
// .ObjectEnd => break,
|
}
|
||||||
// else => {},
|
while (true) {
|
||||||
// }
|
const key = (try tokens.next()) orelse return error.UnexpectedEndOfJson;
|
||||||
//
|
switch (key) {
|
||||||
// try arraylist.ensureTotalCapacity(arraylist.items.len + 1);
|
.ObjectEnd => break,
|
||||||
// const key_val = try parseInternal(try typeForField(ptrInfo.child, "key"), key, tokens, options);
|
else => {},
|
||||||
// const val = (try tokens.next()) orelse return error.UnexpectedEndOfJson;
|
}
|
||||||
// const val_val = try parseInternal(try typeForField(ptrInfo.child, "value"), val, tokens, options);
|
|
||||||
// arraylist.appendAssumeCapacity(.{ .key = key_val, .value = val_val });
|
try arraylist.ensureTotalCapacity(arraylist.items.len + 1);
|
||||||
// }
|
const key_val = try parseInternal(key_type.?, key, tokens, options);
|
||||||
// return arraylist.toOwnedSlice();
|
const val = (try tokens.next()) orelse return error.UnexpectedEndOfJson;
|
||||||
|
const val_val = try parseInternal(value_type.?, val, tokens, options);
|
||||||
|
arraylist.appendAssumeCapacity(.{ .key = key_val, .value = val_val });
|
||||||
|
}
|
||||||
|
return arraylist.toOwnedSlice();
|
||||||
},
|
},
|
||||||
else => return error.UnexpectedToken,
|
else => return error.UnexpectedToken,
|
||||||
}
|
}
|
||||||
|
@ -1859,7 +1860,7 @@ fn parseInternal(comptime T: type, token: Token, tokens: *TokenStream, options:
|
||||||
unreachable;
|
unreachable;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn typeForField(comptime T: type, comptime field_name: []const u8) !type {
|
fn typeForField(comptime T: type, comptime field_name: []const u8) ?type {
|
||||||
const ti = @typeInfo(T);
|
const ti = @typeInfo(T);
|
||||||
switch (ti) {
|
switch (ti) {
|
||||||
.Struct => {
|
.Struct => {
|
||||||
|
@ -1868,9 +1869,9 @@ fn typeForField(comptime T: type, comptime field_name: []const u8) !type {
|
||||||
return field.type;
|
return field.type;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
else => return error.TypeIsNotAStruct, // should not hit this
|
else => return null, //error.TypeIsNotAStruct, // should not hit this
|
||||||
}
|
}
|
||||||
return error.FieldNotFound;
|
return null; //error.FieldNotFound;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn isMapPattern(comptime T: type) bool {
|
fn isMapPattern(comptime T: type) bool {
|
||||||
|
@ -1999,7 +2000,6 @@ test "parse into that allocates a slice" {
|
||||||
}
|
}
|
||||||
|
|
||||||
test "parse into that uses a map pattern" {
|
test "parse into that uses a map pattern" {
|
||||||
if (true) return error.SkipZigTest; // TODO: re-enable when we work through json changes in 0.11 and see if we can move to upstream
|
|
||||||
const options = ParseOptions{ .allocator = testing.allocator };
|
const options = ParseOptions{ .allocator = testing.allocator };
|
||||||
const Map = []struct { key: []const u8, value: []const u8 };
|
const Map = []struct { key: []const u8, value: []const u8 };
|
||||||
const r = try parse(Map, @constCast(&TokenStream.init("{\"foo\": \"bar\"}")), options);
|
const r = try parse(Map, @constCast(&TokenStream.init("{\"foo\": \"bar\"}")), options);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user