From 5e724338cc043b5028da31ebc8f7eae0a533b142 Mon Sep 17 00:00:00 2001 From: Thyge Skoedt Steffensen <31892312+thygesteffensen@users.noreply.github.com> Date: Sun, 28 Feb 2021 12:59:49 +0100 Subject: [PATCH] feat: added data uri function --- .../ConversionFunctions/DataUriFunction.cs | 6 ++++ .../DataUriToBinaryFunction.cs | 32 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 PowerAutomateMockUp/ExpressionParser/Functions/Implementations/ConversionFunctions/DataUriToBinaryFunction.cs diff --git a/PowerAutomateMockUp/ExpressionParser/Functions/Implementations/ConversionFunctions/DataUriFunction.cs b/PowerAutomateMockUp/ExpressionParser/Functions/Implementations/ConversionFunctions/DataUriFunction.cs index 1dc8938..3a3675c 100644 --- a/PowerAutomateMockUp/ExpressionParser/Functions/Implementations/ConversionFunctions/DataUriFunction.cs +++ b/PowerAutomateMockUp/ExpressionParser/Functions/Implementations/ConversionFunctions/DataUriFunction.cs @@ -1,4 +1,5 @@ using Parser.ExpressionParser.Functions.Base; +using Parser.ExpressionParser.Functions.CustomException; namespace Parser.ExpressionParser.Functions.Implementations.ConversionFunctions { @@ -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 => diff --git a/PowerAutomateMockUp/ExpressionParser/Functions/Implementations/ConversionFunctions/DataUriToBinaryFunction.cs b/PowerAutomateMockUp/ExpressionParser/Functions/Implementations/ConversionFunctions/DataUriToBinaryFunction.cs new file mode 100644 index 0000000..b9ffd7f --- /dev/null +++ b/PowerAutomateMockUp/ExpressionParser/Functions/Implementations/ConversionFunctions/DataUriToBinaryFunction.cs @@ -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()) + .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()}.") + }; + } + } +} \ No newline at end of file