FullItemBase.cs (4839B)
1 // ----------------------------------------------------------------------------- 2 // <copyright file="FullItemBase.cs" company="Microsoft Corporation"> 3 // Copyright (c) Microsoft Corporation. Licensed under the MIT License. 4 // </copyright> 5 // ----------------------------------------------------------------------------- 6 7 namespace Microsoft.Management.Configuration.Processor.DSCv3.Schema_2024_04.Outputs 8 { 9 using System.IO; 10 using System.Text.Json; 11 using System.Text.Json.Nodes; 12 using System.Text.Json.Serialization; 13 using Microsoft.Management.Configuration.Processor.DSCv3.Schema_2024_04.Metadata; 14 15 /// <summary> 16 /// The base implementation of the full form output. 17 /// When the retrieved instance is for group resource, adapter resource, or nested inside a group or adapter resource, DSC returns a full result, which also includes the resource type and instance name. 18 /// </summary> 19 /// <typeparam name="TSimple">The simple item type.</typeparam> 20 /// <typeparam name="TFull">The full item type.</typeparam> 21 internal class FullItemBase<TSimple, TFull> 22 where TFull : FullItemBase<TSimple, TFull>, new() 23 { 24 private const string NameProperty = "name"; 25 26 /// <summary> 27 /// Initializes a new instance of the <see cref="FullItemBase{TSimple,TFull}"/> class. 28 /// </summary> 29 public FullItemBase() 30 { 31 } 32 33 /// <summary> 34 /// Gets or sets the metadata for this test result. 35 /// </summary> 36 [JsonRequired] 37 public ResourceInstanceResult? Metadata { get; set; } 38 39 /// <summary> 40 /// Gets or sets the name for this test result. 41 /// </summary> 42 [JsonRequired] 43 public string? Name { get; set; } 44 45 /// <summary> 46 /// Gets or sets the type for the resource. 47 /// </summary> 48 [JsonRequired] 49 public string? Type { get; set; } 50 51 /// <summary> 52 /// Gets or sets the result of the test. 53 /// </summary> 54 [JsonRequired] 55 public JsonNode? Result { get; set; } 56 57 /// <summary> 58 /// Gets or sets the simple result. 59 /// </summary> 60 [JsonIgnore] 61 public TSimple? SimpleResult { get; set; } 62 63 /// <summary> 64 /// Gets or sets the full results. 65 /// </summary> 66 [JsonIgnore] 67 [System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.SpacingRules", "SA1011:Opening square brackets should be spaced correctly", Justification = "Pending SC 1.2 release")] 68 protected TFull[]? FullResults { get; set; } 69 70 /// <summary> 71 /// Initializes a new instance of the TFull class. 72 /// </summary> 73 /// <param name="document">The document to construct from.</param> 74 /// <param name="options">The options to use.</param> 75 /// <returns>The item created.</returns> 76 public static TFull CreateFrom(JsonDocument document, JsonSerializerOptions options) 77 { 78 if (!document.RootElement.TryGetProperty(NameProperty, out JsonElement jsonElement)) 79 { 80 return new () { SimpleResult = JsonSerializer.Deserialize<TSimple>(document, options) }; 81 } 82 else 83 { 84 TFull? result = JsonSerializer.Deserialize<TFull>(document, options); 85 86 if (result == null) 87 { 88 throw new InvalidDataException("Unable to deserialize full result."); 89 } 90 91 result.ProcessResult(options); 92 return result; 93 } 94 } 95 96 /// <summary> 97 /// Converts the Result property into the appropriate simple or full results. 98 /// </summary> 99 /// <param name="options">The options to use.</param> 100 public void ProcessResult(JsonSerializerOptions options) 101 { 102 if (this.Result == null) 103 { 104 throw new System.InvalidOperationException("JSON result has not been initialized."); 105 } 106 107 if (this.Result is JsonObject jsonObject) 108 { 109 this.SimpleResult = JsonSerializer.Deserialize<TSimple>(this.Result, options); 110 } 111 else 112 { 113 this.FullResults = JsonSerializer.Deserialize<TFull[]>(this.Result, options); 114 115 if (this.FullResults == null) 116 { 117 throw new InvalidDataException("Unable to deserialize full results."); 118 } 119 120 foreach (TFull result in this.FullResults) 121 { 122 result.ProcessResult(options); 123 } 124 } 125 } 126 } 127 }