Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

c# Nullable to YQL Option #14

Merged
merged 9 commits into from
Aug 15, 2023
83 changes: 83 additions & 0 deletions src/Ydb.Sdk/src/Value/YdbValueBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,5 +300,88 @@ private static void EnsurePrimitiveTypeId(YdbTypeId typeId)
throw new ArgumentException($"Complex types aren't supported in current method: {typeId}", "typeId");
}
}


private static YdbValue MakeOptionalOf<T>(T? value, YdbTypeId type, Func<T, YdbValue> func) where T : struct
{
return value.HasValue
? MakeOptional(func(value ?? default))
XmasApple marked this conversation as resolved.
Show resolved Hide resolved
: MakeEmptyOptional(type);
}

public static YdbValue MakeOptionalBool(bool? value)
{
return MakeOptionalOf(value, YdbTypeId.Bool, MakeBool);
}

public static YdbValue MakeOptionalInt8(sbyte? value)
{
return MakeOptionalOf(value, YdbTypeId.Int8, MakeInt8);
}

public static YdbValue MakeOptionalUint8(byte? value)
{
return MakeOptionalOf(value, YdbTypeId.Uint8, MakeUint8);
}

public static YdbValue MakeOptionalInt16(short? value)
{
return MakeOptionalOf(value, YdbTypeId.Int16, MakeInt16);
}

public static YdbValue MakeOptionalUint16(ushort? value)
{
return MakeOptionalOf(value, YdbTypeId.Uint16, MakeUint16);
}

public static YdbValue MakeOptionalInt32(int? value)
{
return MakeOptionalOf(value, YdbTypeId.Int32, MakeInt32);
}

public static YdbValue MakeOptionalUint32(uint? value)
{
return MakeOptionalOf(value, YdbTypeId.Uint32, MakeUint32);
}

public static YdbValue MakeOptionalInt64(long? value)
{
return MakeOptionalOf(value, YdbTypeId.Int64, MakeInt64);
}

public static YdbValue MakeOptionalUint64(ulong? value)
{
return MakeOptionalOf(value, YdbTypeId.Uint64, MakeUint64);
}

public static YdbValue MakeOptionalFloat(float? value)
{
return MakeOptionalOf(value, YdbTypeId.Float, MakeFloat);
}

public static YdbValue MakeOptionalDouble(double? value)
{
return MakeOptionalOf(value, YdbTypeId.Double, MakeDouble);
}

public static YdbValue MakeOptionalDate(DateTime? value)
{
return MakeOptionalOf(value, YdbTypeId.Date, MakeDate);
}

public static YdbValue MakeOptionalDatetime(DateTime? value)
{
return MakeOptionalOf(value, YdbTypeId.Datetime, MakeDatetime);
}

public static YdbValue MakeOptionalTimestamp(DateTime? value)
{
return MakeOptionalOf(value, YdbTypeId.Timestamp, MakeTimestamp);
}

public static YdbValue MakeOptionalInterval(TimeSpan? value)
{
return MakeOptionalOf(value, YdbTypeId.Interval, MakeInterval);
}
}
}
131 changes: 131 additions & 0 deletions src/Ydb.Sdk/src/Value/YdbValueCast.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,5 +187,136 @@ private static object GetObject(YdbValue value, System.Type targetType)

}
}

public static explicit operator YdbValue(bool value)
{
return MakeBool(value);
}

public static explicit operator YdbValue(bool? value)
{
return MakeOptionalBool(value);
}

public static explicit operator YdbValue(sbyte value)
{
return MakeInt8(value);
}

public static explicit operator YdbValue(sbyte? value)
{
return MakeOptionalInt8(value);
}

public static explicit operator YdbValue(byte value)
{
return MakeUint8(value);
}

public static explicit operator YdbValue(byte? value)
{
return MakeOptionalUint8(value);
}

public static explicit operator YdbValue(short value)
{
return MakeInt16(value);
}

public static explicit operator YdbValue(short? value)
{
return MakeOptionalInt16(value);
}

public static explicit operator YdbValue(ushort value)
{
return MakeUint16(value);
}

public static explicit operator YdbValue(ushort? value)
{
return MakeOptionalUint16(value);
}

public static explicit operator YdbValue(int value)
{
return MakeInt32(value);
}

public static explicit operator YdbValue(int? value)
{
return MakeOptionalInt32(value);
}

public static explicit operator YdbValue(uint value)
{
return MakeUint32(value);
}

public static explicit operator YdbValue(uint? value)
{
return MakeOptionalUint32(value);
}

public static explicit operator YdbValue(long value)
{
return MakeInt64(value);
}

public static explicit operator YdbValue(long? value)
{
return MakeOptionalInt64(value);
}

public static explicit operator YdbValue(ulong value)
{
return MakeUint64(value);
}

public static explicit operator YdbValue(ulong? value)
{
return MakeOptionalUint64(value);
}

public static explicit operator YdbValue(float value)
{
return MakeFloat(value);
}

public static explicit operator YdbValue(float? value)
{
return MakeOptionalFloat(value);
}

public static explicit operator YdbValue(double value)
{
return MakeDouble(value);
}

public static explicit operator YdbValue(double? value)
{
return MakeOptionalDouble(value);
}


public static explicit operator YdbValue(DateTime value)
XmasApple marked this conversation as resolved.
Show resolved Hide resolved
{
return MakeDate(value);
}

public static explicit operator YdbValue(DateTime? value)
{
return MakeOptionalDate(value);
}

public static explicit operator YdbValue(TimeSpan value)
{
return MakeInterval(value);
}

