Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change InfoResult::Attributes Returned Value to Array of Arrays #295

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
6 changes: 6 additions & 0 deletions src/NRedisStack/ResponseParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ public static RedisResult[] ToArray(this RedisResult result)
throw new ArgumentNullException(nameof(redisResults));
}

public static RedisResult[][] ToArrayArray(this RedisResult result)
{
var redisResults = (RedisResult[])result!;
return redisResults.Select(x => (RedisResult[])x!).ToArray();
}

public static long ToLong(this RedisResult result)
{
if ((long?)result == null)
Expand Down
39 changes: 21 additions & 18 deletions src/NRedisStack/Search/DataTypes/InfoResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class InfoResult
private readonly Dictionary<string, RedisResult> _all = new();
public string IndexName => GetString("index_name")!;
public Dictionary<string, RedisResult> IndexOption => GetRedisResultDictionary("index_options")!;
public Dictionary<string, RedisResult>[] Attributes => GetRedisResultDictionaryArray("attributes")!;
public RedisResult[][] Attributes => GetRedisResultArrayArray("attributes")!;
public long NumDocs => GetLong("num_docs");
public string MaxDocId => GetString("max_doc_id")!;
public long NumTerms => GetLong("num_terms");
Expand Down Expand Up @@ -91,26 +91,29 @@ private double GetDouble(string key)
}

return result;

}

private Dictionary<string, RedisResult>[]? GetRedisResultDictionaryArray(string key)
private RedisResult[][]? GetRedisResultArrayArray(string key)
{
if (!_all.TryGetValue(key, out var value)) return default;
var values = (RedisResult[])value!;
var result = new Dictionary<string, RedisResult>[values.Length];
for (int i = 0; i < values.Length; i++)
{
var fv = (RedisResult[])values[i]!;
var dict = new Dictionary<string, RedisResult>();
for (int j = 0; j < fv.Length; j += 2)
{
dict.Add((string)fv[j]!, fv[j + 1]);
}

result[i] = dict;
}

return result;
return value.ToArrayArray();
}

// private Dictionary<string, RedisResult>[]? GetRedisResultDictionaryArray(string key)
shacharPash marked this conversation as resolved.
Show resolved Hide resolved
// {
// if (!_all.TryGetValue(key, out var value)) return default;
// var values = (RedisResult[])value!;
// var result = new Dictionary<string, RedisResult>[values.Length];
// for (int i = 0; i < values.Length; i++)
// {
// var fv = (RedisResult[])values[i]!;
// var dict = new Dictionary<string, RedisResult>();
// for (int j = 0; j < fv.Length; j += 2)
// {
// dict.Add((string)fv[j]!, fv[j + 1]);
// }
// result[i] = dict;
// }
// return result;
// }
}
16 changes: 8 additions & 8 deletions tests/NRedisStack.Tests/Search/SearchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ public void AlterAdd()
IDatabase db = redisFixture.Redis.GetDatabase();
db.Execute("FLUSHALL");
var ft = db.FT();
Schema sc = new Schema().AddTextField("title", 1.0);
Schema sc = new Schema().AddTextField("title", 1.0, sortable: true);
shacharPash marked this conversation as resolved.
Show resolved Hide resolved

Assert.True(ft.Create(index, FTCreateParams.CreateParams(), sc));

Expand Down Expand Up @@ -733,9 +733,9 @@ public void AlterAdd()
Assert.Equal(index, info.IndexName);
Assert.Empty(info.IndexOption);
// Assert.Equal(,info.IndexDefinition);
Assert.Equal("title", (info.Attributes[0]["identifier"]).ToString());
Assert.Equal("TAG", (info.Attributes[1]["type"]).ToString());
Assert.Equal("name", (info.Attributes[2]["attribute"]).ToString());
Assert.Equal("title", info.Attributes[0][1].ToString());
Assert.Equal("TAG", info.Attributes[1][5].ToString());
Assert.Equal("name", info.Attributes[2][3].ToString());
Assert.Equal(100, info.NumDocs);
Assert.NotNull(info.MaxDocId);
Assert.Equal(102, info.NumTerms);
Expand Down Expand Up @@ -766,7 +766,7 @@ public async Task AlterAddAsync()
IDatabase db = redisFixture.Redis.GetDatabase();
db.Execute("FLUSHALL");
var ft = db.FT();
Schema sc = new Schema().AddTextField("title", 1.0);
Schema sc = new Schema().AddTextField("title", 1.0, sortable: true);

Assert.True(ft.Create(index, FTCreateParams.CreateParams(), sc));

Expand Down Expand Up @@ -795,9 +795,9 @@ public async Task AlterAddAsync()

var info = await ft.InfoAsync(index);
Assert.Equal(index, info.IndexName);
Assert.Equal("title", (info.Attributes[0]["identifier"]).ToString());
Assert.Equal("TAG", (info.Attributes[1]["type"]).ToString());
Assert.Equal("name", (info.Attributes[2]["attribute"]).ToString());
Assert.Equal("title", info.Attributes[0][1].ToString());
Assert.Equal("TAG", info.Attributes[1][5].ToString());
Assert.Equal("name", info.Attributes[2][3].ToString());
Assert.Equal(100, info.NumDocs);
Assert.Equal("300", info.MaxDocId);
Assert.Equal(102, info.NumTerms);
Expand Down