Esempio n. 1
0
        internal override void WriteInner(dynamic row)
        {
            try
            {
                WriteHeadersAndEndRowIfNeeded(row);

                var wholeRowContext = WriteContext.DiscoveringCells(Configuration.Options, RowNumber, Context);

                var options        = Configuration.Options;
                var valueSeparator = Configuration.ValueSeparatorMemory.Span;

                var cellValuesMem = Utils.GetCells(Configuration.DynamicMemoryPool, ref CellBuffer, options.TypeDescriber, in wholeRowContext, row as object);

                var columnNamesValue = Utils.NonNullValue(ColumnNames);

                Utils.ForceInOrder(columnNamesValue, ColumnNameSorter, cellValuesMem);
                var cellValuesEnumerableSpan = cellValuesMem.Span;

                var i = 0;
                foreach (var cell in cellValuesEnumerableSpan)
                {
                    var needsSeparator = i != 0;

                    if (needsSeparator)
                    {
                        PlaceAllInStaging(valueSeparator);
                    }

                    ColumnIdentifier ci;
                    if (i < columnNamesValue.Length)
                    {
                        var name = columnNamesValue.GetColumnAt(i);
                        ci = ColumnIdentifier.CreateInner(i, null, name);
                    }
                    else
                    {
                        ci = ColumnIdentifier.Create(i);
                    }

                    var ctx = WriteContext.WritingColumn(Configuration.Options, RowNumber, ci, Context);

                    var formatter   = cell.Formatter;
                    var delProvider = (ICreatesCacheableDelegate <Formatter.DynamicFormatterDelegate>)formatter;
                    var del         = delProvider.Guarantee(this);

                    var val = cell.Value as object;
                    if (!del(val, in ctx, Buffer))
                    {
                        Throw.SerializationException($"Could not write column {ci}, formatter {formatter} returned false");
                    }

                    ReadOnlySequence <char> res = default;
                    if (!Buffer.MakeSequence(ref res))
                    {
                        // nothing was written, so just move on
                        goto end;
                    }

                    WriteValue(res);
                    Buffer.Reset();

end:
                    i++;
                }

                RowNumber++;
            }
            catch (Exception e)
            {
                Throw.PoisonAndRethrow(this, e);
            }
        }