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}");
        }
Beispiel #2
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.");
            }
        }