Skip to content

Commit

Permalink
Update to URIs.jl (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Sep 2, 2021
1 parent 104c984 commit 2ff8063
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
13 changes: 8 additions & 5 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
name = "JSONSchema"
uuid = "7d188eb4-7ad8-530c-ae41-71a32a6d4692"
version = "0.3.3"
version = "0.3.4"

[deps]
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
ZipFile = "a5390f91-8eb1-5f08-bee0-b1d1ffed6cea"
URIs = "5c2747f8-b7ea-4ff2-ba2e-563bfd36b1d4"

[compat]
HTTP = "0.8, 0.9"
HTTP = "0.9"
JSON = "0.21"
OrderedCollections = "1"
URIs = "1"
ZipFile = "0.8, 0.9"
julia = "1"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
ZipFile = "a5390f91-8eb1-5f08-bee0-b1d1ffed6cea"

[targets]
test = ["Test", "OrderedCollections"]
test = ["Test", "OrderedCollections", "ZipFile"]
1 change: 1 addition & 0 deletions src/JSONSchema.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module JSONSchema

using JSON
import HTTP
import URIs

export Schema, validate

Expand Down
33 changes: 15 additions & 18 deletions src/schema.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ function type_to_dict(x)
return Dict(name => getfield(x, name) for name in fieldnames(typeof(x)))
end

function update_id(uri::HTTP.URI, s::String)
id2 = HTTP.URI(s)
function update_id(uri::URIs.URI, s::String)
id2 = URIs.URI(s)
if !isempty(id2.scheme)
return id2
end
Expand All @@ -27,7 +27,7 @@ function update_id(uri::HTTP.URI, s::String)
els[:path] =
oldpath == nothing ? id2.path : oldpath.captures[1] * id2.path
end
return HTTP.URI(; els...)
return URIs.URI(; els...)
end

function get_element(schema, path::AbstractString)
Expand Down Expand Up @@ -60,7 +60,7 @@ function _recurse_get_element(schema::Vector, element::String)
return schema[index+1]
end

function get_remote_schema(uri::HTTP.URI)
function get_remote_schema(uri::URIs.URI)
r = HTTP.get(uri)
if r.status != 200
error("Unable to get remote schema at $uri. HTTP status = $(r.status)")
Expand All @@ -69,7 +69,7 @@ function get_remote_schema(uri::HTTP.URI)
end

function find_ref(
uri::HTTP.URI,
uri::URIs.URI,
id_map::AbstractDict,
path::String,
parent_dir::String,
Expand All @@ -82,14 +82,11 @@ function find_ref(
uri = update_id(uri, path)
els = type_to_dict(uri)
delete!.(Ref(els), [:uri, :fragment])
uri2 = HTTP.URI(; els...)
uri2 = URIs.URI(; els...)
is_file_uri = startswith(uri2.scheme, "file") || isempty(uri2.scheme)
if is_file_uri && !isabspath(uri2.path)
# Normalize a file path to an absolute path so creating a key is consistent.
uri2 = HTTP.URIs.merge(
uri2;
path = abspath(joinpath(parent_dir, uri2.path)),
)
uri2 = URIs.URI(uri2; path = abspath(joinpath(parent_dir, uri2.path)))
end
if !haskey(id_map, string(uri2))
# id_map doesn't have this key so, fetch the ref and add it to id_map.
Expand All @@ -110,11 +107,11 @@ end

# Recursively find all "$ref" fields and resolve their path.

resolve_refs!(::Any, ::HTTP.URI, ::AbstractDict, ::String) = nothing
resolve_refs!(::Any, ::URIs.URI, ::AbstractDict, ::String) = nothing

function resolve_refs!(
schema::Vector,
uri::HTTP.URI,
uri::URIs.URI,
id_map::AbstractDict,
parent_dir::String,
)
Expand All @@ -126,7 +123,7 @@ end

function resolve_refs!(
schema::AbstractDict,
uri::HTTP.URI,
uri::URIs.URI,
id_map::AbstractDict,
parent_dir::String,
)
Expand Down Expand Up @@ -154,21 +151,21 @@ end

function build_id_map(schema::AbstractDict)
id_map = Dict{String,Any}("" => schema)
build_id_map!(id_map, schema, HTTP.URI())
build_id_map!(id_map, schema, URIs.URI())
return id_map
end

build_id_map!(::AbstractDict, ::Any, ::HTTP.URI) = nothing
build_id_map!(::AbstractDict, ::Any, ::URIs.URI) = nothing

function build_id_map!(id_map::AbstractDict, schema::Vector, uri::HTTP.URI)
function build_id_map!(id_map::AbstractDict, schema::Vector, uri::URIs.URI)
build_id_map!.(Ref(id_map), schema, Ref(uri))
return
end

function build_id_map!(
id_map::AbstractDict,
schema::AbstractDict,
uri::HTTP.URI,
uri::URIs.URI,
)
if haskey(schema, "id") && schema["id"] isa String
# This block is for draft 4.
Expand Down Expand Up @@ -218,7 +215,7 @@ struct Schema
end
schema = deepcopy(schema) # Ensure we don't modify the user's data!
id_map = build_id_map(schema)
resolve_refs!(schema, HTTP.URI(), id_map, parent_dir)
resolve_refs!(schema, URIs.URI(), id_map, parent_dir)
return new(schema)
end
end
Expand Down

2 comments on commit 2ff8063

@odow
Copy link
Collaborator Author

@odow odow commented on 2ff8063 Sep 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/44106

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.3.4 -m "<description of version>" 2ff8063aa9e63189810c93456cc81b8281cb0917
git push origin v0.3.4

Please sign in to comment.