User defined context store types (#339)

allow the context storage to handle any types
This commit is contained in:
Rimas Silkaitis 2017-04-06 11:43:41 -07:00 committed by Serdar Dogruyol
parent 48128696c6
commit 0b4856b741
4 changed files with 43 additions and 2 deletions

View File

@ -34,9 +34,14 @@ describe "Context" do
it "can store variables" do it "can store variables" do
before_get "/" do |env| before_get "/" do |env|
t = TestContextStorageType.new
t.id = 32
a = AnotherContextStorageType.new
env.set "key", "value" env.set "key", "value"
env.set "before_get", "Kemal" env.set "before_get", "Kemal"
env.set "before_get_int", 123 env.set "before_get_int", 123
env.set "before_get_context_test", t
env.set "another_context_test", a
env.set "before_get_float", 3.5 env.set "before_get_float", 3.5
end end
@ -47,6 +52,7 @@ describe "Context" do
before_get: env.get("before_get"), before_get: env.get("before_get"),
before_get_int: env.get("before_get_int"), before_get_int: env.get("before_get_int"),
before_get_float: env.get("before_get_float"), before_get_float: env.get("before_get_float"),
before_get_context_test: env.get("before_get_context_test"),
} }
end end
request = HTTP::Request.new("GET", "/") request = HTTP::Request.new("GET", "/")
@ -59,5 +65,6 @@ describe "Context" do
context.store["before_get"].should eq "Kemal" context.store["before_get"].should eq "Kemal"
context.store["before_get_int"].should eq 123 context.store["before_get_int"].should eq 123
context.store["before_get_float"].should eq 3.5 context.store["before_get_float"].should eq 3.5
context.store["before_get_context_test"].as(TestContextStorageType).id.should eq 32
end end
end end

View File

@ -12,6 +12,23 @@ class CustomLogHandler < Kemal::BaseLogHandler
end end
end end
class TestContextStorageType
property id
@id = 1
def to_s
@id
end
end
class AnotherContextStorageType
property name
@name = "kemal-context"
end
add_context_storage_type(TestContextStorageType)
add_context_storage_type(AnotherContextStorageType)
def create_request_and_return_io(handler, request) def create_request_and_return_io(handler, request)
io = IO::Memory.new io = IO::Memory.new
response = HTTP::Server::Response.new(io) response = HTTP::Server::Response.new(io)

View File

@ -4,8 +4,13 @@
# Instances of this class are passed to an `HTTP::Server` handler. # Instances of this class are passed to an `HTTP::Server` handler.
class HTTP::Server class HTTP::Server
class Context class Context
alias StoreTypes = Nil | String | Int32 | Int64 | Float64 | Bool # :nodoc:
getter store = {} of String => StoreTypes STORE_MAPPINGS = [ Nil, String, Int32, Int64, Float64, Bool ]
macro finished
alias StoreTypes = Union({{ *STORE_MAPPINGS }})
getter store = {} of String => StoreTypes
end
def params def params
@request.url_params ||= route_lookup.params @request.url_params ||= route_lookup.params

View File

@ -76,3 +76,15 @@ macro halt(env, status_code = 200, response = "")
{{env}}.response.close {{env}}.response.close
next next
end end
# Extends context storage with user defined types.
#
# class User
# property name
# end
#
# add_context_storage_type(User)
#
macro add_context_storage_type(type)
{{ HTTP::Server::Context::STORE_MAPPINGS.push(type) }}
end