Skip to content

Commit

Permalink
1.0.0-beta-sun-build37 Version:
Browse files Browse the repository at this point in the history
      Fix issue 31: #31
      Fix issue 32: #32
      Fix issue 33: #33
      Fix issue 34: #34
      Fix issue 35: #35
  • Loading branch information
hherzl committed Jun 29, 2023
1 parent 4f39689 commit d70dbdd
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
24 changes: 23 additions & 1 deletion CatFactory.Tests/RecordDefinitionTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CatFactory.ObjectOrientedProgramming;
using System;
using CatFactory.ObjectOrientedProgramming;
using Xunit;

namespace CatFactory.Tests
Expand Down Expand Up @@ -91,5 +92,26 @@ public void TestConvertClassToRecordWithConvertOptions()
Assert.True(recordDefinition.Fields.Count == classDefinition.Fields.Count);
Assert.True(recordDefinition.Properties.Count == classDefinition.Properties.Count);
}

[Fact]
public void Test_RefactRecordDefinitionFromAnonymous()
{
// Arrange
var anonymousDefinition = new
{
Id = Guid.Empty,
Name = "",
Price = 0m,
ReleaseDate = DateTime.Now
};

// Act
var recordDefinition = anonymousDefinition.RefactRecordDefinition("StockItem");

// Assert
Assert.True(string.IsNullOrEmpty(recordDefinition.Namespace));
Assert.True(recordDefinition.Name == "StockItem");
Assert.True(recordDefinition.Properties.Count == 4);
}
}
}
1 change: 1 addition & 0 deletions CatFactory/CatFactory.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
Fix issue 32: https://github.com/hherzl/CatFactory/issues/32
Fix issue 33: https://github.com/hherzl/CatFactory/issues/33
Fix issue 34: https://github.com/hherzl/CatFactory/issues/34
Fix issue 35: https://github.com/hherzl/CatFactory/issues/35
</PackageReleaseNotes>
<PackageLicenseUrl>https://github.com/hherzl/CatFactory/blob/master/LICENSE</PackageLicenseUrl>
</PropertyGroup>
Expand Down
27 changes: 27 additions & 0 deletions CatFactory/ObjectOrientedProgramming/ObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,32 @@ public static ClassDefinition RefactClassDefinition(this object obj, string name

return classDefinition;
}

/// <summary>
/// Creates a new <see cref="RecordDefinition"/> from instance
/// </summary>
/// <param name="obj">Instance of model</param>
/// <param name="name">Name of <see cref="RecordDefinition"/></param>
/// <param name="ns">Namespace of <see cref="RecordDefinition"/></param>
/// <returns></returns>
public static RecordDefinition RefactRecordDefinition(this object obj, string name = null, string ns = null)
{
var sourceType = obj.GetType();

var recordDefinition = new RecordDefinition
{
Name = string.IsNullOrEmpty(name) ? sourceType.Name : name,
Namespace = string.IsNullOrEmpty(ns) ? string.Empty : ns
};

foreach (var property in sourceType.GetProperties().Where(item => item.CanRead))
{
var type = property.PropertyType.Name;

recordDefinition.AddAutomaticProperty(AccessModifier.Public, type, property.Name);
}

return recordDefinition;
}
}
}

0 comments on commit d70dbdd

Please sign in to comment.