Skip to content

Commit

Permalink
IGNITE-16355 .NET: Support value types in the Table API (#1190)
Browse files Browse the repository at this point in the history
* Remove `where T : class` constraint from all APIs.
* Use `Option<T>` as a return type for APIs which can return "no value" results, such as `Get`, `GetAndUpsert`, `GetAndDelete`.
    * It is not possible to handle nullable value and reference types in a generic way, so we have to use a common wrapper. `Option<T>` also clearly indicates which APIs always return something and which don't.
* Fix `ObjectSerializerHandler` to support value types.
  • Loading branch information
ptupitsyn committed Oct 12, 2022
1 parent 719904f commit c4c6821
Show file tree
Hide file tree
Showing 24 changed files with 471 additions and 219 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,13 @@ public async Task TestExecuteColocated(long key, string nodeName)
var keyPoco = new Poco { Key = key };
var resNodeName2 = await Client.Compute.ExecuteColocatedAsync<string, Poco>(TableName, keyPoco, NodeNameJob);

var keyPocoStruct = new PocoStruct(key, null);
var resNodeName3 = await Client.Compute.ExecuteColocatedAsync<string, PocoStruct>(TableName, keyPocoStruct, NodeNameJob);

var expectedNodeName = PlatformTestNodeRunner + nodeName;
Assert.AreEqual(expectedNodeName, resNodeName);
Assert.AreEqual(expectedNodeName, resNodeName2);
Assert.AreEqual(expectedNodeName, resNodeName3);
}

[Test]
Expand Down
105 changes: 105 additions & 0 deletions modules/platforms/dotnet/Apache.Ignite.Tests/OptionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Apache.Ignite.Tests;

using System;
using NUnit.Framework;

/// <summary>
/// Tests for <see cref="Option{T}"/>.
/// </summary>
public sealed class OptionTests
{
[Test]
public void TestDefaultValueEqualsNone()
{
Assert.AreEqual(default(Option<int>), Option.None<int>());
Assert.IsTrue(Option.None<int>() == default);

Assert.AreEqual(default(Option<string>), Option.None<string>());
Assert.IsTrue(Option.None<string>() == default);
}

[Test]
public void TestNoneValueThrows()
{
var ex = Assert.Throws<InvalidOperationException>(() =>
{
_ = Option.None<int>().Value;
});

Assert.AreEqual("Value is not present. Check HasValue property before accessing Value.", ex!.Message);
}

[Test]
public void TestEquality()
{
Assert.AreEqual(Option.Some(123), (Option<int>)123);
Assert.AreNotEqual(Option.Some(123), Option.Some(124));
}

[Test]
public void TestSomeReferenceTypeDeconstruct()
{
var (val, hasVal) = Option.Some("abc");

Assert.IsTrue(hasVal);
Assert.AreEqual("abc", val);
}

[Test]
public void TestNoneReferenceTypeDeconstruct()
{
var (val, hasVal) = Option.None<string>();

Assert.IsFalse(hasVal);
Assert.IsNull(val);
}

[Test]
public void TestSomeValueTypeDeconstruct()
{
var (val, hasVal) = Option.Some(123L);

Assert.IsTrue(hasVal);
Assert.AreEqual(123L, val);
}

[Test]
public void TestNoneValueTypeDeconstruct()
{
var (val, hasVal) = Option.None<long>();

Assert.IsFalse(hasVal);
Assert.AreEqual(0L, val);
}

[Test]
public void TestNoneToString()
{
Assert.AreEqual("Option { HasValue = False }", Option.None<int>().ToString());
Assert.AreEqual("Option { HasValue = False }", Option.None<string>().ToString());
}

[Test]
public void TestSomeToString()
{
Assert.AreEqual("Option { HasValue = True, Value = 123 }", Option.Some(123).ToString());
Assert.AreEqual("Option { HasValue = True, Value = Foo }", Option.Some("Foo").ToString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public async Task TestPutSqlGetKv()
var table = await Client.Tables.GetTableAsync("TEST");
var res = await table!.RecordBinaryView.GetAsync(null, new IgniteTuple { ["ID"] = 1 });

Assert.AreEqual("s-1", res!["VAL"]);
Assert.AreEqual("s-1", res.Value["VAL"]);
}

[Test]
Expand Down
23 changes: 23 additions & 0 deletions modules/platforms/dotnet/Apache.Ignite.Tests/Table/PocoStruct.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Apache.Ignite.Tests.Table;

/// <summary>
/// Test user struct.
/// </summary>
public record struct PocoStruct(long Key, string? Val, string? UnmappedStr = null);
Loading

0 comments on commit c4c6821

Please sign in to comment.