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

Ensure proper types for uint16 in DataTypeResolver #14

Merged
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
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@
<ItemGroup Label="Code Analyzers">
<PackageReference Include="AsyncFixer" Version="1.6.0" PrivateAssets="All" />
<PackageReference Include="Asyncify" Version="0.9.7" PrivateAssets="All" />
<PackageReference Include="Meziantou.Analyzer" Version="2.0.146" PrivateAssets="All" />
<PackageReference Include="Meziantou.Analyzer" Version="2.0.150" PrivateAssets="All" />
<PackageReference Include="SecurityCodeScan.VS2019" Version="5.6.7" PrivateAssets="All" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.321" PrivateAssets="All" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.23.1.88495" PrivateAssets="All" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.24.0.89429" PrivateAssets="All" />
</ItemGroup>

</Project>
5 changes: 5 additions & 0 deletions sample/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
# StyleCop
# https://github.com/DotNetAnalyzers/StyleCopAnalyzers

# SonarAnalyzer.CSharp
# https://rules.sonarsource.com/csharp

dotnet_diagnostic.S1075.severity = none # Refactor your code not to use hardcoded absolute paths or URIs


##########################################
# Custom - Code Analyzers Rules
Expand Down
32 changes: 20 additions & 12 deletions src/Atc.Opc.Ua.CLI/.editorconfig
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
# ATC coding rules - https://github.com/atc-net/atc-coding-rules
# Version: 1.0.9
# Updated: 01-02-2022
# Location: app-type=CLI
# Distribution: DotNet6
# Inspired by: https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/code-style-rule-options
# Version: 1.0.0
# Updated: 11-04-2024
# Location: cli
# Distribution: Frameworks

##########################################
# Code Analyzers Rules
##########################################
[*.cs]
[*.{cs}]

dotnet_diagnostic.CA1031.severity = none # Not needed in CLI projects
dotnet_diagnostic.CA1303.severity = none # Not needed in CLI projects
dotnet_diagnostic.CA1819.severity = none # Not needed in CLI projects
dotnet_diagnostic.CA1848.severity = none # Not needed in CLI projects
dotnet_diagnostic.CA2000.severity = none # Not needed in CLI projects
dotnet_diagnostic.CA2254.severity = none # Not needed in CLI projects
dotnet_diagnostic.CA1031.severity = none # Do not catch general exception types
dotnet_diagnostic.CA1303.severity = none # Do not pass literals as localized parameters
dotnet_diagnostic.CA1819.severity = none # Properties should not return arrays
dotnet_diagnostic.CA1848.severity = none # Use the LoggerMessage delegates
dotnet_diagnostic.CA2000.severity = none # Dispose objects before losing scope
dotnet_diagnostic.CA2254.severity = none # Template should be a static expression

dotnet_diagnostic.MA0076.severity = none # Do not use implicit culture-sensitive ToString in interpolated strings

dotnet_diagnostic.S2629.severity = none # Don't use string interpolation in logging message templates.


##########################################
# Custom - Code Analyzers Rules
##########################################
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ private async Task<int> ExecuteInternalAsync(
var dataTypeAsString = settings.DataType;
var value = settings.Value;

var dataType = SimpleTypeHelper.GetTypeByName(dataTypeAsString);
var dataTypeResolver = new DataTypeResolver();
var dataType = dataTypeResolver.GetTypeByName(dataTypeAsString);
var typeConverter = TypeDescriptor.GetConverter(dataType);
var convertedValue = typeConverter.ConvertFromString(value);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public override ValidationResult Validate()
return ValidationResult.Error("DataValue is missing.");
}

if (!SimpleTypeHelper.TryGetTypeByName(DataType, out _))
var dataTypeResolver = new DataTypeResolver();
if (!dataTypeResolver.TryGetTypeByName(DataType, out _))
{
return ValidationResult.Error("DataType is not supported.");
}
Expand Down
4 changes: 1 addition & 3 deletions src/Atc.Opc.Ua.CLI/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
global using System.Diagnostics;
global using System.Diagnostics.CodeAnalysis;
global using System.Text;

global using Atc.Console.Spectre;
global using Atc.Console.Spectre.CommandSettings;
global using Atc.Console.Spectre.Factories;
Expand All @@ -12,11 +11,10 @@
global using Atc.Opc.Ua.CLI.Commands.Settings;
global using Atc.Opc.Ua.CLI.Extensions;
global using Atc.Opc.Ua.Contracts;
global using Atc.Opc.Ua.Resolvers;
global using Atc.Opc.Ua.Services;

global using Microsoft.Extensions.Configuration;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.Logging;

global using Spectre.Console;
global using Spectre.Console.Cli;
1 change: 0 additions & 1 deletion src/Atc.Opc.Ua/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
global using System;
global using System.Diagnostics.CodeAnalysis;
global using System.Globalization;
global using System.Text;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
// ReSharper disable InvertIf
namespace Atc.Opc.Ua.CLI;
namespace Atc.Opc.Ua.Resolvers;

