WebServiceClientBuilder { void buildPath(CodeBuilder builder, String path, Map<String, Integer> pathParamIndexes) { if (pathParamIndexes.isEmpty()) { builder.indent(1).append("String path = {};\n", variable(path)); } else { for (Map.Entry<String, Integer> entry : pathParamIndexes.entrySet()) { builder.indent(1).append("if (param{} == null) throw new Error(\"path param must not be null, name={}\");\n", entry.getValue(), entry.getKey()); } builder.indent(1).append("StringBuilder builder = new StringBuilder();\n"); int currentIndex = 0; while (true) { int variableStart = path.indexOf(':', currentIndex); if (variableStart < 0) break; int variableEnd = path.indexOf('/', variableStart); if (variableEnd < 0) variableEnd = path.length(); builder.indent(1).append("builder.append({}).append({}.toString(param{}));\n", variable(path.substring(currentIndex, variableStart)), type(PathParamHelper.class), pathParamIndexes.get(path.substring(variableStart + 1, variableEnd))); currentIndex = variableEnd; } if (currentIndex < path.length()) { builder.indent(1).append("builder.append({});\n", variable(path.substring(currentIndex))); } builder.indent(1).append("String path = builder.toString();\n"); } } WebServiceClientBuilder(Class<T> serviceInterface, WebServiceClient client); T build();  }
@Test void buildPath() { var builder = new CodeBuilder(); this.builder.buildPath(builder, "/test", Map.of()); assertThat(builder.build()).isEqualToIgnoringWhitespace("String path = \"/test\";"); builder = new CodeBuilder(); this.builder.buildPath(builder, "/test/:id", Map.of("id", 0)); assertThat(builder.build()).contains("builder.append(\"/test/\").append(core.framework.internal.web.service.PathParamHelper.toString(param0));"); builder = new CodeBuilder(); this.builder.buildPath(builder, "/:id/status", Map.of("id", 0)); assertThat(builder.build()) .contains("builder.append(\"/\").append(core.framework.internal.web.service.PathParamHelper.toString(param0));") .contains("builder.append(\"/status\");"); builder = new CodeBuilder(); this.builder.buildPath(builder, "/test/:key1/:key2", Map.of("key1", 0, "key2", 1)); assertThat(builder.build()) .contains("builder.append(\"/test/\").append(core.framework.internal.web.service.PathParamHelper.toString(param0));") .contains("builder.append(\"/\").append(core.framework.internal.web.service.PathParamHelper.toString(param1));"); builder = new CodeBuilder(); this.builder.buildPath(builder, "/test/:key1/:key2/", Map.of("key1", 0, "key2", 1)); assertThat(builder.build()) .contains("builder.append(\"/test/\").append(core.framework.internal.web.service.PathParamHelper.toString(param0));") .contains("builder.append(\"/\").append(core.framework.internal.web.service.PathParamHelper.toString(param1));") .contains("builder.append(\"/\");"); }