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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ jobs:
- name: Test
run: |
cd src
dotnet test
dotnet test --filter "Category=Unit"
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
- Add methods for castion to c# nullable to YQL Optional
- Add explicit cast operator for some types
- Tests refactoring
- Add Bool type support
152 changes: 150 additions & 2 deletions src/Ydb.Sdk/src/Value/YdbValueBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,10 @@ public static YdbValue MakeEmptyOptional(YdbTypeId typeId)
public static YdbValue MakeOptional(YdbValue value)
{
return new YdbValue(
new Ydb.Type { OptionalType = new OptionalType { Item = value._protoType }},
new Ydb.Type { OptionalType = new OptionalType { Item = value._protoType } },
value.TypeId != YdbTypeId.OptionalType
? value._protoValue
: new Ydb.Value { NestedValue = value._protoValue});
: new Ydb.Value { NestedValue = value._protoValue });
}

// TODO: MakeEmptyList with complex types
Expand Down Expand Up @@ -300,5 +300,153 @@ 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
{
if (value is null)
{
return MakeEmptyOptional(type);
}
else
{
return MakeOptional(func((T)value));
}
}

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);
}

public static YdbValue MakeOptionalString(byte[]? value)
{
if (value is null)
{
return MakeEmptyOptional(YdbTypeId.String);
}
else
{
return MakeOptional(MakeString(value));
}
}

public static YdbValue MakeOptionalUtf8(string? value)
{
if (value is null)
{
return MakeEmptyOptional(YdbTypeId.Utf8);
}
else
{
return MakeOptional(MakeUtf8(value));
}
}

public static YdbValue MakeOptionalYson(byte[]? value)
{
if (value is null)
{
return MakeEmptyOptional(YdbTypeId.Yson);
}
else
{
return MakeOptional(MakeYson(value));
}
}

public static YdbValue MakeOptionalJson(string? value)
{
if (value is null)
{
return MakeEmptyOptional(YdbTypeId.Json);
}
else
{
return MakeOptional(MakeJson(value));
}
}

public static YdbValue MakeOptionalJsonDocument(string? value)
{
if (value is null)
{
return MakeEmptyOptional(YdbTypeId.JsonDocument);
}
else
{
return MakeOptional(MakeJsonDocument(value));
}
}
}
}
120 changes: 120 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,125 @@ 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(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
Loading