Пример #1
0
 internal static Modifier GetModifier(this ISchemaField schemaField)
 {
     return(schemaField.GetAttribute <RequiredAttribute>() != null
         ? Modifier.Required
         : schemaField.GetAttribute <RequiredOptionalAttribute>() != null
         ? Modifier.RequiredOptional
         : Modifier.Optional);
 }
Пример #2
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);
        }
Пример #3
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);
        }
Пример #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);
        }
Пример #5
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);
        }