diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b93e58e..76ac4eb 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -33,6 +33,10 @@ jobs: run: dotnet build Ling.Configuration.Database.slnx --configuration Release --no-restore - name: Test run: dotnet test Ling.Configuration.Database.slnx --configuration Release --no-build --verbosity normal + - name: Verify Native AOT publish and execution + run: | + dotnet publish tests/Ling.Configuration.Database.NativeAotSmoke/Ling.Configuration.Database.NativeAotSmoke.csproj --configuration Release --runtime linux-x64 --self-contained true -warnaserror + ./tests/Ling.Configuration.Database.NativeAotSmoke/bin/Release/net11.0/linux-x64/publish/Ling.Configuration.Database.NativeAotSmoke - name: Pack NuGet packages run: | mkdir -p artifacts/packages diff --git a/Directory.Build.props b/Directory.Build.props index 5a903f5..575371e 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -4,7 +4,7 @@ enable enable false - 0.2.0 + 0.3.0 $(VersionPrefix) $(VersionPrefix).0 true diff --git a/Ling.Configuration.Database.slnx b/Ling.Configuration.Database.slnx index ede9344..330763d 100644 --- a/Ling.Configuration.Database.slnx +++ b/Ling.Configuration.Database.slnx @@ -4,6 +4,7 @@ + diff --git a/src/Ling.Configuration.Database/AdoNetDatabaseConfigurationLoader.cs b/src/Ling.Configuration.Database/AdoNetDatabaseConfigurationLoader.cs index 1485ce5..f580575 100644 --- a/src/Ling.Configuration.Database/AdoNetDatabaseConfigurationLoader.cs +++ b/src/Ling.Configuration.Database/AdoNetDatabaseConfigurationLoader.cs @@ -4,7 +4,7 @@ namespace Ling.Configuration.Database; -internal sealed class AdoNetDatabaseConfigurationLoader : IDatabaseConfigurationLoader +internal sealed partial class AdoNetDatabaseConfigurationLoader : IDatabaseConfigurationLoader { private readonly string _connectionString; private readonly DbProviderFactory _providerFactory; @@ -91,7 +91,7 @@ private DbCommand CreateCommand(DbConnection connection) { string QuoteIdentifier(string identifier) { - if (!Regex.IsMatch(identifier, @"^[A-Za-z_][A-Za-z0-9_]*$", RegexOptions.CultureInvariant)) + if (!IdentifierRegex().IsMatch(identifier)) { throw new ArgumentException($"Database identifier '{identifier}' is invalid. Use letters, digits, and underscores, or provide a provider-specific IdentifierQuoter."); } @@ -134,4 +134,7 @@ private ConfigurationEntry ReadEntry(DbDataReader reader) && Convert.ToBoolean(reader.GetValue(2), CultureInfo.InvariantCulture); return new ConfigurationEntry(key, value, isEncrypted); } + + [GeneratedRegex(@"^[A-Za-z_][A-Za-z0-9_]*$", RegexOptions.CultureInvariant)] + private static partial Regex IdentifierRegex(); } diff --git a/src/Ling.Configuration.Database/Ling.Configuration.Database.csproj b/src/Ling.Configuration.Database/Ling.Configuration.Database.csproj index 2164eb4..a5875b1 100644 --- a/src/Ling.Configuration.Database/Ling.Configuration.Database.csproj +++ b/src/Ling.Configuration.Database/Ling.Configuration.Database.csproj @@ -2,6 +2,8 @@ net8.0;net9.0;net10.0;net11.0 true + true + true Ling.Configuration.Database Ling.Configuration.Database Database backed Microsoft.Extensions.Configuration provider with polling reload support. diff --git a/tests/Ling.Configuration.Database.NativeAotSmoke/Ling.Configuration.Database.NativeAotSmoke.csproj b/tests/Ling.Configuration.Database.NativeAotSmoke/Ling.Configuration.Database.NativeAotSmoke.csproj new file mode 100644 index 0000000..3e9deb3 --- /dev/null +++ b/tests/Ling.Configuration.Database.NativeAotSmoke/Ling.Configuration.Database.NativeAotSmoke.csproj @@ -0,0 +1,16 @@ + + + Exe + net11.0 + true + true + true + + + + + + + + + diff --git a/tests/Ling.Configuration.Database.NativeAotSmoke/Program.cs b/tests/Ling.Configuration.Database.NativeAotSmoke/Program.cs new file mode 100644 index 0000000..9b0c305 --- /dev/null +++ b/tests/Ling.Configuration.Database.NativeAotSmoke/Program.cs @@ -0,0 +1,128 @@ +using System.Collections; +using System.Data; +using System.Data.Common; +using System.Diagnostics.CodeAnalysis; +using Ling.Configuration.Database; +using Microsoft.Extensions.Configuration; + +var entries = new List<(string Key, string? Value, bool IsEncrypted)> +{ + ("Feature:Enabled", "true", false), + ("Secrets:Token", "cipher", true), +}; + +var factory = new InMemoryProviderFactory(entries); +var configuration = new ConfigurationBuilder() + .AddDatabase( + "InMemory", + factory, + options => + { + options.PollingInterval = TimeSpan.FromMinutes(5); + options.Decryptor = entry => $"decrypted:{entry.Value}"; + }) + .Build(); + +try +{ + if (configuration["Feature:Enabled"] != "true" + || configuration["Secrets:Token"] != "decrypted:cipher") + { + return 1; + } + + Console.WriteLine("Ling.Configuration.Database Native AOT smoke passed."); + return 0; +} +finally +{ + ((IDisposable)configuration).Dispose(); +} + +file sealed class InMemoryProviderFactory(IReadOnlyList<(string Key, string? Value, bool IsEncrypted)> entries) : DbProviderFactory +{ + public override DbConnection CreateConnection() => new InMemoryConnection(entries); +} + +file sealed class InMemoryConnection(IReadOnlyList<(string Key, string? Value, bool IsEncrypted)> entries) : DbConnection +{ + private ConnectionState _state = ConnectionState.Closed; + + [AllowNull] + public override string ConnectionString { get; set; } = string.Empty; + public override string Database => "InMemory"; + public override string DataSource => "InMemory"; + public override string ServerVersion => "1.0"; + public override ConnectionState State => _state; + public override void ChangeDatabase(string databaseName) { } + public override void Close() => _state = ConnectionState.Closed; + public override void Open() => _state = ConnectionState.Open; + protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLevel) => throw new NotSupportedException(); + protected override DbCommand CreateDbCommand() => new InMemoryCommand(entries); +} + +file sealed class InMemoryCommand(IReadOnlyList<(string Key, string? Value, bool IsEncrypted)> entries) : DbCommand +{ + [AllowNull] + public override string CommandText { get; set; } = string.Empty; + public override int CommandTimeout { get; set; } + public override CommandType CommandType { get; set; } + public override bool DesignTimeVisible { get; set; } + public override UpdateRowSource UpdatedRowSource { get; set; } + protected override DbConnection? DbConnection { get; set; } + protected override DbParameterCollection DbParameterCollection => throw new NotSupportedException(); + protected override DbTransaction? DbTransaction { get; set; } + public override void Cancel() { } + public override int ExecuteNonQuery() => 0; + public override object? ExecuteScalar() => null; + public override void Prepare() { } + protected override DbParameter CreateDbParameter() => throw new NotSupportedException(); + protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior) => new InMemoryDataReader(entries); +} + +file sealed class InMemoryDataReader(IReadOnlyList<(string Key, string? Value, bool IsEncrypted)> entries) : DbDataReader +{ + private int _index = -1; + + public override object this[int ordinal] => GetValue(ordinal); + public override object this[string name] => throw new NotSupportedException(); + public override int Depth => 0; + public override int FieldCount => 3; + public override bool HasRows => entries.Count > 0; + public override bool IsClosed => false; + public override int RecordsAffected => 0; + + public override bool Read() => ++_index < entries.Count; + public override bool IsDBNull(int ordinal) => ordinal == 1 && entries[_index].Value is null; + + public override object GetValue(int ordinal) => ordinal switch + { + 0 => entries[_index].Key, + 1 => entries[_index].Value ?? (object)DBNull.Value, + 2 => entries[_index].IsEncrypted, + _ => throw new ArgumentOutOfRangeException(nameof(ordinal)), + }; + + public override bool GetBoolean(int ordinal) => throw new NotSupportedException(); + public override byte GetByte(int ordinal) => throw new NotSupportedException(); + public override long GetBytes(int ordinal, long dataOffset, byte[]? buffer, int bufferOffset, int length) => throw new NotSupportedException(); + public override char GetChar(int ordinal) => throw new NotSupportedException(); + public override long GetChars(int ordinal, long dataOffset, char[]? buffer, int bufferOffset, int length) => throw new NotSupportedException(); + public override string GetDataTypeName(int ordinal) => string.Empty; + public override DateTime GetDateTime(int ordinal) => throw new NotSupportedException(); + public override decimal GetDecimal(int ordinal) => throw new NotSupportedException(); + public override double GetDouble(int ordinal) => throw new NotSupportedException(); + [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicFields)] + public override Type GetFieldType(int ordinal) => typeof(object); + public override float GetFloat(int ordinal) => throw new NotSupportedException(); + public override Guid GetGuid(int ordinal) => throw new NotSupportedException(); + public override short GetInt16(int ordinal) => throw new NotSupportedException(); + public override int GetInt32(int ordinal) => throw new NotSupportedException(); + public override long GetInt64(int ordinal) => throw new NotSupportedException(); + public override string GetName(int ordinal) => string.Empty; + public override int GetOrdinal(string name) => throw new NotSupportedException(); + public override string GetString(int ordinal) => (string)GetValue(ordinal); + public override int GetValues(object[] values) => throw new NotSupportedException(); + public override bool NextResult() => false; + public override IEnumerator GetEnumerator() => throw new NotSupportedException(); +}