Skip to content

Commit 7274f07

Browse files
committed
Merge LuaTable.GetValueOrDefault and allow nullable TValue
1 parent 60e4d5e commit 7274f07

File tree

2 files changed

+31
-20
lines changed

2 files changed

+31
-20
lines changed

src/Laylua.Tests/Tests/Library/Entities/Table/LuaTableTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,34 @@ public void SetAndGetValue_StringInt_ReturnsValidValue()
116116
Assert.That(value, Is.EqualTo(1));
117117
}
118118

119+
[Test]
120+
public void GetValueOrDefault_MissingNullableInt_ReturnsNull()
121+
{
122+
// Arrange
123+
using var table = Lua.CreateTable();
124+
125+
// Act
126+
table.SetValue("MissingValue", (int?) null);
127+
var missingValue = table.GetValueOrDefault<string, int?>("MissingValue");
128+
129+
// Assert
130+
Assert.That(missingValue, Is.Null);
131+
}
132+
133+
[Test]
134+
public void GetValueOrDefault_MissingNullableInt_DefaultValue_ReturnsDefaultValue()
135+
{
136+
// Arrange
137+
using var table = Lua.CreateTable();
138+
139+
// Act
140+
table.SetValue("MissingValue", (int?) null);
141+
var missingValue = table.GetValueOrDefault<string, int?>("MissingValue", 42);
142+
143+
// Assert
144+
Assert.That(missingValue, Is.EqualTo(42));
145+
}
146+
119147
[Test]
120148
public void Count_ReturnsValidCount()
121149
{

src/Laylua/Library/Entities/Reference/Table/LuaTable.cs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -348,24 +348,6 @@ public TValue GetValue<TKey, TValue>(TKey key)
348348
}
349349
}
350350

351-
/// <inheritdoc cref="LuaReference._reference"/>
352-
/// <summary>
353-
/// Gets a value with the given key in the table.
354-
/// </summary>
355-
/// <param name="key"> The key of the value to get. </param>
356-
/// <typeparam name="TKey"> The type of the key. </typeparam>
357-
/// <typeparam name="TValue"> The type of the value. </typeparam>
358-
/// <returns>
359-
/// The value from the table or <see langword="default"/>.
360-
/// </returns>
361-
[MethodImpl(MethodImplOptions.NoInlining)]
362-
public TValue? GetValueOrDefault<TKey, TValue>(TKey key)
363-
where TKey : notnull
364-
where TValue : notnull
365-
{
366-
return TryGetValue<TKey, TValue>(key, out var value) ? value : default;
367-
}
368-
369351
/// <inheritdoc cref="LuaReference._reference"/>
370352
/// <summary>
371353
/// Gets a value with the given key in the table.
@@ -378,11 +360,12 @@ public TValue GetValue<TKey, TValue>(TKey key)
378360
/// The value from the table or <paramref name="defaultValue"/>.
379361
/// </returns>
380362
[MethodImpl(MethodImplOptions.NoInlining)]
381-
public TValue? GetValueOrDefault<TKey, TValue>(TKey key, TValue defaultValue)
363+
public TValue? GetValueOrDefault<TKey, TValue>(TKey key, TValue? defaultValue = default)
382364
where TKey : notnull
383-
where TValue : notnull
384365
{
366+
#pragma warning disable CS8714 // The type cannot be used as type parameter in the generic type or method. Nullability of type argument doesn't match 'notnull' constraint.
385367
return TryGetValue<TKey, TValue>(key, out var value) ? value : defaultValue;
368+
#pragma warning restore CS8714 // The type cannot be used as type parameter in the generic type or method. Nullability of type argument doesn't match 'notnull' constraint.
386369
}
387370

388371
/// <summary>

0 commit comments

Comments
 (0)