Skip to content

Commit

Permalink
param with ctx
Browse files Browse the repository at this point in the history
  • Loading branch information
vdayanand committed Jul 4, 2024
1 parent 4f61a73 commit 20f733f
Showing 1 changed file with 36 additions and 49 deletions.
85 changes: 36 additions & 49 deletions src/server.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Servers
using JSON
using HTTP

import ..OpenAPI: APIModel, ValidationException, from_json, to_json
import ..OpenAPI: APIModel, ValidationException, from_json, to_json, convert_dicts_to_arrays

function middleware(impl, read, validate, invoke;
init=nothing,
Expand Down Expand Up @@ -56,38 +56,8 @@ function convert_to_dict(params::Vector{Param})::Dict{String, Any}
end
current = current[part]
end
float_matched = match(r"^[+-]?\d*\.\d+$", param.value)
int_matched = match(r"^[+-]?\d+$", param.value)
fin_val = if !isnothing(float_matched)
tryparse(Float64, param.value)
elseif !isnothing(int_matched)
tryparse(Int, param.value)
else
param.value
end
current[param.keylist[end]] = something(fin_val, param.value)
end

function convert_dicts_to_arrays!(dict)
for (k, v) in dict
if v isa Dict
keys_are_int = all(key -> occursin(r"^\d+$", key), keys(v))
if keys_are_int
sorted_keys = sort(collect(keys(v)), by=x->parse(Int, x))
dict[k] = []
for key in sorted_keys
if v[key] isa Dict
convert_dicts_to_arrays!(v[key])
end
push!(dict[k], v[key])
end
else
convert_dicts_to_arrays!(v)
end
end
end
current[param.keylist[end]] = param.value
end
convert_dicts_to_arrays!(deserialized_dict)
return deserialized_dict
end

Expand Down Expand Up @@ -115,33 +85,51 @@ function get_param(source::Vector{HTTP.Forms.Multipart}, name::String, required:
end


function to_param_type(::Type{T}, strval::String) where {T <: Number}
function to_param_type(::Type{T}, strval::String; stylectx=nothing) where {T <: Number}
parse(T, strval)
end

to_param_type(::Type{T}, val::T) where {T} = val
to_param_type(::Type{T}, ::Nothing) where {T} = nothing
to_param_type(::Type{String}, val::Vector{UInt8}) = String(copy(val))
to_param_type(::Type{Vector{UInt8}}, val::String) = convert(Vector{UInt8}, copy(codeunits(val)))
to_param_type(::Type{Vector{T}}, val::Vector{T}, _collection_format::Union{String,Nothing}) where {T} = val
to_param_type(::Type{Vector{T}}, json::Vector{Any}) where {T <: APIModel} = [to_param_type(T, x) for x in json]
to_param_type(::Type{T}, val::T; stylectx=nothing) where {T} = val
to_param_type(::Type{T}, ::Nothing; stylectx=nothing) where {T} = nothing
to_param_type(::Type{String}, val::Vector{UInt8}; stylectx=nothing) = String(copy(val))
to_param_type(::Type{Vector{UInt8}}, val::String; stylectx=nothing) = convert(Vector{UInt8}, copy(codeunits(val)))
to_param_type(::Type{Vector{T}}, val::Vector{T}, _collection_format::Union{String,Nothing}; stylectx=nothing) where {T} = val
to_param_type(::Type{Vector{T}}, json::Vector{Any}; stylectx=nothing) where {T} = [to_param_type(T, x; stylectx) for x in json]

function to_param_type(::Type{Vector{T}}, json::Dict{String, Any}; stylectx=nothing) where {T <: APIModel}
if !isnothing(stylectx) && stylectx.name == "deepObject" && stylectx.is_explode
cvt = convert_dicts_to_arrays(json)
if isa(cvt, Vector)
to_param_type(Vector{T}, cvt; stylectx)
else
to_param_type(T, json; stylectx)
end
else
to_param_type(T, json; stylectx)
end
end

function to_param_type(::Type{T}, strval::String) where {T <: APIModel}
from_json(T, JSON.parse(strval))
function to_param_type(::Type{T}, strval::String; stylectx=nothing) where {T <: APIModel}
from_json(T, JSON.parse(strval); stylectx)
end

function to_param_type(::Type{T}, json::Dict{String,Any}) where {T <: APIModel}
from_json(T, json)
function to_param_type(::Type{T}, json::Dict{String,Any}; stylectx=nothing) where {T <: APIModel}
from_json(T, json; stylectx)
end

function to_param_type(::Type{Vector{T}}, strval::String, delim::String) where {T}
function to_param_type(::Type{Vector{T}}, strval::String, delim::String; stylectx=nothing) where {T}
elems = string.(strip.(split(strval, delim)))
return map(x->to_param_type(T, x), elems)
return map(x->to_param_type(T, x; stylectx), elems)
end

function to_param_type(::Type{Vector{T}}, strval::String) where {T}
function to_param_type(::Type{Vector{T}}, strval::String; stylectx=nothing) where {T}
elems = JSON.parse(strval)
return map(x->to_param_type(T, x), elems)
return map(x->to_param_type(T, x; stylectx), elems)
end

struct StyleCtx
name::String
is_explode::Bool
end

function to_param(T, source::Dict, name::String; required::Bool=false, collection_format::Union{String,Nothing}=",", multipart::Bool=false, isfile::Bool=false, style::String="form", is_explode::Bool=true)
Expand All @@ -150,7 +138,6 @@ function to_param(T, source::Dict, name::String; required::Bool=false, collectio
source = deserialize_deep_object(source)
end
param = get_param(source, name, required)

if param === nothing
return nothing
end
Expand All @@ -159,7 +146,7 @@ function to_param(T, source::Dict, name::String; required::Bool=false, collectio
param = isfile ? param.data : String(param.data)
end
if deep_explode
return to_param_type(T, param)
return to_param_type(T, param; stylectx=StyleCtx(style, is_explode))
end
if T <: Vector
to_param_type(T, param, collection_format)
Expand Down

0 comments on commit 20f733f

Please sign in to comment.