예제 #1
0
        public OutputReferencesPattern(string pattern, string referencesCollectionName = null)
        {
            ReferencesCollectionName = referencesCollectionName;

            var fieldToFormatPosition = ValidatePattern(pattern, out var formattedPattern);

            _builder = new DocumentIdBuilder(pattern, formattedPattern, fieldToFormatPosition);
        }
예제 #2
0
        public OutputReferencesPattern(string pattern)
        {
            var matches = FieldsRegex.Matches(pattern);

            if (matches.Count == 0)
            {
                throw new InvalidOperationException("Provided pattern is not supported: " + pattern);
            }

            var fieldToFormatPosition = new Dictionary <string, int>(matches.Count);
            int numberOfFields        = 0;

            var formattedPattern = FieldsRegex.Replace(pattern, StringFormatEvaluator);

            _builder = new DocumentIdBuilder(pattern, formattedPattern, fieldToFormatPosition);

            string StringFormatEvaluator(Match m)
            {
                var groups = m.Groups;

                try
                {
                    if (groups.Count == 2)
                    {
                        // {OrderedAt}

                        return($"{{{numberOfFields}}}");
                    }

                    if (groups.Count == 3)
                    {
                        // {OrderedAt:yyyy-MM-dd}

                        return($"{{{numberOfFields}:{groups[2]}}}");
                    }
                }
                finally
                {
                    string fieldName = groups[1].ToString();

                    if (fieldToFormatPosition.ContainsKey(fieldName))
                    {
                        throw new NotSupportedException($"Pattern should contain unique fields only. Duplicated field: '{fieldName}'");
                    }

                    fieldToFormatPosition.Add(fieldName, numberOfFields);
                    numberOfFields++;
                }

                throw new NotSupportedException("Provided pattern is not supported: " + pattern);
            }
        }
예제 #3
0
        public IDisposable BuildReferenceDocumentId(out DocumentIdBuilder builder)
        {
            builder = _builder;

            return(_builder);
        }