Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/cSpell.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"revisioned",
"Revisioned",
"Vogen",
"upserts"
"upserts",
"Citus"
],
"ignoreWords": [
"JSONB",
Expand Down
4 changes: 2 additions & 2 deletions src/Marten/MartenRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public DocumentMappingExpression<T> DocumentAlias(string alias)
/// </param>
/// <param name="dbType">Optional, overrides the Npgsql DbType for any parameter usage of this property</param>
/// <returns></returns>
public DocumentMappingExpression<T> Duplicate(Expression<Func<T, object>> expression, string? pgType = null,
public DocumentMappingExpression<T> Duplicate(Expression<Func<T, object?>> expression, string? pgType = null,
NpgsqlDbType? dbType = null, Action<DocumentIndex>? configure = null, bool notNull = false)
{
_builder.Alter = mapping =>
Expand All @@ -201,7 +201,7 @@ public DocumentMappingExpression<T> Duplicate(Expression<Func<T, object>> expres
/// <param name="expression"></param>
/// <param name="configure"></param>
/// <returns></returns>
public DocumentMappingExpression<T> Index(Expression<Func<T, object>> expression,
public DocumentMappingExpression<T> Index(Expression<Func<T, object?>> expression,
Action<ComputedIndex>? configure = null)
{
_builder.Alter = m => m.Index(expression, configure);
Expand Down
43 changes: 23 additions & 20 deletions src/Marten/Schema/DocumentMapping.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
Expand Down Expand Up @@ -77,9 +78,9 @@ public interface IDocumentType
Type TypeFor(string alias);
}

public class DocumentMapping: IDocumentMapping, IDocumentType
public partial class DocumentMapping: IDocumentMapping, IDocumentType
{
internal static bool IsValidIdentityType(Type identityType)
internal static bool IsValidIdentityType([NotNullWhen(true)]Type? identityType)
{
if (identityType == null)
return false;
Expand All @@ -95,9 +96,11 @@ internal static bool IsValidIdentityType(Type identityType)
out var fSharpDiscriminatedUnionIdGeneration);
}

private static readonly Regex _aliasSanitizer = new("<|>", RegexOptions.Compiled);
internal static readonly Type[] ValidIdTypes = { typeof(int), typeof(Guid), typeof(long), typeof(string) };
private readonly List<DuplicatedField> _duplicates = new();
[GeneratedRegex("<|>")]
private static partial Regex AliasSanitizer();

internal static readonly Type[] ValidIdTypes = [typeof(int), typeof(Guid), typeof(long), typeof(string)];
private readonly List<DuplicatedField> _duplicates = [];
private readonly Lazy<DocumentSchema> _schema;

private string _alias;
Expand Down Expand Up @@ -662,13 +665,13 @@ private static string defaultDocumentAliasName(Type documentType)
var nameToAlias = documentType.Name;
if (documentType.GetTypeInfo().IsGenericType)
{
nameToAlias = _aliasSanitizer.Replace(documentType.GetPrettyName(), string.Empty).Replace(",", "_");
nameToAlias = AliasSanitizer().Replace(documentType.GetPrettyName(), string.Empty).Replace(",", "_");
}

var parts = new List<string> { nameToAlias.ToLower() };
if (documentType.IsNested)
{
parts.Insert(0, documentType.DeclaringType.Name.ToLower());
parts.Insert(0, documentType.DeclaringType!.Name.ToLower());
}

return string.Join("_", parts);
Expand Down Expand Up @@ -704,7 +707,7 @@ public DuplicatedField DuplicateField(MemberInfo[] members, string? pgType = nul
bool notNull = false)
{
var member = QueryMembers.FindMember(members[0]);
var parent = (IHasChildrenMembers)QueryMembers;
IHasChildrenMembers parent = QueryMembers;
for (var i = 1; i < members.Length; i++)
{
parent = member.As<IHasChildrenMembers>();
Expand Down Expand Up @@ -827,7 +830,7 @@ public class DocumentMapping<T>: DocumentMapping
public DocumentMapping(StoreOptions storeOptions) : base(typeof(T), storeOptions)
{
var configure = typeof(T).GetMethod("ConfigureMarten", BindingFlags.Static | BindingFlags.Public);
configure?.Invoke(null, new object[] { this });
configure?.Invoke(null, [this]);
}

/// <summary>
Expand All @@ -841,7 +844,7 @@ public DocumentMapping(StoreOptions storeOptions) : base(typeof(T), storeOptions
/// field
/// </param>
/// <returns></returns>
public void Duplicate(Expression<Func<T, object>> expression, string? pgType = null, NpgsqlDbType? dbType = null,
public void Duplicate(Expression<Func<T, object?>> expression, string? pgType = null, NpgsqlDbType? dbType = null,
Action<DocumentIndex>? configure = null, bool notNull = false)
{
var visitor = new FindMembers();
Expand All @@ -864,7 +867,7 @@ public void Duplicate(Expression<Func<T, object>> expression, string? pgType = n
/// </summary>
/// <param name="expression"></param>
/// <param name="configure"></param>
public void Index(Expression<Func<T, object>> expression, Action<ComputedIndex>? configure = null)
public void Index(Expression<Func<T, object?>> expression, Action<ComputedIndex>? configure = null)
{
if (expression.Body is NewExpression newExpression)
{
Expand All @@ -878,15 +881,15 @@ public void Index(Expression<Func<T, object>> expression, Action<ComputedIndex>?
}


Index(new[] { expression }, configure);
Index([expression], configure);
}

/// <summary>
/// Adds a computed index
/// </summary>
/// <param name="expressions"></param>
/// <param name="configure"></param>
public void Index(IReadOnlyCollection<Expression<Func<T, object>>> expressions,
public void Index(IReadOnlyCollection<Expression<Func<T, object?>>> expressions,
Action<ComputedIndex>? configure = null)
{
var members = expressions
Expand All @@ -897,14 +900,14 @@ public void Index(IReadOnlyCollection<Expression<Func<T, object>>> expressions,
Indexes.Add(index);
}

public void UniqueIndex(UniqueIndexType indexType, string indexName,
params Expression<Func<T, object>>[] expressions)
public void UniqueIndex(UniqueIndexType indexType, string? indexName,
params Expression<Func<T, object?>>[] expressions)
{
UniqueIndex(indexType, indexName, TenancyScope.Global, expressions);
}

public void UniqueIndex(UniqueIndexType indexType, string indexName,
TenancyScope tenancyScope = TenancyScope.Global, params Expression<Func<T, object>>[] expressions)
public void UniqueIndex(UniqueIndexType indexType, string? indexName,
TenancyScope tenancyScope = TenancyScope.Global, params Expression<Func<T, object?>>[] expressions)
{
var members = expressions
.Select(e =>
Expand Down Expand Up @@ -937,7 +940,7 @@ public void UniqueIndex(UniqueIndexType indexType, string indexName,
/// <remarks>
/// See: https://www.postgresql.org/docs/10/static/textsearch-controls.html#TEXTSEARCH-PARSING-DOCUMENTS
/// </remarks>
public FullTextIndexDefinition FullTextIndex(string regConfig, params Expression<Func<T, object>>[] expressions)
public FullTextIndexDefinition FullTextIndex(string regConfig, params Expression<Func<T, object?>>[] expressions)
{
return AddFullTextIndex(
expressions
Expand All @@ -950,7 +953,7 @@ public FullTextIndexDefinition FullTextIndex(string regConfig, params Expression
/// Adds an ngram index.
/// </summary>
/// <param name="expression">Document field that should be use by ngram index</param>
public NgramIndex NgramIndex(Expression<Func<T, object>> expression)
public NgramIndex NgramIndex(Expression<Func<T, object?>> expression)
{
var visitor = new FindMembers();
visitor.Visit(expression);
Expand All @@ -962,7 +965,7 @@ public NgramIndex NgramIndex(Expression<Func<T, object>> expression)
/// Adds a full text index with default region config set to 'english'
/// </summary>
/// <param name="expressions">Document fields that should be use by full text index</param>
public NgramIndex NgramIndex(Action<NgramIndex> configure, Expression<Func<T, object>> expression)
public NgramIndex NgramIndex(Action<NgramIndex> configure, Expression<Func<T, object?>> expression)
{
var index = NgramIndex(expression);
configure(index);
Expand Down
Loading