public static explicit operator YdbValue(TimeSpan? value)
{
return MakeOptionalInterval(value);
}
}
}
6 changes: 6 additions & 0 deletions src/Ydb.Sdk/src/Value/YdbValueParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ public string GetJsonDocument()
return _protoValue.TextValue;
}

public bool? GetOptionalBool()
{
return GetOptional()?.GetBool();
}


public sbyte? GetOptionalInt8()
{
return GetOptional()?.GetInt8();
Expand Down
70 changes: 66 additions & 4 deletions src/Ydb.Sdk/tests/Value/TestBasic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ public void PrimitiveTypes()
Assert.Equal("{type=\"yson\"}", Encoding.ASCII.GetString(elements[16].GetYson()));
Assert.Equal("{\"type\": \"json\"}", elements[17].GetJson());
Assert.Equal("{\"type\": \"jsondoc\"}", elements[18].GetJsonDocument());
Assert.Equal(true, elements[19].GetBool());
Assert.Equal(false, elements[20].GetBool());
Assert.True(elements[19].GetBool());
Assert.False(elements[20].GetBool());

Assert.Equal(-1, (sbyte)elements[0]);
Assert.Equal(200u, (byte)elements[1]);
Expand All @@ -91,8 +91,70 @@ public void PrimitiveTypes()
Assert.Equal("{type=\"yson\"}", Encoding.ASCII.GetString((byte[])elements[16]!));
Assert.Equal("{\"type\": \"json\"}", (string)elements[17]!);
Assert.Equal("{\"type\": \"jsondoc\"}", (string)elements[18]!);
Assert.Equal(true, (bool)elements[19]);
Assert.Equal(false, (bool)elements[20]);
Assert.True((bool)elements[19]);
Assert.False((bool)elements[20]);
}

[Fact]
public void OptimalPrimitiveTypes()
{
var value = YdbValue.MakeTuple(new YdbValue[] {
(YdbValue)(sbyte?)-1,
(YdbValue)(byte?)200,
(YdbValue)(short?)-200,
(YdbValue)(ushort?)40_000,
(YdbValue)(int?)-40000,
(YdbValue)(uint?)4_000_000_000,
(YdbValue)(long?)-4_000_000_000,
(YdbValue)(ulong?)10_000_000_000,
(YdbValue)(float?)-1.7f,
(YdbValue)(double?)123.45,
YdbValue.MakeOptionalDate(new DateTime(2021, 08, 21)),
YdbValue.MakeOptionalDatetime(new DateTime(2021, 08, 21, 23, 30, 47)),
YdbValue.MakeOptionalTimestamp(new DateTime(2021, 08, 21, 23, 30, 47, 581)),
YdbValue.MakeOptionalInterval(-new TimeSpan(3, 7, 40, 27, 729)),
(YdbValue)(bool?)true,
(YdbValue)(bool?)false,
});

_output.WriteLine(value.ToString());

var elements = value.GetTuple();
Assert.Equal(16, elements.Count);

Assert.Equal((sbyte?)-1, elements[0].GetOptionalInt8());
Assert.Equal((byte?)200, elements[1].GetOptionalUint8());
Assert.Equal((short?)-200, elements[2].GetOptionalInt16());
Assert.Equal((ushort?)40_000, elements[3].GetOptionalUint16());
Assert.Equal(-40_000, elements[4].GetOptionalInt32());
Assert.Equal(4_000_000_000, elements[5].GetOptionalUint32());
Assert.Equal(-4_000_000_000, elements[6].GetOptionalInt64());
Assert.Equal(10_000_000_000ul, elements[7].GetOptionalUint64());
Assert.Equal(-1.7f, elements[8].GetOptionalFloat());
Assert.Equal(123.45, elements[9].GetOptionalDouble());
Assert.Equal(new DateTime(2021, 08, 21), elements[10].GetOptionalDate());
Assert.Equal(new DateTime(2021, 08, 21, 23, 30, 47), elements[11].GetOptionalDatetime());
Assert.Equal(new DateTime(2021, 08, 21, 23, 30, 47, 581), elements[12].GetOptionalTimestamp());
Assert.Equal(-new TimeSpan(3, 7, 40, 27, 729), elements[13].GetOptionalInterval());
Assert.Equal(true, elements[14].GetOptionalBool());
Assert.Equal(false, elements[15].GetOptionalBool());

Assert.Equal((sbyte?)-1, (sbyte?)elements[0]);
Assert.Equal((byte?)200u, (byte?)elements[1]);
Assert.Equal((short?)-200, (short?)elements[2]);
Assert.Equal((ushort?)40_000, (ushort?) elements[3]);
Assert.Equal(-40_000, (int?) elements[4]);
Assert.Equal(4_000_000_000, (uint?)elements[5]);
Assert.Equal(-4_000_000_000, (long?)elements[6]);
Assert.Equal(10_000_000_000ul, (ulong?)elements[7]);
Assert.Equal(-1.7f, (float?)elements[8]);
Assert.Equal(123.45, (double?)elements[9]);
Assert.Equal(new DateTime(2021, 08, 21), (DateTime?)elements[10]);
Assert.Equal(new DateTime(2021, 08, 21, 23, 30, 47), (DateTime?)elements[11]);
Assert.Equal(new DateTime(2021, 08, 21, 23, 30, 47, 581), (DateTime?)elements[12]);
Assert.Equal(-new TimeSpan(3, 7, 40, 27, 729), (TimeSpan?)elements[13]);
Assert.True((bool?)elements[14]);
Assert.False((bool?)elements[15]);
}

[Fact]
Expand Down