private BaseBlock ToExpression(Type entityType, DataType returnDataType)
        {
            String text = _sb.ToString();

            if (this._fnArgs == null)
            {
                if (PropertySignature.IsProperty(entityType, text))
                {
                    // TODO: we could check that the PropBlock dataType is compatible with the returnDataType
                    return(new PropBlock(text, entityType));
                }
                else
                {
                    return(new LitBlock(text, returnDataType));
                }
            }
            else
            {
                String fnName   = text;
                var    argTypes = FnBlock.GetArgTypes(fnName);
                if (argTypes.Count != _fnArgs.Count)
                {
                    throw new Exception("Incorrect number of arguments to '" + fnName
                                        + "' function; was expecting " + argTypes.Count);
                }
                var exprs = _fnArgs.Select((token, ix) => token.ToExpression(entityType, argTypes[ix])).ToList();
                // TODO: we could check that the FnBlock dataType is compatible with the returnDataType
                return(new FnBlock(text, exprs));
            }
        }
Exemple #2
0
        // will return either a PropBlock or a LitBlock
        public static BaseBlock CreateRHSBlock(object exprSource,
                                               Type entityType, DataType otherExprDataType)
        {
            if (exprSource == null)
            {
                return(new LitBlock(exprSource, otherExprDataType));
            }

            if (exprSource is string)
            {
                var source = (string)exprSource;
                if (entityType == null)
                {
                    // if entityType is unknown then assume that the rhs is a
                    // literal
                    return(new LitBlock(source, otherExprDataType));
                }

                if (PropertySignature.IsProperty(entityType, source))
                {
                    return(new PropBlock(source, entityType));
                }
                else
                {
                    return(new LitBlock(source, otherExprDataType));
                }
            }

            if (TypeFns.IsPredefinedType(exprSource.GetType()))
            {
                return(new LitBlock(exprSource, otherExprDataType));
            }

            if (exprSource is IDictionary <string, object> )
            {
                var exprMap = (IDictionary <string, object>)exprSource;
                // note that this is NOT the same a using get and checking for null
                // because null is a valid 'value'.
                if (!exprMap.ContainsKey("value"))
                {
                    throw new Exception(
                              "Unable to locate a 'value' property on: "
                              + exprMap.ToString());
                }
                var value = exprMap["value"];

                if (exprMap.ContainsKey("isProperty"))
                {
                    return(new PropBlock((string)value, entityType));
                }
                else
                {
                    var dt       = (string)exprMap["dataType"];
                    var dataType = (dt != null) ? DataType.FromName(dt) : otherExprDataType;
                    return(new LitBlock(value, dataType));
                }
            }

            if (exprSource is IList)
            {
                // right now this pretty much implies the values on an 'in' clause
                return(new LitBlock(exprSource, otherExprDataType));
            }

            if (TypeFns.IsEnumType(exprSource.GetType()))
            {
                return(new LitBlock(exprSource, otherExprDataType));
            }

            throw new Exception(
                      "Unable to parse the right hand side of this BinaryExpression: "
                      + exprSource.ToString());
        }