diff --git a/src/Nexus/API/CatalogsController.cs b/src/Nexus/API/CatalogsController.cs index 9046611c..69574cd8 100644 --- a/src/Nexus/API/CatalogsController.cs +++ b/src/Nexus/API/CatalogsController.cs @@ -298,7 +298,7 @@ public async Task> if (license is null) { var catalogInfo = await catalogContainer.GetLazyCatalogInfoAsync(cancellationToken); - license = catalogInfo.Catalog.Properties?.GetStringValue([DataModelExtensions.LicenseKey]); + license = catalogInfo.Catalog.Properties?.GetStringValue(DataModelExtensions.LicenseKey); } return license; diff --git a/src/Nexus/Extensibility/DataSource/DataSourceController.cs b/src/Nexus/Extensibility/DataSource/DataSourceController.cs index 6481ebdf..fd94e370 100644 --- a/src/Nexus/Extensibility/DataSource/DataSourceController.cs +++ b/src/Nexus/Extensibility/DataSource/DataSourceController.cs @@ -59,7 +59,6 @@ internal class DataSourceController( DataOptions dataOptions, ILogger logger) : IDataSourceController { - private static readonly string[] _pipelinePositionPath = [DataModelExtensions.NEXUS_KEY, DataModelExtensions.PIPELINE_POSITION_KEY]; internal readonly IReadOnlyDictionary? _requestConfiguration = requestConfiguration; @@ -422,7 +421,11 @@ private async Task ReadOriginalAsync( var currentReadRequests = readRequests .Where(request => request.CatalogItem.Resource.Properties is null || - request.CatalogItem.Resource.Properties.GetIntValue(_pipelinePositionPath) == pipelinePosition + request.CatalogItem.Resource.Properties + .GetIntValue( + DataModelExtensions.NEXUS_KEY, + DataModelExtensions.PIPELINE_POSITION_KEY + ) == pipelinePosition ) .ToArray(); diff --git a/src/Nexus/Extensions/Sources/Sample.cs b/src/Nexus/Extensions/Sources/Sample.cs index 105c506c..7a4cc826 100644 --- a/src/Nexus/Extensions/Sources/Sample.cs +++ b/src/Nexus/Extensions/Sources/Sample.cs @@ -158,8 +158,8 @@ public async Task ReadAsync( // check credentials if (catalog.Id == RemoteCatalogId) { - var user = Context.RequestConfiguration?.GetStringValue([typeof(Sample).FullName!, "user"]); - var password = Context.RequestConfiguration?.GetStringValue([typeof(Sample).FullName!, "password"]); + var user = Context.RequestConfiguration?.GetStringValue(typeof(Sample).FullName!, "user"); + var password = Context.RequestConfiguration?.GetStringValue(typeof(Sample).FullName!, "password"); if (user != RemoteUsername || password != RemotePassword) throw new Exception("The provided credentials are invalid."); diff --git a/src/Nexus/Extensions/Writers/Csv.cs b/src/Nexus/Extensions/Writers/Csv.cs index 5b4eba42..0aebaf95 100644 --- a/src/Nexus/Extensions/Writers/Csv.cs +++ b/src/Nexus/Extensions/Writers/Csv.cs @@ -26,8 +26,6 @@ namespace Nexus.Writers; "https://github.com/nexus-main/nexus/blob/master/src/Nexus/Extensions/Writers/Csv.cs")] internal class Csv : IDataWriter, IDisposable { - private static readonly string[] _unitPath = [DataModelExtensions.UnitKey]; - private const string DESCRIPTION = """ { "label":"CSV + Schema (*.csv)", @@ -114,7 +112,7 @@ public async Task OpenAsync( if (!_resourceMap.TryGetValue(resourceFilePath, out var resource)) { - var rowIndexFormat = Context.RequestConfiguration?.GetStringValue(["row-index-format"]) ?? "index"; + var rowIndexFormat = Context.RequestConfiguration?.GetStringValue("row-index-format") ?? "index"; var constraints = new Constraints(Required: true); var timestampField = rowIndexFormat switch @@ -211,9 +209,9 @@ public async Task WriteAsync(TimeSpan fileOffset, WriteRequest[] requests, IProg .GroupBy(request => request.CatalogItem.Catalog.Id) .ToList(); - var rowIndexFormat = Context.RequestConfiguration?.GetStringValue(["row-index-format"]) ?? "index"; + var rowIndexFormat = Context.RequestConfiguration?.GetStringValue("row-index-format") ?? "index"; - var significantFigures = int.Parse(Context.RequestConfiguration?.GetStringValue(["significant-figures"]) ?? "4"); + var significantFigures = int.Parse(Context.RequestConfiguration?.GetStringValue("significant-figures") ?? "4"); significantFigures = Math.Clamp(significantFigures, 0, 30); var groupIndex = 0; @@ -307,7 +305,7 @@ private static void AppendWindowsNewLine(StringBuilder stringBuilder) private static string GetFieldName(CatalogItem catalogItem) { var unit = catalogItem.Resource.Properties? - .GetStringValue(_unitPath); + .GetStringValue(DataModelExtensions.UnitKey); var fieldName = $"{catalogItem.Resource.Id}_{catalogItem.Representation.Id}{DataModelUtilities.GetRepresentationParameterString(catalogItem.Parameters)}"; diff --git a/src/Nexus/Services/AppStateManager.cs b/src/Nexus/Services/AppStateManager.cs index a797a461..8187e70c 100644 --- a/src/Nexus/Services/AppStateManager.cs +++ b/src/Nexus/Services/AppStateManager.cs @@ -162,7 +162,7 @@ private void LoadDataWriters() } var additionalInformation = attribute.Description; - var label = (additionalInformation?.GetStringValue([UI.Core.Constants.DATA_WRITER_LABEL_KEY])) ?? throw new Exception($"The description of data writer {fullName} has no label property"); + var label = (additionalInformation?.GetStringValue(UI.Core.Constants.DATA_WRITER_LABEL_KEY)) ?? throw new Exception($"The description of data writer {fullName} has no label property"); var version = dataWriterType.Assembly .GetCustomAttribute()! .InformationalVersion; diff --git a/src/extensibility/dotnet-extensibility/DataModel/DataModelExtensions.cs b/src/extensibility/dotnet-extensibility/DataModel/DataModelExtensions.cs index afc13b9f..7cf9d71a 100644 --- a/src/extensibility/dotnet-extensibility/DataModel/DataModelExtensions.cs +++ b/src/extensibility/dotnet-extensibility/DataModel/DataModelExtensions.cs @@ -230,18 +230,19 @@ IDataSource[] dataSources var isModified = false; var newResources = new List(); - string[] originalNamePath = [OriginalNameKey]; - string[] pipelinePositionPath = [NEXUS_KEY, PIPELINE_POSITION_KEY]; - string[] groupsPath = [GroupsKey]; - foreach (var resource in catalog.Resources) { var resourceProperties = resource.Properties; var newResource = resource; - var originalName = resourceProperties?.GetStringValue(originalNamePath); - var currentPipelinePosition = resourceProperties?.GetIntValue(pipelinePositionPath); - var groups = resourceProperties?.GetStringArray(groupsPath); + var originalName = resourceProperties? + .GetStringValue(OriginalNameKey); + + var currentPipelinePosition = resourceProperties? + .GetIntValue(NEXUS_KEY, PIPELINE_POSITION_KEY); + + var groups = resourceProperties? + .GetStringArray(GroupsKey); var distinctGroups = groups is null ? default diff --git a/src/extensibility/dotnet-extensibility/DataModel/PropertiesExtensions.cs b/src/extensibility/dotnet-extensibility/DataModel/PropertiesExtensions.cs index 2ef71984..c0576850 100644 --- a/src/extensibility/dotnet-extensibility/DataModel/PropertiesExtensions.cs +++ b/src/extensibility/dotnet-extensibility/DataModel/PropertiesExtensions.cs @@ -17,10 +17,8 @@ public static class PropertiesExtensions /// The properties. /// The propery path segments. /// - public static string? GetStringValue(this IReadOnlyDictionary properties, Span pathSegments) + public static string? GetStringValue(this IReadOnlyDictionary properties, params ReadOnlySpan pathSegments) { - // TODO Maybe use params collection (Span) in future? https://github.com/dotnet/csharplang/issues/7700 - if (properties.TryGetValue(pathSegments[0], out var element)) { pathSegments = pathSegments[1..]; @@ -44,7 +42,7 @@ public static class PropertiesExtensions /// The properties. /// The propery path segments. /// - public static string? GetStringValue(this JsonElement properties, Span pathSegments) + public static string? GetStringValue(this JsonElement properties, params ReadOnlySpan pathSegments) { var root = properties.GetJsonObjectFromPath(pathSegments[0..^1]); var propertyName = pathSegments[^1]; @@ -66,7 +64,7 @@ public static class PropertiesExtensions /// The properties. /// The property path segments. /// - public static string?[]? GetStringArray(this IReadOnlyDictionary properties, Span pathSegments) + public static string?[]? GetStringArray(this IReadOnlyDictionary properties, params ReadOnlySpan pathSegments) { if (properties.TryGetValue(pathSegments[0], out var element)) { @@ -96,7 +94,7 @@ public static class PropertiesExtensions /// The properties. /// The property path segments. /// - public static string?[]? GetStringArray(this JsonElement properties, Span pathSegments) + public static string?[]? GetStringArray(this JsonElement properties, params ReadOnlySpan pathSegments) { var root = properties.GetJsonObjectFromPath(pathSegments[0..^1]); var propertyName = pathSegments[^1]; @@ -116,7 +114,7 @@ public static class PropertiesExtensions return default; } - internal static int? GetIntValue(this IReadOnlyDictionary properties, Span pathSegments) + internal static int? GetIntValue(this IReadOnlyDictionary properties, params ReadOnlySpan pathSegments) { if (properties.TryGetValue(pathSegments[0], out var element)) { @@ -132,7 +130,7 @@ public static class PropertiesExtensions return default; } - internal static int? GetIntValue(this JsonElement properties, Span pathSegments) + internal static int? GetIntValue(this JsonElement properties, params ReadOnlySpan pathSegments) { var root = properties.GetJsonObjectFromPath(pathSegments[0..^1]); var propertyName = pathSegments[^1]; @@ -148,7 +146,7 @@ public static class PropertiesExtensions return default; } - private static JsonElement GetJsonObjectFromPath(this JsonElement root, Span pathSegements) + private static JsonElement GetJsonObjectFromPath(this JsonElement root, ReadOnlySpan pathSegements) { if (pathSegements.Length == 0) return root; diff --git a/src/extensibility/dotnet-extensibility/DataModel/ResourceCatalog.cs b/src/extensibility/dotnet-extensibility/DataModel/ResourceCatalog.cs index d0e97f60..6cb1c894 100644 --- a/src/extensibility/dotnet-extensibility/DataModel/ResourceCatalog.cs +++ b/src/extensibility/dotnet-extensibility/DataModel/ResourceCatalog.cs @@ -152,8 +152,6 @@ internal bool TryFind(ResourcePathParseResult parseResult, [NotNullWhen(true)] o } } - string[] typePath = ["type"]; - var parametersAreOK = (representation.Parameters is null && parameters is null) || @@ -163,8 +161,8 @@ internal bool TryFind(ResourcePathParseResult parseResult, [NotNullWhen(true)] o parameters.ContainsKey(current.Key) && - (current.Value.GetStringValue(typePath) == "input-integer" && long.TryParse(parameters[current.Key], out var _) || - current.Value.GetStringValue(typePath) == "select" /* no validation here */))); + (current.Value.GetStringValue("type") == "input-integer" && long.TryParse(parameters[current.Key], out var _) || + current.Value.GetStringValue("type") == "select" /* no validation here */))); if (!parametersAreOK) return false; diff --git a/src/extensibility/dotnet-extensibility/Extensibility/DataSource/DataSourceTypes.cs b/src/extensibility/dotnet-extensibility/Extensibility/DataSource/DataSourceTypes.cs index 01ea1514..a27080eb 100644 --- a/src/extensibility/dotnet-extensibility/Extensibility/DataSource/DataSourceTypes.cs +++ b/src/extensibility/dotnet-extensibility/Extensibility/DataSource/DataSourceTypes.cs @@ -58,7 +58,7 @@ internal class ReadRequestManager : IDisposable public ReadRequestManager(CatalogItem catalogItem, int elementCount) { var byteCount = elementCount * catalogItem.Representation.ElementSize; - var originalResourceName = catalogItem.Resource.Properties!.GetStringValue([DataModelExtensions.OriginalNameKey])!; + var originalResourceName = catalogItem.Resource.Properties!.GetStringValue(DataModelExtensions.OriginalNameKey)!; /* data memory */ var dataOwner = MemoryPool.Shared.Rent(byteCount); diff --git a/tests/Nexus.Tests/DataSource/SampleDataSourceTests.cs b/tests/Nexus.Tests/DataSource/SampleDataSourceTests.cs index b1e02302..555ce727 100644 --- a/tests/Nexus.Tests/DataSource/SampleDataSourceTests.cs +++ b/tests/Nexus.Tests/DataSource/SampleDataSourceTests.cs @@ -31,8 +31,8 @@ public async Task ProvidesCatalog() // assert var actualIds = actual.Resources!.Select(resource => resource.Id).ToList(); - var actualUnits = actual.Resources!.Select(resource => resource.Properties?.GetStringValue(["unit"])).ToList(); - var actualGroups = actual.Resources!.SelectMany(resource => resource.Properties?.GetStringArray(["groups"]) ?? []); + var actualUnits = actual.Resources!.Select(resource => resource.Properties?.GetStringValue(DataModelExtensions.UnitKey)).ToList(); + var actualGroups = actual.Resources!.SelectMany(resource => resource.Properties?.GetStringArray(DataModelExtensions.GroupsKey) ?? []); var actualDataTypes = actual.Resources!.SelectMany(resource => resource.Representations!.Select(representation => representation.DataType)).ToList(); var expectedIds = new List() { "T1", "V1", "unix_time1", "unix_time2" };