public static string GetDataContainerFactoryType(this SourceDefinition sourceDefinition)
        {
            string factoryName;

            switch (sourceDefinition.ValueType)
            {
            case ValueType.Value:
                factoryName = "DataContainerFactory";
                break;

            case ValueType.Array:
                factoryName = "CollectionDataContainerFactory";
                break;

            case ValueType.NullableArray:
                factoryName = "CollectionNullableDataContainerFactory";
                break;

            default:
                throw new Exception("Unrecognized source definition type.");
            }

            if (sourceDefinition.IsNullable)
            {
                return($"Nullable{factoryName}");
            }

            return(factoryName);
        }
        public static string Generate(SourceDefinition source)
        {
            IEnumerable <ValidatorGroups[]> paths;

            switch (source.ValueType)
            {
            case ValueType.Value:
                paths = Data.ValueValidationPaths;
                break;

            case ValueType.Array:
                paths = Data.ArrayValidationPaths;
                break;

            default:
                return(String.Empty);
            }

            var root = new ExtensionPathNode(null, null);

            foreach (var path in paths)
            {
                AddValidators(root, path);
            }

            var extensions = ExtensionGenerator.GenerateInvertExtensionOne(source)
                             .Concat(GenerateExtensions(source, root));

            var header = _header.Replace("__StateValidator__", source.GetSourceName(Option.None <string>()));

            return($"{header}{String.Join($"{Environment.NewLine}{Environment.NewLine}", extensions)}{_footer}");
        }
 public static string GetParameters(this ExtensionDefinition extension, SourceDefinition sourceDefinition, string valueGenericName)
 => String
 .Join
 (
     String.Empty,
     extension
     .Parameters
     .Where(p => !p.Value.HasValue())
     .Select(p => $", {p.GetTypeName(GetValidatorValueType(sourceDefinition, valueGenericName, Option.Some(extension.Validator)))} {p.Name}{p.DefaultValue.Match(v => $" = {v}", () => String.Empty)}")
 );
        public static string GetDataValueType(this SourceDefinition sourceDefinition, string valueName)
        {
            var dataValueName = GetValidatorValueType(sourceDefinition, valueName, Option.None <ValidatorDefinition>());

            if (sourceDefinition.IsNullable)
            {
                return($"Option<{dataValueName}>");
            }

            return(dataValueName);
        }
        public static IEnumerable <string> GenerateExtensionOne(SourceDefinition sourceDefinition, Option <string> dataType, ExtensionDefinition extension)
        {
            if (sourceDefinition.ValueType == ValueType.Value && extension.Validator.ValueType != ValueType.Value)
            {
                throw new Exception("Array extensions cannot be generated for value type sources.");
            }

            var valueGenericName = dataType.Match(_ => _, () => "TValue");

            var genericParameters = dataType.Match(_ => Array.Empty <string>(), () => new[] { "TValue" });

            yield return(PopulateTemplate(_singleExtensionTemplate, sourceDefinition, valueGenericName, genericParameters, null, null, extension, false, false));
        }
 private static string PopulateTemplate(string template, SourceDefinition sourceDefinition, string valueGenericName, string[] genericParameters, ValidatorDefinition validatorOne, ValidatorDefinition validatorTwo, ExtensionDefinition extension, bool invertOne, bool invertTwo)
 => template
 .Replace("__Nullable__", $"{(sourceDefinition.IsNullable ? "Nullable" : String.Empty)}")
 .Replace("__InvertOne__", $"{(invertOne ? "Inverted" : "Standard")}")
 .Replace("__InvertTwo__", $"{(invertTwo ? "Inverted" : "Standard")}")
 .Replace("__StateValidator__", sourceDefinition.GetSourceName(Option.Some(valueGenericName)))
 .Replace("__ValueValidatorOne__", validatorOne?.GetValidatorName(GetValueForValidator(sourceDefinition, validatorOne, valueGenericName)) ?? String.Empty)
 .Replace("__ValueValidatorTwo__", validatorTwo?.GetValidatorName(GetValueForValidator(sourceDefinition, validatorTwo, valueGenericName)) ?? String.Empty)
 .Replace("__NewValueValidator__", extension?.Validator.GetValidatorName(GetValueForValidator(sourceDefinition, extension?.Validator, valueGenericName)) ?? String.Empty)
 .Replace("__TValueType__", sourceDefinition.ValueType == ValueType.Array ? $"{valueGenericName}[]" : valueGenericName)
 .Replace("__ExtensionName__", extension?.ExtensionName ?? String.Empty)
 .Replace("__GenericParameters__", genericParameters.Any() ? $"<{String.Join(", ", genericParameters)}>" : String.Empty)
 .Replace("__ExtensionParameters__", String.Join(String.Empty, extension?.Parameters.Where(p => !p.Value.HasValue()).Select(p => $", {p.GetTypeName(GetValueForValidator(sourceDefinition, extension?.Validator, valueGenericName))} {p.Name}{p.DefaultValue.Match(v => $" = {v}", () => String.Empty)}") ?? Enumerable.Empty <string>()))
 .Replace("__Parameters__", String.Join(", ", extension?.Parameters.Select(p => p.Value.Match(v => v, () => p.Name)) ?? Enumerable.Empty <string>()));
        public static IEnumerable <string> GenerateInvertExtensionTwo(SourceDefinition sourceDefinition, Option <string> dataType, ValidatorDefinition validatorOne)
        {
            if (sourceDefinition.ValueType == ValueType.Value && validatorOne.ValueType != ValueType.Value)
            {
                throw new Exception("Array extensions cannot be generated for value type sources.");
            }

            var valueGenericName = dataType.Match(_ => _, () => "TValue");

            var genericParameters = dataType.Match(_ => new[] { "TValueValidator" }, () => new[] { "TValueValidator", "TValue" });

            yield return(PopulateTemplate(_doubleNotTemplate, sourceDefinition, valueGenericName, genericParameters, validatorOne, null, null, false, false));

            yield return(PopulateTemplate(_doubleNotTemplate, sourceDefinition, valueGenericName, genericParameters, validatorOne, null, null, true, false));
        }
        public static IEnumerable <string> GenerateExtensionThree(SourceDefinition sourceDefinition, Option <string> dataType, ValidatorDefinition validatorOne, ValidatorDefinition validatorTwo, ExtensionDefinition extension)
        {
            if (sourceDefinition.ValueType == ValueType.Value && (validatorOne.ValueType != ValueType.Value || validatorTwo.ValueType != ValueType.Value || extension.Validator.ValueType != ValueType.Value))
            {
                throw new Exception("Array extensions cannot be generated for value type sources.");
            }

            var valueGenericName = dataType.Match(_ => _, () => ValueGeneric);

            var genericParameters = dataType.Match(_ => Array.Empty <string>(), () => new[] { ValueGeneric });

            yield return(PopulateTemplate(_tripleExtensionTemplate, sourceDefinition, valueGenericName, genericParameters, validatorOne, validatorTwo, extension, false, false));

            yield return(PopulateTemplate(_tripleExtensionTemplate, sourceDefinition, valueGenericName, genericParameters, validatorOne, validatorTwo, extension, true, false));

            yield return(PopulateTemplate(_tripleExtensionTemplate, sourceDefinition, valueGenericName, genericParameters, validatorOne, validatorTwo, extension, false, true));

            yield return(PopulateTemplate(_tripleExtensionTemplate, sourceDefinition, valueGenericName, genericParameters, validatorOne, validatorTwo, extension, true, true));
        }
        public static string GetValidatorValueType(this SourceDefinition sourceDefinition, string valueName, Option <ValidatorDefinition> validator)
        {
            var validatorType = validator.Select(v => v.ValueType).ToNullable();

            switch (sourceDefinition.ValueType)
            {
            case ValueType.Value:
                switch (validatorType ?? ValueType.Value)
                {
                case ValueType.Value:
                    return(valueName);
                }
                break;

            case ValueType.Array:
                switch (validatorType ?? ValueType.Value)
                {
                case ValueType.Value:
                    return($"{valueName}[]");

                case ValueType.Array:
                    return(valueName);
                }
                break;

            case ValueType.NullableArray:
                switch (validatorType ?? ValueType.Value)
                {
                case ValueType.Value:
                    return($"Option<{valueName}>[]");

                case ValueType.Array:
                    return($"Option<{valueName}>");

                case ValueType.NullableArray:
                    return(valueName);
                }
                break;
            }

            throw new Exception("Unsupported source and validator type combination.");
        }
 private static string PopulateTemplate(string template, SourceDefinition sourceDefinition, string valueGenericName, string[] genericParameters, ValidatorDefinition validatorOne, ValidatorDefinition validatorTwo, ExtensionDefinition extension, bool invertOne, bool invertTwo, bool joinSourceAndValue = false)
 => template
 .Replace("__InvertOne__", $"{(invertOne ? "Inverted" : "Standard")}")
 .Replace("__InvertTwo__", $"{(invertTwo ? "Inverted" : "Standard")}")
 .Replace("__Inverted__", $"{((extension?.Invert ?? false) ? "Inverted" : "Standard")}")
 .Replace("__DataContainerFactory__", sourceDefinition.GetDataContainerFactoryType())
 .Replace("__StateValidator__", sourceDefinition.GetSourceName(valueGenericName))
 .Replace("__TSourceType__", joinSourceAndValue ? valueGenericName : SourceGeneric)
 .Replace("__TValueType__", valueGenericName)
 .Replace("__TDataValueType__", sourceDefinition.GetDataValueType(valueGenericName))
 .Replace("__TValidatorValueType__", sourceDefinition.GetValidatorValueType(valueGenericName, Option.None <ValidatorDefinition>()))
 .Replace("__ValueValidatorOne__", validatorOne?.GetName(sourceDefinition, valueGenericName) ?? String.Empty)
 .Replace("__ValueValidatorTwo__", validatorTwo?.GetName(sourceDefinition, valueGenericName) ?? String.Empty)
 .Replace("__NewValueValidator__", extension?.Validator.GetName(sourceDefinition, valueGenericName) ?? String.Empty)
 .Replace("__ExtensionName__", extension?.ExtensionName ?? String.Empty)
 .Replace("__GenericParameters__", HelperExtensions.ConstructorGenericParameters(genericParameters, Option.Create(!joinSourceAndValue, SourceGeneric)))
 .Replace("__ExtensionParameters__", extension?.GetParameters(sourceDefinition, valueGenericName) ?? String.Empty)
 .Replace("__Arguments__", extension?.GetArguments() ?? String.Empty)
 .Replace("__NotOpen__", $"{((extension?.Invert ?? false) ? @".Not(s => s" : String.Empty)}")
 .Replace("__NotClose__", $"{((extension?.Invert ?? false) ? ")" : String.Empty)}");
