Exemplo n.º 1
0
        public static RedisResult <RoleResult> NewValueToRole(this RedisResult <object> rt) =>
        rt.NewValue(a =>
        {
            var objs = a as List <object>;
            if (objs.Any())
            {
                var role = new RoleResult {
                    role = objs[0].ConvertTo <RoleType>()
                };
                switch (role.role)
                {
                case RoleType.Master:
                    role.data = new RoleResult.MasterInfo
                    {
                        _replication_offset = objs[1].ConvertTo <long>(),
                        _slaves             = (objs[2] as List <object>)?.Select(x =>
                        {
                            var xs = x as List <object>;
                            return(new RoleResult.MasterInfo.SlaveInfo
                            {
                                ip = xs[0].ConvertTo <string>(),
                                port = xs[1].ConvertTo <int>(),
                                slave_offset = xs[2].ConvertTo <long>()
                            });
                        }).ToArray()
                    };
                    break;

                case RoleType.Slave:
                    role.data = new RoleResult.SlaveInfo
                    {
                        master_ip         = objs[1].ConvertTo <string>(),
                        master_port       = objs[2].ConvertTo <int>(),
                        replication_state = objs[3].ConvertTo <string>(),
                        data_received     = objs[4].ConvertTo <long>()
                    };
                    break;

                case RoleType.Sentinel:
                    role.data = objs[1].ConvertTo <string[]>();
                    break;
                }
                return(role);
            }
            return(null);
        });