Skip to content

Commit

Permalink
Update to Julia 0.4.
Browse files Browse the repository at this point in the history
  • Loading branch information
eraviart committed May 23, 2016
1 parent d32a040 commit 00e93be
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 110 deletions.
2 changes: 0 additions & 2 deletions src/Biryani.jl
Expand Up @@ -45,8 +45,6 @@ module DatesConverters
export date_to_iso8601_string, iso8601_input_to_date, iso8601_string_to_date, to_date


import Dates: Date

import ..Biryani: call, Context, Convertible, N_, pipe, strip, test


Expand Down
56 changes: 28 additions & 28 deletions src/base.jl
Expand Up @@ -36,7 +36,7 @@ type EmptyContext <: Context
end


_(context::EmptyContext, message::String) = message
_(context::EmptyContext, message::AbstractString) = message


N_(message) = message
Expand Down Expand Up @@ -120,7 +120,7 @@ function embed_error(value, error)
for (child_key, child_error) in error
child_error = embed_error(get(value, child_key, nothing), child_error)
if child_error !== nothing
errors = get!(value, "errors", (typeof(child_key) => Any)[])
errors = get!(value, "errors", Dict{typeof(child_key), Any}())
errors[child_key] = child_error
end
end
Expand All @@ -140,9 +140,9 @@ function embed_error(value, error)
end
return nothing
end
if all(key -> isa(key, String) && isdigit(key) && 1 <= int(key) <= size(value, 1), error)
if all(key -> isa(key, AbstractString) && isdigit(key) && 1 <= parse(Int, key) <= size(value, 1), error)
for (child_key, child_error) in error
child_error = embed_error(value[int(child_key)], child_error)
child_error = embed_error(value[parse(Int, child_key)], child_error)
@assert(child_error === nothing, child_error)
# if child_error !== nothing
# return error
Expand Down Expand Up @@ -179,9 +179,9 @@ end

eval_error(context::Context, func::Function) = func(context)

eval_error(context::Context, ::Nothing) = nothing
eval_error(context::Context, ::Void) = nothing

eval_error(context::Context, message::String) = _(context, message)
eval_error(context::Context, message::AbstractString) = _(context, message)


extract_when_singleton(convertible::Convertible) = condition(
Expand All @@ -203,9 +203,9 @@ function fail(convertible::Convertible; error = nothing)
)
end

fail(error::String) = convertible::Convertible -> fail(convertible, error = error)
fail(error::AbstractString) = convertible::Convertible -> fail(convertible, error = error)

fail(; error::String = nothing) = convertible::Convertible -> fail(convertible, error = error)
fail(; error::AbstractString = nothing) = convertible::Convertible -> fail(convertible, error = error)


function first_match(converters::Function...; error = nothing)
Expand Down Expand Up @@ -254,7 +254,7 @@ function guess_bool(convertible::Convertible)
if convertible.error !== nothing || convertible.value === nothing
return convertible
end
if !isa(convertible.value, String)
if !isa(convertible.value, AbstractString)
return to_bool(convertible)
end
lower_value = strip(convertible.value) |> lowercase
Expand Down Expand Up @@ -421,7 +421,7 @@ function string_to_email(convertible::Convertible; accept_ip_address = false)
return convertible
end
value = lowercase(convertible.value)
if beginswith(value, "mailto:")
if startswith(value, "mailto:")
value = replace(value, "mailto:", "", 1)
end
split_value = split(value, '@')
Expand Down Expand Up @@ -458,7 +458,7 @@ function string_to_ipv4(convertible::Convertible)
if !ismatch(r"^\d+\.\d+\.\d+\.\d+$", convertible.value)
return Convertible(convertible.value, convertible.context, N_("Invalid IPv4 address."))
end
bytes = map(int, split(convertible.value, '.'))
bytes = map(value -> parse(Int, value), split(convertible.value, '.'))
if !all(byte -> 0 <= byte <= 255, bytes)
return Convertible(convertible.value, convertible.context, N_("Invalid IPv4 address."))
end
Expand Down Expand Up @@ -525,7 +525,7 @@ function struct(converters::Tuple; default = nothing)
values = tuple(values..., nothing)
end
converted_values = cell(length(values_converter))
error_by_index = (Int => Any)[]
error_by_index = Dict{Int,Any}()
for (index, (converter, value)) in enumerate(zip(values_converter, values))
converted = converter(Convertible(value, convertible.context))
converted_values[index] = converted.value
Expand Down Expand Up @@ -658,7 +658,7 @@ function test_in(values; error = nothing, handle_nothing = false)
end


