public static void fromJson(Iterable<java.util.Map.Entry<String, Object>> json, ServiceList obj) { for (java.util.Map.Entry<String, Object> member : json) { switch (member.getKey()) { case "index": if (member.getValue() instanceof Number) { obj.setIndex(((Number)member.getValue()).longValue()); } break; case "list": if (member.getValue() instanceof JsonArray) { java.util.ArrayList<io.vertx.ext.consul.Service> list = new java.util.ArrayList<>(); ((Iterable<Object>)member.getValue()).forEach( item -> { if (item instanceof JsonObject) list.add(new io.vertx.ext.consul.Service((JsonObject)item)); }); obj.setList(list); } break; } } }
@Override public ConsulClient catalogServicesWithOptions(BlockingQueryOptions options, Handler<AsyncResult<ServiceList>> resultHandler) { requestObject(HttpMethod.GET, "/v1/catalog/services", Query.of(options), null, resultHandler, (json, headers) -> { List<Service> list = json.stream().map(ServiceParser::parseCatalogInfo).collect(Collectors.toList()); return new ServiceList().setList(list).setIndex(Long.parseLong(headers.get(INDEX_HEADER))); }); return this; }
@Override public ConsulClient catalogNodeServicesWithOptions(String node, BlockingQueryOptions options, Handler<AsyncResult<ServiceList>> resultHandler) { requestObject(HttpMethod.GET, "/v1/catalog/node/" + urlEncode(node), Query.of(options), null, resultHandler, (json, headers) -> { JsonObject nodeInfo = json.getJsonObject("Node"); String nodeName = nodeInfo.getString("Node"); String nodeAddress = nodeInfo.getString("Address"); List<Service> list = json.getJsonObject("Services").stream() .map(obj -> ServiceParser.parseNodeInfo(nodeName, nodeAddress, (JsonObject) obj.getValue())) .collect(Collectors.toList()); return new ServiceList().setList(list).setIndex(Long.parseLong(headers.get(INDEX_HEADER))); }); return this; }
@Override public ConsulClient catalogServiceNodesWithOptions(String service, ServiceQueryOptions options, Handler<AsyncResult<ServiceList>> resultHandler) { Query query = options == null ? null : Query.of("tag", options.getTag()).put("near", options.getNear()).put(options.getBlockingOptions()); requestArray(HttpMethod.GET, "/v1/catalog/service/" + urlEncode(service), query, null, resultHandler, (arr, headers) -> { List<Service> list = arr.stream().map(obj -> new Service((JsonObject) obj)).collect(Collectors.toList()); return new ServiceList().setList(list).setIndex(Long.parseLong(headers.get(INDEX_HEADER))); }); return this; }
@Test public void testServiceListCopy() { List<Service> services = new ArrayList<>(); services.add(randomService()); services.add(randomService()); ServiceList list = new ServiceList() .setIndex(randomPositiveInt()) .setList(services); checkServiceList(list, new ServiceList(list)); checkServiceList(list, new ServiceList(list.toJson())); List<Service> shuffledServices = new ArrayList<>(); shuffledServices.add(services.get(1)); shuffledServices.add(services.get(0)); ServiceList shuffled = new ServiceList() .setIndex(list.getIndex()) .setList(shuffledServices); checkServiceList(list, shuffled); }