Ejemplo n.º 1
0
        /// <summary>
        /// Parses a Thrift field from the specified PropertyInfo.
        /// If the PropertyInfo is not declared as a Thrift field, returns null.
        /// </summary>
        private static ThriftField ParseField(PropertyInfo propertyInfo)
        {
            var attr = propertyInfo.GetCustomAttribute <ThriftFieldAttribute>();

            if (attr == null)
            {
                return(null);
            }

            var propertyTypeInfo = propertyInfo.PropertyType.GetTypeInfo();
            var nullableType     = Nullable.GetUnderlyingType(propertyInfo.PropertyType);

            if (attr.IsRequired)
            {
                if (attr.DefaultValue != null)
                {
                    throw ThriftParsingException.DefaultValueOnRequiredField(propertyInfo);
                }
                if (nullableType != null)
                {
                    throw ThriftParsingException.RequiredNullableField(propertyInfo);
                }
            }
            else
            {
                if (attr.DefaultValue == null && propertyTypeInfo.IsValueType && nullableType == null)
                {
                    throw ThriftParsingException.OptionalValueField(propertyInfo);
                }
            }

            if (attr.DefaultValue != null)
            {
                if (attr.Converter == null)
                {
                    if (attr.DefaultValue.GetType() != (nullableType ?? propertyInfo.PropertyType))
                    {
                        throw ThriftParsingException.DefaultValueOfWrongType(propertyInfo);
                    }
                }
                else
                {
                    if (attr.DefaultValue.GetType() != attr.ThriftConverter.FromType)
                    {
                        throw ThriftParsingException.DefaultValueOfWrongType(propertyInfo);
                    }
                }
            }

            return(new ThriftField(attr.Id, attr.Name, attr.IsRequired, attr.DefaultValue, attr.ThriftConverter, propertyInfo));
        }