Skip to content

Commit

Permalink
FtDocument Repository
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Oct 20, 2024
1 parent 4d9ac2e commit bfbffb9
Show file tree
Hide file tree
Showing 7 changed files with 1,068 additions and 13 deletions.
6 changes: 6 additions & 0 deletions src/FreeRedis/ConnectionStringBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public string Host
public int Retry { get; set; } = 0;
public bool ExitAutoDisposePool { get; set; } = true;
public bool SubscribeReadbytes { get; set; } = false;
public int FtDialect { get; set; } = 0;
public string FtLanguage { get; set; }

public RemoteCertificateValidationCallback CertificateValidation;
public LocalCertificateSelectionCallback CertificateSelection;
Expand Down Expand Up @@ -64,6 +66,8 @@ public override string ToString()
if (Retry != 0) sb.Append(",retry=").Append(Retry);
if (ExitAutoDisposePool != true) sb.Append(",exitAutoDisposePool=false");
if (SubscribeReadbytes != false) sb.Append(",subscribeReadbytes=true");
if (FtDialect != 0) sb.Append(",ftdialect=").Append(FtDialect);
if (!string.IsNullOrWhiteSpace(FtLanguage)) sb.Append(",ftlanguage=").Append(FtLanguage);
return sb.ToString();
}

Expand Down Expand Up @@ -106,6 +110,8 @@ public static ConnectionStringBuilder Parse(string connectionString)
case "exitautodisposepool": if (kv.Length > 1 && new[] { "false", "0" }.Contains(kv[1].Trim())) ret.ExitAutoDisposePool = false; break;
case "subscriblereadbytes": //history error
case "subscribereadbytes": if (kv.Length > 1 && kv[1].ToLower().Trim() == "true") ret.SubscribeReadbytes = true; break;
case "ftdialect": if (kv.Length > 1 && int.TryParse(kv[1].Trim(), out var dialect) && dialect > 0) ret.FtDialect = dialect; break;
case "ftlanguage": if (kv.Length > 1) ret.FtLanguage = kv[1].Trim(); break;
}
}
return ret;
Expand Down
31 changes: 23 additions & 8 deletions src/FreeRedis/Internal/RespHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -580,13 +580,13 @@ public static void SetPropertyOrFieldValue(this Type entityType, object entity,
[typeof(decimal)] = 3,
[typeof(decimal?)] = 3
});
static bool IsIntegerType(this Type that) => that == null ? false : (_dicIsNumberType.Value.TryGetValue(that, out var tryval) ? tryval == 1 : false);
static bool IsNumberType(this Type that) => that == null ? false : _dicIsNumberType.Value.ContainsKey(that);
static bool IsNullableType(this Type that) => that.IsArray == false && that?.FullName.StartsWith("System.Nullable`1[") == true;
static bool IsAnonymousType(this Type that) => that?.FullName.StartsWith("<>f__AnonymousType") == true;
static bool IsArrayOrList(this Type that) => that == null ? false : (that.IsArray || typeof(IList).IsAssignableFrom(that));
static Type NullableTypeOrThis(this Type that) => that?.IsNullableType() == true ? that.GetGenericArguments().First() : that;
static string DisplayCsharp(this Type type, bool isNameSpace = true)
internal static bool IsIntegerType(this Type that) => that == null ? false : (_dicIsNumberType.Value.TryGetValue(that, out var tryval) ? tryval == 1 : false);
internal static bool IsNumberType(this Type that) => that == null ? false : _dicIsNumberType.Value.ContainsKey(that);
internal static bool IsNullableType(this Type that) => that.IsArray == false && that?.FullName.StartsWith("System.Nullable`1[") == true;
internal static bool IsAnonymousType(this Type that) => that?.FullName.StartsWith("<>f__AnonymousType") == true;
internal static bool IsArrayOrList(this Type that) => that == null ? false : (that.IsArray || typeof(IList).IsAssignableFrom(that));
internal static Type NullableTypeOrThis(this Type that) => that?.IsNullableType() == true ? that.GetGenericArguments().First() : that;
internal static string DisplayCsharp(this Type type, bool isNameSpace = true)
{
if (type == null) return null;
if (type == typeof(void)) return "void";
Expand Down Expand Up @@ -743,7 +743,7 @@ public static Type GetPropertyOrFieldType(this MemberInfo that)
}
#endregion

#region 类型转换
#region 类型转换
internal static string ToInvariantCultureToString(this object obj) => obj is string objstr ? objstr : string.Format(CultureInfo.InvariantCulture, @"{0}", obj);
public static void MapSetListValue(this object[] list, Dictionary<string, Func<object[], object>> valueHandlers)
{
Expand All @@ -758,6 +758,21 @@ public static void MapSetListValue(this object[] list, Dictionary<string, Func<o
}
}
}
public static T MapToClass<T>(this Dictionary<string, object> dict)
{
if (dict == null) return default(T);
var ttype = typeof(T);
var ret = (T)ttype.CreateInstanceGetDefaultValue();
foreach(var kv in dict)
{
var name = kv.Key.Replace("-", "_");
var prop = ttype.GetPropertyOrFieldIgnoreCase(name);
if (prop == null) continue; // throw new ArgumentException($"{typeof(T).DisplayCsharp()} undefined Property {list[a]}");
if (kv.Value == null) continue;
ttype.SetPropertyOrFieldValue(ret, prop.Name, prop.GetPropertyOrFieldType().FromObject(kv.Value));
}
return ret;
}
public static T MapToClass<T>(this object[] list, Encoding encoding)
{
if (list == null) return default(T);
Expand Down
40 changes: 40 additions & 0 deletions src/FreeRedis/RedisClient/Modules/RediSearch.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using FreeRedis.RediSearch;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace FreeRedis
{
partial class RedisClient
{
public FtDocumentRepository<T> FtDocumentRepository<T>() => new FtDocumentRepository<T>(this);

public string[] Ft_List() => Call("FT._LIST", rt => rt.ThrowOrValue<string[]>());
public AggregateBuilder FtAggregate(string index, string query) => new AggregateBuilder(this, index, query);

Expand Down Expand Up @@ -91,5 +94,42 @@ public static object ThrowOrValueToFtCursorRead(this RedisResult rt) =>
{
return a;
});

public static bool IsParameter(this Expression exp)
{
var test = new TestParameterExpressionVisitor();
test.Visit(exp);
return test.Result;
}
}

class ReplaceVisitor : ExpressionVisitor
{
private Expression _oldexp;
private Expression _newexp;
public Expression Modify(Expression find, Expression oldexp, Expression newexp)
{
this._oldexp = oldexp;
this._newexp = newexp;
return Visit(find);
}
protected override Expression VisitMember(MemberExpression node)
{
if (node.Expression == _oldexp)
return Expression.Property(_newexp, node.Member.Name);
if (node == _oldexp)
return _newexp;
return base.VisitMember(node);
}
}
class TestParameterExpressionVisitor : ExpressionVisitor
{
public bool Result { get; private set; }

protected override Expression VisitParameter(ParameterExpression node)
{
if (!Result) Result = true;
return node;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public TBuilder AddTagField(string name, TagFieldOptions options)
_schemaArgs.Add("TAG");
if (options.NoIndex) _schemaArgs.Add("NOINDEX");
if (options.WithSuffixTrie) _schemaArgs.Add("WITHSUFFIXTRIE");
if (options.Separator != ",")
if (!string.IsNullOrWhiteSpace(options.Separator) && options.Separator != ",")
{

_schemaArgs.Add("SEPARATOR");
Expand Down
Loading

0 comments on commit bfbffb9

Please sign in to comment.