Exemple #1
0
        Expression FieldValue(IParser parser, Expression var, Expression valueType, Type schemaType, bool initialize)
        {
            Expression body;

            if (schemaType.IsBondStruct() && var.Type.IsValueType())
            {
                // Special handling for properties of struct types: we deserialize into
                // a temp variable and then assign the value to the property.
                var temp = Expression.Variable(var.Type, $"{var.Type.Name}_temp");
                body = Expression.Block(
                    new[] { temp },
                    Value(parser, temp, valueType, schemaType, true),
                    Expression.Assign(var, temp));
            }
            else
            {
                body = Value(parser, var, valueType, schemaType, initialize);
            }

            if (schemaType.IsBondContainer() || schemaType.IsBondStruct() || schemaType.IsBondNullable())
            {
                var expectedType = Expression.Constant(schemaType.GetBondDataType());
                return(PrunedExpression.IfThenElse(
                           Expression.Equal(valueType, expectedType),
                           body,
                           ThrowExpression.InvalidTypeException(expectedType, valueType)));
            }

            return(body);
        }
Exemple #2
0
        Expression Value(IParser parser, Expression valueType)
        {
            if (parser.IsBonded)
            {
                return(parser.Bonded(value =>
                                     writer.WriteBonded(PrunedExpression.Convert(value, typeof(IBonded)))));
            }

            var switchCases = new List <DeferredSwitchCase>
            {
                PrunedExpression.SwitchCase(
                    () => GenerateSerialize(Container, parser),
                    BondDataType.BT_LIST,
                    BondDataType.BT_SET),
                PrunedExpression.SwitchCase(
                    () => GenerateSerialize(Map, parser),
                    BondDataType.BT_MAP),
                PrunedExpression.SwitchCase(
                    () => GenerateSerialize(Struct, parser),
                    BondDataType.BT_STRUCT)
            };

            switchCases.AddRange(
                from type in new[]
            {
                BondDataType.BT_BOOL,
                BondDataType.BT_UINT8,
                BondDataType.BT_UINT16,
                BondDataType.BT_UINT32,
                BondDataType.BT_UINT64,
                BondDataType.BT_FLOAT,
                BondDataType.BT_DOUBLE,
                BondDataType.BT_STRING,
                BondDataType.BT_INT8,
                BondDataType.BT_INT16,
                BondDataType.BT_INT32,
                BondDataType.BT_INT64,
                BondDataType.BT_WSTRING
            }
                select
                PrunedExpression.SwitchCase(
                    () => parser.Scalar(Expression.Constant(type), type,
                                        value => writer.Write(value, type)),
                    type));

            return(PrunedExpression.Switch(
                       valueType,
                       ThrowExpression.InvalidTypeException(valueType),
                       switchCases));
        }