From e604d3ef36ad72e90bc75a1ba2dec7501d277e62 Mon Sep 17 00:00:00 2001 From: Adrian Date: Wed, 4 Dec 2019 19:52:01 +0100 Subject: [PATCH] v0.22.7 --- CHANGELOG.md | 5 +++++ Project.toml | 2 +- src/AppServer.jl | 8 ++++---- src/Configuration.jl | 2 +- src/Flax.jl | 37 ++++++++++++++++++++++--------------- src/HTMLRenderer.jl | 34 +++++++++++++++++----------------- src/JSRenderer.jl | 2 +- src/Requests.jl | 38 +++++++++++++++++++------------------- src/Router.jl | 18 +++++++++--------- 9 files changed, 79 insertions(+), 67 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a6374bfe3..ef6d11297 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## v0.22.5 - 2019-12-04 + +* added support for `if` blocks +* performance optimisations + ## v0.22.5 - 2019-12-03 * bug fixes diff --git a/Project.toml b/Project.toml index 311a323ae..5cdc13a19 100755 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "Genie" uuid = "c43c736e-a2d1-11e8-161f-af95117fbd1e" authors = ["Adrian Salceanu "] -version = "0.22.6" +version = "0.22.7" [deps] ArgParse = "c7e460c6-2fb9-53a9-8c5b-16f535851c63" diff --git a/src/AppServer.jl b/src/AppServer.jl index 7701b1696..96d6aa821 100755 --- a/src/AppServer.jl +++ b/src/AppServer.jl @@ -103,7 +103,7 @@ end Adds a signature header to the response using the value in `Genie.config.server_signature`. If `Genie.config.server_signature` is empty, the header is not added. """ -@inline function sign_response!(res::HTTP.Response) :: HTTP.Response +function sign_response!(res::HTTP.Response) :: HTTP.Response headers = Dict(res.headers) isempty(Genie.config.server_signature) || (headers["Server"] = Genie.config.server_signature) @@ -117,7 +117,7 @@ end Http server handler function - invoked when the server gets a request. """ -@inline function handle_request(req::HTTP.Request, res::HTTP.Response, ip::Sockets.IPv4 = Sockets.IPv4(Genie.config.server_host)) :: HTTP.Response +function handle_request(req::HTTP.Request, res::HTTP.Response, ip::Sockets.IPv4 = Sockets.IPv4(Genie.config.server_host)) :: HTTP.Response isempty(Genie.config.server_signature) && sign_response!(res) try @@ -159,7 +159,7 @@ end Configures the handler for the HTTP Request and handles errors. """ -@inline function setup_http_handler(req::HTTP.Request, res::HTTP.Response = HTTP.Response()) :: HTTP.Response +function setup_http_handler(req::HTTP.Request, res::HTTP.Response = HTTP.Response()) :: HTTP.Response try Distributed.@fetch handle_request(req, res) catch ex # ex is a Distributed.RemoteException @@ -189,7 +189,7 @@ end Configures the handler for WebSockets requests. """ -@inline function setup_ws_handler(req::HTTP.Request, ws_client) :: Nothing +function setup_ws_handler(req::HTTP.Request, ws_client) :: Nothing while ! eof(ws_client) write(ws_client, String(Distributed.@fetch handle_ws_request(req, String(readavailable(ws_client)), ws_client))) end diff --git a/src/Configuration.jl b/src/Configuration.jl index 4559354ad..276d573f5 100755 --- a/src/Configuration.jl +++ b/src/Configuration.jl @@ -3,7 +3,7 @@ Core genie configuration / settings functionality. """ module Configuration -const GENIE_VERSION = v"0.22.6" +const GENIE_VERSION = v"0.22.7" import Logging import Genie diff --git a/src/Flax.jl b/src/Flax.jl index 70df71f7b..98b015893 100755 --- a/src/Flax.jl +++ b/src/Flax.jl @@ -62,7 +62,7 @@ end Renders a template file. """ -@inline function template(path::String; partial::Bool = true, context::Module = @__MODULE__) :: String +function template(path::String; partial::Bool = true, context::Module = @__MODULE__) :: String Base.invokelatest(HTMLRenderer.get_template(path, partial = partial, context = context))::String end @@ -90,7 +90,7 @@ end Loads the rendering vars into the task's scope """ -@inline function registervars(vars...) :: Nothing +function registervars(vars...) :: Nothing init_task_local_storage() task_local_storage(:__vars, merge(task_local_storage(:__vars), Dict{Symbol,Any}(vars))) @@ -98,7 +98,7 @@ Loads the rendering vars into the task's scope end -@inline function vars_signature() :: String +function vars_signature() :: String task_local_storage(:__vars) |> keys |> collect |> sort |> string end @@ -108,7 +108,7 @@ end Generates function name for generated Flax views. """ -@inline function function_name(file_path::String) :: String +function function_name(file_path::String) :: String "func_$(SHA.sha1( relpath(isempty(file_path) ? " " : file_path) * vars_signature() ) |> bytes2hex)" end @@ -118,7 +118,7 @@ end Generates module name for generated Flax views. """ -@inline function m_name(file_path::String) :: String +function m_name(file_path::String) :: String string(SHA.sha1( relpath(isempty(file_path) ? " " : file_path) * vars_signature()) |> bytes2hex) end @@ -128,7 +128,7 @@ end Converts a HTML document to a Flax document. """ -@inline function html_to_flax(file_path::String; partial = true) :: String +function html_to_flax(file_path::String; partial = true) :: String to_flax(file_path, parse_template, partial = partial) end @@ -138,7 +138,7 @@ end Converts string view data to Flax code """ -@inline function string_to_flax(content::String; partial = true, f_name::Union{Symbol,Nothing} = nothing, prepend = "") :: String +function string_to_flax(content::String; partial = true, f_name::Union{Symbol,Nothing} = nothing, prepend = "") :: String to_flax(content, parse_string, partial = partial, f_name = f_name, prepend = prepend) end @@ -148,7 +148,7 @@ end Converts an input file to Flax code """ -@inline function to_flax(input::String, f::Function; partial = true, f_name::Union{Symbol,Nothing} = nothing, prepend = "\n") :: String +function to_flax(input::String, f::Function; partial = true, f_name::Union{Symbol,Nothing} = nothing, prepend = "\n") :: String f_name = (f_name === nothing) ? function_name(string(input, partial)) : f_name string("function $(f_name)() \n", @@ -220,7 +220,7 @@ end Parses a HTML file into Flax code. """ -@inline function parse_template(file_path::String; partial::Bool = true) :: String +function parse_template(file_path::String; partial::Bool = true) :: String parse(read_template_file(file_path), partial = partial) end @@ -230,12 +230,12 @@ end Parses a HTML string into Flax code. """ -@inline function parse_string(data::String; partial::Bool = true) :: String +function parse_string(data::String; partial::Bool = true) :: String parse(parsetags(data), partial = partial) end -@inline function parse(input::String; partial::Bool = true) :: String +function parse(input::String; partial::Bool = true) :: String HTMLRenderer.parsehtml(input, partial = partial) end @@ -245,11 +245,12 @@ end Parses special Flax tags. """ -@inline function parsetags(line::Tuple{Int,String}) :: String +function parsetags(line::Tuple{Int,String}) :: String parsetags(line[2]) end -@inline function parsetags(code::String) :: String +function parsetags(code::String) :: String code = replace(code, "<%"=>"""""") end @@ -259,7 +260,7 @@ end Generated functions that represent Flax functions definitions corresponding to HTML elements. """ -@inline function register_elements() :: Nothing +function register_elements() :: Nothing for elem in HTMLRenderer.NORMAL_ELEMENTS register_normal_element(elem) end @@ -272,7 +273,7 @@ Generated functions that represent Flax functions definitions corresponding to H end -@inline function register_element(elem::Union{Symbol,String}, elem_type::Union{Symbol,String} = :normal; context = Flax) :: Nothing +function register_element(elem::Union{Symbol,String}, elem_type::Union{Symbol,String} = :normal; context = Flax) :: Nothing elem = string(elem) occursin('-', elem) && (elem = HTMLRenderer.denormalize_element(elem)) @@ -335,6 +336,12 @@ macro foreach(f, arr) end +macro condblock(expr) + expr.args[2] = esc(:([$([arg for arg in expr.args[2].args if !isa(arg, LineNumberNode)]...),])) + expr +end + + register_elements() diff --git a/src/HTMLRenderer.jl b/src/HTMLRenderer.jl index 422ff5694..4c9af4eac 100644 --- a/src/HTMLRenderer.jl +++ b/src/HTMLRenderer.jl @@ -41,25 +41,25 @@ export HTMLString Generates a HTML element in the form <...> """ -@inline function normal_element(f::Function, elem::String, args = [], attrs::Vector{Pair{Symbol,Any}} = Pair{Symbol,Any}[]) :: HTMLString +function normal_element(f::Function, elem::String, args = [], attrs::Vector{Pair{Symbol,Any}} = Pair{Symbol,Any}[]) :: HTMLString normal_element(f(), elem, args, attrs...) end -@inline function normal_element(children::Union{String,Vector{String}}, elem::String, args, attrs::Pair{Symbol,Any}) :: HTMLString +function normal_element(children::Union{String,Vector{String}}, elem::String, args, attrs::Pair{Symbol,Any}) :: HTMLString normal_element(children, elem, args, Pair{Symbol,Any}[attrs]) end -@inline function normal_element(children::Union{String,Vector{String}}, elem::String, args, attrs...) :: HTMLString +function normal_element(children::Union{String,Vector{String}}, elem::String, args, attrs...) :: HTMLString normal_element(children, elem, args, Pair{Symbol,Any}[attrs...]) end -@inline function normal_element(children::Union{String,Vector{String}}, elem::String, args = [], attrs::Vector{Pair{Symbol,Any}} = Pair{Symbol,Any}[]) :: HTMLString +function normal_element(children::Union{String,Vector{String}}, elem::String, args = [], attrs::Vector{Pair{Symbol,Any}} = Pair{Symbol,Any}[]) :: HTMLString children = join(children) elem = normalize_element(elem) attribs = rstrip(attributes(attrs)) string("<", elem, (isempty(attribs) ? "" : " $attribs"), (isempty(args) ? "" : " $(join(args, " "))"), ">", prepare_template(children), "") end -@inline function normal_element(elem::String, attrs::Vector{Pair{Symbol,Any}} = Pair{Symbol,Any}[]) :: HTMLString +function normal_element(elem::String, attrs::Vector{Pair{Symbol,Any}} = Pair{Symbol,Any}[]) :: HTMLString normal_element("", elem, attrs...) end -@inline function normal_element(elems::Vector, elem::String, args = [], attrs...) :: HTMLString +function normal_element(elems::Vector, elem::String, args = [], attrs...) :: HTMLString io = IOBuffer() for e in elems @@ -74,7 +74,7 @@ end normal_element(String(take!(io)), elem, args, attrs...) end -@inline function normal_element(_::Nothing, __::Any) :: HTMLString +function normal_element(_::Nothing, __::Any) :: HTMLString "" end @@ -85,10 +85,10 @@ end Cleans up the template before rendering (ex by removing empty nodes). """ -@inline function prepare_template(s::String) :: String +function prepare_template(s::String) :: String s end -@inline function prepare_template(v::Vector{T})::String where {T} +function prepare_template(v::Vector{T})::String where {T} filter!(v) do (x) ! isa(x, Nothing) end @@ -118,10 +118,10 @@ end Cleans up problematic characters or DOM elements. """ -@inline function normalize_element(elem::String) +function normalize_element(elem::String) replace(string(lowercase(elem)), "_____"=>"-") end -@inline function denormalize_element(elem::String) +function denormalize_element(elem::String) replace(string(lowercase(elem)), "-"=>"_____") end @@ -131,7 +131,7 @@ end Generates a void HTML element in the form <...> """ -@inline function void_element(elem::String, args = [], attrs::Vector{Pair{Symbol,Any}} = Pair{Symbol,Any}[]) :: HTMLString +function void_element(elem::String, args = [], attrs::Vector{Pair{Symbol,Any}} = Pair{Symbol,Any}[]) :: HTMLString attribs = rstrip(attributes(attrs)) string("<", normalize_element(elem), (isempty(attribs) ? "" : " $attribs"), (isempty(args) ? "" : " $(join(args, " "))"), ">") end @@ -143,10 +143,10 @@ end Cleans up empty elements. """ -@inline function skip_element(f::Function) :: HTMLString +function skip_element(f::Function) :: HTMLString "$(prepare_template(f()))" end -@inline function skip_element() :: HTMLString +function skip_element() :: HTMLString "" end @@ -215,7 +215,7 @@ end """ Outputs document's doctype. """ -@inline function doctype(doctype::Symbol = :html) :: HTMLString +function doctype(doctype::Symbol = :html) :: HTMLString "" end @@ -223,10 +223,10 @@ end """ Outputs document's doctype. """ -@inline function doc(html::String) :: HTMLString +function doc(html::String) :: HTMLString doctype() * "\n" * html end -@inline function doc(doctype::Symbol, html::String) :: HTMLString +function doc(doctype::Symbol, html::String) :: HTMLString doctype(doctype) * "\n" * html end diff --git a/src/JSRenderer.jl b/src/JSRenderer.jl index c08f93a6d..558fab32e 100644 --- a/src/JSRenderer.jl +++ b/src/JSRenderer.jl @@ -44,7 +44,7 @@ end """ """ -@inline function to_js(data::String; prepend = "\n") :: String +function to_js(data::String; prepend = "\n") :: String string("function $(Flax.function_name(data))() \n", Flax.injectvars(), prepend, diff --git a/src/Requests.jl b/src/Requests.jl index 16cf46739..7c2706098 100644 --- a/src/Requests.jl +++ b/src/Requests.jl @@ -17,7 +17,7 @@ Reexport.@reexport using HTTP Processes an `application/json` `POST` request. If it fails to successfully parse the `JSON` data it returns `nothing`. The original payload can still be accessed invoking `rawpayload()` """ -@inline function jsonpayload() +function jsonpayload() Router.@params(Genie.PARAMS_JSON_PAYLOAD) end @@ -28,7 +28,7 @@ end Processes an `application/json` `POST` request attempting to convert the payload into a value of type `T`. If it fails to successfully parse and convert the `JSON` data, it throws an exception. The original payload can still be accessed invoking `rawpayload()` """ -@inline function jsonpayload(::Type{T})::T where {T} +function jsonpayload(::Type{T})::T where {T} Router.@params(Genie.PARAMS_JSON_PAYLOAD)::T end @@ -38,7 +38,7 @@ end Returns the raw `POST` payload as a `String`. """ -@inline function rawpayload() :: String +function rawpayload() :: String Router.@params(Genie.PARAMS_RAW_PAYLOAD) end @@ -48,7 +48,7 @@ end Collection of form uploaded files. """ -@inline function filespayload() :: Dict{String,Input.HttpFile} +function filespayload() :: Dict{String,Input.HttpFile} Router.@params(Genie.PARAMS_FILES) end @@ -58,7 +58,7 @@ end Returns the `HttpFile` uploaded through the `key` input name. """ -@inline function filespayload(key::Union{String,Symbol}) :: Input.HttpFile +function filespayload(key::Union{String,Symbol}) :: Input.HttpFile Router.@params(Genie.PARAMS_FILES)[string(key)] end @@ -68,7 +68,7 @@ end Checks if the collection of uploaded files contains a file stored under the `key` name. """ -@inline function infilespayload(key::Union{String,Symbol}) :: Bool +function infilespayload(key::Union{String,Symbol}) :: Bool haskey(filespayload(), string(key)) end @@ -99,7 +99,7 @@ end Original filename of the uploaded `HttpFile` `file`. """ -@inline function filename(file::Input.HttpFile) :: String +function filename(file::Input.HttpFile) :: String file.name end @@ -109,7 +109,7 @@ end A dict representing the POST variables payload of the request (corresponding to a `form-data` request) """ -@inline function postpayload() :: Dict{Symbol,Any} +function postpayload() :: Dict{Symbol,Any} Router.@params(Genie.PARAMS_POST_KEY) end @@ -119,7 +119,7 @@ end Returns the value of the POST variables `key`. """ -@inline function postpayload(key::Symbol) +function postpayload(key::Symbol) postpayload()[key] end @@ -129,7 +129,7 @@ end Returns the value of the POST variables `key` or the `default` value if `key` is not defined. """ -@inline function postpayload(key::Symbol, default::Any) +function postpayload(key::Symbol, default::Any) haskey(postpayload(), key) ? postpayload(key) : default end @@ -139,7 +139,7 @@ end A dict representing the GET/query variables payload of the request (the part correspoding to `?foo=bar&baz=moo`) """ -@inline function getpayload() :: Dict{Symbol,Any} +function getpayload() :: Dict{Symbol,Any} Router.@params(Genie.PARAMS_GET_KEY) end @@ -149,7 +149,7 @@ end The value of the GET/query variable `key`, as in `?key=value` """ -@inline function getpayload(key::Symbol) :: Any +function getpayload(key::Symbol) :: Any getpayload()[key] end @@ -159,7 +159,7 @@ end The value of the GET/query variable `key`, as in `?key=value`. If `key` is not defined, `default` is returned. """ -@inline function getpayload(key::Symbol, default::Any) :: Any +function getpayload(key::Symbol, default::Any) :: Any haskey(getpayload(), key) ? getpayload(key) : default end @@ -169,7 +169,7 @@ end Returns the raw HTTP.Request object associated with the request. """ -@inline function request() :: HTTP.Request +function request() :: HTTP.Request Router.@params(Genie.PARAMS_REQUEST_KEY) end @@ -179,7 +179,7 @@ end Utility function for accessing the `@params` collection, which holds the request variables. """ -@inline function payload() :: Dict +function payload() :: Dict Router.@params end @@ -189,7 +189,7 @@ end Utility function for accessing the `key` value within the `@params` collection of request variables. """ -@inline function payload(key::Symbol) :: Any +function payload(key::Symbol) :: Any Router.@params()[key] end @@ -200,7 +200,7 @@ end Utility function for accessing the `key` value within the `@params` collection of request variables. If `key` is not defined, `default_value` is returned. """ -@inline function payload(key::Symbol, default_value::T)::T where {T} +function payload(key::Symbol, default_value::T)::T where {T} haskey(Router.@params(), key) ? Router.@params()[key] : default_value end @@ -210,7 +210,7 @@ end Returns the `Route` object which was matched for the current request. """ -@inline function matchedroute() :: Genie.Router.Route +function matchedroute() :: Genie.Router.Route Router.@params(Genie.PARAMS_ROUTE_KEY) end @@ -220,7 +220,7 @@ end Returns the `Channel` object which was matched for the current request. """ -@inline function matchedchannel() :: Genie.Router.Channel +function matchedchannel() :: Genie.Router.Channel Router.@params(Genie.PARAMS_CHANNELS_KEY) end diff --git a/src/Router.jl b/src/Router.jl index f0c0c6eb6..886f62f20 100755 --- a/src/Router.jl +++ b/src/Router.jl @@ -272,7 +272,7 @@ end Computes the name of a channel. """ -@inline function channelname(params::Channel) :: Symbol +function channelname(params::Channel) :: Symbol baptizer(params, String[]) end @@ -290,7 +290,7 @@ end """ The list of the defined named routes. """ -@inline function named_routes() :: OrderedCollections.OrderedDict{Symbol,Route} +function named_routes() :: OrderedCollections.OrderedDict{Symbol,Route} _routes end const namedroutes = named_routes @@ -311,7 +311,7 @@ end The list of the defined named channels. """ -@inline function named_channels() :: OrderedCollections.OrderedDict{Symbol,Channel} +function named_channels() :: OrderedCollections.OrderedDict{Symbol,Channel} _channels end const namedchannels = named_channels @@ -330,7 +330,7 @@ end """ Gets the `Route` correspoding to `routename` """ -@inline function get_route(route_name::Symbol) :: Route +function get_route(route_name::Symbol) :: Route haskey(named_routes(), route_name) ? named_routes()[route_name] : error("Route named `$route_name` is not defined") end @@ -340,7 +340,7 @@ end Returns a vector of defined routes. """ -@inline function routes() :: Vector{Route} +function routes() :: Vector{Route} collect(values(_routes)) |> reverse end @@ -350,7 +350,7 @@ end Returns a vector of defined channels. """ -@inline function channels() :: Vector{Channel} +function channels() :: Vector{Channel} collect(values(_channels)) |> reverse end @@ -360,7 +360,7 @@ end Removes the route with the corresponding name from the routes collection and returns the collection of remaining routes. """ -@inline function delete!(routes::OrderedCollections.OrderedDict{Symbol,Route}, key::Symbol) :: OrderedCollections.OrderedDict{Symbol,Route} +function delete!(routes::OrderedCollections.OrderedDict{Symbol,Route}, key::Symbol) :: OrderedCollections.OrderedDict{Symbol,Route} OrderedCollections.delete!(routes, key) end @@ -415,7 +415,7 @@ end """ Generates the HTTP link corresponding to `route_name` using the parameters in `route_params`. """ -@inline function to_link(route_name::Symbol; preserve_query::Bool = true, extra_query::Dict = Dict(), route_params...) :: String +function to_link(route_name::Symbol; preserve_query::Bool = true, extra_query::Dict = Dict(), route_params...) :: String to_link(route_name, route_params_to_dict(route_params), preserve_query = preserve_query, extra_query = extra_query) end @@ -430,7 +430,7 @@ const toroute = to_link Converts the route params to a `Dict`. """ -@inline function route_params_to_dict(route_params) :: Dict{Symbol,Any} +function route_params_to_dict(route_params) :: Dict{Symbol,Any} Dict{Symbol,Any}(route_params) end