Example #11
0
        private static IEnumerable <string> GenerateExtension(SourceDefinition source, ExtensionPathNode node)
        {
            if (!node.Extensions.Any())
            {
                return(Enumerable.Empty <string>());
            }

            var nodes = new List <ExtensionPathNode>();

            var current = node;

            while (current != null)
            {
                nodes.Insert(0, current);
                current = current.Parent;
            }

            switch (nodes.Count)
            {
            case 1:
                return(nodes[0]
                       .Extensions
                       .SelectMany(extension => ExtensionGenerator.GenerateExtensionOne(source, node.DataType.Match(Option.Some, () => extension.DataType), extension)));

            case 2:
                return(nodes[1]
                       .Extensions
                       .SelectMany(extension => ExtensionGenerator.GenerateExtensionTwo(source, node.DataType.Match(Option.Some, () => extension.DataType), nodes[1].Validator, extension))
                       .Concat(ExtensionGenerator.GenerateInvertExtensionTwo(source, node.DataType, nodes[1].Validator)));

            case 3:
                return(nodes[2]
                       .Extensions
                       .SelectMany(extension => ExtensionGenerator.GenerateExtensionThree(source, node.DataType.Match(Option.Some, () => extension.DataType), nodes[1].Validator, nodes[2].Validator, extension))
                       .Concat(ExtensionGenerator.GenerateInvertExtensionThree(source, node.DataType, nodes[1].Validator, nodes[2].Validator)));

            default:
                throw new Exception("Extensions of this length are not supported.");
            }
        }
 public static string GetName(this ValidatorDefinition validator, SourceDefinition sourceDefinition, string valueGenericName)
 => validator.GetValidatorName(sourceDefinition.GetValidatorValueType(valueGenericName, Option.Some(validator)));
 private static string GetValueForValidator(SourceDefinition sourceDefinition, ValidatorDefinition validator, string valueGenericName)
 => sourceDefinition.ValueType == ValueType.Array && validator?.ValueType == ValueType.Value ? $"{valueGenericName}[]" : valueGenericName;
 public static IEnumerable <string> GenerateInvertExtensionOne(SourceDefinition sourceDefinition)
 {
     yield return(PopulateTemplate(_singleNotTemplate, sourceDefinition, "TValue", new[] { "TValueValidator", "TValue" }, null, null, null, false, false));
 }
        public static IEnumerable <string> GenerateInvertExtensionOne(SourceDefinition sourceDefinition)
        {
            yield return(PopulateTemplate(_validatorNotTemplate, sourceDefinition, ValueGeneric, new[] { ValueGeneric, ValueValidatorGeneric }, null, null, null, false, false, true));

            yield return(PopulateTemplate(_singleNotTemplate, sourceDefinition, ValueGeneric, new[] { ValueGeneric, ValueValidatorGeneric }, null, null, null, false, false));
        }
Example #16
0
 private static IEnumerable <string> GenerateExtensions(SourceDefinition source, ExtensionPathNode node)
 => GenerateExtension(source, node)
 .Concat(node.Children.SelectMany(child => GenerateExtensions(source, child)));