Skip to content

Commit e02677e

Browse files
committed
Add non-generic TryGetValue
1 parent b951525 commit e02677e

File tree

3 files changed

+86
-4
lines changed

3 files changed

+86
-4
lines changed

Source/Common/JsonLdObject.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ public class JsonLdObject : IEquatable<JsonLdObject>
5454
/// <returns><see langword="true"/> when the value has successfully been set; otherwise, <see langword="false"/>.</returns>
5555
public virtual bool TrySetValue(string property, IEnumerable<object> value) => false;
5656

57+
/// <summary>
58+
/// Attempts to retrieve the value for <paramref name="property"/>.
59+
/// </summary>
60+
/// <param name="property">The property to get the value from.</param>
61+
/// <param name="result">The value on the property.</param>
62+
/// <returns><see langword="true"/> when the value has successfully been retrieved; otherwise, <see langword="false"/>.</returns>
63+
public virtual bool TryGetValue(string property, out IValues? result)
64+
{
65+
result = default;
66+
return false;
67+
}
68+
5769
/// <summary>
5870
/// Attempts to retrieve the <typeparamref name="TValue"/> from <paramref name="property"/>.
5971
/// </summary>

Tests/Schema.NET.Test/ThingTest.cs

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,12 +233,59 @@ public void TryGetValue_ValidProperty_OneOrMany()
233233
Name = "TestName",
234234
};
235235

236+
Assert.True(thing.TryGetValue("Name", out var result));
237+
var name = Assert.Single(result);
238+
Assert.Equal("TestName", name);
239+
}
240+
241+
[Fact]
242+
public void TryGetValue_ValidProperty_Values()
243+
{
244+
var thing = new Thing
245+
{
246+
Identifier = new Uri("https://example.org/test-identifier"),
247+
};
248+
249+
Assert.True(thing.TryGetValue("Identifier", out var result));
250+
var identifier = Assert.Single(result);
251+
Assert.Equal(new Uri("https://example.org/test-identifier"), identifier);
252+
}
253+
254+
[Fact]
255+
public void TryGetValue_InvalidProperty_InvalidName()
256+
{
257+
var thing = new Thing();
258+
259+
Assert.False(thing.TryGetValue("InvalidName", out _));
260+
}
261+
262+
[Fact]
263+
public void TryGetValue_CaseInsensitive()
264+
{
265+
var thing = new Thing
266+
{
267+
Name = "TestName",
268+
};
269+
270+
Assert.True(thing.TryGetValue("name", out var result));
271+
var name = Assert.Single(result);
272+
Assert.Equal("TestName", name);
273+
}
274+
275+
[Fact]
276+
public void TryGetValue_Generic_ValidProperty_OneOrMany()
277+
{
278+
var thing = new Thing
279+
{
280+
Name = "TestName",
281+
};
282+
236283
Assert.True(thing.TryGetValue<string>("Name", out var result));
237284
Assert.Equal("TestName", result);
238285
}
239286

240287
[Fact]
241-
public void TryGetValue_ValidProperty_Values()
288+
public void TryGetValue_Generic_ValidProperty_Values()
242289
{
243290
var thing = new Thing
244291
{
@@ -251,15 +298,15 @@ public void TryGetValue_ValidProperty_Values()
251298
}
252299

253300
[Fact]
254-
public void TryGetValue_InvalidProperty_InvalidName()
301+
public void TryGetValue_Generic_InvalidProperty_InvalidName()
255302
{
256303
var thing = new Thing();
257304

258305
Assert.False(thing.TryGetValue<string>("InvalidName", out _));
259306
}
260307

261308
[Fact]
262-
public void TryGetValue_InvalidProperty_InvalidType()
309+
public void TryGetValue_Generic_InvalidProperty_InvalidType()
263310
{
264311
var thing = new Thing
265312
{
@@ -270,7 +317,7 @@ public void TryGetValue_InvalidProperty_InvalidType()
270317
}
271318

272319
[Fact]
273-
public void TryGetValue_CaseInsensitive()
320+
public void TryGetValue_Generic_CaseInsensitive()
274321
{
275322
var thing = new Thing
276323
{

Tools/Schema.NET.Tool/SchemaSourceGenerator.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,29 @@ public override bool TryGetValue<TValue>(string property, out OneOrMany<TValue>
179179
return success;
180180
}}
181181

182+
/// <inheritdoc/>
183+
public override bool TryGetValue(string property, out IValues result)
184+
{{
185+
if (string.IsNullOrWhiteSpace(property))
186+
{{
187+
result = default;
188+
return false;
189+
}}
190+
191+
var success = false;
192+
{SourceUtility.RenderItems(allProperties, property => $@"if (""{property.Name}"".Equals(property, StringComparison.OrdinalIgnoreCase))
193+
{{
194+
result = (IValues)this.{property.Name};
195+
success = true;
196+
}}
197+
else ")}
198+
{{
199+
success = base.TryGetValue(property, out result);
200+
}}
201+
202+
return success;
203+
}}
204+
182205
/// <inheritdoc/>
183206
public bool Equals({schemaClass.Name} other)
184207
{{

0 commit comments

Comments
 (0)