Skip to content

added double and decimal argument binders #2

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
60 changes: 59 additions & 1 deletion Deployf.Botf/ArgumentBinder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
using System.Reflection;
using System;
using System.Collections.Generic;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it here?
I don't see any requirements for this from code

using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Telegram.Bot.Framework.Abstractions;

namespace Deployf.Botf;
Expand Down Expand Up @@ -35,6 +40,59 @@ public string Encode(ParameterInfo parameter, object argument, IUpdateContext _)
}
}

public class ArgumentBindDecimal : IArgumentBind
{
public bool CanDecode(ParameterInfo parameter, object argument)
{
return parameter.ParameterType == typeof(decimal);
}

public bool CanEncode(ParameterInfo parameter, object argument)
{
return parameter.ParameterType == typeof(decimal);
}

public ValueTask<object> Decode(ParameterInfo parameter, object argument, IUpdateContext _)
{
return new (decimal.Parse(argument.ToString()!));
}

public string Encode(ParameterInfo parameter, object argument, IUpdateContext _)
{
return argument.ToString()!;
}
}

public class ArgumentBindDouble : IArgumentBind
{
public bool CanDecode(ParameterInfo parameter, object argument)
{
return parameter.ParameterType == typeof(double);
}

public bool CanEncode(ParameterInfo parameter, object argument)
{
return parameter.ParameterType == typeof(double);
}

public ValueTask<object> Decode(ParameterInfo parameter, object argument, IUpdateContext _)
{
// for parse '.' and ',' delimeter
var isParsed = double.TryParse(argument.ToString()!, NumberStyles.Any, CultureInfo.CurrentCulture, out var number) ||
double.TryParse(argument.ToString()!, NumberStyles.Any, CultureInfo.GetCultureInfo("en-US"), out number) ||
double.TryParse(argument.ToString()!, NumberStyles.Any, CultureInfo.InvariantCulture, out number);

return isParsed
? new (number)
: throw new ArgumentBinderException();
}

public string Encode(ParameterInfo parameter, object argument, IUpdateContext _)
{
return argument.ToString()!;
}
}

public class ArgumentBindBoolean : IArgumentBind
{
public bool CanDecode(ParameterInfo parameter, object argument)
Expand Down
10 changes: 10 additions & 0 deletions Deployf.Botf/Exceptions/ArgumentBinderException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace Deployf.Botf.Exceptions;

public class ArgumentBinderException : Exception
{
public ArgumentBinderException(string message = "Failed to convert the passed argument") : base(message)
{
}
}