using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CrazyStudio.Core.Common.Tools.Extensions
{
    public static class GuidExtension
    {
        /// <summary>
        /// 将object转换为Guid，若转换失败，则返回Guid.Empty。不抛出异常。
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static Guid ParseToGuid(this object obj)
        {
            try
            {
                return Guid.Parse(obj.ToString() ?? string.Empty);
            }
            catch
            {
                return Guid.Empty;
            }
        }

        public static bool IsNullOrGuidEmpty([NotNullWhen(false)] this object? value)
        {
            return value is null || value.ParseToGuid() == Guid.Empty;
        }
    }
}
