Skip to content

Commit

Permalink
feat: added data uri function
Browse files Browse the repository at this point in the history
  • Loading branch information
thygesteffensen committed Feb 28, 2021
1 parent fa18aa5 commit 5e72433
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Parser.ExpressionParser.Functions.Base;
using Parser.ExpressionParser.Functions.CustomException;

namespace Parser.ExpressionParser.Functions.Implementations.ConversionFunctions
{
Expand All @@ -10,6 +11,11 @@ public DataUriFunction() : base("dataUri")

public override ValueContainer ExecuteFunction(params ValueContainer[] parameters)
{
if (parameters.Length == 0)
{
throw InvalidTemplateException.BuildInvalidLanguageFunction("SomeActon", "dataUri");
}

return parameters[0].Type() switch
{
ValueContainer.ValueType.String =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Linq;
using System.Text;
using Parser.ExpressionParser.Functions.Base;
using Parser.ExpressionParser.Functions.CustomException;

namespace Parser.ExpressionParser.Functions.Implementations.ConversionFunctions
{
public class DataUriToBinaryFunction : Function
{
public DataUriToBinaryFunction() : base("dataUriToBinary")
{
}

public override ValueContainer ExecuteFunction(params ValueContainer[] parameters)
{
if (parameters.Length == 0)
{
throw InvalidTemplateException.BuildInvalidLanguageFunction("SomeActon", "dataUriToBinary");
}

return parameters[0].Type() switch
{
ValueContainer.ValueType.String =>
new ValueContainer(Encoding.UTF8.GetBytes(parameters[0].GetValue<string>())
.Aggregate("", (s, b) => s + Convert.ToString(b, 2).PadLeft(8, '0'))),
_ => throw new PowerAutomateMockUpException(
$"Array function can only operate on strings, not {parameters[0].Type()}.")
};
}
}
}

0 comments on commit 5e72433

Please sign in to comment.