public TModel Parse <TModel>([NotNull] ITableParser tableParser, [NotNull] RenderingTemplate template, Action <string, string> addFieldMapping)
            where TModel : new()
        {
            var model = new TModel();

            var enumerablesLengths = GetEnumerablesLengths <TModel>(tableParser, template);

            foreach (var row in template.Content.Cells)
            {
                foreach (var cell in row)
                {
                    tableParser.PushState(cell.CellPosition);

                    var expression = cell.StringValue;

                    if (TemplateDescriptionHelper.IsCorrectValueDescription(expression))
                    {
                        ParseCellularValue(tableParser, addFieldMapping, model, ExcelTemplatePath.FromRawExpression(expression), enumerablesLengths);
                        continue;
                    }
                    if (TemplateDescriptionHelper.IsCorrectFormValueDescription(expression))
                    {
                        ParseFormValue(tableParser, addFieldMapping, model, cell, ExcelTemplatePath.FromRawExpression(expression));
                        continue;
                    }

                    tableParser.PopState();
                }
            }

            return(model);
        }
示例#2
0
        public int GetLength(ITableParser tableParser, Type modelType, IEnumerable <ICell> primaryParts)
        {
            var parserState = new List <(IAtomicValueParser parser, ICell cell, Type itemType)>();

            foreach (var primaryPart in primaryParts)
            {
                var childModelPath = ExcelTemplatePath.FromRawExpression(primaryPart.StringValue);
                var childModelType = ObjectPropertiesExtractor.ExtractChildObjectTypeFromPath(modelType, childModelPath);

                var parser = parserCollection.GetAtomicValueParser();

                parserState.Add((parser, primaryPart, childModelType));
            }

            for (var i = 0; i <= ParsingParameters.MaxEnumerableLength; i++)
            {
                var parsed = false;
                foreach (var(parser, cell, type) in parserState)
                {
                    tableParser.PushState(cell.CellPosition.Add(new ObjectSize(0, i)));
                    if (parser.TryParse(tableParser, type, out var result) && result != null)
                    {
                        parsed = true;
                    }
                    tableParser.PopState();
                }
                if (!parsed)
                {
                    return(i);
                }
            }
            throw new EnumerableTooLongException(ParsingParameters.MaxEnumerableLength);
        }
示例#3
0
        public List <object> Parse([NotNull] ITableParser tableParser, [NotNull] Type modelType, int count, [NotNull] Action <string, string> addFieldMapping)
        {
            if (count < 0)
            {
                throw new InvalidOperationException($"Count should be positive ({count} found)");
            }
            if (count > ParsingParameters.MaxEnumerableLength)
            {
                throw new InvalidOperationException($"Lists longer than {ParsingParameters.MaxEnumerableLength} are not supported");
            }

            var parser = parserCollection.GetAtomicValueParser();
            var result = new List <object>();

            for (var i = 0; i < count; i++)
            {
                if (i != 0)
                {
                    tableParser.MoveToNextLayer();
                }

                tableParser.PushState();

                if (!parser.TryParse(tableParser, modelType, out var item) || item == null)
                {
                    item = GetDefault(modelType);
                }

                addFieldMapping($"[{i}]", tableParser.CurrentState.Cursor.CellReference);
                result.Add(item);
                tableParser.PopState();
            }

            return(result);
        }