Przeglądaj źródła

Using doubles - re-wrote the Converters.cs

James 2 miesięcy temu
rodzic
commit
5b17ded352

+ 1 - 4
RackPeek.Domain/Resources/Hardware/Models/AccessPoint.cs

@@ -1,10 +1,7 @@
-using RackPeek.Domain.Resources.Hardware.Parsing;
-
 namespace RackPeek.Domain.Resources.Hardware.Models;
 
 public class AccessPoint : Hardware
 {
     public string? Model { get; set; }
-    public string? Speed { get; set; }
-    public double SpeedGb => UnitParser.ParseGbValue(Speed);
+    public double? Speed { get; set; }
 }

+ 0 - 12
RackPeek.Domain/Resources/Hardware/Parsing/UnitParser.cs

@@ -1,12 +0,0 @@
-namespace RackPeek.Domain.Resources.Hardware.Parsing;
-
-public class UnitParser
-{
-    public static double ParseGbValue(string? raw)
-    {
-        if (string.IsNullOrWhiteSpace(raw)) return 0;
-        raw = raw.ToLower().Trim();
-        if (raw.EndsWith("gb")) raw = raw.Replace("gb", "");
-        return double.TryParse(raw, out var value) ? value : 0;
-    }
-}

+ 1 - 1
RackPeek.Domain/Resources/Hardware/Reports/AccessPointHardwareReport.cs

@@ -23,7 +23,7 @@ public class AccessPointHardwareReportUseCase(IHardwareRepository repository)
             return new AccessPointHardwareRow(
                 Name: ap.Name,
                 Model: ap.Model ?? "Unknown",
-                SpeedGb: ap.SpeedGb
+                SpeedGb: ap.Speed ?? 0
             );
         }).ToList();
 

+ 27 - 21
RackPeek/Yaml/Converters.cs

@@ -1,32 +1,25 @@
+using System.Globalization;
+using System.Text.RegularExpressions;
 using YamlDotNet.Core;
 using YamlDotNet.Core.Events;
 using YamlDotNet.Serialization;
-using System.Globalization;
-using System.Text.RegularExpressions;
 
 namespace RackPeek;
 
 public static class StorageSizeParser
 {
-    private static readonly Regex SizeRegex =
-        new(@"^\s*(\d+(?:\.\d+)?)\s*(gb|tb)?\s*$",
-            RegexOptions.IgnoreCase | RegexOptions.Compiled);
+    private static readonly Regex SizeRegex = new(@"^\s*(\d+(?:\.\d+)?)\s*(gb|tb)?\s*$",
+        RegexOptions.IgnoreCase | RegexOptions.Compiled);
 
-    public static int ParseToGb(string input)
+    public static double ParseToGbDouble(string input)
     {
         var match = SizeRegex.Match(input);
-
-        if (!match.Success)
-            throw new FormatException($"Invalid storage size: '{input}'");
-
+        if (!match.Success) throw new FormatException($"Invalid storage size: '{input}'");
         var value = double.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture);
         var unit = match.Groups[2].Value.ToLowerInvariant();
-
         return unit switch
         {
-            "tb" => (int)Math.Round(value * 1024),
-            "gb" or "" => (int)Math.Round(value),
-            _ => throw new FormatException($"Unknown unit in '{input}'")
+            "tb" => value * 1024, "gb" or "" => value, _ => throw new FormatException($"Unknown unit in '{input}'")
         };
     }
 }
@@ -34,7 +27,10 @@ public static class StorageSizeParser
 public class StorageSizeYamlConverter : IYamlTypeConverter
 {
     public bool Accepts(Type type) =>
-        type == typeof(int) || type == typeof(int?);
+        type == typeof(int) ||
+        type == typeof(int?) ||
+        type == typeof(double) ||
+        type == typeof(double?);
 
     public object? ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer)
     {
@@ -44,13 +40,23 @@ public class StorageSizeYamlConverter : IYamlTypeConverter
         if (string.IsNullOrWhiteSpace(value))
             return null;
 
-        // If it's already a number, just parse it
-        if (int.TryParse(value, out var numeric))
-            return numeric;
+        // If it's already a number, parse directly
+        if (double.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var numericDouble))
+        {
+            if (type == typeof(double) || type == typeof(double?))
+                return numericDouble;
+
+            if (type == typeof(int) || type == typeof(int?))
+                return (int)numericDouble;
+        }
+
+        // Otherwise parse with size parser
+        var gb = StorageSizeParser.ParseToGbDouble(value);
+
+        if (type == typeof(double) || type == typeof(double?))
+            return gb;
 
-        // Otherwise try size parsing
-        return StorageSizeParser.ParseToGb(value);
-        
+        return (int)Math.Round(gb);
     }
 
     public void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)

+ 2 - 2
Tests/Yaml/HardwareDeserializationTests.cs

@@ -403,7 +403,7 @@ resources:
   - kind: AccessPoint
     name: lounge-ap
     model: Unifi-Ap-Pro
-    speed: 2.5gb
+    speed: 2.5Gb
 ";
 
         var sut = CreateSut(yaml);
@@ -421,7 +421,7 @@ resources:
 
         Assert.Equal("lounge-ap", accessPoint.Name);
         Assert.Equal("Unifi-Ap-Pro", accessPoint.Model);
-        Assert.Equal(2.5, accessPoint.SpeedGb);
+        Assert.Equal(2.5, accessPoint.Speed);
 
     }