Info.fromResult constructor

Info.fromResult(
  1. Object reqResult
)

Implementation

factory Info.fromResult(Object reqResult) {
  Map<String, String> infoMap = {};

  if (reqResult is RespType2<dynamic>) {
    final bulkString = reqResult.toBulkString().payload;
    if (bulkString != null) {
      List<String> result = bulkString
          .split('\n')
          .where((e) => e.isNotEmpty)
          .toList(growable: false);

      for (String line in result) {
        // 忽略空行和以 # 开头的行(节名)
        if (line.isEmpty || line.startsWith('#')) {
          continue;
        }

        // 分割属性行到键值对
        int delimiterIndex = line.indexOf(':');
        if (delimiterIndex != -1) {
          String key = line.substring(0, delimiterIndex);
          String value = line.substring(delimiterIndex + 1);
          infoMap[key] = value;
        }
      }
    }
  } else if (reqResult is RespType3<dynamic>) {
    final bulkString = reqResult.toBulkString().payload;
    if (bulkString != null) {
      List<String> result = bulkString
          .split('\n')
          .where((e) => e.isNotEmpty)
          .toList(growable: false);

      for (String line in result) {
        // 忽略空行和以 # 开头的行(节名)
        if (line.isEmpty || line.startsWith('#')) {
          continue;
        }

        // 分割属性行到键值对
        int delimiterIndex = line.indexOf(':');
        if (delimiterIndex != -1) {
          String key = line.substring(0, delimiterIndex);
          String value = line.substring(delimiterIndex + 1);
          infoMap[key] = value;
        }
      }
    }
  }

  return Info(
    server: ServerInfo.fromMap(infoMap),
    clients: ClientsInfo.fromMap(infoMap),
    memory: MemoryInfo.fromMap(infoMap),
    persistence: PersistenceInfo.fromMap(infoMap),
    stats: StatsInfo.fromMap(infoMap),
    replication: ReplicationInfo.fromMap(infoMap),
    cpu: CPUInfo.fromMap(infoMap),
    cluster: ClusterInfo.fromMap(infoMap),
    keyspace: KeyspaceInfo.fromMap(infoMap),
  );
}