Giter Club home page Giter Club logo

miscutilities's People

Contributors

candoumbe avatar renovate-bot avatar renovate[bot] avatar

Stargazers

 avatar  avatar

miscutilities's Issues

[BUG] ๐Ÿž `IsAssignableToGenericType` inheritance fails

Usage Information

Package version :
0.5.3

Relevant Code / Invocations**

public record MyId : StrongId<Guid>();

public record StrongId<T>();

bool actual  = typeof(MyId).IsAssignableToGenericType(typeof(StrongId<Guid>));

Expected Behavior

Expected actual to be true

[BUG] ๐Ÿž ToQueryString does not work properly when source type has `TypeConverter`

Usage Information

Package version : 0.5.1

Relevant Code / Invocations

public static class StronglyTypedIdHelper
    {
        private static readonly ConcurrentDictionary<Type, Delegate> StronglyTypedIdFactories = new();

        public static Func<TValue, object> GetFactory<TValue>(Type stronglyTypedIdType)
            where TValue : notnull
        {
            return (Func<TValue, object>)StronglyTypedIdFactories.GetOrAdd(
                stronglyTypedIdType,
                CreateFactory<TValue>);
        }

        private static Func<TValue, object> CreateFactory<TValue>(Type stronglyTypedIdType)
            where TValue : notnull
        {
            if (!IsStronglyTypedId(stronglyTypedIdType))
            {
                throw new ArgumentException($"Type '{stronglyTypedIdType}' is not a strongly-typed id type", nameof(stronglyTypedIdType));
            }

            System.Reflection.ConstructorInfo ctor = stronglyTypedIdType.GetConstructor(new[] { typeof(TValue) });
            if (ctor is null)
            {
                throw new ArgumentException($"Type '{stronglyTypedIdType}' doesn't have a constructor with one parameter of type '{typeof(TValue)}'", nameof(stronglyTypedIdType));
            }

            ParameterExpression param = Expression.Parameter(typeof(TValue), "value");
            NewExpression body = Expression.New(ctor, param);
            Expression<Func<TValue, object>> lambda = Expression.Lambda<Func<TValue, object>>(body, param);

            return lambda.Compile();
        }

        public static bool IsStronglyTypedId(Type type) => IsStronglyTypedId(type, out _);

        public static bool IsStronglyTypedId(Type type, [NotNullWhen(true)] out Type idType)
        {
            if (type is null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            bool isStronglyType = false;
            if (type.BaseType is Type baseType &&
                baseType.IsGenericType &&
                baseType.GetGenericTypeDefinition() == typeof(StronglyTypedId<>))
            {
                idType = baseType.GetGenericArguments()[0];
                isStronglyType = true;
            }
            else if (type.BaseType == typeof(StronglyTypedGuidId))
            {
                idType = typeof(Guid);
                isStronglyType = true;
            }
            else
            {
                idType = null;
            }

            return isStronglyType;
        }
    }

public class StronglyTypedIdConverter<TValue> : TypeConverter where TValue : notnull
    {
        private static readonly TypeConverter IdValueConverter = GetIdValueConverter();

        private static TypeConverter GetIdValueConverter()
        {
            TypeConverter converter = TypeDescriptor.GetConverter(typeof(TValue));
            if (!converter.CanConvertFrom(typeof(string)))
            {
                throw new InvalidOperationException(
                    $"Type '{typeof(TValue)}' doesn't have a converter that can convert from string");
            }

            return converter;
        }

        private readonly Type _type;
        public StronglyTypedIdConverter(Type type)
        {
            _type = type;
        }

        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return sourceType == typeof(string)
                || sourceType == typeof(TValue)
                || base.CanConvertFrom(context, sourceType);
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            return destinationType == typeof(string)
                || destinationType == typeof(TValue)
                || base.CanConvertTo(context, destinationType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value is string s)
            {
                value = IdValueConverter.ConvertFrom(s);
            }

            object result;

            if (value is TValue idValue)
            {
                Func<TValue, object> factory = StronglyTypedIdHelper.GetFactory<TValue>(_type);
                result = factory(idValue);
            }
            else
            {
                result = base.ConvertFrom(context, culture, value);
            }

            return result;
        }

        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (value is null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            StronglyTypedId<TValue> stronglyTypedId = (StronglyTypedId<TValue>)value;
            TValue idValue = stronglyTypedId.Value;

            object result;

            if (destinationType == typeof(string))
            {
                result = idValue.ToString()!;
            }
            else if (destinationType == typeof(TValue))
            {
                result = idValue;
            }
            else
            {
                result = base.ConvertTo(context, culture, value, destinationType);
            }

            return result;
        }
    }



[TypeConverter(typeof(StronglyTypedIdConverter))]
public abstract record StronglyTypedId<TValue>
        where TValue : notnull
    {
        public TValue Value { get; }

        protected StronglyTypedId(TValue value) => Value = value;

        public override string ToString() => Value.ToString();
    }

public record Identifier : StronglyTypedId<Guid>(Value)
{
    public static Identifier New() => new(Guid.NewGuid());
}

and a RouteValueDictionary instance as follow

RouteValueDictionary routeValues = new() {
    ["simpleId"] = "abc",
    ["strongId"] = Identifier.New()
};

routesValue.ToQueryString() // "simpleId=abc"

Expected Behavior

Given that there is a TypeConverterthat can convert to a primitive type, the consumer of the library would expect the output to be simpleId=abc&strongId=<value of the guid>

Stacktrace / Exception

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Edited/Blocked

These updates have been manually edited so Renovate will no longer make changes. To discard all commits and start over, click on a checkbox.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/delivery.yml
  • actions/checkout v3
  • actions/cache v3
  • actions/upload-artifact v3
  • actions/upload-artifact v3
  • actions/upload-artifact v3
  • actions/upload-artifact v3
.github/workflows/integration.yml
  • actions/checkout v3
  • actions/cache v3
  • actions/upload-artifact v3
  • actions/upload-artifact v3
  • actions/upload-artifact v3
  • actions/upload-artifact v3
nuget
.config/dotnet-tools.json
  • nuke.globaltool 7.0.1
  • dotnet-stryker 3.9.0
build/_build.csproj
  • ReportGenerator 5.1.9
  • Candoumbe.Pipelines 0.3.0
  • Nuke.Common 6.3.0
core.props
  • Microsoft.SourceLink.GitHub 1.1.1
  • Microsoft.SourceLink.AzureRepos.Git 1.1.1
global.json
  • dotnet-sdk 7.0.302
src/Candoumbe.MiscUtilities/Candoumbe.MiscUtilities.csproj
  • Microsoft.Extensions.Primitives 7.0.0
  • System.ValueTuple 4.5.0
  • System.Text.Json 7.0.2
  • System.Text.Json 7.0.2
  • Newtonsoft.Json 13.0.3
tests.props
  • FsCheck.Xunit 3.0.0-beta2
  • FluentAssertions.Json 6.1.0
  • FluentAssertions 6.11.0
  • xunit.categories 2.0.6
  • xunit.runner.visualstudio 2.4.5
  • xunit 2.4.2
  • Bogus 34.0.2

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.