Exemplo n.º 1
0
        /// <summary>
        /// Get the Type of the schema field, including any type annotations from TypeAttribute
        /// </summary>
        /// <remarks>
        /// In some cases this may not be the actual type of the property or field.
        /// If the property or field has a TypeAttribute, this will be the attribute's value
        /// and can provide schema information that is not available on the actual
        /// property/field type.
        /// </remarks>
        public static Type GetSchemaType(this ISchemaField schemaField)
        {
            var type = schemaField.MemberType;

            // If a custom type mapping was specified via TypeAttribute, we respect it completely.
            // Otherwise, we check the type against the custom registry and try to replace all
            // instances of mapped types to their mappings.
            var typeAttr = schemaField.GetAttribute <TypeAttribute>();

            if (typeAttr != null)
            {
                type = ResolveTypeArgumentTags(type, typeAttr.Value);
            }
            else
            {
                var t = CustomTypeRegistry.ResolveFieldType(schemaField);
                if (t != null)
                {
                    type = t;
                }
                else
                {
                    type = ResolveTypeArgumentTagsWithCustomRegistry(type, type);
                }
            }

            return(type);
        }
Exemplo n.º 2
0
        internal static object GetDefaultValue(this ISchemaField schemaField)
        {
            var declaringType     = schemaField.DeclaringType;
            var declaringTypeInfo = declaringType.GetTypeInfo();
            var defaultAttribute  = schemaField.GetAttribute <DefaultAttribute>();

            // For interfaces determine member default value from the type and/or DefaultAttribute
            if (declaringTypeInfo.IsInterface)
            {
                var schemaType = schemaField.GetSchemaType();

                if (defaultAttribute != null)
                {
                    if (defaultAttribute.Value == null)
                    {
                        return(null);
                    }

                    if (schemaType.IsBondNullable() || schemaType.IsBondStruct() || schemaType.IsBondContainer())
                    {
                        InvalidDefaultAttribute(schemaField, defaultAttribute.Value);
                    }

                    return(defaultAttribute.Value);
                }
                else
                {
                    if (schemaType.IsBondNullable())
                    {
                        return(null);
                    }

                    if (schemaType.IsBondStruct() || schemaType.IsBonded() || schemaType.IsBondContainer() || schemaType.IsBondBlob())
                    {
                        return(Empty);
                    }

                    if (schemaType.IsBondString())
                    {
                        return(string.Empty);
                    }

                    return(Activator.CreateInstance(schemaField.MemberType));
                }
            }

            if (defaultAttribute != null)
            {
                InvalidDefaultAttribute(schemaField, defaultAttribute.Value);
            }

            // For classes create a default instance and get the actual default value of the member
            var objectType    = declaringType.GetObjectType();
            var objectMemeber = objectType.GetSchemaFields().Single(m => m.Id == schemaField.Id);
            var obj           = Activator.CreateInstance(objectType);
            var defaultValue  = objectMemeber.GetValue(obj);

            return(defaultValue);
        }
Exemplo n.º 3
0
 internal static Modifier GetModifier(this ISchemaField schemaField)
 {
     return(schemaField.GetAttribute <RequiredAttribute>() != null
         ? Modifier.Required
         : schemaField.GetAttribute <RequiredOptionalAttribute>() != null
         ? Modifier.RequiredOptional
         : Modifier.Optional);
 }
