NestedParamsMiddleware extends AbstractWebMiddleware<HttpRequest, NRES> { protected Object assocNested(Parameters map, String[] keys, List<String> values) { if (keys.length > 0) { String[] ks = new String[keys.length - 1]; if (ks.length > 0) { System.arraycopy(keys, 1, ks, 0, ks.length); String j = ks[0]; if (j.isEmpty()) { String[] js = new String[keys.length - 2]; if (js.length > 0) { System.arraycopy(keys, 2, js, 0, js.length); } List<Object> nestedList = map.getList(keys[0]); for (int i = nestedList.size(); i < values.size(); i++) nestedList.add(null); for (int i = 0; i < values.size(); i++) { List<String> vs = new ArrayList<>(); vs.add(values.get(i)); if (js.length > 0) { nestedList.set(i, assocNested((Parameters) Optional.ofNullable(nestedList.get(i)).orElse(Parameters.empty()) , js, vs)); } else { nestedList.set(i, vs.get(0)); } } if (map.containsKey(keys[0])) { map.replace(keys[0], nestedList); } else { map.put(keys[0], nestedList); } return map; } else { Parameters submap = (Parameters) map.getRawType(keys[0]); if (submap == null) submap = Parameters.empty(); map.put(keys[0], assocNested(submap, ks, values)); return map; } } else { return assocConj(map, keys[0], values); } } else { return values; } }  HttpRequest nestedParamsRequest(HttpRequest request, Function<String, String[]> keyParser); @Override HttpResponse handle(HttpRequest request, MiddlewareChain<HttpRequest, NRES, ?, ?> chain);  }
@Test void testNestedParams() { Parameters params = Parameters.empty(); assocNested(params, new String[]{"val", ""}, new ArrayList<String>(){{ add("hoge"); }}); assertThat(params.getIn("val", 0)) .isEqualTo("hoge"); params = Parameters.empty(); assocNested(params, new String[]{"foo", "bar"}, new ArrayList<String>(){{ add("baz"); }}); assertThat(params.getIn("foo", "bar")) .isEqualTo("baz"); params = Parameters.empty(); assocNested(params, new String[]{"foo", "bar"}, new ArrayList<String>(){{ add("baz"); add("bay"); }}); assertThat(params.getIn("foo", "bar", 1)) .isEqualTo("bay"); }