Skip to content

Commit 7aef0ad

Browse files
authored
Merge pull request #1156 from progressonderwijs/fons/pp_try_get
Added PocoProperties<T>.TryGetByName method to allow fluent lookup of external provided names.
2 parents f4e2b30 + 3fd4d38 commit 7aef0ad

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

src/ProgressOnderwijsUtils/Pocos/PocoProperties.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ public Type PocoType
3535
IndexByName = dictionary;
3636
}
3737

38+
public bool TryGetByName(string name, [NotNullWhen(true)] out IPocoProperty<T>? property)
39+
{
40+
property = IndexByName.TryGetValue(name, out var index) ? Properties[index] : null;
41+
return property != null;
42+
}
43+
3844
public IPocoProperty<T> GetByName(string name)
3945
=> Properties[IndexByName[name]];
4046

src/ProgressOnderwijsUtils/ProgressOnderwijsUtils.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<Import Project="..\NugetPackagesCommon.props" />
33
<PropertyGroup Label="Configuration">
4-
<Version>105.6.0</Version>
5-
<PackageReleaseNotes>HTML DSL: better support for https://html.spec.whatwg.org/#boolean-attribute </PackageReleaseNotes>
4+
<Version>105.7.0</Version>
5+
<PackageReleaseNotes>Added PocoProperties<T>.TryGetByName method to allow fluent lookup of external provided names.</T></PackageReleaseNotes>
66
<Title>ProgressOnderwijsUtils</Title>
77
<Description>Collection of utilities developed by ProgressOnderwijs</Description>
88
<PackageTags>ProgressOnderwijs</PackageTags>

test/ProgressOnderwijsUtils.Tests/PocoUtilsTest.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,21 @@ public void CanSetAndGet()
108108
PAssert.That(() => o.Equals(new() { Property = "aha", LabelledProperty = "really", }));
109109
}
110110

111+
[Fact]
112+
public void CanTryGet()
113+
{
114+
var o = new SimpleObject { LabelledProperty = "bar", };
115+
var moDef = PocoUtils.GetProperties<SimpleObject>();
116+
117+
var existing = moDef.TryGetByName(nameof(SimpleObject.LabelledProperty), out var prop);
118+
PAssert.That(() => existing);
119+
PAssert.That(() => (string?)prop.AssertNotNull().Getter.AssertNotNull()(o) == "bar");
120+
121+
existing = moDef.TryGetByName("NonExisting", out var noprop);
122+
PAssert.That(() => !existing);
123+
PAssert.That(() => noprop == null);
124+
}
125+
111126
[Fact]
112127
public void CanGetByExpression()
113128
{

0 commit comments

Comments
 (0)