﻿[  
// ------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a source generator named Vogen (https://github.com/SteveDunn/Vogen)
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
// ------------------------------------------------------------------------------
// Suppress warnings about [Obsolete] member usage in generated code.
#pragma warning disable CS0618
// Suppress warnings for 'Override methods on comparable types'.
#pragma warning disable CA1036
// Suppress Error MA0097 : A class that implements IComparable<T> or IComparable should override comparison operators
#pragma warning disable MA0097
// Suppress warning for 'The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.'
// The generator copies signatures from the BCL, e.g. for `TryParse`, and some of those have nullable annotations.
#pragma warning disable CS8669, CS8632
// Suppress warnings about CS1591: Missing XML comment for publicly visible type or member 'Type_or_Member'
#pragma warning disable CS1591
namespace generator;
public class VogenTypesFactory : global::System.Text.Json.Serialization.JsonConverterFactory
{
    public VogenTypesFactory()
    {
    }

    private static readonly global::System.Collections.Generic.Dictionary<global::System.Type, global::System.Lazy<global::System.Text.Json.Serialization.JsonConverter>> _lookup = new global::System.Collections.Generic.Dictionary<global::System.Type, global::System.Lazy<global::System.Text.Json.Serialization.JsonConverter>>
    {
        {
            typeof(BugFix516Vo),
            new global::System.Lazy<global::System.Text.Json.Serialization.JsonConverter>(() => new BugFix516Vo.BugFix516VoSystemTextJsonConverter())
        }
    };
    public override bool CanConvert(global::System.Type typeToConvert) => _lookup.ContainsKey(typeToConvert);
    public override global::System.Text.Json.Serialization.JsonConverter CreateConverter(global::System.Type typeToConvert, global::System.Text.Json.JsonSerializerOptions options) => _lookup[typeToConvert].Value;
}
  
// ------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a source generator named Vogen (https://github.com/SteveDunn/Vogen)
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
// ------------------------------------------------------------------------------
// Suppress warnings about [Obsolete] member usage in generated code.
#pragma warning disable CS0618
// Suppress warnings for 'Override methods on comparable types'.
#pragma warning disable CA1036
// Suppress Error MA0097 : A class that implements IComparable<T> or IComparable should override comparison operators
#pragma warning disable MA0097
// Suppress warning for 'The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.'
// The generator copies signatures from the BCL, e.g. for `TryParse`, and some of those have nullable annotations.
#pragma warning disable CS8669, CS8632
// Suppress warnings about CS1591: Missing XML comment for publicly visible type or member 'Type_or_Member'
#pragma warning disable CS1591
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Vogen", "1.0.0.0")]
[global::System.Text.Json.Serialization.JsonConverter(typeof(BugFix516VoSystemTextJsonConverter))]
[global::System.ComponentModel.TypeConverter(typeof(BugFix516VoTypeConverter))]
[global::System.Diagnostics.DebuggerTypeProxyAttribute(typeof(BugFix516VoDebugView))]
[global::System.Diagnostics.DebuggerDisplayAttribute("Underlying type: System.String, Value = { _value }")]
public readonly partial record struct BugFix516Vo : global::System.IEquatable<BugFix516Vo>, global::System.IEquatable<System.String>, global::System.IComparable<BugFix516Vo>, global::System.IComparable, global::System.IParsable<BugFix516Vo>
{
#if DEBUG
private readonly global::System.Diagnostics.StackTrace _stackTrace = null!;
#endif
#if !VOGEN_NO_VALIDATION
    private readonly global::System.Boolean _isInitialized;
#endif
    private readonly System.String _value;
    /// <summary>
    /// Gets the underlying <see cref = "System.String"/> value if set, otherwise a <see cref = "Vogen.ValueObjectValidationException"/> is thrown.
    /// </summary>
    public readonly System.String Value
    {
        [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
        [global::System.Diagnostics.DebuggerStepThroughAttribute]
        get
        {
            EnsureInitialized();
            return _value;
        }

        [global::System.Diagnostics.DebuggerStepThroughAttribute]
        init
        {
            if (value is null)
            {
                ThrowHelper.ThrowWhenCreatedWithNull();
                return;
            }

            _value = value;
        }
    }

    [global::System.Diagnostics.DebuggerStepThroughAttribute]
    [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
    public BugFix516Vo()
    {
#if DEBUG
            _stackTrace = new global::System.Diagnostics.StackTrace();
#endif
#if !VOGEN_NO_VALIDATION
        _isInitialized = false;
#endif
        _value = default;
    }

    [global::System.Diagnostics.DebuggerStepThroughAttribute]
    private BugFix516Vo(System.String value)
    {
        _value = value;
#if !VOGEN_NO_VALIDATION
        _isInitialized = true;
#endif
    }

    /// <summary>
    /// Builds an instance from the provided underlying type.
    /// </summary>
    /// <param name = "value">The underlying type.</param>
    /// <returns>An instance of this type.</returns>
    [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
    public static BugFix516Vo From(System.String value)
    {
        return new BugFix516Vo(value);
    }

    /// <summary>
    /// Tries to build an instance from the provided underlying type.
    /// If a normalization method is provided, it will be called.
    /// If validation is provided, and it fails, false will be returned.
    /// </summary>
    /// <param name = "value">The underlying type.</param>
    /// <param name = "vo">An instance of the value object.</param>
    /// <returns>True if the value object can be built, otherwise false.</returns>
    
#pragma warning disable CS8767 // Nullability of reference types in type of parameter doesn't match implicitly implemented member because of nullability attributes.

    public static bool TryFrom(
#if NETCOREAPP3_0_OR_GREATER
[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
#endif
    System.String value, 
#if NETCOREAPP3_0_OR_GREATER
[global::System.Diagnostics.CodeAnalysis.MaybeNullWhen(false)]
#endif
    out BugFix516Vo vo)
#pragma warning restore CS8767 // Nullability of reference types in type of parameter doesn't match implicitly implemented member because of nullability attributes.

    {
        if (value is null)
        {
            vo = default;
            return false;
        }

        vo = new BugFix516Vo(value);
        return true;
    }

    /// <summary>
    /// Tries to build an instance from the provided underlying value.
    /// If a normalization method is provided, it will be called.
    /// If validation is provided, and it fails, an error will be returned.
    /// </summary>
    /// <param name = "value">The primitive value.</param>
    /// <returns>A <see cref = "Vogen.ValueObjectOrError{T}"/> containing either the value object, or an error.</returns>
    public static Vogen.ValueObjectOrError<BugFix516Vo> TryFrom(System.String value)
    {
        if (value is null)
        {
            return new Vogen.ValueObjectOrError<BugFix516Vo>(Vogen.Validation.Invalid("The value provided was null"));
        }

        return new Vogen.ValueObjectOrError<BugFix516Vo>(new BugFix516Vo(value));
    }

    [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#if VOGEN_NO_VALIDATION
#pragma warning disable CS8775
  public readonly bool IsInitialized() => true;
#pragma warning restore CS8775
#else
    public readonly bool IsInitialized() => _isInitialized;
#endif
    public static explicit operator BugFix516Vo(System.String value) => From(value);
    public static explicit operator System.String(BugFix516Vo value) => value.Value;
    // only called internally when something has been deserialized into
    // its primitive type.
    private static BugFix516Vo __Deserialize(System.String value)
    {
        return new BugFix516Vo(value);
    }

    public global::System.Boolean Equals(BugFix516Vo other, global::System.Collections.Generic.IEqualityComparer<BugFix516Vo> comparer)
    {
        return comparer.Equals(this, other);
    }

    public readonly global::System.Boolean Equals(System.String primitive)
    {
        return Value.Equals(primitive);
    }

    public readonly global::System.Boolean Equals(System.String primitive, global::System.StringComparer comparer)
    {
        return comparer.Equals(Value, primitive);
    }

    public static global::System.Boolean operator ==(BugFix516Vo left, System.String right) => left.Value.Equals(right);
    public static global::System.Boolean operator ==(System.String left, BugFix516Vo right) => right.Value.Equals(left);
    public static global::System.Boolean operator !=(System.String left, BugFix516Vo right) => !(left == right);
    public static global::System.Boolean operator !=(BugFix516Vo left, System.String right) => !(left == right);
    public int CompareTo(BugFix516Vo other) => Value.CompareTo(other.Value);
    public int CompareTo(object other)
    {
        if (other is null)
            return 1;
        if (other is BugFix516Vo x)
            return CompareTo(x);
        ThrowHelper.ThrowArgumentException("Cannot compare to object as it is not of type BugFix516Vo", nameof(other));
        return 0;
    }

    /// <summary>
    /// </summary>
    /// <returns>
    /// True if the value passes any validation (after running any optional normalization).
    /// </returns>
    public static global::System.Boolean TryParse(
#if NETCOREAPP3_0_OR_GREATER
[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
#endif
    global::System.String s, global::System.IFormatProvider provider, 
#if NETCOREAPP3_0_OR_GREATER
[global::System.Diagnostics.CodeAnalysis.MaybeNullWhen(false)]
#endif
    out BugFix516Vo result)
    {
        if (s is null)
        {
            result = default;
            return false;
        }

        result = new BugFix516Vo(s);
        return true;
    }

    /// <summary>
    /// </summary>
    /// <returns>
    /// The value created via the <see cref = "From(global::System.String)"/> method.
    /// </returns>
    /// <exception cref = "global::Vogen.ValueObjectValidationException">Thrown when the value can be parsed, but is not valid.</exception>
    public static BugFix516Vo Parse(global::System.String s, global::System.IFormatProvider provider)
    {
        return From(s);
    }

#nullable disable
#nullable restore
    [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
    private readonly void EnsureInitialized()
    {
        if (!IsInitialized())
        {
#if DEBUG
               ThrowHelper.ThrowWhenNotInitialized(_stackTrace);
#else
            ThrowHelper.ThrowWhenNotInitialized();
#endif
        }
    }

    // record enumerates fields - we just want our Value and to throw if it's not initialized.
    /// <inheritdoc cref = "System.String.ToString()"/>
    public override global::System.String ToString() => IsInitialized() ? Value.ToString() ?? "" : "[UNINITIALIZED]";
    /// <inheritdoc cref = "System.String.ToString(System.IFormatProvider? )"/>
    public global::System.String ToString(global::System.IFormatProvider provider) => IsInitialized() ? Value.ToString(provider) ?? "" : "[UNINITIALIZED]";
#nullable disable
    /// <summary>
    /// Converts a BugFix516Vo to or from JSON.
    /// </summary>
    public class BugFix516VoSystemTextJsonConverter : global::System.Text.Json.Serialization.JsonConverter<BugFix516Vo>
    {
        public override BugFix516Vo Read(ref global::System.Text.Json.Utf8JsonReader reader, global::System.Type typeToConvert, global::System.Text.Json.JsonSerializerOptions options)
        {
            return DeserializeJson(reader.GetString());
        }

        public override void Write(System.Text.Json.Utf8JsonWriter writer, BugFix516Vo value, global::System.Text.Json.JsonSerializerOptions options)
        {
            writer.WriteStringValue(value.Value);
        }

#if NET6_0_OR_GREATER
            public override BugFix516Vo ReadAsPropertyName(ref global::System.Text.Json.Utf8JsonReader reader, global::System.Type typeToConvert, global::System.Text.Json.JsonSerializerOptions options)
            {
                return DeserializeJson(reader.GetString());
            }

            public override void WriteAsPropertyName(System.Text.Json.Utf8JsonWriter writer, BugFix516Vo value, global::System.Text.Json.JsonSerializerOptions options)
            {
                writer.WritePropertyName(value.Value);
            }
#endif
#if NETCOREAPP3_0_OR_GREATER
            [global::System.Diagnostics.CodeAnalysis.DoesNotReturnAttribute]
#endif
        private static void ThrowJsonExceptionWhenValidationFails(Vogen.Validation validation)
        {
            var e = ThrowHelper.CreateValidationException(validation);
            throw new global::System.Text.Json.JsonException(null, e);
        }

        private static void ThrowJsonExceptionWhenNull(System.String value)
        {
            if (value == null)
            {
                var e = ThrowHelper.CreateCannotBeNullException();
                throw new global::System.Text.Json.JsonException(null, e);
            }
        }

        public static BugFix516Vo DeserializeJson(System.String value)
        {
            ThrowJsonExceptionWhenNull(value);
            return new BugFix516Vo(value);
        }
    }

#nullable restore
#nullable disable
    class BugFix516VoTypeConverter : global::System.ComponentModel.TypeConverter
    {
        public override global::System.Boolean CanConvertFrom(global::System.ComponentModel.ITypeDescriptorContext context, global::System.Type sourceType)
        {
            return sourceType == typeof(global::System.String) || base.CanConvertFrom(context, sourceType);
        }

        public override global::System.Object ConvertFrom(global::System.ComponentModel.ITypeDescriptorContext context, global::System.Globalization.CultureInfo culture, global::System.Object value)
        {
            var stringValue = value as global::System.String;
            if (stringValue is not null)
            {
                return BugFix516Vo.__Deserialize(stringValue);
            }

            return base.ConvertFrom(context, culture, value);
        }

        public override bool CanConvertTo(global::System.ComponentModel.ITypeDescriptorContext context, global::System.Type sourceType)
        {
            return sourceType == typeof(global::System.String) || base.CanConvertTo(context, sourceType);
        }

        public override object ConvertTo(global::System.ComponentModel.ITypeDescriptorContext context, global::System.Globalization.CultureInfo culture, global::System.Object value, global::System.Type destinationType)
        {
            if (value is BugFix516Vo idValue)
            {
                if (destinationType == typeof(global::System.String))
                {
                    return idValue.Value;
                }
            }

            return base.ConvertTo(context, culture, value, destinationType);
        }
    }

#nullable restore
#nullable disable
    internal sealed class BugFix516VoDebugView
    {
        private readonly BugFix516Vo _t;
        BugFix516VoDebugView(BugFix516Vo t)
        {
            _t = t;
        }

        public global::System.Boolean IsInitialized => _t.IsInitialized();
        public global::System.String UnderlyingType => "System.String";
        public global::System.String Value => _t.IsInitialized() ? _t._value.ToString() : "[not initialized]";
#if DEBUG
        public global::System.String CreatedWith => _t._stackTrace.ToString() ?? "the From method";
#endif
        public global::System.String Conversions => @"Default";
    }

#nullable restore
    static class ThrowHelper
    {
#if NETCOREAPP3_0_OR_GREATER
    [global::System.Diagnostics.CodeAnalysis.DoesNotReturnAttribute]
#endif
        internal static void ThrowInvalidOperationException(string message) => throw new global::System.InvalidOperationException(message);
#if NETCOREAPP3_0_OR_GREATER
    [global::System.Diagnostics.CodeAnalysis.DoesNotReturnAttribute]
#endif
        internal static void ThrowArgumentException(string message, string arg) => throw new global::System.ArgumentException(message, arg);
#if NETCOREAPP3_0_OR_GREATER
    [global::System.Diagnostics.CodeAnalysis.DoesNotReturnAttribute]
#endif
        internal static void ThrowWhenCreatedWithNull() => throw CreateCannotBeNullException();
#if NETCOREAPP3_0_OR_GREATER
    [global::System.Diagnostics.CodeAnalysis.DoesNotReturnAttribute]
#endif
        internal static void ThrowWhenNotInitialized() => throw CreateValidationException("Use of uninitialized Value Object.");
#if NETCOREAPP3_0_OR_GREATER
    [global::System.Diagnostics.CodeAnalysis.DoesNotReturnAttribute]
#endif
        internal static void ThrowWhenNotInitialized(global::System.Diagnostics.StackTrace stackTrace) => throw CreateValidationException("Use of uninitialized Value Object at: " + stackTrace ?? "");
#if NETCOREAPP3_0_OR_GREATER
    [global::System.Diagnostics.CodeAnalysis.DoesNotReturnAttribute]
#endif
        internal static void ThrowWhenValidationFails(Vogen.Validation validation)
        {
            throw CreateValidationException(validation);
        }

        internal static System.Exception CreateValidationException(string message) => new global::Vogen.ValueObjectValidationException(message);
        internal static System.Exception CreateCannotBeNullException() => new global::Vogen.ValueObjectValidationException("Cannot create a value object with null.");
        internal static System.Exception CreateValidationException(Vogen.Validation validation)
        {
            var ex = CreateValidationException(validation.ErrorMessage);
            if (validation.Data is not null)
            {
                foreach (var kvp in validation.Data)
                {
                    ex.Data[kvp.Key] = kvp.Value;
                }
            }

            return ex;
        }
    }
}
]