Skip to content

Commit 914b896

Browse files
author
Pascal Post
committed
changed base uri map
1 parent 4d26622 commit 914b896

File tree

1 file changed

+11
-76
lines changed

1 file changed

+11
-76
lines changed

src/uri.zig

Lines changed: 11 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ const Stack = @import("stack.zig").Stack;
33

44
const BaseUriMap = struct {
55
arena: std.heap.ArenaAllocator,
6-
map: UriHashMap,
6+
map: std.StringHashMap([]const u8),
77

88
fn init(allocator: std.mem.Allocator, schema: std.json.Value, stack: *Stack) !BaseUriMap {
99
var arena = std.heap.ArenaAllocator.init(allocator);
10-
var base_uri_map = UriHashMap.init(allocator);
10+
var base_uri_map = std.StringHashMap([]const u8).init(allocator);
1111
try addFromSchema(arena.allocator(), schema, null, stack, &base_uri_map);
1212
stack.clearRetainCapacity();
1313
return .{
@@ -21,7 +21,7 @@ const BaseUriMap = struct {
2121
self.map.deinit();
2222
}
2323

24-
fn addFromSchema(allocator: std.mem.Allocator, root: std.json.Value, uri_base: ?std.Uri, stack: *Stack, base_uri_map: *UriHashMap) !void {
24+
fn addFromSchema(allocator: std.mem.Allocator, root: std.json.Value, uri_base: ?std.Uri, stack: *Stack, base_uri_map: *std.StringHashMap([]const u8)) !void {
2525
switch (root) {
2626
.bool, .string, .number_string, .float, .integer, .null => {},
2727
.object => |object| {
@@ -52,9 +52,10 @@ const BaseUriMap = struct {
5252
}
5353
};
5454

55+
const uri_str = try std.fmt.allocPrint(allocator, "{}", .{uri});
5556
const path = try stack.constructPath(allocator);
5657

57-
try base_uri_map.put(uri, path);
58+
try base_uri_map.put(uri_str, path);
5859

5960
break :blk uri;
6061
} else uri_base;
@@ -81,42 +82,6 @@ const BaseUriMap = struct {
8182
}
8283
};
8384

84-
const UriHashMap = std.HashMap(std.Uri, []const u8, UriContext, std.hash_map.default_max_load_percentage);
85-
86-
const UriContext = struct {
87-
pub fn hash(self: UriContext, uri: std.Uri) u64 {
88-
_ = self;
89-
90-
var hasher = std.hash.Wyhash.init(0);
91-
hasher.update(uri.scheme);
92-
if (uri.user) |component| switch (component) {
93-
.raw, .percent_encoded => |string| hasher.update(string),
94-
};
95-
if (uri.password) |component| switch (component) {
96-
.raw, .percent_encoded => |string| hasher.update(string),
97-
};
98-
if (uri.host) |component| switch (component) {
99-
.raw, .percent_encoded => |string| hasher.update(string),
100-
};
101-
if (uri.port) |port| hasher.update(&std.mem.toBytes(port));
102-
switch (uri.path) {
103-
.raw, .percent_encoded => |string| hasher.update(string),
104-
}
105-
if (uri.query) |component| switch (component) {
106-
.raw, .percent_encoded => |string| hasher.update(string),
107-
};
108-
if (uri.fragment) |component| switch (component) {
109-
.raw, .percent_encoded => |string| hasher.update(string),
110-
};
111-
return hasher.final();
112-
}
113-
114-
pub fn eql(self: UriContext, a: std.Uri, b: std.Uri) bool {
115-
_ = self;
116-
return std.meta.eql(a, b);
117-
}
118-
};
119-
12085
test "schema identification examples" {
12186
const schema =
12287
\\{
@@ -141,46 +106,16 @@ test "schema identification examples" {
141106
const schema_parsed = try std.json.parseFromSlice(std.json.Value, allocator, schema, .{});
142107
defer schema_parsed.deinit();
143108

144-
// TODO add a stack to capture current path
145109
var stack = try Stack.init(allocator, schema_parsed.value, 10);
146110
defer stack.deinit();
147111

148112
var base_uri_map = try BaseUriMap.init(allocator, schema_parsed.value, &stack);
149113
defer base_uri_map.deinit();
150114

151-
var it = base_uri_map.map.iterator();
152-
while (it.next()) |entry| {
153-
const key = entry.key_ptr.*;
154-
const value = entry.value_ptr.*;
155-
std.debug.print("{} : {s}\n", .{ key, value });
156-
}
115+
try std.testing.expectEqualStrings("#", base_uri_map.map.get("http://example.com/root.json").?);
116+
try std.testing.expectEqualStrings("#/definitions/A", base_uri_map.map.get("http://example.com/root.json#foo").?);
117+
try std.testing.expectEqualStrings("#/definitions/B", base_uri_map.map.get("http://example.com/other.json").?);
118+
try std.testing.expectEqualStrings("#/definitions/B/definitions/X", base_uri_map.map.get("http://example.com/other.json#bar").?);
119+
try std.testing.expectEqualStrings("#/definitions/B/definitions/Y", base_uri_map.map.get("http://example.com/t/inner.json").?);
120+
try std.testing.expectEqualStrings("#/definitions/C", base_uri_map.map.get("urn:uuid:ee564b8a-7a87-4125-8c96-e9f123d6766f").?);
157121
}
158-
159-
// test "schema" {
160-
// const schema =
161-
// \\{
162-
// \\ "$id": "http://localhost:1234/sibling_id/base/",
163-
// \\ "definitions": {
164-
// \\ "foo": {
165-
// \\ "$id": "http://localhost:1234/sibling_id/foo.json",
166-
// \\ "type": "string"
167-
// \\ },
168-
// \\ "base_foo": {
169-
// \\ "$comment": "this canonical uri is http://localhost:1234/sibling_id/base/foo.json",
170-
// \\ "$id": "foo.json",
171-
// \\ "type": "number"
172-
// \\ }
173-
// \\ },
174-
// \\ "allOf": [
175-
// \\ {
176-
// \\ "$comment": "$ref resolves to http://localhost:1234/sibling_id/base/foo.json, not http://localhost:1234/sibling_id/foo.json",
177-
// \\ "$id": "http://localhost:1234/sibling_id/",
178-
// \\ "$ref": "foo.json"
179-
// \\ }
180-
// \\ ]
181-
// \\}
182-
// ;
183-
184-
// // http://localhost:1234/sibling_id/base/ -> #
185-
// //
186-
// }

0 commit comments

Comments
 (0)