public MetadataResponse(Struct struct) {
    super(struct);
    Map<String, Errors> errors = new HashMap<String, Errors>();
    Map<Integer, Node> brokers = new HashMap<Integer, Node>();
    Object[] brokerStructs = (Object[]) struct.get(BROKERS_KEY_NAME);
    for (int i = 0; i < brokerStructs.length; i++) {
      Struct broker = (Struct) brokerStructs[i];
      int nodeId = broker.getInt(NODE_ID_KEY_NAME);
      String host = broker.getString(HOST_KEY_NAME);
      int port = broker.getInt(PORT_KEY_NAME);
      brokers.put(nodeId, new Node(nodeId, host, port));
    }
    List<PartitionInfo> partitions = new ArrayList<PartitionInfo>();
    Object[] topicInfos = (Object[]) struct.get(TOPIC_METATDATA_KEY_NAME);
    for (int i = 0; i < topicInfos.length; i++) {
      Struct topicInfo = (Struct) topicInfos[i];
      short topicError = topicInfo.getShort(TOPIC_ERROR_CODE_KEY_NAME);
      String topic = topicInfo.getString(TOPIC_KEY_NAME);
      if (topicError == Errors.NONE.code()) {
        Object[] partitionInfos = (Object[]) topicInfo.get(PARTITION_METADATA_KEY_NAME);
        for (int j = 0; j < partitionInfos.length; j++) {
          Struct partitionInfo = (Struct) partitionInfos[j];
          int partition = partitionInfo.getInt(PARTITION_KEY_NAME);
          int leader = partitionInfo.getInt(LEADER_KEY_NAME);
          Node leaderNode = leader == -1 ? null : brokers.get(leader);
          Object[] replicas = (Object[]) partitionInfo.get(REPLICAS_KEY_NAME);
          Node[] replicaNodes = new Node[replicas.length];
          for (int k = 0; k < replicas.length; k++) replicaNodes[k] = brokers.get(replicas[k]);
          Object[] isr = (Object[]) partitionInfo.get(ISR_KEY_NAME);
          Node[] isrNodes = new Node[isr.length];
          for (int k = 0; k < isr.length; k++) isrNodes[k] = brokers.get(isr[k]);
          partitions.add(new PartitionInfo(topic, partition, leaderNode, replicaNodes, isrNodes));
        }
      } else {
        errors.put(topic, Errors.forCode(topicError));
      }
    }

    this.errors = errors;
    this.cluster = new Cluster(brokers.values(), partitions, unauthorizedTopics(errors));
  }
 public ControlledShutdownResponse(Struct struct) {
   super(struct);
   errorCode = struct.getShort(ERROR_CODE_KEY_NAME);
   Set<TopicPartition> partitions = new HashSet<>();
   for (Object topicPartitionObj : struct.getArray(PARTITIONS_REMAINING_KEY_NAME)) {
     Struct topicPartition = (Struct) topicPartitionObj;
     String topic = topicPartition.getString(TOPIC_KEY_NAME);
     int partition = topicPartition.getInt(PARTITION_KEY_NAME);
     partitions.add(new TopicPartition(topic, partition));
   }
   partitionsRemaining = partitions;
 }