Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 9bd0cc3

Browse files
committed
Add PopulateFromPropertiesWithoutAttribute to AutoMapping utils
1 parent cde2546 commit 9bd0cc3

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/ServiceStack.Text/AutoMappingUtils.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,18 @@ public static To PopulateFromPropertiesWithAttribute<To, From>(this To to, From
286286
return to;
287287
}
288288

289+
public static To PopulateFromPropertiesWithoutAttribute<To, From>(this To to, From from,
290+
Type attributeType)
291+
{
292+
if (Equals(to, default(To)) || Equals(from, default(From))) return default(To);
293+
294+
var assignmentDefinition = GetAssignmentDefinition(to.GetType(), from.GetType());
295+
296+
assignmentDefinition.PopulateFromPropertiesWithoutAttribute(to, from, attributeType);
297+
298+
return to;
299+
}
300+
289301
public static void SetProperty(this PropertyInfo propertyInfo, object obj, object value)
290302
{
291303
if (!propertyInfo.CanWrite)
@@ -593,6 +605,13 @@ public void PopulateFromPropertiesWithAttribute(object to, object from, Type att
593605
Populate(to, from, hasAttributePredicate, null);
594606
}
595607

608+
public void PopulateFromPropertiesWithoutAttribute(object to, object from, Type attributeType)
609+
{
610+
var hasAttributePredicate = (Func<PropertyInfo, bool>)
611+
(x => x.AllAttributes(attributeType).Length == 0);
612+
Populate(to, from, hasAttributePredicate, null);
613+
}
614+
596615
public void PopulateWithNonDefaultValues(object to, object from)
597616
{
598617
var nonDefaultPredicate = (Func<object, Type, bool>)((x, t) =>

tests/ServiceStack.Text.Tests/AutoMappingTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,17 @@ public class ModelWithHashSet
143143
public HashSet<User> Collection { get; set; }
144144
}
145145

146+
public class ModelWithIgnoredFields
147+
{
148+
public int Id { get; set; }
149+
public string Name { get; set; }
150+
151+
[ReadOnly]
152+
public int Ignored { get; set; }
153+
}
154+
155+
public class ReadOnlyAttribute : AttributeBase {}
156+
146157
[TestFixture]
147158
public class AutoMappingTests
148159
{
@@ -509,5 +520,24 @@ public void Does_retain_null_properties()
509520

510521
Assert.That(dto.CompanyInfo, Is.Null);
511522
}
523+
524+
[Test]
525+
public void Does_ignore_properties_without_attributes()
526+
{
527+
var model = new ModelWithIgnoredFields
528+
{
529+
Id = 1,
530+
Name = "Foo",
531+
Ignored = 2
532+
};
533+
534+
var dto = new ModelWithIgnoredFields { Ignored = 10 }
535+
.PopulateFromPropertiesWithoutAttribute(model, typeof(ReadOnlyAttribute));
536+
537+
Assert.That(dto.Id, Is.EqualTo(model.Id));
538+
Assert.That(dto.Name, Is.EqualTo(model.Name));
539+
Assert.That(dto.Ignored, Is.EqualTo(10));
540+
}
541+
512542
}
513543
}

0 commit comments

Comments
 (0)