Ejemplo n.º 1
0
            static Variant GetDefaultValue(ISchemaField schemaField)
            {
                var defaultValue = schemaField.GetDefaultValue();
                var variant      = new Variant();

                if (defaultValue == null)
                {
                    if (!schemaField.GetSchemaType().IsBondNullable())
                    {
                        variant.nothing = true;
                    }
                }
                else
                {
                    var alias = defaultValue.GetType() != schemaField.GetSchemaType();

                    switch (schemaField.GetSchemaType().GetBondDataType())
                    {
                    case BondDataType.BT_BOOL:
                        variant.uint_value = (bool)defaultValue ? 1ul : 0ul;
                        break;

                    case BondDataType.BT_UINT8:
                    case BondDataType.BT_UINT16:
                    case BondDataType.BT_UINT32:
                    case BondDataType.BT_UINT64:
                        variant.uint_value = alias ? 0 : Convert.ToUInt64(defaultValue);
                        break;

                    case BondDataType.BT_INT8:
                    case BondDataType.BT_INT16:
                    case BondDataType.BT_INT32:
                    case BondDataType.BT_INT64:
                        variant.int_value = alias ? 0 : Convert.ToInt64(defaultValue);
                        break;

                    case BondDataType.BT_FLOAT:
                        variant.double_value = alias ? 0 : Convert.ToSingle(defaultValue);
                        break;

                    case BondDataType.BT_DOUBLE:
                        variant.double_value = alias ? 0 : Convert.ToDouble(defaultValue);
                        break;

                    case BondDataType.BT_STRING:
                        variant.string_value = alias ? string.Empty : (string)defaultValue;
                        break;

                    case BondDataType.BT_WSTRING:
                        variant.wstring_value = (string)defaultValue;
                        break;
                    }
                }

                return(variant);
            }
Ejemplo n.º 2
0
        Expression Field(ITransform transform, Expression structVar, UInt16 id, ISchemaField schemaField, IField field)
        {
            var fieldSchemaType = schemaField.GetSchemaType();
            var fieldId         = Expression.Constant(id);
            var fieldType       = Expression.Constant(fieldSchemaType.GetBondDataType());
            var fieldValue      = DataExpression.PropertyOrField(structVar, schemaField.Name);
            var parser          = new ObjectParser(this, fieldValue, fieldSchemaType);

            var processField = field != null
                ? field.Value(parser, fieldType)
                : transform.UnknownField(parser, fieldType, fieldId) ?? Expression.Empty();

            var omitField = field != null
                ? field.Omitted : Expression.Empty();

            Expression cannotOmit;

            if (fieldSchemaType.IsBondStruct() || fieldSchemaType.IsBonded() || schemaField.GetModifier() != Modifier.Optional)
            {
                cannotOmit = Expression.Constant(true);
            }
            else
            {
                var defaultValue = schemaField.GetDefaultValue();

                if (fieldSchemaType.IsBondBlob())
                {
                    cannotOmit = Expression.NotEqual(
                        typeAlias.Convert(fieldValue, fieldSchemaType),
                        Expression.Default(typeof(ArraySegment <byte>)));
                }
                else if (fieldSchemaType.IsBondContainer())
                {
                    cannotOmit = defaultValue == null
                        ? Expression.NotEqual(fieldValue, Expression.Constant(null))
                        : Expression.NotEqual(ContainerCount(fieldValue), Expression.Constant(0));
                }
                else
                {
                    cannotOmit = Expression.NotEqual(fieldValue, Expression.Constant(defaultValue));
                }
            }

            return(PrunedExpression.IfThenElse(cannotOmit, processField, omitField));
        }
Ejemplo n.º 3
0
        Expression Field(ITransform transform, Expression structVar, UInt16 id, ISchemaField schemaField, IField field)
        {
            var fieldSchemaType = schemaField.GetSchemaType();
            var fieldId = Expression.Constant(id);
            var fieldType = Expression.Constant(fieldSchemaType.GetBondDataType());
            var fieldValue = DataExpression.PropertyOrField(structVar, schemaField.Name);
            var parser = new ObjectParser(this, fieldValue, fieldSchemaType);

            var processField = field != null
                ? field.Value(parser, fieldType)
                : transform.UnknownField(parser, fieldType, fieldId) ?? Expression.Empty();

            var omitField = field != null
                ? field.Omitted : Expression.Empty();

            Expression cannotOmit;

            if (fieldSchemaType.IsBondStruct() || fieldSchemaType.IsBonded() || schemaField.GetModifier() != Modifier.Optional)
            {
                cannotOmit = Expression.Constant(true);
            }
            else
            {
                var defaultValue = schemaField.GetDefaultValue();

                if (fieldSchemaType.IsBondBlob())
                {
                    cannotOmit = Expression.NotEqual(
                        typeAlias.Convert(fieldValue, fieldSchemaType), 
                        Expression.Default(typeof(ArraySegment<byte>)));
                }
                else if (fieldSchemaType.IsBondContainer())
                {
                    cannotOmit = defaultValue == null
                        ? Expression.NotEqual(fieldValue, Expression.Constant(null))
                        : Expression.NotEqual(ContainerCount(fieldValue), Expression.Constant(0));
                }
                else
                {
                    cannotOmit = Expression.NotEqual(fieldValue, Expression.Constant(defaultValue));
                }
            }

            return PrunedExpression.IfThenElse(cannotOmit, processField, omitField);
        }
