diff --git a/Consequences/Consequences/IStreamingProcessor.cs b/Consequences/Consequences/IStreamingProcessor.cs index e67857c..239487e 100644 --- a/Consequences/Consequences/IStreamingProcessor.cs +++ b/Consequences/Consequences/IStreamingProcessor.cs @@ -1,7 +1,8 @@ -using USACE.HEC.Geography; +using System.Diagnostics.CodeAnalysis; +using USACE.HEC.Geography; namespace USACE.HEC.Consequences; public interface IStreamingProcessor { - public void Process(Action consequenceReceptorProcess) where T : IConsequencesReceptor, new(); + public void Process<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>(Action consequenceReceptorProcess) where T : IConsequencesReceptor, new(); } diff --git a/Consequences/Consequences/NSIStreamingProcessor.cs b/Consequences/Consequences/NSIStreamingProcessor.cs index c287d6a..a970e28 100644 --- a/Consequences/Consequences/NSIStreamingProcessor.cs +++ b/Consequences/Consequences/NSIStreamingProcessor.cs @@ -44,13 +44,7 @@ private static async Task ProcessCollection(BoundingBox boundingBox, Action(propertiesElement.GetRawText()); - #pragma warning restore IL2026 - #pragma warning restore IL3050 - + Structure s = JsonSerializer.Deserialize(propertiesElement.GetRawText(), SourceGenerationContext.Default.Structure); // apply the action to the deserialized structure ConsequenceReceptorProcess(s); } @@ -81,14 +75,7 @@ private static async Task ProcessStream(BoundingBox boundingBox, Action(propertiesElement.GetRawText()); - #pragma warning restore IL2026 - #pragma warning restore IL3050 - + Structure s = JsonSerializer.Deserialize(propertiesElement.GetRawText(), SourceGenerationContext.Default.Structure); // apply the action to the deserialized structure ConsequenceReceptorProcess(s); } diff --git a/Consequences/Consequences/Structure.cs b/Consequences/Consequences/Structure.cs index 559cd1b..b1aad0a 100644 --- a/Consequences/Consequences/Structure.cs +++ b/Consequences/Consequences/Structure.cs @@ -73,7 +73,7 @@ public Result Compute(IHazard hazard) { resultItems.Add(new ResultItem { ResultName = "Depth", - Result = hazard.Get(HazardParameter.Depth) + ResultValue = hazard.Get(HazardParameter.Depth) }); } @@ -81,7 +81,7 @@ public Result Compute(IHazard hazard) { resultItems.Add(new ResultItem { ResultName = "Velocity", - Result = hazard.Get(HazardParameter.Velocity) + ResultValue = hazard.Get(HazardParameter.Velocity) }); } @@ -89,7 +89,7 @@ public Result Compute(IHazard hazard) { resultItems.Add(new ResultItem { ResultName = "ArrivalTime", - Result = hazard.Get(HazardParameter.ArrivalTime) + ResultValue = hazard.Get(HazardParameter.ArrivalTime) }); } @@ -97,10 +97,16 @@ public Result Compute(IHazard hazard) { resultItems.Add(new ResultItem { ResultName = "ArrivalTime2ft", - Result = hazard.Get(HazardParameter.ArrivalTime2ft) + ResultValue = hazard.Get(HazardParameter.ArrivalTime2ft) }); } return new Result([.. resultItems]); } } + +[JsonSourceGenerationOptions(WriteIndented = true)] +[JsonSerializable(typeof(Structure))] +internal partial class SourceGenerationContext : JsonSerializerContext +{ +} diff --git a/Consequences/Results/ConsoleWriter.cs b/Consequences/Results/ConsoleWriter.cs index 831bdb5..dc28bb7 100644 --- a/Consequences/Results/ConsoleWriter.cs +++ b/Consequences/Results/ConsoleWriter.cs @@ -47,7 +47,7 @@ public string WriteString(Result res) CheckIfSameHeaders(res); foreach (string header in headers) { - object val = res.Fetch(header).Result; + object val = res.Fetch(header).ResultValue; output.Append(val.ToString()); if (header != headers.Last()) { diff --git a/Consequences/Results/Result.cs b/Consequences/Results/Result.cs index f49f980..1f7a303 100644 --- a/Consequences/Results/Result.cs +++ b/Consequences/Results/Result.cs @@ -14,7 +14,6 @@ public ResultItem Fetch(string name) for (int i = 0; i < ResultItems.Length; i++) if (ResultItems[i].ResultName == name) return ResultItems[i]; - // return empty ResultItem if not found - return new ResultItem { ResultName = name }; + throw new ArgumentException($"{name} not found in result"); } } diff --git a/Consequences/Results/ResultItem.cs b/Consequences/Results/ResultItem.cs index fae3e72..f514404 100644 --- a/Consequences/Results/ResultItem.cs +++ b/Consequences/Results/ResultItem.cs @@ -2,5 +2,5 @@ public struct ResultItem { public string ResultName; - public object Result; + public object ResultValue; } diff --git a/Consequences/Results/Utilities.cs b/Consequences/Results/Utilities.cs index 128d229..9998274 100644 --- a/Consequences/Results/Utilities.cs +++ b/Consequences/Results/Utilities.cs @@ -1,11 +1,12 @@ -using System.Reflection; +using System.Diagnostics.CodeAnalysis; +using System.Reflection; using System.Text.Json.Serialization; using USACE.HEC.Consequences; namespace USACE.HEC.Results; public class Utilities { - public static Result ConsequenceReceptorToResult(IConsequencesReceptor cr) where T: IConsequencesReceptor + public static Result ConsequenceReceptorToResult<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>(IConsequencesReceptor cr) where T: IConsequencesReceptor { List resultItems = []; @@ -15,7 +16,7 @@ public static Result ConsequenceReceptorToResult(IConsequencesReceptor cr) wh ResultItem item; JsonPropertyNameAttribute jsonPropertyAttribute = property.GetCustomAttribute(); item.ResultName = jsonPropertyAttribute != null ? jsonPropertyAttribute.Name : property.Name; - item.Result = property.GetValue(cr); + item.ResultValue = property.GetValue(cr); resultItems.Add(item); } diff --git a/ConsequencesTest/ConsoleWriterTest.cs b/ConsequencesTest/ConsoleWriterTest.cs index 5a292ba..f7f990d 100644 --- a/ConsequencesTest/ConsoleWriterTest.cs +++ b/ConsequencesTest/ConsoleWriterTest.cs @@ -4,9 +4,9 @@ namespace ConsequencesTest; public class ConsoleWrite { - static ResultItem r1 = new ResultItem { ResultName = "Depth", Result = 1.03f }; - static ResultItem r2 = new ResultItem { ResultName = "Velocity", Result = 2.02f }; - static ResultItem r3 = new ResultItem { ResultName = "ArrivalTime2ft", Result = new DateTime() }; + static ResultItem r1 = new ResultItem { ResultName = "Depth", ResultValue = 1.03f }; + static ResultItem r2 = new ResultItem { ResultName = "Velocity", ResultValue = 2.02f }; + static ResultItem r3 = new ResultItem { ResultName = "ArrivalTime2ft", ResultValue = new DateTime() }; static ResultItem[] resultItems = { r1, r2, r3 }; Result res = new Result(resultItems); [Fact] diff --git a/ConsequencesTest/StructureTest.cs b/ConsequencesTest/StructureTest.cs index 9e4b71b..c589563 100644 --- a/ConsequencesTest/StructureTest.cs +++ b/ConsequencesTest/StructureTest.cs @@ -16,8 +16,8 @@ public void Compute_SimpleDepth() ResultItem item1 = res.Fetch("Depth"); Assert.Equal("Depth", item1.ResultName); - Assert.Equal(typeof(float), item1.Result.GetType()); - Assert.Equal(4.56f, item1.Result); + Assert.Equal(typeof(float), item1.ResultValue.GetType()); + Assert.Equal(4.56f, item1.ResultValue); } [Fact] @@ -47,10 +47,10 @@ public void Compute_ArrayDepth_CorrectConsoleOutput() ResultItem depthItem = res.Fetch("Depth"); Assert.Equal("Depth", depthItem.ResultName); - Assert.Equal(typeof(float), depthItem.Result.GetType()); - Assert.Equal(depthHazard.Get(HazardParameter.Depth), depthItem.Result); + Assert.Equal(typeof(float), depthItem.ResultValue.GetType()); + Assert.Equal(depthHazard.Get(HazardParameter.Depth), depthItem.ResultValue); - expectedConsoleOutput += depthItem.Result.ToString() + "\r\n"; + expectedConsoleOutput += depthItem.ResultValue.ToString() + "\r\n"; actualOutput += cw.WriteString(res); } } @@ -89,22 +89,22 @@ public void Compute_ArrayLifeLoss_CorrectConsoleOutput() ResultItem depthItem = res.Fetch("Depth"); Assert.Equal("Depth", depthItem.ResultName); - Assert.Equal(typeof(float), depthItem.Result.GetType()); - Assert.Equal(lifeLossHazard.Get(HazardParameter.Depth), depthItem.Result); + Assert.Equal(typeof(float), depthItem.ResultValue.GetType()); + Assert.Equal(lifeLossHazard.Get(HazardParameter.Depth), depthItem.ResultValue); ResultItem velocityItem = res.Fetch("Velocity"); Assert.Equal("Velocity", velocityItem.ResultName); - Assert.Equal(typeof(float), velocityItem.Result.GetType()); - Assert.Equal(lifeLossHazard.Get(HazardParameter.Velocity), velocityItem.Result); + Assert.Equal(typeof(float), velocityItem.ResultValue.GetType()); + Assert.Equal(lifeLossHazard.Get(HazardParameter.Velocity), velocityItem.ResultValue); ResultItem arrivalTime2ftItem = res.Fetch("ArrivalTime2ft"); Assert.Equal("ArrivalTime2ft", arrivalTime2ftItem.ResultName); - Assert.Equal(typeof(DateTime), arrivalTime2ftItem.Result.GetType()); - Assert.Equal(lifeLossHazard.Get(HazardParameter.ArrivalTime2ft), arrivalTime2ftItem.Result); + Assert.Equal(typeof(DateTime), arrivalTime2ftItem.ResultValue.GetType()); + Assert.Equal(lifeLossHazard.Get(HazardParameter.ArrivalTime2ft), arrivalTime2ftItem.ResultValue); - expectedConsoleOutput += depthItem.Result.ToString() + ','; - expectedConsoleOutput += velocityItem.Result.ToString() + ','; - expectedConsoleOutput += arrivalTime2ftItem.Result.ToString() + "\r\n"; + expectedConsoleOutput += depthItem.ResultValue.ToString() + ','; + expectedConsoleOutput += velocityItem.ResultValue.ToString() + ','; + expectedConsoleOutput += arrivalTime2ftItem.ResultValue.ToString() + "\r\n"; actualOutput += cw.WriteString(res); } } diff --git a/Geospatial/Geospatial.csproj b/Geospatial/Geospatial.csproj index bd8d374..8949560 100644 --- a/Geospatial/Geospatial.csproj +++ b/Geospatial/Geospatial.csproj @@ -1,29 +1,30 @@  - - net8.0 - enable - enable - Library - - - - - - - - - - - - - - -