From 8bafdb0003f17e6522deff552e55b6a921ac48a4 Mon Sep 17 00:00:00 2001 From: JT Date: Wed, 23 Jul 2025 20:01:03 +0800 Subject: [PATCH] Fix NRT annotations in MartenRegistry + mild cleanup --- docs/cSpell.json | 3 +- src/Marten/MartenRegistry.cs | 4 +-- src/Marten/Schema/DocumentMapping.cs | 43 +++++++++++++++------------- 3 files changed, 27 insertions(+), 23 deletions(-) diff --git a/docs/cSpell.json b/docs/cSpell.json index 66c75b85ab..5a097a360a 100644 --- a/docs/cSpell.json +++ b/docs/cSpell.json @@ -41,7 +41,8 @@ "revisioned", "Revisioned", "Vogen", - "upserts" + "upserts", + "Citus" ], "ignoreWords": [ "JSONB", diff --git a/src/Marten/MartenRegistry.cs b/src/Marten/MartenRegistry.cs index 4fa035e0f5..714bc9bfd9 100644 --- a/src/Marten/MartenRegistry.cs +++ b/src/Marten/MartenRegistry.cs @@ -185,7 +185,7 @@ public DocumentMappingExpression DocumentAlias(string alias) /// /// Optional, overrides the Npgsql DbType for any parameter usage of this property /// - public DocumentMappingExpression Duplicate(Expression> expression, string? pgType = null, + public DocumentMappingExpression Duplicate(Expression> expression, string? pgType = null, NpgsqlDbType? dbType = null, Action? configure = null, bool notNull = false) { _builder.Alter = mapping => @@ -201,7 +201,7 @@ public DocumentMappingExpression Duplicate(Expression> expres /// /// /// - public DocumentMappingExpression Index(Expression> expression, + public DocumentMappingExpression Index(Expression> expression, Action? configure = null) { _builder.Alter = m => m.Index(expression, configure); diff --git a/src/Marten/Schema/DocumentMapping.cs b/src/Marten/Schema/DocumentMapping.cs index 325a34551d..90dcb48bee 100644 --- a/src/Marten/Schema/DocumentMapping.cs +++ b/src/Marten/Schema/DocumentMapping.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Linq.Expressions; using System.Reflection; @@ -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; @@ -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 _duplicates = new(); + [GeneratedRegex("<|>")] + private static partial Regex AliasSanitizer(); + + internal static readonly Type[] ValidIdTypes = [typeof(int), typeof(Guid), typeof(long), typeof(string)]; + private readonly List _duplicates = []; private readonly Lazy _schema; private string _alias; @@ -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 { nameToAlias.ToLower() }; if (documentType.IsNested) { - parts.Insert(0, documentType.DeclaringType.Name.ToLower()); + parts.Insert(0, documentType.DeclaringType!.Name.ToLower()); } return string.Join("_", parts); @@ -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(); @@ -827,7 +830,7 @@ public class DocumentMapping: 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]); } /// @@ -841,7 +844,7 @@ public DocumentMapping(StoreOptions storeOptions) : base(typeof(T), storeOptions /// field /// /// - public void Duplicate(Expression> expression, string? pgType = null, NpgsqlDbType? dbType = null, + public void Duplicate(Expression> expression, string? pgType = null, NpgsqlDbType? dbType = null, Action? configure = null, bool notNull = false) { var visitor = new FindMembers(); @@ -864,7 +867,7 @@ public void Duplicate(Expression> expression, string? pgType = n /// /// /// - public void Index(Expression> expression, Action? configure = null) + public void Index(Expression> expression, Action? configure = null) { if (expression.Body is NewExpression newExpression) { @@ -878,7 +881,7 @@ public void Index(Expression> expression, Action? } - Index(new[] { expression }, configure); + Index([expression], configure); } /// @@ -886,7 +889,7 @@ public void Index(Expression> expression, Action? /// /// /// - public void Index(IReadOnlyCollection>> expressions, + public void Index(IReadOnlyCollection>> expressions, Action? configure = null) { var members = expressions @@ -897,14 +900,14 @@ public void Index(IReadOnlyCollection>> expressions, Indexes.Add(index); } - public void UniqueIndex(UniqueIndexType indexType, string indexName, - params Expression>[] expressions) + public void UniqueIndex(UniqueIndexType indexType, string? indexName, + params Expression>[] expressions) { UniqueIndex(indexType, indexName, TenancyScope.Global, expressions); } - public void UniqueIndex(UniqueIndexType indexType, string indexName, - TenancyScope tenancyScope = TenancyScope.Global, params Expression>[] expressions) + public void UniqueIndex(UniqueIndexType indexType, string? indexName, + TenancyScope tenancyScope = TenancyScope.Global, params Expression>[] expressions) { var members = expressions .Select(e => @@ -937,7 +940,7 @@ public void UniqueIndex(UniqueIndexType indexType, string indexName, /// /// See: https://www.postgresql.org/docs/10/static/textsearch-controls.html#TEXTSEARCH-PARSING-DOCUMENTS /// - public FullTextIndexDefinition FullTextIndex(string regConfig, params Expression>[] expressions) + public FullTextIndexDefinition FullTextIndex(string regConfig, params Expression>[] expressions) { return AddFullTextIndex( expressions @@ -950,7 +953,7 @@ public FullTextIndexDefinition FullTextIndex(string regConfig, params Expression /// Adds an ngram index. /// /// Document field that should be use by ngram index - public NgramIndex NgramIndex(Expression> expression) + public NgramIndex NgramIndex(Expression> expression) { var visitor = new FindMembers(); visitor.Visit(expression); @@ -962,7 +965,7 @@ public NgramIndex NgramIndex(Expression> expression) /// Adds a full text index with default region config set to 'english' /// /// Document fields that should be use by full text index - public NgramIndex NgramIndex(Action configure, Expression> expression) + public NgramIndex NgramIndex(Action configure, Expression> expression) { var index = NgramIndex(expression); configure(index);