Exemplo n.º 4
0
        internal static object GetDefaultValue(this ISchemaField schemaField)
        {
            var declaringType     = schemaField.DeclaringType;
            var declaringTypeInfo = declaringType.GetTypeInfo();
            var defaultAttribute  = schemaField.GetAttribute <DefaultAttribute>();

            // For interfaces determine member default value from the type and/or DefaultAttribute
            if (declaringTypeInfo.IsInterface)
            {
                var schemaType = schemaField.GetSchemaType();

                if (defaultAttribute != null)
                {
                    if (defaultAttribute.Value == null)
                    {
                        return(null);
                    }

                    if (schemaType.IsBondNullable() || schemaType.IsBondStruct() || schemaType.IsBondContainer())
                    {
                        InvalidDefaultAttribute(schemaField, defaultAttribute.Value);
                    }

                    return(defaultAttribute.Value);
                }
                else
                {
                    if (schemaType.IsBondNullable())
                    {
                        return(null);
                    }

                    if (schemaType.IsBondStruct() || schemaType.IsBondContainer())
                    {
                        return(Empty);
                    }

                    throw new InvalidOperationException(string.Format(
                                                            CultureInfo.InvariantCulture,
                                                            "Default value for property {0}.{1} must be specified using the DefaultAttribute",
                                                            schemaField.DeclaringType.Name,
                                                            schemaField.Name));
                }
            }

            if (defaultAttribute != null)
            {
                InvalidDefaultAttribute(schemaField, defaultAttribute.Value);
            }

            // For classes create a default instance and get the actual default value of the member
            var objectType    = declaringType.GetObjectType();
            var objectMemeber = objectType.GetSchemaFields().Single(m => m.Id == schemaField.Id);
            var obj           = Activator.CreateInstance(objectType);
            var defaultValue  = objectMemeber.GetValue(obj);

            return(defaultValue);
        }
Exemplo n.º 5
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);
            }
Exemplo n.º 6
0
        public static Type ResolveSchemaField(ISchemaField Field)
        {
            if (Field.MemberType == typeof(IGenericSerializable) ||
                (Field.MemberType.IsSubclassOf(typeof(IGenericSerializable)) && Field.MemberInfo.GetCustomAttributes(typeof(GenericSerializationAttribute), false) != null))
            {
                return(typeof(nullable <blob>));
            }

            return(null);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Get the Type of the schema field, including any type annotations from TypeAttribute
        /// </summary>
        /// <remarks>
        /// In some cases this may not be the actual type of the property or field.
        /// If the property or field has a TypeAttribute, this will be the attribute's value
        /// and can provide schema information that is not available on the actual
        /// property/field type.
        /// </remarks>
        public static Type GetSchemaType(this ISchemaField schemaField)
        {
            var type = schemaField.MemberType;

            var typeAttr = schemaField.GetAttribute <TypeAttribute>();

            if (typeAttr != null)
            {
                type = ResolveTypeArgumentTags(type, typeAttr.Value);
            }

            return(type);
        }
Exemplo n.º 8
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));
        }
Exemplo n.º 9
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);
        }
Exemplo n.º 10
0
 internal static Type ResolveFieldType(ISchemaField Field)
 {
     try
     {
         Lock.EnterReadLock();
         Type Result;
         foreach (var Callback in TypeResolutionCallbacks)
         {
             if ((Result = Callback?.Invoke(Field)) != null)
             {
                 return(Result);
             }
         }
         return(null);
     }
     finally
     {
         Lock.ExitReadLock();
     }
 }
Exemplo n.º 11
0
 static T GetAttribute <T>(this ISchemaField schemaField)
     where T : class
 {
     return(schemaField.MemberInfo.GetAttribute <T>());
 }
Exemplo n.º 12
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;
            }
Exemplo n.º 13
0
        public static FieldSchemaMetadata MetadataFor(this ISchemaMetadataCache cache, ISchemaField field)
        {
            var clarifyField = field as SchemaFieldBase;

            if (clarifyField == null)
            {
                return new FieldSchemaMetadata {
                           Name = field.Name
                }
            }
            ;

            var tableName     = clarifyField.Parent.Name;
            var tableMetadata = cache.MetadataFor(tableName);

            return(tableMetadata.MetadataFor(field.Name));
        }
    }
Exemplo n.º 14
0
 public MappedProperty(PropertyInfo property, ISchemaField field, FieldSchemaMetadata metadata)
 {
     Property = property;
     Field    = field;
     Metadata = metadata;
 }
Exemplo n.º 15
0
 public static Type GetSchemaType(ISchemaField schemaField)
 {
     return(Reflection.GetSchemaType(schemaField));
 }
Exemplo n.º 16
0
 static void InvalidDefaultAttribute(ISchemaField schemaField, object value)
 {
     throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture,
                                                       "Invalid default value '{2}' specified by DefaultAttribute for {0}.{1}",
                                                       schemaField.DeclaringType, schemaField.Name, value == null ? "null" : value.ToString()));
 }
Exemplo n.º 17
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));
        }