Skip to content

Commit

Permalink
Implement circular reference handling. (#4)
Browse files Browse the repository at this point in the history
* Implements circular reference handler.

* Update project files add assets.
  • Loading branch information
manusoft authored Dec 28, 2024
1 parent 27d40eb commit 139679d
Show file tree
Hide file tree
Showing 7 changed files with 317 additions and 214 deletions.
37 changes: 0 additions & 37 deletions src/Mappy.Test/ObjectMapperPrivatePropertiesTest.cs

This file was deleted.

72 changes: 72 additions & 0 deletions src/Mappy.Test/ObjectMapperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,47 @@ public void Map_Performance_ShouldBeWithinExpectedLimits()
// Assert
Assert.True(duration < 1000, "Mapping took longer than expected");
}

[Fact]
public void Map_ShouldMapPublicAndPrivateProperties()
{
// Arrange
var source = new PrivateSourceClass(10, "secret");

// Act
var destination = source.Map<PrivateDestinationClass>();

// Assert
Assert.Equal(source.PublicProperty, destination.PublicProperty);
Assert.Equal(source.GetType().GetProperty("PrivateProperty", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
.GetValue(source), destination.GetType().GetProperty("PrivateProperty").GetValue(destination));
}

[Fact]
public void Map_CircularReferenceInCollection_ShouldHandleGracefully()
{
// Arrange
var circularSource = new CircularSource
{
Name = "Root",
Children = new List<CircularSource>
{
new CircularSource { Name = "Child1" },
new CircularSource { Name = "Child2" }
}
};
circularSource.Children.Add(circularSource); // Circular reference

// Act
var result = circularSource.Map<CircularDestination>();

// Assert
Assert.NotNull(result);
Assert.Equal("Root", result.Name);
Assert.NotNull(result.Children);
Assert.Equal(3, result.Children.Count);
Assert.Null(result.Children[2]); // Circular reference should map to null
}
}

public class Source
Expand All @@ -163,4 +204,35 @@ public class InvalidDestination
{
public DateTime Id { get; set; } // Different type from Source.Id
public string Name { get; set; }
}

public class PrivateSourceClass
{
public int PublicProperty { get; set; }
private string PrivateProperty { get; set; }

public PrivateSourceClass(int publicValue, string privateValue)
{
PublicProperty = publicValue;
PrivateProperty = privateValue;
}
}

public class PrivateDestinationClass
{
public int PublicProperty { get; set; }
public string PrivateProperty { get; set; }
}

public class CircularSource
{
public string Name { get; set; }
public List<CircularSource> Children { get; set; } = new List<CircularSource>();
}


public class CircularDestination
{
public string Name { get; set; }
public List<CircularDestination> Children { get; set; } = new List<CircularDestination>();
}
21 changes: 21 additions & 0 deletions src/Mappy/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 ManuHub

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
24 changes: 17 additions & 7 deletions src/Mappy/Mappy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,28 @@
<TargetFrameworks>net9.0;net8.0;net6.0;</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<NoWarn>CS8600,CS8602,CS8603,CS8604,CS8625</NoWarn>
<AssemblyName>Mappy</AssemblyName>
<RootNamespace>Mappy</RootNamespace>
<PackageId>Mappy.dotNet</PackageId>
<Description>Mappy is a lightweight object mapping utility for C# applications.</Description>
<Copyright>Copyright (c) 2024 ManuHub</Copyright>
<Authors>Manojbabu</Authors>
<PackageId>Mappy.dotNet</PackageId>
<PackageTags>Mappy;Mapper;AutoMapper;Fast;Mapping</PackageTags>
<Copyright>Copyright © 2025 ManuHub</Copyright>
<Authors>Manojbabu</Authors>
<PackageTags>Mappy;Mapper;AutoMapper;Mapster;Fast;Object;Mapping</PackageTags>
<PackageProjectUrl>https://github.com/manusoft/Mappy</PackageProjectUrl>
<RepositoryUrl>https://github.com/manusoft/Mappy</RepositoryUrl>
<PackageLicenseUrl></PackageLicenseUrl>
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
<Icon>icon.png</Icon>
<PackageIcon>icon.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
<Version>2.1.0</Version>
<IsPackable>true</IsPackable>
<RootNamespace>Mappy</RootNamespace>
<Version>2.0.1</Version>
</PropertyGroup>

<ItemGroup>
<Content Include="icon.png" Pack="true" PackagePath="" />
<Content Include="README.md" Pack="true" PackagePath="" />
<Content Include="LICENSE.md" Pack="true" PackagePath="" />
</ItemGroup>

</Project>
Loading

0 comments on commit 139679d

Please sign in to comment.