public IDtoPropertyIsValidCheckConfig GeneratePropertyIsValidCheckRules(
            IPropertySymbol propertySymbol,
            AttributeSymbols attributeSymbols,
            ImmutableArray <string> allClassesAddingDto)
        {
            var    propertyAttributes = propertySymbol.GetAttributes();
            string fullDataTypeName   = PropertyUtilities.GenerateDataTypeFullNameFromProperty(propertySymbol);

            if (DataTypeIsNullable(fullDataTypeName))
            {
                return(CreateAllowNullPropertyConfig());
            }
            else if (DataTypeIsString(fullDataTypeName))
            {
                return(CreateDtoPropertyCheckConfigForString(propertyAttributes, attributeSymbols));
            }
            else if (DataTypeIsAnotherDto(fullDataTypeName, allClassesAddingDto))
            {
                return(CreateDtoPropertyCheckConfigForDto(propertyAttributes, attributeSymbols));
            }

            return(CreateDefaultPropertyConfig());
        }
        private DtoBasicPropertyIsValidCheckConfig?CreateBasicPropertyCheckFromAttribute(ImmutableArray <AttributeData> propertyAttributes, AttributeSymbols attributeSymbols)
        {
            var basicPropertyAttribute = propertyAttributes.FirstOrDefault(x => x.AttributeClass?.Equals(attributeSymbols.DtoBasicPropertyCheckAttributeSymbol, SymbolEqualityComparer.Default) is true);

            if (basicPropertyAttribute is object)
            {
                var allowNullValue = basicPropertyAttribute.NamedArguments.FirstOrDefault(x => x.Key == nameof(BasicPropertyCheckAttribute.AllowNull)).Value;
                var allowNull      = (bool)allowNullValue.Value !;

                return(new DtoBasicPropertyIsValidCheckConfig(allowNull));
            }

            return(null);
        }
        private IDtoPropertyIsValidCheckConfig CreateDtoPropertyCheckConfigForBasicProperty(ImmutableArray <AttributeData> propertyAttributes, AttributeSymbols attributeSymbols)
        {
            DtoBasicPropertyIsValidCheckConfig?propertyCheckConfig = CreateBasicPropertyCheckFromAttribute(propertyAttributes, attributeSymbols);

            if (propertyCheckConfig is object)
            {
                return(propertyCheckConfig);
            }

            return(new DtoBasicPropertyIsValidCheckConfig(AllowNull: false));
        }
        private IDtoPropertyIsValidCheckConfig CreateDtoPropertyCheckConfigForString(ImmutableArray <AttributeData> propertyAttributes, AttributeSymbols attributeSymbols)
        {
            var stringPropertyAttribute = propertyAttributes.FirstOrDefault(x => x.AttributeClass?.Equals(attributeSymbols.DtoStringPropertyCheckAttributeSymbol, SymbolEqualityComparer.Default) is true);

            if (stringPropertyAttribute is object)
            {
                var value           = stringPropertyAttribute.NamedArguments.FirstOrDefault(x => x.Key == nameof(StringPropertyCheckAttribute.StringIsValidCheckType)).Value;
                var stringCheckType = (StringIsValidCheckType)(value.Value !);

                return(new DtoStringPropertyIsValidCheckConfig(stringCheckType));
            }

            //A string could instead have the DtoBasicPropertyCheckAttribute attribute applied. Check for that too
            DtoBasicPropertyIsValidCheckConfig?propertyCheckConfig = CreateBasicPropertyCheckFromAttribute(propertyAttributes, attributeSymbols);

            if (propertyCheckConfig is object && propertyCheckConfig.AllowNull)
            {
                return(new DtoStringPropertyIsValidCheckConfig(StringIsValidCheckType.AllowNull));
            }

            return(new DtoStringPropertyIsValidCheckConfig(StringPropertyCheckAttribute.DefaultStringIsValidCheckType));
        }
        private IDtoPropertyIsValidCheckConfig CreateDtoPropertyCheckConfigForDto(ImmutableArray <AttributeData> propertyAttributes, AttributeSymbols attributeSymbols)
        {
            var dtoPropertyAttribute = propertyAttributes.FirstOrDefault(x => x.AttributeClass?.Equals(attributeSymbols.DtoPropertyCheckAttributeSymbol, SymbolEqualityComparer.Default) is true);

            if (dtoPropertyAttribute is object)
            {
                var checkIsValidValue = dtoPropertyAttribute.NamedArguments.FirstOrDefault(x => x.Key == nameof(DtoPropertyCheckAttribute.CheckIsValid)).Value;
                var checkIsValid      = (bool)checkIsValidValue.Value !;

                var allowNullValue = dtoPropertyAttribute.NamedArguments.FirstOrDefault(x => x.Key == nameof(BasicPropertyCheckAttribute.AllowNull)).Value;
                var allowNull      = (bool)allowNullValue.Value !;

                return(new DtoPropertyIsValidCheckConfig(allowNull, checkIsValid));
            }

            DtoBasicPropertyIsValidCheckConfig?propertyCheckConfig = CreateBasicPropertyCheckFromAttribute(propertyAttributes, attributeSymbols);

            if (propertyCheckConfig is object)
            {
                return(propertyCheckConfig);
            }

            return(new DtoPropertyIsValidCheckConfig(CheckIsValid: DtoPropertyCheckAttribute.DefaultCheckIsValid, AllowNull: BasicPropertyCheckAttribute.DefaultAllowNull));
        }
Exemple #6
0
        private SourceText GenerateDTO(DtoClassInfo newClassInfo, ImmutableArray <string> allClassesAddingDto, AttributeSymbols attributeSymbols)
        {
            var classSymbol    = newClassInfo.ClassSymbol;
            var className      = GenerateDTOClassName(classSymbol);
            var classNamespace = classSymbol.ContainingNamespace.ToDisplayString();

            bool newClassIsRecord = newClassInfo.IsRecord;

            var isValidCheckCodeConfigGenerator = new IsValidCheckCodeConfigGenerator();
            var properties = classSymbol.GetMembers()
                             .Where(x => x is IPropertySymbol)
                             .Cast <IPropertySymbol>()
                             .Select(propertySymbol =>
            {
                //Skip any properties auto-generated for records
                if (newClassIsRecord &&
                    string.Equals(propertySymbol.Name, "EqualityContract", StringComparison.Ordinal))       //Casing matters for this
                {
                    return(null);
                }

                /*
                 * Possible data types to consider
                 *
                 * int, string
                 * int?, string?
                 * Nullable<int>, Nullable<string>
                 * Integer, String
                 * List<int>, List<string>
                 * int[], string[],
                 * int[]?, string[]?
                 * int?[]?, string?[]?
                 *
                 *
                 * MyClass
                 */

                var isValidCheckConfig = isValidCheckCodeConfigGenerator.GeneratePropertyIsValidCheckRules(propertySymbol, attributeSymbols, allClassesAddingDto);

                return(new DtoProperty(
                           PropertySymbol: propertySymbol,
                           IsValidCheckConfig: isValidCheckConfig));
            })
                             .Where(x => x is object)
                             .Select(x => x !)
                             .ToImmutableArray();

            string sourceText = GenerateDtoClassSourceText(className, classNamespace, properties);

            return(SourceText.From(sourceText, encoding: Encoding.UTF8));
        }