コード例 #1
0
        private static IExtractionType DetermineExtractionType(string ext)
        {
            //Incorrectly formatted extraction string.
            if (string.IsNullOrWhiteSpace(ext))
            {
                return(new UnknownExtractionType(ext));
            }

            //If the extraction contains a '[' or ']'
            if (ext.Contains("[") || ext.Contains("]"))
            {
                //Extraction must contain ONE pair of square brackets only
                if (ext.Count(x => x == '[') != 1 && ext.Count(x => x == ']') != 1)
                {
                    return(new UnknownExtractionType(ext));
                }

                var openIndex    = ext.IndexOf('[');
                var closeIndex   = ext.IndexOf(']');
                var propertyName = ext.Substring(0, openIndex);

                //If the final character isn't a close bracket or the property name is empty, fail.
                if (!ext.EndsWith("]") || string.IsNullOrEmpty(propertyName))
                {
                    return(new UnknownExtractionType(ext));
                }

                //Parse contents of bracket.
                var contents = ext.Substring(openIndex + 1, closeIndex - openIndex - 1);
                if (int.TryParse(contents, out int index))
                {
                    return(new EnumerableExtractionType(propertyName, index));
                }
                else if (!contents.Contains("|"))
                {
                    return(new KeyEnumerableExtractionType(propertyName, contents));
                }

                var kvp = contents.Split(new[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
                if (kvp.Length != 2)
                {
                    return(new UnknownExtractionType(ext));
                }
                else
                {
                    return(new KeyValueExtractionType(propertyName, kvp[0], kvp[1]));
                }
            }

            if (!IdentifierExtensions.IsValidIdentifier(ext))
            {
                return(new UnknownExtractionType(ext));
            }

            return(new PropertyExtractionType(ext));
        }
コード例 #2
0
        private static IAssignmentType DetermineAssignmentType(string assign)
        {
            if (string.IsNullOrWhiteSpace(assign))
            {
                return(new UnknownAssignmentType(assign));
            }

            if (assign.Contains("[") || assign.Contains("]"))
            {
                //Extraction must contain ONE pair of square brackets only
                if (assign.Count(x => x == '[') != 1 && assign.Count(x => x == ']') != 1)
                {
                    return(new UnknownAssignmentType(assign));
                }

                var openIndex    = assign.IndexOf('[');
                var closeIndex   = assign.IndexOf(']');
                var propertyName = assign.Substring(0, openIndex);

                //If the final character isn't a close bracket or the property name is empty, fail.
                if (!assign.EndsWith("]") || string.IsNullOrEmpty(propertyName))
                {
                    return(new UnknownAssignmentType(assign));
                }

                //Parse contents of bracket.
                var contents = assign.Substring(openIndex + 1, closeIndex - openIndex - 1);
                if (int.TryParse(contents, out int index))
                {
                    return(new EnumerableAssignmentType(propertyName, index));
                }

                var kvp = contents.Split(new[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
                if (kvp.Length != 2)
                {
                    return(new UnknownAssignmentType(assign));
                }
                else
                {
                    return(new KeyValueAssignmentType(propertyName, kvp[0], kvp[1]));
                }
            }

            if (!IdentifierExtensions.IsValidIdentifier(assign))
            {
                return(new UnknownAssignmentType(assign));
            }

            return(new PropertyAssignmentType(assign));
        }