jonwagner / Insight.Database

Fast, lightweight .NET micro-ORM
Other
861 stars 145 forks source link

TypeConverterGenerator parent to child conversion attempt missing #486

Closed mcmikecreations closed 1 year ago

mcmikecreations commented 2 years ago

Describe the bug

I encountered this issue while trying to work with PostGIS Point. If a class is defined with a field or property of type Point and postgres returns Geometry of type Point, the conversion here can't perform the cast and an exception is thrown.

        private class DynamicGeometryObject : IComparable<DynamicGeometryObject>
        {
            public int GID { get; }
            public string Title { get; }
            public Point Geom { get; } // Replacing with Geometry Geom works
            public DynamicGeometryObject(int gid, string title, Geometry geom)
            {
                GID = gid;
                Title = title;
                Geom = (Point)geom;
            }
            public override string ToString() => $"DynamicGeometryObject(GID={GID},Title={Title},Geom={Geom.ToText()})";

            public int CompareTo(DynamicGeometryObject? other)
            {
                if (other == null) return 1;
                return GID.CompareTo(other.GID);
            }
        }

CREATE TABLE IF NOT EXISTS public.dynamicgeometryobject
(
    gid integer NOT NULL,
    title text COLLATE pg_catalog."default" NOT NULL,
    geom geometry(Point,26918),
    CONSTRAINT dynamicgeometryobject_pk PRIMARY KEY (gid)
)

If I replace the property type with Geometry everything works. If I then do myObject.Geom.GetType() it returns Point instead of Geometry, so the deserializer works correctly, but the IL generation fails to create a cast. An ideal solution would be to generate IL code that tries to perform a cast if the targetType is a child of the sourceType in EmitConversion.

Steps to reproduce

  1. Create a DB table and a class from the code above
  2. Fill the table with some data
  3. Read it using the default settings
  4. Observe exception here

Expected behavior

I expect to have an auto-conversion between Geometry and Point/Polygon/etc. but this also applies to other similar cases.

jonwagner commented 1 year ago

If you're getting that exception, we've tried lots of different conversion methods, including direct casts, IConvertible, looking for implicit and explicit conversion operators, constructors on the target type.

Can you post the exception and stack trace? It's not clear to me exactly what conversion it's attempting.

Or better yet, submit a PR with a failing test case so we can just debug an fix.

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.