~ruther/NosSmooth

86662c7e149e76b42f8069ae4429bb35e93bbb50 — František Boháček 3 years ago 552c6c8
feat: allow nullables in converters
M Core/NosSmooth.Packets/Converters/BaseTypeConverter.cs => Core/NosSmooth.Packets/Converters/BaseTypeConverter.cs +4 -4
@@ 16,17 16,17 @@ namespace NosSmooth.Packets.Converters;
public abstract class BaseTypeConverter<TParseType> : ITypeConverter<TParseType>
{
    /// <inheritdoc />
    public abstract Result Serialize(TParseType obj, PacketStringBuilder builder);
    public abstract Result Serialize(TParseType? obj, PacketStringBuilder builder);

    /// <inheritdoc />
    public abstract Result<TParseType> Deserialize(PacketStringEnumerator stringEnumerator);
    public abstract Result<TParseType?> Deserialize(PacketStringEnumerator stringEnumerator);

    /// <inheritdoc/>
    Result<object> ITypeConverter.Deserialize(PacketStringEnumerator stringEnumerator)
    Result<object?> ITypeConverter.Deserialize(PacketStringEnumerator stringEnumerator)
        => Deserialize(stringEnumerator);

    /// <inheritdoc/>
    Result ITypeConverter.Serialize(object obj, PacketStringBuilder builder)
    Result ITypeConverter.Serialize(object? obj, PacketStringBuilder builder)
    {
        if (!(obj is TParseType parseType))
        {

M Core/NosSmooth.Packets/Converters/Basic/BasicTypeConverter.cs => Core/NosSmooth.Packets/Converters/Basic/BasicTypeConverter.cs +9 -4
@@ 16,19 16,24 @@ namespace NosSmooth.Packets.Converters.Basic;
public abstract class BasicTypeConverter<TBasicType> : BaseTypeConverter<TBasicType>
{
    /// <inheritdoc />
    public override Result Serialize(TBasicType obj, PacketStringBuilder builder)
    public override Result Serialize(TBasicType? obj, PacketStringBuilder builder)
    {
        builder.Append(obj?.ToString() ?? "-");
        return Result.FromSuccess();
    }

    /// <inheritdoc />
    public override Result<TBasicType> Deserialize(PacketStringEnumerator stringEnumerator)
    public override Result<TBasicType?> Deserialize(PacketStringEnumerator stringEnumerator)
    {
        var nextTokenResult = stringEnumerator.GetNextToken();
        if (!nextTokenResult.IsSuccess)
        {
            return Result<TBasicType>.FromError(nextTokenResult);
            return Result<TBasicType?>.FromError(nextTokenResult);
        }

        if (nextTokenResult.Entity.Token == "-")
        {
            return Result<TBasicType?>.FromSuccess(default);
        }

        return Deserialize(nextTokenResult.Entity.Token);


@@ 39,5 44,5 @@ public abstract class BasicTypeConverter<TBasicType> : BaseTypeConverter<TBasicT
    /// </summary>
    /// <param name="value">The value to deserialize.</param>
    /// <returns>The deserialized value or an error.</returns>
    protected abstract Result<TBasicType> Deserialize(string value);
    protected abstract Result<TBasicType?> Deserialize(string value);
}
\ No newline at end of file

M Core/NosSmooth.Packets/Converters/ITypeConverter.cs => Core/NosSmooth.Packets/Converters/ITypeConverter.cs +4 -4
@@ 18,7 18,7 @@ public interface ITypeConverter
    /// </summary>
    /// <param name="stringEnumerator">The packet string enumerator with the current position.</param>
    /// <returns>The parsed object or an error.</returns>
    public Result<object> Deserialize(PacketStringEnumerator stringEnumerator);
    public Result<object?> Deserialize(PacketStringEnumerator stringEnumerator);

    /// <summary>
    /// Serializes the given object to string by appending to the packet string builder.


@@ 26,7 26,7 @@ public interface ITypeConverter
    /// <param name="obj">The object to serialize.</param>
    /// <param name="builder">The string builder to append to.</param>
    /// <returns>A result that may or may not have succeeded.</returns>
    public Result Serialize(object obj, PacketStringBuilder builder);
    public Result Serialize(object? obj, PacketStringBuilder builder);
}

/// <summary>


@@ 43,7 43,7 @@ public interface ITypeConverter<TParseType> : ITypeConverter
    /// </summary>
    /// <param name="stringEnumerator">The packet string enumerator with the current position.</param>
    /// <returns>The parsed object or an error.</returns>
    public new Result<TParseType> Deserialize(PacketStringEnumerator stringEnumerator);
    public new Result<TParseType?> Deserialize(PacketStringEnumerator stringEnumerator);

    /// <summary>
    /// Serializes the given object to string by appending to the packet string builder.


@@ 51,5 51,5 @@ public interface ITypeConverter<TParseType> : ITypeConverter
    /// <param name="obj">The object to serialize.</param>
    /// <param name="builder">The string builder to append to.</param>
    /// <returns>A result that may or may not have succeeded.</returns>
    public Result Serialize(TParseType obj, PacketStringBuilder builder);
    public Result Serialize(TParseType? obj, PacketStringBuilder builder);
}
\ No newline at end of file

M Core/NosSmooth.Packets/Converters/Packets/InPacketConverter.cs => Core/NosSmooth.Packets/Converters/Packets/InPacketConverter.cs +2 -2
@@ 15,13 15,13 @@ namespace NosSmooth.Packets.Converters.Packets;
public class InPacketConverter : BaseTypeConverter<InPacket>
{
    /// <inheritdoc />
    public override Result Serialize(InPacket obj, PacketStringBuilder builder)
    public override Result Serialize(InPacket? obj, PacketStringBuilder builder)
    {
        throw new System.NotImplementedException();
    }

    /// <inheritdoc />
    public override Result<InPacket> Deserialize(PacketStringEnumerator stringEnumerator)
    public override Result<InPacket?> Deserialize(PacketStringEnumerator stringEnumerator)
    {
        throw new System.NotImplementedException();
    }

M Core/NosSmooth.Packets/Converters/TypeConverterRepository.cs => Core/NosSmooth.Packets/Converters/TypeConverterRepository.cs +4 -4
@@ 138,7 138,7 @@ public class TypeConverterRepository : ITypeConverterRepository
    /// <param name="stringEnumerator">The packet string enumerator with the current position.</param>
    /// <typeparam name="TParseType">The type of the object to serialize.</typeparam>
    /// <returns>The parsed object or an error.</returns>
    public Result<TParseType> Deserialize<TParseType>(PacketStringEnumerator stringEnumerator)
    public Result<TParseType?> Deserialize<TParseType>(PacketStringEnumerator stringEnumerator)
    {
        var specialConverter = GetSpecialConverter(typeof(TParseType));
        if (specialConverter is not null)


@@ 146,7 146,7 @@ public class TypeConverterRepository : ITypeConverterRepository
            var deserializeResult = specialConverter.Deserialize(typeof(TParseType), stringEnumerator);
            if (!deserializeResult.IsSuccess)
            {
                return Result<TParseType>.FromError(deserializeResult);
                return Result<TParseType?>.FromError(deserializeResult);
            }

            if (deserializeResult.Entity is null)


@@ 156,7 156,7 @@ public class TypeConverterRepository : ITypeConverterRepository
                    return default;
                }

                return Result<TParseType>.FromError(new DeserializedValueNullError(typeof(TParseType)));
                return Result<TParseType?>.FromError(new DeserializedValueNullError(typeof(TParseType)));
            }

            return (TParseType)deserializeResult.Entity;


@@ 165,7 165,7 @@ public class TypeConverterRepository : ITypeConverterRepository
        var converterResult = GetTypeConverter<TParseType>();
        if (!converterResult.IsSuccess)
        {
            return Result<TParseType>.FromError(converterResult);
            return Result<TParseType?>.FromError(converterResult);
        }

        return converterResult.Entity.Deserialize(stringEnumerator);

M Core/NosSmooth.Packets/Errors/WrongTypeError.cs => Core/NosSmooth.Packets/Errors/WrongTypeError.cs +2 -2
@@ 16,5 16,5 @@ namespace NosSmooth.Packets.Errors;
/// <param name="TypeConverter">The converter that failed to convert the object.</param>
/// <param name="ExpectedType">The expected type of the converting object.</param>
/// <param name="ActualObject">The actual object the converter got.</param>
public record WrongTypeError(ITypeConverter TypeConverter, Type ExpectedType, object ActualObject)
    : ResultError($"{TypeConverter.GetType().FullName} expected type {ExpectedType.FullName}, but got {ActualObject.GetType().FullName}");
\ No newline at end of file
public record WrongTypeError(ITypeConverter TypeConverter, Type ExpectedType, object? ActualObject)
    : ResultError($"{TypeConverter.GetType().FullName} expected type {ExpectedType.FullName}, but got {ActualObject?.GetType().FullName ?? "null"}");
\ No newline at end of file

Do not follow this link