public static class SimpleTypeHelper
public sealed class DataTypeResolver : IDataTypeResolver
{
[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1401:Fields should be private", Justification = "OK.")]
[SuppressMessage("Minor Code Smell", "S2386:Mutable fields should not be \"public static\"", Justification = "OK.")]
[SuppressMessage("Minor Bug", "S3887:Mutable, non-private fields should not be \"readonly\"", Justification = "OK.")]
[SuppressMessage("Design", "MA0016:Prefer returning collection abstraction instead of implementation", Justification = "OK.")]
[SuppressMessage("Design", "CA1002:Do not expose generic lists", Justification = "OK.")]
public static readonly List<Tuple<Type, string>> DotnetSimpleTypeLookup = new List<Tuple<Type, string>>
{
private readonly List<Tuple<Type, string>> dotnetSimpleTypeLookup =
[
Tuple.Create(typeof(bool), "bool"),
Tuple.Create(typeof(bool), "bool"),
Tuple.Create(typeof(bool), "Boolean"),
Expand All @@ -24,7 +18,7 @@ public static class SimpleTypeHelper
Tuple.Create(typeof(Half), "half"),
Tuple.Create(typeof(Guid), "Guid"),
Tuple.Create(typeof(int), "int"),
Tuple.Create(typeof(int), "Int16"),
Tuple.Create(typeof(short), "Int16"),
Tuple.Create(typeof(int), "Int32"),
Tuple.Create(typeof(int), "integer"),
Tuple.Create(typeof(long), "long"),
Expand All @@ -34,7 +28,7 @@ public static class SimpleTypeHelper
Tuple.Create(typeof(short), "short"),
Tuple.Create(typeof(string), "string"),
Tuple.Create(typeof(uint), "uint"),
Tuple.Create(typeof(uint), "UInt16"),
Tuple.Create(typeof(ushort), "UInt16"),
Tuple.Create(typeof(uint), "UInt32"),
Tuple.Create(typeof(ulong), "ulong"),
Tuple.Create(typeof(ulong), "UInt64"),
Expand All @@ -53,22 +47,35 @@ public static class SimpleTypeHelper
Tuple.Create(typeof(Half), "half?"),
Tuple.Create(typeof(Guid?), "Guid?"),
Tuple.Create(typeof(int?), "int?"),
Tuple.Create(typeof(int?), "Int16?"),
Tuple.Create(typeof(short?), "Int16?"),
Tuple.Create(typeof(int?), "Int32?"),
Tuple.Create(typeof(int?), "integer?"),
Tuple.Create(typeof(long?), "long?"),
Tuple.Create(typeof(long?), "Int64?"),
Tuple.Create(typeof(sbyte?), "sbyte?"),
Tuple.Create(typeof(short?), "short?"),
Tuple.Create(typeof(uint?), "uint?"),
Tuple.Create(typeof(uint?), "UInt16?"),
Tuple.Create(typeof(ushort?), "UInt16?"),
Tuple.Create(typeof(uint?), "UInt32?"),
Tuple.Create(typeof(ulong?), "ulong?"),
Tuple.Create(typeof(ulong?), "Uint64?"),
Tuple.Create(typeof(ulong?), "UInt64?"),
Tuple.Create(typeof(ushort?), "ushort?"),
};
];

public Type GetTypeByName(
string? value)
{
ArgumentNullException.ThrowIfNull(value);

if (TryGetTypeByName(value, out var result))
{
return result!;
}

throw new NotSupportedException($"Value is not supported: {value}");
}

public static bool TryGetTypeByName(
public bool TryGetTypeByName(
string? value,
out Type? type)
{
Expand All @@ -84,7 +91,7 @@ public static bool TryGetTypeByName(
value = value.Split('.', StringSplitOptions.RemoveEmptyEntries).Last();
}

var item = DotnetSimpleTypeLookup.Find(x => x.Item2.Equals(value, StringComparison.OrdinalIgnoreCase));
var item = dotnetSimpleTypeLookup.Find(x => x.Item2.Equals(value, StringComparison.OrdinalIgnoreCase));
if (item?.Item1 is null)
{
return false;
Expand All @@ -93,17 +100,4 @@ public static bool TryGetTypeByName(
type = item.Item1;
return true;
}

public static Type GetTypeByName(
string? value)
{
ArgumentNullException.ThrowIfNull(value);

if (TryGetTypeByName(value, out var result))
{
return result!;
}

throw new NotSupportedException($"Value is not supported: {value}");
}
}
15 changes: 15 additions & 0 deletions src/Atc.Opc.Ua/Resolvers/IDataTypeResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Atc.Opc.Ua.Resolvers;

/// <summary>
/// Defines a mechanism for resolving a .NET type from its name representation.
/// </summary>
public interface IDataTypeResolver
{
/// <summary>
/// Gets the corresponding .NET type for a given type name.
/// </summary>
/// <param name="value">The name of the type to resolve.</param>
/// <returns>The corresponding .NET Type.</returns>
Type GetTypeByName(
string? value);
}