From 9d0cebd51ddf73075db5b1fb1bedece3a2c70851 Mon Sep 17 00:00:00 2001 From: Kyle Plump Date: Tue, 31 Dec 2024 17:51:56 -0500 Subject: [PATCH 1/6] initialize matcher on leaf init --- lib/hanami/router/leaf.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/hanami/router/leaf.rb b/lib/hanami/router/leaf.rb index df976137..a215783a 100644 --- a/lib/hanami/router/leaf.rb +++ b/lib/hanami/router/leaf.rb @@ -18,6 +18,7 @@ def initialize(route, to, constraints) @to = to @constraints = constraints @params = nil + @matcher = Mustermann.new(@route, type: :rails, version: "5.0", capture: @constraints) end # @api private From 0836b196bc81bc7b71550c78d6f00e7ef8949262 Mon Sep 17 00:00:00 2001 From: Kyle Plump Date: Fri, 3 Jan 2025 12:08:27 -0500 Subject: [PATCH 2/6] split on string instead of regex, memo segments --- lib/hanami/router/node.rb | 3 +-- lib/hanami/router/trie.rb | 10 ++++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/hanami/router/node.rb b/lib/hanami/router/node.rb index 588d21c1..e7a20973 100644 --- a/lib/hanami/router/node.rb +++ b/lib/hanami/router/node.rb @@ -18,7 +18,7 @@ class Node def initialize @variable = nil @fixed = nil - @leaves = nil + @leaves = [] end # @api private @@ -41,7 +41,6 @@ def get(segment) # @api private # @since 2.0.0 def leaf!(route, to, constraints) - @leaves ||= [] @leaves << Leaf.new(route, to, constraints) end diff --git a/lib/hanami/router/trie.rb b/lib/hanami/router/trie.rb index d46cc5af..9a040114 100644 --- a/lib/hanami/router/trie.rb +++ b/lib/hanami/router/trie.rb @@ -17,6 +17,7 @@ class Trie # @since 2.0.0 def initialize @root = Node.new + @segments_map = {} end # @api private @@ -47,15 +48,20 @@ def find(path) # @api private # @since 2.0.0 - SEGMENT_SEPARATOR = /\// + SEGMENT_SEPARATOR = "/" private_constant :SEGMENT_SEPARATOR # @api private # @since 2.2.0 def segments_from(path) + if @segments_map[path] + @segments_map[path] + else _, *segments = path.split(SEGMENT_SEPARATOR) - + + @segments_map[path] = segments segments + end end end end From 2a7728b94fcb9ec586c870870d3489172503b103 Mon Sep 17 00:00:00 2001 From: Kyle Plump Date: Sun, 26 Jan 2025 10:45:54 -0500 Subject: [PATCH 3/6] cleanup / finalize proposal --- lib/hanami/router.rb | 2 +- lib/hanami/router/leaf.rb | 12 +++++------- lib/hanami/router/node.rb | 3 ++- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/lib/hanami/router.rb b/lib/hanami/router.rb index 379225e8..e59cfe6e 100644 --- a/lib/hanami/router.rb +++ b/lib/hanami/router.rb @@ -923,7 +923,7 @@ def _params(env, params) end env[PARAMS].merge!(::Rack::Utils.parse_nested_query(env[::Rack::QUERY_STRING])) - env[PARAMS].merge!(params) + env[PARAMS].merge!(params) unless params.empty? env[PARAMS] = Params.deep_symbolize(env[PARAMS]) env end diff --git a/lib/hanami/router/leaf.rb b/lib/hanami/router/leaf.rb index a215783a..6dca2916 100644 --- a/lib/hanami/router/leaf.rb +++ b/lib/hanami/router/leaf.rb @@ -9,16 +9,14 @@ class Leaf # # @api private # @since 2.2.0 - attr_reader :to, :params + attr_reader :to, :params, :matcher # @api private # @since 2.2.0 def initialize(route, to, constraints) - @route = route @to = to - @constraints = constraints @params = nil - @matcher = Mustermann.new(@route, type: :rails, version: "5.0", capture: @constraints) + @matcher = Mustermann.new(route, type: :rails, version: "5.0", capture: constraints) end # @api private @@ -35,9 +33,9 @@ def match(path) # @api private # @since 2.2.0 - def matcher - @matcher ||= Mustermann.new(@route, type: :rails, version: "5.0", capture: @constraints) - end + # def matcher + # @matcher ||= Mustermann.new(@route, type: :rails, version: "5.0", capture: @constraints) + # end end end end diff --git a/lib/hanami/router/node.rb b/lib/hanami/router/node.rb index e7a20973..588d21c1 100644 --- a/lib/hanami/router/node.rb +++ b/lib/hanami/router/node.rb @@ -18,7 +18,7 @@ class Node def initialize @variable = nil @fixed = nil - @leaves = [] + @leaves = nil end # @api private @@ -41,6 +41,7 @@ def get(segment) # @api private # @since 2.0.0 def leaf!(route, to, constraints) + @leaves ||= [] @leaves << Leaf.new(route, to, constraints) end From e5a3b3321e235a5127d3b3ced265d338b7cf072c Mon Sep 17 00:00:00 2001 From: Kyle Plump Date: Sun, 26 Jan 2025 10:46:32 -0500 Subject: [PATCH 4/6] remove comment --- lib/hanami/router/leaf.rb | 8 -------- 1 file changed, 8 deletions(-) diff --git a/lib/hanami/router/leaf.rb b/lib/hanami/router/leaf.rb index 6dca2916..40065b00 100644 --- a/lib/hanami/router/leaf.rb +++ b/lib/hanami/router/leaf.rb @@ -28,14 +28,6 @@ def match(path) match end - - private - - # @api private - # @since 2.2.0 - # def matcher - # @matcher ||= Mustermann.new(@route, type: :rails, version: "5.0", capture: @constraints) - # end end end end From b4b37981a272bf2f9d0ebaab2d698ad325628f90 Mon Sep 17 00:00:00 2001 From: Kyle Plump Date: Fri, 7 Feb 2025 14:10:22 -0500 Subject: [PATCH 5/6] suggested fixes --- lib/hanami/router.rb | 2 +- lib/hanami/router/trie.rb | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/hanami/router.rb b/lib/hanami/router.rb index e59cfe6e..379225e8 100644 --- a/lib/hanami/router.rb +++ b/lib/hanami/router.rb @@ -923,7 +923,7 @@ def _params(env, params) end env[PARAMS].merge!(::Rack::Utils.parse_nested_query(env[::Rack::QUERY_STRING])) - env[PARAMS].merge!(params) unless params.empty? + env[PARAMS].merge!(params) env[PARAMS] = Params.deep_symbolize(env[PARAMS]) env end diff --git a/lib/hanami/router/trie.rb b/lib/hanami/router/trie.rb index 9a040114..8261086d 100644 --- a/lib/hanami/router/trie.rb +++ b/lib/hanami/router/trie.rb @@ -57,10 +57,8 @@ def segments_from(path) if @segments_map[path] @segments_map[path] else - _, *segments = path.split(SEGMENT_SEPARATOR) - - @segments_map[path] = segments - segments + _, *segments = path.split(SEGMENT_SEPARATOR) + @segments_map[path] = segments end end end From 2460254ca0bc0601783f6aed58fcf7fc2bbf0b3d Mon Sep 17 00:00:00 2001 From: kyle plump Date: Fri, 7 Feb 2025 14:13:21 -0500 Subject: [PATCH 6/6] rubo --- lib/hanami/router/trie.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/hanami/router/trie.rb b/lib/hanami/router/trie.rb index 8261086d..e285193d 100644 --- a/lib/hanami/router/trie.rb +++ b/lib/hanami/router/trie.rb @@ -57,8 +57,8 @@ def segments_from(path) if @segments_map[path] @segments_map[path] else - _, *segments = path.split(SEGMENT_SEPARATOR) - @segments_map[path] = segments + _, *segments = path.split(SEGMENT_SEPARATOR) + @segments_map[path] = segments end end end