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

Add Format and apply rules #544

Merged
merged 14 commits into from
May 3, 2024
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ csharp_new_line_before_members_in_anonymous_types = true
# Sort using and Import directives with System.* appearing first
dotnet_sort_system_directives_first = false

## Primary constructors
dotnet_analyzer_diagnostic.category-Style.severity = none

# Avoid "this." if not necessary
dotnet_style_qualification_for_field = false : suggestion
dotnet_style_qualification_for_property = false : suggestion
Expand Down
2 changes: 1 addition & 1 deletion samples/YesSql.Samples.Hi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace YesSql.Samples.Hi
{
internal class Program
internal sealed class Program
{
static void Main(string[] args)
{
Expand Down
77 changes: 77 additions & 0 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,81 @@
</PackageReference>
</ItemGroup>

<PropertyGroup Label="Analysis rules">

<AnalysisLevel>latest-Recommended</AnalysisLevel>

<!-- Member is explicitly initialized to its default value -->
<NoWarn>$(NoWarn);CA1805</NoWarn>

<!-- The behavior could vary based on the current user's locale setting -->
<NoWarn>$(NoWarn);CA1304;CA1305;CA1310</NoWarn>

<!-- Specify a culture or use an invariant version to avoid implicit dependency on current culture -->
<NoWarn>$(NoWarn);CA1311</NoWarn>

<!-- Do not declare static members on generic types -->
<NoWarn>$(NoWarn);CA1000</NoWarn>

<!-- For improved performance, use the LoggerMessage delegates -->
<NoWarn>$(NoWarn);CA1848</NoWarn>

<!-- Identifier contains type name -->
<NoWarn>$(NoWarn);CA1720</NoWarn>

<!-- Do not declare visible instance fields -->
<NoWarn>$(NoWarn);CA1051</NoWarn>

<!-- Avoid using cref tags with a prefix -->
<NoWarn>$(NoWarn);CA1200</NoWarn>

<!-- Rename type name X so that it does not end in 'Delegate', 'EventHandler', 'Permission' etc -->
<NoWarn>$(NoWarn);CA1711</NoWarn>

<!-- Parameter name differs from original overriden implemented name -->
<NoWarn>$(NoWarn);CA1725</NoWarn>

<!-- Reserved keyword -->
<NoWarn>$(NoWarn);CA1716</NoWarn>

<!-- Type owns disposable field(s) -->
<NoWarn>$(NoWarn);CA1001</NoWarn>

<!-- Exception type is not sufficiently specific -->
<NoWarn>$(NoWarn);CA2201</NoWarn>

<!-- Remove the underscores from member name -->
<NoWarn>$(NoWarn);CA1707</NoWarn>

<!-- Use PascalCase for named placeholders in the logging message template -->
<NoWarn>$(NoWarn);CA1727</NoWarn>

<!-- CA1861: Prefer 'static readonly' fields over constant array arguments if the called method is called repeatedly and is not mutating the passed array -->
<NoWarn>$(NoWarn);CA1861</NoWarn>

<!-- NU1603: 'PackageA' 4.0.0 depends on 'PackageB' (>= 4.0.0) but 4.0.0 was not found. An approximate best match of 5.0.0 was resolved. -->
<NoWarn>$(NoWarn);NU1603</NoWarn>

<NoWarn>$(NoWarn);612</NoWarn>

<NoWarn>$(NoWarn);618</NoWarn>

<NoWarn>$(NoWarn);CA2211</NoWarn>

<!-- CA1016: Mark assemblies with AssemblyVersionAttribute -->
<NoWarn>$(NoWarn);CA1016</NoWarn>

<!-- CA1016: Mark assemblies with AssemblyVersionAttribute -->
<NoWarn>$(NoWarn);CA2254</NoWarn>

<NoWarn>$(NoWarn);CA2208</NoWarn>

<!-- SA0001: All diagnostics of XML documentation comments has been disabled due to the current project configuration. -->
<NoWarn>$(NoWarn),1573,1591,1712</NoWarn>

<!-- NETSDK1206: Found version-specific or distribution-specific runtime identifier(s). -->
<NoWarn>$(NoWarn);NETSDK1206</NoWarn>

</PropertyGroup>

</Project>
8 changes: 4 additions & 4 deletions src/YesSql.Abstractions/ISqlDialect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public interface ISqlDialect

void ResetTypeHandlers();

void AddTypeHandler<T, U>(Func<T, U> handler);
void AddTypeHandler<T, TU>(Func<T, TU> handler);

/// <summary>
/// Gets the name of the dialect.
Expand All @@ -38,7 +38,7 @@ public interface ISqlDialect
/// Gets the cascade constraint sql statement.
/// </summary>
string CascadeConstraintsString { get; }

/// <summary>
/// Gets the create table sql statement.
/// </summary>
Expand All @@ -64,7 +64,7 @@ public interface ISqlDialect
/// Whether the underlying database support batching.
/// </summary>
bool SupportsBatching { get; }

/// <summary>
/// Whether the dialect support unique queries.
/// </summary>
Expand Down Expand Up @@ -94,7 +94,7 @@ public interface ISqlDialect
/// Gets the Int64 primary key with identity column SQL statement.
/// </summary>
string LegacyIdentityColumnString { get; }

/// <summary>
/// Gets the identity select SQL statement to append to an insert in order to return the last generated identifier.
/// </summary>
Expand Down
16 changes: 4 additions & 12 deletions src/YesSql.Abstractions/Indexes/DescribeFor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,11 @@ public IGroupFor<TIndex> Map(Func<T, Task<TIndex>> map)

public IReduceFor<TIndex, TKeyG> Group<TKeyG>(Expression<Func<TIndex, TKeyG>> group)
{
var memberExpression = group.Body as MemberExpression;
var memberExpression = group.Body as MemberExpression
?? throw new ArgumentException("Group expression is not a valid member of: " + typeof(TIndex).Name);

if (memberExpression == null)
{
throw new ArgumentException("Group expression is not a valid member of: " + typeof(TIndex).Name);
}

var property = memberExpression.Member as PropertyInfo;

if (property == null)
{
throw new ArgumentException("Group expression is not a valid property of: " + typeof(TIndex).Name);
}
var property = memberExpression.Member as PropertyInfo
?? throw new ArgumentException("Group expression is not a valid property of: " + typeof(TIndex).Name);

GroupProperty = property;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ namespace YesSql
/// <summary>
/// Use this attribute to provide a custom string representation of a type.
/// </summary>
public class SimplifiedTypeName : Attribute
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Enum | AttributeTargets.Interface | AttributeTargets.Delegate)]
public class SimplifiedTypeNameAttribute : Attribute
{
public string Name { get; set; }

public SimplifiedTypeName(string name)
public SimplifiedTypeNameAttribute(string name)
{
Name = name;
}
Expand Down
2 changes: 1 addition & 1 deletion src/YesSql.Core/Commands/BatchCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class BatchCommand : IIndexCommand
public static int DefaultBuilderCapacity = 10 * 1024;

public List<string> Queries { get; set; } = new List<string>();
public DbCommand Command { get; set; }
public DbCommand Command { get; set; }
public List<Action<DbDataReader>> Actions = new();
public int ExecutionOrder => 0;

Expand Down
8 changes: 6 additions & 2 deletions src/YesSql.Core/Commands/IndexCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ protected string Inserts(Type type, ISqlDialect dialect)
for (var i = 0; i < allProperties.Length; i++)
{
var property = allProperties.ElementAt(i);
sbParameterList.Append("@").Append(property.Name).Append(ParameterSuffix);

sbParameterList.Append('@')
.Append(property.Name)
.Append(ParameterSuffix);

if (i < allProperties.Length - 1)
{
sbParameterList.Append(", ");
Expand Down Expand Up @@ -186,6 +190,6 @@ private static bool IsWriteable(PropertyInfo pi)

public abstract bool AddToBatch(ISqlDialect dialect, List<string> queries, DbCommand batchCommand, List<Action<DbDataReader>> actions, int index);

private record CompoundKey(string Dialect, string Type, string Schema, string Prefix, string Collection);
private sealed record CompoundKey(string Dialect, string Type, string Schema, string Prefix, string Collection);
}
}
30 changes: 14 additions & 16 deletions src/YesSql.Core/Data/NullableThumbprintFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ namespace YesSql.Data
/// If nullable arguments (including strings) are used, the SQL should vary.
/// This class allows to generate a Thumbprint for each set of nullable compiled query properties.
/// </summary>
internal class NullableThumbprintFactory
internal sealed class NullableThumbprintFactory
{
private static Dictionary<Type, NullableThumbprintBuilder> _discriminatorFactories = new();
private static readonly Dictionary<Type, NullableThumbprintBuilder> _discriminatorFactories = new();

internal static NullableThumbprintBuilder GetNullableThumbprintBuilder(Type type)
{
Expand All @@ -32,29 +32,27 @@ internal static NullableThumbprintBuilder GetNullableThumbprintBuilder(Type type

public static long GetNullableThumbprint(object item)
{
if (item == null)
{
throw new ArgumentNullException(nameof(item));
}
ArgumentNullException.ThrowIfNull(item);

var builder = GetNullableThumbprintBuilder(item.GetType());

return builder.GetNullableThumbprint(item);
}
}

internal class NullableThumbprintBuilder
internal sealed class NullableThumbprintBuilder
{
private Type _type;
private readonly Type _type;
private static int _globalTypeIndex;
private long _typeIndex;
private readonly long _typeIndex;
private const int MaxTypeIndex = 1 << 16; // 65536 types max, 16 bits for the type
private const int MaxProperties = 48;

private List<INullablePropertyAccessor> _nullableAccessors;
private readonly List<INullablePropertyAccessor> _nullableAccessors;


public NullableThumbprintBuilder(Type type)
{
{
_type = type;

// Each type gets a unique type index
Expand Down Expand Up @@ -96,7 +94,7 @@ private interface INullablePropertyAccessor
bool IsPropertyNull(object obj);
}

private class NullableAccessor<T, TU> : INullablePropertyAccessor where T : class
private sealed class NullableAccessor<T, TU> : INullablePropertyAccessor where T : class
{
private readonly Func<T, TU> _getter;

Expand All @@ -111,6 +109,8 @@ bool INullablePropertyAccessor.IsPropertyNull(object obj)
}
}

private const long _long1 = 1;

/// <summary>
/// Returns an 64 bits integer representing the unique set of nullable fields as a bit mask. The 16 MSB represent the type, and the 48 LSB represent individual fields
/// </summary>
Expand All @@ -122,14 +122,12 @@ public long GetNullableThumbprint(object o)
{
return mask;
}

const long long1 = 1;

for (var i= 0; i < _nullableAccessors.Count; i++)
for (var i = 0; i < _nullableAccessors.Count; i++)
{
if (_nullableAccessors[i].IsPropertyNull(o))
{
mask = mask | (long1 << i);
mask = mask | (_long1 << i);
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/YesSql.Core/Data/PropertyAccessorFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ namespace YesSql.Data
/// </summary>
public class PropertyAccessorFactory : IAccessorFactory
{
const BindingFlags DefaultBindingFlags = BindingFlags.IgnoreCase
| BindingFlags.Public
| BindingFlags.Instance
| BindingFlags.GetProperty
| BindingFlags.SetProperty
const BindingFlags DefaultBindingFlags = BindingFlags.IgnoreCase
| BindingFlags.Public
| BindingFlags.Instance
| BindingFlags.GetProperty
| BindingFlags.SetProperty
;

private readonly string _propertyName;
Expand Down Expand Up @@ -41,7 +41,7 @@ public IAccessor<T> CreateAccessor<T>(Type tContainer)
var setter = propertyInfo.GetSetMethod(true).CreateDelegate(setType);

Type accessorType = null;

if (tProperty == typeof(int))
{
accessorType = typeof(IntAccessor<>);
Expand All @@ -66,7 +66,7 @@ public IAccessor<T> CreateAccessor<T>(Type tContainer)
/// An accessor to an Int32 Id property
/// </summary>
/// <typeparam name="T"></typeparam>
internal class IntAccessor<T> : IAccessor<long>
internal sealed class IntAccessor<T> : IAccessor<long>
{
private readonly Func<T, int> _getter;
private readonly Action<T, int> _setter;
Expand All @@ -92,7 +92,7 @@ void IAccessor<long>.Set(object obj, long value)
/// An accessor to an Int64 Id property
/// </summary>
/// <typeparam name="T"></typeparam>
internal class LongAccessor<T> : IAccessor<long>
internal sealed class LongAccessor<T> : IAccessor<long>
{
private readonly Func<T, long> _getter;
private readonly Action<T, long> _setter;
Expand Down
24 changes: 14 additions & 10 deletions src/YesSql.Core/Data/WorkerQueryKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,9 @@ namespace YesSql.Data

public WorkerQueryKey(string prefix, long[] ids)
{
if (prefix == null)
{
throw new ArgumentNullException(nameof(prefix));
}
ArgumentNullException.ThrowIfNull(prefix);

if (ids == null)
{
throw new ArgumentNullException(nameof(ids));
}
ArgumentNullException.ThrowIfNull(ids);

_prefix = prefix;
_parameters = null;
Expand Down Expand Up @@ -59,12 +53,12 @@ public bool Equals(WorkerQueryKey other)
{
return false;
}

if (_parameters != null || other._parameters != null)
{
return SameParameters(_parameters, other._parameters);
}

if (_ids != null || other._ids != null)
{
return SameIds(_ids, other._ids);
Expand Down Expand Up @@ -181,5 +175,15 @@ private static bool SameIds(long[] values1, long[] values2)

return true;
}

public static bool operator ==(WorkerQueryKey left, WorkerQueryKey right)
{
return left.Equals(right);
}

public static bool operator !=(WorkerQueryKey left, WorkerQueryKey right)
{
return !(left == right);
}
}
}
Loading
Loading