private string FormatFunction(ExpressionContext context)
        {
            var adapterVersion = AdapterVersion.Default;

            if (FunctionToOperatorMapping.TryGetOperatorMapping(functionCaller, Function, adapterVersion, out var operatorMapping))
            {
                return(FormatMappedOperator(context, operatorMapping));
            }
            if (FunctionMapping.TryGetFunctionMapping(Function.FunctionName, Function.Arguments.Count(), adapterVersion, out var functionMapping))
            {
                return(FormatMappedFunction(context, functionMapping));
            }

            if (string.Equals(Function.FunctionName, ODataLiteral.Any, StringComparison.OrdinalIgnoreCase) ||
                string.Equals(Function.FunctionName, ODataLiteral.All, StringComparison.OrdinalIgnoreCase))
            {
                return(FormatAnyAllFunction(context));
            }
            if (string.Equals(Function.FunctionName, ODataLiteral.IsOf, StringComparison.OrdinalIgnoreCase) ||
                string.Equals(Function.FunctionName, ODataLiteral.Cast, StringComparison.OrdinalIgnoreCase))
            {
                return(FormatIsOfCastFunction(context));
            }
            if (string.Equals(Function.FunctionName, "get_Item", StringComparison.Ordinal) &&
                Function.Arguments.Count == 1)
            {
                return(FormatArrayIndexFunction(context));
            }
            if (string.Equals(Function.FunctionName, "HasFlag", StringComparison.Ordinal) &&
                Function.Arguments.Count == 1)
            {
                return(FormatEnumHasFlagFunction(context));
            }
            if (string.Equals(Function.FunctionName, "ToString", StringComparison.Ordinal) &&
                Function.Arguments.Count == 0)
            {
                return(FormatToStringFunction(context));
            }
            if (Function.Arguments.Count == 1)
            {
                var val = Function.Arguments.First();
                if (val.Value != null)
                {
                    var formattedVal = FromValue(
                        string.Equals(Function.FunctionName, "ToBoolean", StringComparison.Ordinal) ? Convert.ToBoolean(val.Value) :
                        string.Equals(Function.FunctionName, "ToByte", StringComparison.Ordinal) ? Convert.ToByte(val.Value) :
                        string.Equals(Function.FunctionName, "ToChar", StringComparison.Ordinal) ? Convert.ToChar(val.Value) :
                        string.Equals(Function.FunctionName, "ToDateTime", StringComparison.Ordinal) ? Convert.ToDateTime(val.Value) :
                        string.Equals(Function.FunctionName, "ToDecimal", StringComparison.Ordinal) ? Convert.ToDecimal(val.Value) :
                        string.Equals(Function.FunctionName, "ToDouble", StringComparison.Ordinal) ? Convert.ToDouble(val.Value) :
                        string.Equals(Function.FunctionName, "ToInt16", StringComparison.Ordinal) ? Convert.ToInt16(val.Value) :
                        string.Equals(Function.FunctionName, "ToInt32", StringComparison.Ordinal) ? Convert.ToInt32(val.Value) :
                        string.Equals(Function.FunctionName, "ToInt64", StringComparison.Ordinal) ? Convert.ToInt64(val.Value) :
                        string.Equals(Function.FunctionName, "ToSByte", StringComparison.Ordinal) ? Convert.ToSByte(val.Value) :
                        string.Equals(Function.FunctionName, "ToSingle", StringComparison.Ordinal) ? Convert.ToSingle(val.Value) :
                        string.Equals(Function.FunctionName, "ToString", StringComparison.Ordinal) ? Convert.ToString(val.Value) :
                        string.Equals(Function.FunctionName, "ToUInt16", StringComparison.Ordinal) ? Convert.ToUInt16(val.Value) :
                        string.Equals(Function.FunctionName, "ToUInt32", StringComparison.Ordinal) ? Convert.ToUInt32(val.Value) :
                        string.Equals(Function.FunctionName, "ToUInt64", StringComparison.Ordinal) ? (object)Convert.ToUInt64(val.Value)
                        : null);
                    if (formattedVal.Value != null)
                    {
                        return(FormatExpression(formattedVal, context));
                    }
                }
            }

            throw new NotSupportedException($"The function {Function.FunctionName} is not supported or called with wrong number of arguments");
        }
예제 #2
0
 public static bool TryGetOperatorMapping(ODataExpression functionCaller, ExpressionFunction function, AdapterVersion adapterVersion,
                                          out FunctionToOperatorMapping operatorMapping)
 {
     operatorMapping = DefinedMappings.SingleOrDefault(x => x.CanMap(function.FunctionName, function.Arguments.Count, functionCaller, adapterVersion));
     return(operatorMapping != null);
 }
 private string FormatMappedOperator(ExpressionContext context, FunctionToOperatorMapping mapping)
 {
     return(mapping.Format(context, functionCaller, Function.Arguments));
 }