function test_isa(data_type::Union(DataType, UnionType); error = nothing, handle_nothing = false)
function test_isa(data_type::Union{DataType, Union}; error = nothing, handle_nothing = false)
"""Return a converter that accepts only an instance of given type."""
return test(
value -> isa(value, data_type),
Expand Down Expand Up @@ -703,15 +703,15 @@ end

function to_bool(value, context::Context)
try
return Convertible(bool(convert(Int, value)), context)
return Convertible(convert(Int, value) != 0, context)
catch
return Convertible(value, context, N_("Value must be a boolean."))
end
end

function to_bool(value::String, context::Context)
function to_bool(value::AbstractString, context::Context)
try
return Convertible(bool(int(value)), context)
return Convertible(parse(Int, value) != 0, context)
catch
return Convertible(value, context, N_("Value must be a boolean."))
end
Expand All @@ -727,7 +727,7 @@ function to_float(convertible::Convertible; accept_expression = false)
return convertible
end
value = convertible.value
if accept_expression && isa(value, String)
if accept_expression && isa(value, AbstractString)
value = strip(value)
if !ismatch(r"^[ \t\n\r\d.+\-*/()]+$", value)
return Convertible(value, convertible.context, N_("Value must be a valid floating point expression."))
Expand All @@ -745,7 +745,7 @@ function to_float(; accept_expression = false)
return convertible::Convertible -> to_float(convertible; accept_expression = accept_expression)
end

function to_float(value::String, context::Context)
function to_float(value::AbstractString, context::Context)
try
return Convertible(float(value), context)
catch
Expand All @@ -771,7 +771,7 @@ function to_int(convertible::Convertible; accept_expression = false)
return convertible
end
value = convertible.value
if accept_expression && isa(value, String)
if accept_expression && isa(value, AbstractString)
value = strip(value)
if !ismatch(r"^[ \t\n\r\d.+\-*/()]+$", value)
return Convertible(value, convertible.context, N_("Value must be a valid integer expression."))
Expand All @@ -789,9 +789,9 @@ function to_int(; accept_expression = false)
return convertible::Convertible -> to_int(convertible; accept_expression = accept_expression)
end

function to_int(value::String, context::Context)
function to_int(value::AbstractString, context::Context)
try
return Convertible(int(value), context)
return Convertible(parse(Int, value), context)
catch
return Convertible(value, context, N_("Value must be an integer number."))
end
Expand Down Expand Up @@ -838,7 +838,7 @@ function uniform_mapping(key_converter::Function, value_converters::Function...;
if convertible.error !== nothing || convertible.value === nothing
return convertible
end
error_by_key = (Any => Any)[]
error_by_key = Dict{Any, Any}()
value_by_key = OrderedDict{Any, Any}()
for (key, value) in convertible.value
key_converted = key_converter(Convertible(key, convertible.context))
Expand All @@ -859,10 +859,10 @@ function uniform_mapping(key_converter::Function, value_converters::Function...;

typed_value_by_key = OrderedDict{
key_type === nothing ?
(isempty(value_by_key) ? Any : mapreduce(typeof, promote_type, Nothing, keys(value_by_key))) :
(isempty(value_by_key) ? Any : mapreduce(typeof, promote_type, Void, keys(value_by_key))) :
key_type,
value_type === nothing ?
(isempty(value_by_key) ? Any : mapreduce(typeof, promote_type, Nothing, values(value_by_key))) :
(isempty(value_by_key) ? Any : mapreduce(typeof, promote_type, Void, values(value_by_key))) :
value_type,
}()
for (key, value) in value_by_key
Expand All @@ -873,8 +873,8 @@ function uniform_mapping(key_converter::Function, value_converters::Function...;
return Convertible(typed_value_by_key, convertible.context)
else
typed_error_by_key = OrderedDict{
key_type === nothing ? mapreduce(typeof, promote_type, Nothing, keys(error_by_key)) : key_type,
value_type === nothing ? mapreduce(typeof, promote_type, Nothing, values(error_by_key)) : value_type,
key_type === nothing ? mapreduce(typeof, promote_type, Void, keys(error_by_key)) : key_type,
value_type === nothing ? mapreduce(typeof, promote_type, Void, values(error_by_key)) : value_type,
}()
for (key, error) in error_by_key
typed_error_by_key[key] = error
Expand All @@ -892,7 +892,7 @@ function uniform_sequence(converters::Function...; drop_nothing = false, item_ty
if convertible.error !== nothing || convertible.value === nothing
return convertible
end
error_by_index = (Int => Any)[]
error_by_index = Dict{Any, Any}()
values = Any[]
for (index, value) in enumerate(convertible.value)
converted = pipe(converters...)(Convertible(value, convertible.context))
Expand All @@ -905,7 +905,7 @@ function uniform_sequence(converters::Function...; drop_nothing = false, item_ty
end
return Convertible(
collect(item_type === nothing ?
(isempty(values) ? Any : mapreduce(typeof, promote_type, Nothing, values)) :
(isempty(values) ? Any : mapreduce(typeof, promote_type, Void, values)) :
item_type, values),
convertible.context,
isempty(error_by_index) ? nothing : error_by_index,
Expand Down
10 changes: 8 additions & 2 deletions src/dates.jl
Expand Up @@ -52,9 +52,15 @@ function to_date(convertible::Convertible)
# return Convertible(Date(convertible.value), convertible.context)
end

to_date(value::(Int...), context::Context) = Convertible(Date(value...), context)
to_date(year::Int, context::Context) = Convertible(Date(year), context)
to_date(year::Int, month::Int, context::Context) = Convertible(Date(year, month), context)
to_date(year::Int, month::Int, day::Int, context::Context) = Convertible(Date(year, month, day), context)

function to_date(value::String, context::Context)
to_date(value::Tuple{Int}, context::Context) = Convertible(Date(value[1]), context)
to_date(value::Tuple{Int, Int}, context::Context) = Convertible(Date(value[1], value[2]), context)
to_date(value::Tuple{Int, Int, Int}, context::Context) = Convertible(Date(value[1], value[2], value[3]), context)

function to_date(value::AbstractString, context::Context)
if !('-' in value)
# Work around bug in Julia 0.3: Date("2013") throws:
# ERROR: ArgumentError("Delimiter mismatch. Couldn't find first delimiter, \"-\", in date string")
Expand Down
1 change: 0 additions & 1 deletion test/REQUIRE
@@ -1,3 +1,2 @@
Dates
JSON
Slugify

0 comments on commit 00e93be

Please sign in to comment.