Policy.Autorest/custom/Any.cs

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// Code generated by Microsoft (R) AutoRest Code Generator.
// Changes may cause incorrect behavior and will be lost if the code is regenerated.
 
namespace Microsoft.Azure.PowerShell.Cmdlets.Policy.Models
{
    using static Microsoft.Azure.PowerShell.Cmdlets.Policy.Runtime.Extensions;
 
    /// <summary>Anything — a free-form dictionary of string keys to arbitrary values.</summary>
    public partial class Any :
        Microsoft.Azure.PowerShell.Cmdlets.Policy.Runtime.IAssociativeArray<global::System.Object>
    {
        // Backing store for all key-value pairs carried by this instance.
        protected global::System.Collections.Generic.Dictionary<global::System.String, global::System.Object> __additionalProperties = new global::System.Collections.Generic.Dictionary<global::System.String, global::System.Object>(global::System.StringComparer.OrdinalIgnoreCase);
 
        // ── IAssociativeArray<object> ────────────────────────────────────────────
 
        global::System.Collections.Generic.IDictionary<global::System.String, global::System.Object> Microsoft.Azure.PowerShell.Cmdlets.Policy.Runtime.IAssociativeArray<global::System.Object>.AdditionalProperties { get => __additionalProperties; }
 
        public global::System.Object this[global::System.String index] { get => __additionalProperties[index]; set => __additionalProperties[index] = value; }
 
        public void Add(global::System.String key, global::System.Object value) => __additionalProperties.Add(key, value);
 
        public void Clear() => __additionalProperties.Clear();
 
        public bool ContainsKey(global::System.String key) => __additionalProperties.ContainsKey(key);
 
        public bool Remove(global::System.String key) => __additionalProperties.Remove(key);
 
        public bool TryGetValue(global::System.String key, out global::System.Object value) => __additionalProperties.TryGetValue(key, out value);
 
        // ── Copy helpers (PowerShell deserialization) ────────────────────────────
 
        /// <summary>Copies entries from an untyped <see cref="global::System.Collections.IDictionary" /> (e.g. a PowerShell Hashtable).</summary>
        public void CopyFrom(global::System.Collections.IDictionary source)
        {
            if (null != source)
            {
                foreach (var property in Microsoft.Azure.PowerShell.Cmdlets.Policy.Runtime.PowerShell.TypeConverterExtensions.GetFilteredProperties(source, new global::System.Collections.Generic.HashSet<global::System.String>() { }))
                {
                    if (null != property.Key)
                    {
                        // Preserve null values — they must round-trip as explicit JSON null.
                        // Previously, the `null != property.Value` guard silently dropped null-valued keys.
                        this.__additionalProperties[property.Key.ToString()] = property.Value == null
                            ? null
                            : global::System.Management.Automation.LanguagePrimitives.ConvertTo<global::System.Object>(property.Value);
                    }
                }
            }
        }
 
        /// <summary>Copies entries from a <see cref="global::System.Management.Automation.PSObject" />.</summary>
        public void CopyFrom(global::System.Management.Automation.PSObject source)
        {
            if (null != source)
            {
                foreach (var property in Microsoft.Azure.PowerShell.Cmdlets.Policy.Runtime.PowerShell.TypeConverterExtensions.GetFilteredProperties(source, new global::System.Collections.Generic.HashSet<global::System.String>() { }))
                {
                    if (null != property.Key)
                    {
                        // Preserve null values — they must round-trip as explicit JSON null.
                        this.__additionalProperties[property.Key.ToString()] = property.Value == null
                            ? null
                            : global::System.Management.Automation.LanguagePrimitives.ConvertTo<global::System.Object>(property.Value);
                    }
                }
            }
        }
 
        // ── Scalar support ───────────────────────────────────────────────────────
        // An Any instance can represent either a key-value dictionary (the common case)
        // or a single scalar value (string, number, boolean, or array) when the JSON
        // value is not a JSON object. The scalar is stored separately from
        // __additionalProperties so the normal dictionary serialization path is unaffected.
 
        private object _scalar;
        private bool _isScalar;
 
        /// <summary>True when this instance wraps a scalar value rather than a key-value dictionary.</summary>
        public bool IsScalar => _isScalar;
 
        /// <summary>The scalar value. Only meaningful when <see cref="IsScalar"/> is true.</summary>
        public object ScalarValue => _scalar;
 
        /// <summary>Creates an <see cref="Any"/> that wraps a single scalar (string, number, bool, or array).</summary>
        public static Any FromScalar(object value)
        {
            return new Any { _scalar = value, _isScalar = true };
        }
 
        // ── Implicit conversion ──────────────────────────────────────────────────
 
        public static implicit operator global::System.Collections.Generic.Dictionary<global::System.String, global::System.Object>(Microsoft.Azure.PowerShell.Cmdlets.Policy.Models.Any source) => source.__additionalProperties;
 
        // ── JSON partial-method implementations ──────────────────────────────────
 
        /// <summary>Populates the backing dictionary from the deserialized <see cref="Microsoft.Azure.PowerShell.Cmdlets.Policy.Runtime.Json.JsonObject" />.</summary>
        partial void AfterFromJson(Microsoft.Azure.PowerShell.Cmdlets.Policy.Runtime.Json.JsonObject json)
        {
            Microsoft.Azure.PowerShell.Cmdlets.Policy.Runtime.JsonSerializable.FromJson(
                json,
                ((Microsoft.Azure.PowerShell.Cmdlets.Policy.Runtime.IAssociativeArray<global::System.Object>)this).AdditionalProperties,
                Microsoft.Azure.PowerShell.Cmdlets.Policy.Runtime.JsonSerializable.DeserializeDictionary(() => new global::System.Collections.Generic.Dictionary<global::System.String, global::System.Object>()));
        }
 
        /// <summary>
        /// Replaces null values in the backing dictionary with a sentinel <see cref="global::System.DateTime" /> (Year=1)
        /// so the AutoRest serializer emits an explicit JSON null instead of omitting the key entirely.
        /// </summary>
        partial void BeforeToJson(ref Microsoft.Azure.PowerShell.Cmdlets.Policy.Runtime.Json.JsonObject container, ref bool returnNow)
        {
            SerializationHelpers.SetSentinel(__additionalProperties);
        }
 
        /// <summary>Serializes the backing dictionary into the outgoing <see cref="Microsoft.Azure.PowerShell.Cmdlets.Policy.Runtime.Json.JsonObject" /> container,
        /// then restores any sentinel <see cref="global::System.DateTime" /> values (Year=1) back to <c>null</c> so that the in-memory object is not permanently mutated.</summary>
        partial void AfterToJson(ref Microsoft.Azure.PowerShell.Cmdlets.Policy.Runtime.Json.JsonObject container)
        {
            Microsoft.Azure.PowerShell.Cmdlets.Policy.Runtime.JsonSerializable.ToJson(
                ((Microsoft.Azure.PowerShell.Cmdlets.Policy.Runtime.IAssociativeArray<global::System.Object>)this).AdditionalProperties,
                container);
            // Restore sentinel DateTime(1,1,1) markers back to null so repeated calls to ToJson()
            // and any post-serialization use of this object see the original null values.
            SerializationHelpers.RestoreSentinel(__additionalProperties);
        }
 
        // ── PowerShell partial-method implementations ────────────────────────────
 
        /// <summary>Loads the backing dictionary from a PowerShell <see cref="global::System.Collections.IDictionary" /> (e.g. Hashtable).</summary>
        partial void AfterDeserializeDictionary(global::System.Collections.IDictionary content)
        {
            CopyFrom(content);
        }
 
        /// <summary>Loads the backing dictionary from a <see cref="global::System.Management.Automation.PSObject" />.</summary>
        partial void AfterDeserializePSObject(global::System.Management.Automation.PSObject content)
        {
            CopyFrom(content);
        }
    }
}