Ejemplo n.º 4
0
            static Variant GetDefaultValue(ISchemaField schemaField)
            {
                var defaultValue = schemaField.GetDefaultValue();
                var variant = new Variant();

                if (defaultValue == null)
                {
                    if (!schemaField.GetSchemaType().IsBondNullable())
                        variant.nothing = true;
                }
                else
                {
                    var alias = defaultValue.GetType() != schemaField.GetSchemaType();

                    switch (schemaField.GetSchemaType().GetBondDataType())
                    {
                        case BondDataType.BT_BOOL:
                            variant.uint_value = (bool) defaultValue ? 1ul : 0ul;
                            break;

                        case BondDataType.BT_UINT8:
                        case BondDataType.BT_UINT16:
                        case BondDataType.BT_UINT32:
                        case BondDataType.BT_UINT64:
                            variant.uint_value = alias ? 0 : Convert.ToUInt64(defaultValue);
                            break;

                        case BondDataType.BT_INT8:
                        case BondDataType.BT_INT16:
                        case BondDataType.BT_INT32:
                        case BondDataType.BT_INT64:
                            variant.int_value = alias ? 0 : Convert.ToInt64(defaultValue);
                            break;

                        case BondDataType.BT_FLOAT:
                            variant.double_value = alias ? 0 : Convert.ToSingle(defaultValue);
                            break;

                        case BondDataType.BT_DOUBLE:
                            variant.double_value = alias ? 0 : Convert.ToDouble(defaultValue);
                            break;

                        case BondDataType.BT_STRING:
                            variant.string_value = alias ? string.Empty : (string)defaultValue;
                            break;

                        case BondDataType.BT_WSTRING:
                            variant.wstring_value = (string)defaultValue;
                            break;
                    }
                }

                return variant;
            }
Ejemplo n.º 5
0
        Expression Field(ITransform transform, Expression structVar, UInt16 id, ISchemaField schemaField, IField field)
        {
            var fieldSchemaType = schemaField.GetSchemaType();
            var fieldId         = Expression.Constant(id);
            var fieldType       = Expression.Constant(fieldSchemaType.GetBondDataType());
            var fieldValue      = DataExpression.PropertyOrField(structVar, schemaField.Name);

            ObjectParser parser = null;

            Expression          blob          = null;
            ParameterExpression convertedBlob = null;

            // To avoid calling Convert multiple times on the same aliased Blob
            // we must construct a new ObjectParser with the expected return type of
            // of Convert
            if (fieldSchemaType.IsBondBlob())
            {
                blob          = typeAlias.Convert(fieldValue, fieldSchemaType);
                convertedBlob = Expression.Variable(blob.Type, "convertedBlob");

                if (blob.Type != fieldValue.Type)
                {
                    parser = new ObjectParser(this, convertedBlob, convertedBlob.Type);
                }
            }

            parser = parser ?? new ObjectParser(this, fieldValue, fieldSchemaType);

            var processField = field != null
                ? field.Value(parser, fieldType)
                : transform.UnknownField(parser, fieldType, fieldId) ?? Expression.Empty();

            var omitField = field != null
                ? field.Omitted : Expression.Empty();

            Expression cannotOmit;

            if (fieldSchemaType.IsBondStruct() || fieldSchemaType.IsBonded() || schemaField.GetModifier() != Modifier.Optional)
            {
                cannotOmit = Expression.Constant(true);

                if (fieldSchemaType.IsBondBlob())
                {
                    return(Expression.Block(
                               new[] { convertedBlob },
                               Expression.Assign(convertedBlob, blob),
                               processField));
                }
            }
            else
            {
                var defaultValue = schemaField.GetDefaultValue();

                if (fieldSchemaType.IsBondBlob())
                {
                    var notEqual = Expression.NotEqual(
                        convertedBlob,
                        Expression.Default(typeof(ArraySegment <byte>)));

                    return(Expression.Block(
                               new[] { convertedBlob },
                               Expression.Assign(convertedBlob, blob),
                               PrunedExpression.IfThenElse(notEqual, processField, omitField)));
                }
                else if (fieldSchemaType.IsBondContainer())
                {
                    cannotOmit = defaultValue == null
                        ? Expression.NotEqual(fieldValue, Expression.Constant(null))
                        : Expression.NotEqual(ContainerCount(fieldValue), Expression.Constant(0));
                }
                else
                {
                    cannotOmit = Expression.NotEqual(fieldValue, Expression.Constant(defaultValue));
                }
            }

            return(PrunedExpression.IfThenElse(cannotOmit, processField, omitField));
        }