Example #1
0
        public static TFile Load(TextReader reader)
        {
            var file = new TFile();

            file.Rows = file.Read(RowReader.Create(reader));
            return(file);
        }
        public void TestInitialize()
        {
            _description = new CsvFileDescription
            {
                SeparatorChar = ',',
                FirstLineHasColumnNames = true,
                IgnoreUnknownColumns = true,
            };
            _input =
@"Id,Name,Last Name,Age,City
1,John,Doe,15,Washington
2,Jane,Doe,20,New York
";
            Expected = new[]
                {
                    new Person
                        {
                            Name = "John",
                            LastName = "Doe",
                            Age = 15
                        },
                    new Person
                        {
                            Name = "Jane",
                            LastName = "Doe",
                            Age = 20
                        }
                };
            _stream = _input.GetStreamReader();
            _dataAccess = new FileDataAccess(_stream, _description);
            _reader = _dataAccess.ReadDataPreparation<Person>(null);
            _dataAccess.Row = new DataRow();
        }
 /// <summary>
 /// Constructor that will deal with streams.
 /// </summary>
 /// <param name="stream">The stream the csv data exists in.</param>
 /// <param name="options">The stream options we need to use for parsing.</param>
 public CsvStreamReader(Stream stream, CsvStreamOptions options)
 {
     _options      = options;
     _stream       = stream;
     _rowReader    = new RowReader(new CharReader(_stream), _options.RowDelimiter, _options.Wrapper);
     _headerReader = new RowReader(new CharReader(_stream), _options.HeaderRowDelimiter, _options.Wrapper);
 }
        internal static Result ReadOperationResult(Memory <byte> input, out BatchOperationResult batchOperationResult)
        {
            RowBuffer row = new RowBuffer(input.Length);

            if (!row.ReadFrom(
                    input.Span,
                    HybridRowVersion.V1,
                    BatchSchemaProvider.BatchLayoutResolver))
            {
                batchOperationResult = null;
                return(Result.Failure);
            }

            RowReader reader = new RowReader(ref row);
            Result    result = BatchOperationResult.ReadOperationResult(ref reader, out batchOperationResult);

            if (result != Result.Success)
            {
                return(result);
            }

            // Ensure the mandatory fields were populated
            if (batchOperationResult.StatusCode == default(HttpStatusCode))
            {
                return(Result.Failure);
            }

            return(Result.Success);
        }
    public static IEnumerable <CountryInfo> ReadRows(string path)
    {
        foreach (var split in RowReader.ReadRows(path))
        {
            CountryInfo row = new()
            {
                Iso        = split[0] !,
                Iso3       = split[1] !,
                IsoNumeric = ushort.Parse(split[2] !),
                Fips       = split[3] !,
                Name       = split[4] !,
                Capital    = split[5] !,
                //Population = uint.Parse(split[7]),
                Continent      = split[8] !,
                TopLevelDomain = split[9] !,
                CurrencyCode   = split[10] !,
                CurrencyName   = split[11] !,
                PhonePrefix    = split[12] !,
                PostCodeFormat = split[13] !,
                PostCodeRegex  = split[14] !,
            };
            var languages = split[15];
            if (languages != null)
            {
                row.Languages = languages.Split(',').ToList();
            }
            var s = split[6] !;
            row.Area = double.Parse(s);

            yield return(row);
        }
    }
}
Example #6
0
    public static IEnumerable <PostCodeRow> ReadRows(string allCountriesTxtPath)
    {
        foreach (var split in RowReader.ReadRows(allCountriesTxtPath))
        {
            var row = new PostCodeRow
            {
                CountryCode   = split[0],
                PostalCode    = split[1],
                PlaceName     = split[2],
                State         = split[3],
                StateCode     = split[4],
                Province      = split[5],
                ProvinceCode  = split[6],
                Community     = split[7],
                CommunityCode = split[8],
                Latitude      = double.Parse(split[9]),
                Longitude     = double.Parse(split[10]),
            };
            Assert.NotEmpty(row.PostalCode);
            var accuracy = split[11];
            if (accuracy != null)
            {
                row.Accuracy = ushort.Parse(accuracy);
            }

            yield return(row);
        }
    }
        public void TestInitialize()
        {
            _description = new CsvFileDescription
            {
                SeparatorChar           = ',',
                FirstLineHasColumnNames = true,
                IgnoreUnknownColumns    = true,
            };
            _input =
                @"Id,Name,Last Name,Age,City
1,John,Doe,15,Washington
2,Jane,Doe,20,New York
";
            Expected = new[]
            {
                new Person
                {
                    Name     = "John",
                    LastName = "Doe",
                    Age      = 15
                },
                new Person
                {
                    Name     = "Jane",
                    LastName = "Doe",
                    Age      = 20
                }
            };
            _stream         = _input.GetStreamReader();
            _dataAccess     = new FileDataAccess(_stream, _description);
            _reader         = _dataAccess.ReadDataPreparation <Person>(null);
            _dataAccess.Row = new DataRow();
        }
Example #8
0
        public void TestLogRecord_FullFields_2()
        {
            FieldMapperReading <LogRecord> fm = new FieldMapperReading <LogRecord>(_fileDescriptionNamesUs, null, false);

            Assert.IsNotNull(fm);
            Assert.IsNotNull(fm.FieldIndexInfo);
            Assert.IsNotNull(fm.NameToInfo);
            Assert.AreEqual(fm.NameToInfo.Count, 27);
            Assert.IsNotNull(fm.NameToInfo["PCI"]);
            Assert.AreEqual(fm.NameToInfo["PCI"].HasColumnAttribute, true);
            List <int> charLengths = fm.GetCharLengths();

            Assert.IsNull(charLengths);

            FileDataAccess dataAccess = new FileDataAccess(_testInput.GetStreamReader(), _fileDescriptionNamesUs);

            Assert.IsNotNull(dataAccess);
            RowReader <LogRecord> reader = dataAccess.ReadDataPreparation <LogRecord>(null);

            Assert.IsNotNull(reader);
            dataAccess.Row = new DataRow();

            List <LogRecord> records = dataAccess.ReadFieldDataRows(reader, null, fm, null).ToList();

            Assert.IsNotNull(records);
            Assert.AreEqual(records.Count, 1);
            Assert.AreEqual(records[0].Id, 0);
            Assert.AreEqual(records[0].Time.Millisecond, 359);
            Assert.AreEqual(records[0].Sinr, 10.1);
            Assert.AreEqual(records[0].UlMcs, 17);
            Assert.AreEqual(records[0].DlThroughput, 10749096);
        }
        public void TestInitialize()
        {
            _description = new CsvFileDescription
            {
                SeparatorChar           = ',',
                FirstLineHasColumnNames = true,
                IgnoreUnknownColumns    = true,
            };
            _input =
                @"Id,Name,Last Name,Age,City
1,John,Doe,15,Washington";

            byte[] stringAsByteArray = Encoding.UTF8.GetBytes(_input);
            Stream stream            = new MemoryStream(stringAsByteArray);

            _sr         = new StreamReader(stream, Encoding.UTF8);
            _dataAccess = new FileDataAccess(_sr, _description);

            _cs = new CsvStream(_sr, null, _description.SeparatorChar,
                                _description.IgnoreTrailingSeparatorChar);
            _row    = new DataRow();
            _fm     = new FieldMapperReading <Person>(_description, null, false);
            _ae     = new AggregatedException(typeof(Person).ToString(), null, _description.MaximumNbrExceptions);
            _reader = new RowReader <Person>(_description, _ae);
        }
        private void RowReaderButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                // The RowReader class can parse strings for you directly and handle
                // quotes and other things you will encounter in a CSV file
                using (MemoryStream ms = new MemoryStream())
                    using (StreamWriter sw = new StreamWriter(ms))
                    {
                        sw.WriteLine("This,is,\"a row with, commas\",in,     it");
                        sw.Flush();
                        ms.Seek(0, SeekOrigin.Begin);

                        using (StreamReader sr = new StreamReader(ms))
                        {
                            var reader = new RowReader(sr);
                            while (reader.CanRead())
                            {
                                PrintColumnList(reader.ReadRow());
                            }
                        }
                    }
            }
            catch (Exception ex)
            {
                LogError(ex);
            }
        }
		public override void Prepare()
		{
			// Connection should be open at this point, but just in case
			using (DataManager.Current.OpenConnection())
			{
				using (SqlBulkCopy bulk = DataManager.CreateBulkCopy(SqlBulkCopyOptions.Default))
				{
					bool tableInit = true;
					bulk.BatchSize = BulkBatchSize;
					bulk.DestinationTableName = "TEMP_ImportedData";

					using (RowReader<ImportFileRow> reader = Activator.CreateInstance(_readerType))
					{
						while (reader.Read())
						{
							if (tableInit)
							{
								// Init the temp table using the first row
								SqlCommand createTableCmd = DataManager.CreateCommand(
									"create table #TEMP_ImportedData
							}
							//bulk.WriteToServer(
						}
					}
				}
			}
		}
Example #12
0
        /// <summary>
        /// 初始化数据读取器。
        /// </summary>
        /// <param name="dbDataReader">数据源</param>
        /// <param name="options">配置项</param>
        public OverrideDbDataReader(System.Data.IDataReader dbDataReader, DataTableRWOptions options = DataTableRWOptions.None)
        {
            this.dbDataReader = dbDataReader;

            Options = options;

            Reader = new RowReader(dbDataReader);
        }
        /// <summary>
        /// 初始化数据读取器。
        /// </summary>
        /// <param name="dbDataReader">数据源</param>
        /// <param name="options">配置项</param>
        public DbDataReaderReader(DbDataReader dbDataReader, DataTableRWOptions options = DataTableRWOptions.None)
        {
            Options = options;

            this.dbDataReader = dbDataReader;

            Reader = new RowReader(dbDataReader);
        }
Example #14
0
        public Parser(CharReader reader, ParseOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            _reader = reader ?? throw new ArgumentNullException(nameof(reader));

            _rowReader = new RowReader(new ValueReader(_reader, options));
        }
Example #15
0
            public static TestDoc FromHybridRowStream(Stream stream)
            {
                uint length = 0;

                using (BinaryReader binaryReader = new BinaryReader(stream, Encoding.Default, leaveOpen: true))
                {
                    TestDoc.SkipBinaryField(binaryReader); // binaryId
                    TestDoc.SkipBinaryField(binaryReader); // EPK

                    binaryReader.ReadByte();
                    length = binaryReader.ReadUInt32();
                }

                RowBuffer row = new RowBuffer((int)length);

                Assert.IsTrue(row.ReadFrom(stream, (int)length, HybridRowVersion.V1, BatchTestBase.LayoutResolver));
                RowReader reader = new RowReader(ref row);

                TestDoc testDoc = new TestDoc();

                while (reader.Read())
                {
                    Result r;
                    switch (reader.Path)
                    {
                    case "Id":
                        r = reader.ReadString(out string id);
                        Assert.AreEqual(Result.Success, r);
                        testDoc.Id = id;
                        break;

                    case "Cost":
                        r = reader.ReadInt32(out int cost);
                        Assert.AreEqual(Result.Success, r);
                        testDoc.Cost = cost;
                        break;

                    case "Status":
                        r = reader.ReadString(out string status);
                        Assert.AreEqual(Result.Success, r);
                        testDoc.Status = status;
                        break;

                    case "Description":
                        r = reader.ReadString(out string description);
                        Assert.AreEqual(Result.Success, r);
                        testDoc.Description = description;
                        break;
                    }
                }

                return(testDoc);
            }
        private Result ReadOperation(Memory <byte> input, out ItemBatchOperation operation)
        {
            RowBuffer row = new RowBuffer(input.Length);

            if (!row.ReadFrom(input.Span, HybridRowVersion.V1, BatchSchemaProvider.BatchLayoutResolver))
            {
                operation = null;
                return(Result.Failure);
            }

            RowReader reader = new RowReader(ref row);

            return(BatchRequestPayloadReader.ReadOperation(ref reader, this.operations.Count, out operation));
        }
Example #17
0
     protected override IEnumerable <FullName> Read(RowReader reader)
     {
         using (reader)
             while (reader.Read())
             {
                 yield return new FullName
                        {
                            First = reader.Get <string>("First"),
                            Last  = reader.Get <string>("Last")
                        }
             }
         ;
     }
 }
        public void TestWriteOperationWithBinaryIdByteArray()
        {
            ISpanResizer <byte> resizer = new MemorySpanResizer <byte>(100);
            RowBuffer           row     = new RowBuffer(capacity: 100, resizer: resizer);

            row.InitLayout(HybridRowVersion.V1, BatchSchemaProvider.BatchOperationLayout, BatchSchemaProvider.BatchLayoutResolver);

            byte[]             testBinaryId   = new byte[] { 1, 2, 3, 4, };
            ItemRequestOptions requestOptions = new();

            requestOptions.Properties = new Dictionary <string, object>()
            {
                { WFConstants.BackendHeaders.BinaryId, testBinaryId },
            };
            TransactionalBatchItemRequestOptions transactionalBatchItemRequestOptions =
                TransactionalBatchItemRequestOptions.FromItemRequestOptions(requestOptions);
            ItemBatchOperation operation = new ItemBatchOperation(
                operationType: OperationType.Patch,
                operationIndex: 0,
                partitionKey: Cosmos.PartitionKey.Null,
                requestOptions: transactionalBatchItemRequestOptions);

            int length = operation.GetApproximateSerializedLength();

            Assert.AreEqual(testBinaryId.Length, length);

            Result r = RowWriter.WriteBuffer(ref row, operation, ItemBatchOperation.WriteOperation);

            if (r != Result.Success)
            {
                Assert.Fail(r.ToString());
            }

            bool      foundBinaryId = false;
            RowReader reader        = new RowReader(ref row);

            while (reader.Read())
            {
                if (reader.PathSpan == Utf8String.TranscodeUtf16("binaryId"))
                {
                    foundBinaryId = true;
                    reader.ReadBinary(out byte[] binaryId);
                    CollectionAssert.AreEqual(testBinaryId, binaryId);
                }
            }

            Assert.IsTrue(foundBinaryId);
        }
Example #19
0
        public void FirstRow(int cellIndex, string address, string value)
        {
            // Arrange
            var reader = RowReader.Create(_package.Workbook[FirstSheetName]);

            // Act
            var result = reader.Read();

            // Assert
            Assert.IsTrue(result);
            Assert.IsNotNull(reader.Row);
            Assert.AreEqual(3, reader.Row.Cells.Length);

            Assert.AreEqual(address, reader.Row.Cells[cellIndex].Address);
            Assert.AreEqual(value, reader.Row.Cells[cellIndex].Value);
        }
Example #20
0
        public void TestLogRecord_FullFields()
        {
            FieldMapperReading <LogRecord> fm = new FieldMapperReading <LogRecord>(_fileDescriptionNamesUs, null, false);

            Assert.IsNotNull(fm);
            Assert.IsNotNull(fm.FieldIndexInfo);
            Assert.IsNotNull(fm.NameToInfo);
            Assert.AreEqual(fm.NameToInfo.Count, 27);
            Assert.IsNotNull(fm.NameToInfo["PCI"]);
            Assert.AreEqual(fm.NameToInfo["PCI"].HasColumnAttribute, true);
            List <int> charLengths = fm.GetCharLengths();

            Assert.IsNull(charLengths);

            FileDataAccess dataAccess = new FileDataAccess(_testInput.GetStreamReader(), _fileDescriptionNamesUs);

            Assert.IsNotNull(dataAccess);
            RowReader <LogRecord> reader = dataAccess.ReadDataPreparation <LogRecord>(null);

            Assert.IsNotNull(reader);

            Assert.IsNotNull(dataAccess.Cs);
            dataAccess.Row = new DataRow();
            Assert.IsTrue(dataAccess.Cs.ReadRow(dataAccess.Row));

            bool readingResult = reader.ReadingOneFieldRow(fm, dataAccess.Row, true);

            Assert.IsFalse(readingResult);

            Assert.IsTrue(dataAccess.Cs.ReadRow(dataAccess.Row));
            Assert.AreEqual(dataAccess.Row[0].Value, "0");
            Assert.AreEqual(dataAccess.Row[1].Value, "13:58:08:359");
            Assert.AreEqual(dataAccess.Row.Count, 18, "row count");
            Assert.AreEqual(fm.FieldIndexInfo.IndexToInfo.Length, 27, "index to info");

            Assert.AreEqual(fm.FieldIndexInfo.GetMaxRowCount(18), 18);

            TypeFieldInfo tfi = fm.FieldIndexInfo.QueryTypeFieldInfo(true, 1);

            Assert.IsNotNull(tfi);
            Assert.AreEqual(tfi.OutputFormat, "HH:mm:ss.fff");

            string value = dataAccess.Row[1].Value;

            Assert.AreEqual(value, "13:58:08:359");
        }
Example #21
0
        private void ProcessData(Delivery delivery)
        {
            Type objType = null;

            switch (Instance.Configuration.Options["ReportReaderType"])
            {
            case "BingKeywordReportReader":
                objType = typeof(BingKeywordReportReader);
                break;

            case "BingAdPerformanceReportReader":
                objType = typeof(BingAdPerformanceReportReader);
                break;
            }
            string[] filesPath = new string[2];
            for (int i = 0; delivery.Files.Count >= i + 1; i++)
            {
                Type dRederType = Type.GetType(delivery.Files[i].ReaderType);
                if (dRederType == typeof(BingAdPerformanceReportReader))
                {
                    filesPath[1] = delivery.Files[i].FilePath;
                }
                else if (dRederType == typeof(BingKeywordReportReader))
                {
                    filesPath[0] = delivery.Files[i].FilePath;
                }
            }

            using (RowReader <PpcDataUnit> reader = (RowReader <PpcDataUnit>)Activator.CreateInstance(objType, new object[] { filesPath }))
            {
                using (DataManager.Current.OpenConnection())
                {
                    DataManager.Current.StartTransaction();

                    while (reader.Read())
                    {
                        if (reader.CurrentRow.AccountID > 0)
                        {
                            reader.CurrentRow.Save();
                        }
                    }
                    DataManager.Current.CommitTransaction();
                }
            }
        }
Example #22
0
        public IEnumerable <Record> GetRows(string format, string sheetName, IProgress <ProgressData> progress = null)
        {
            Sheet sheet = sheets.Descendants <Sheet>().FirstOrDefault(sheet => sheet.Name == sheetName);

            if (sheet == null)
            {
                return(Enumerable.Empty <Record>());
            }
            WorksheetPart worksheetPart = (WorksheetPart)(workbookPart.GetPartById(sheet.Id));
            var           rows          = worksheetPart.Worksheet.Descendants <Row>();
            List <Record> list          = new List <Record>();
            int           total         = rows.Count();
            int           count         = 0;

            string[] headers = null;
            foreach (Row row in rows)
            {
                count++;
                if (row.RowIndex == 1)
                {
                    RowReader rowReader = new RowReader(this, row);
                    int       length    = format.Count(c => "bsicaBSIX".Contains(c));
                    headers = new string[length];
                    for (int i = 0; i < length; ++i)
                    {
                        headers[i] = rowReader.ReadString();
                    }
                }
                else
                {
                    Record record = new Record(format, headers);
                    record.ReadRow(this, row);
                    progress?.Report(new ProgressData {
                        count = count, total = total, filename = record[0].ToString()
                    });
                    list.Add(record);
                }
            }
            return(list);
        }
        private void BothButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var columnList = new List <string>();

                List <string> stringList = new List <string>();
                stringList.Add("jack, says");
                stringList.Add("he ");
                stringList.Add("ran");
                stringList.Add("over");
                stringList.Add("John's");
                stringList.Add("foot");

                using (MemoryStream ms = new MemoryStream())
                    using (StreamWriter sw = new StreamWriter(ms))
                    {
                        var writer = new RowWriter(sw);
                        writer.Write(stringList);
                        sw.Flush();

                        ms.Seek(0, SeekOrigin.Begin);
                        using (StreamReader sr = new StreamReader(ms))
                        {
                            var reader = new RowReader(sr);
                            while (reader.CanRead())
                            {
                                PrintColumnList(reader.ReadRow());
                            }
                        }
                    }
            }
            catch (Exception ex)
            {
                LogError(ex);
            }
        }
Example #24
0
        public void TestInitialize()
        {
            _description = new CsvFileDescription
            {
                SeparatorChar = ',',
                FirstLineHasColumnNames = true,
                IgnoreUnknownColumns = true,
            };
            _input =
@"Id,Name,Last Name,Age,City
1,John,Doe,15,Washington";

            byte[] stringAsByteArray = Encoding.UTF8.GetBytes(_input);
            Stream stream = new MemoryStream(stringAsByteArray);
            _sr = new StreamReader(stream, Encoding.UTF8);
            _dataAccess = new FileDataAccess(_sr, _description);
            
            _cs = new CsvStream(_sr, null, _description.SeparatorChar,
                _description.IgnoreTrailingSeparatorChar);
            _row = new DataRow();
            _fm = new FieldMapperReading<Person>(_description, null, false);
            _ae = new AggregatedException(typeof(Person).ToString(), null, _description.MaximumNbrExceptions);
            _reader = new RowReader<Person>(_description, _ae);
        }
        private static Result ReadOperationResult(ref RowReader reader, out BatchOperationResult batchOperationResult)
        {
            batchOperationResult = new BatchOperationResult();
            while (reader.Read())
            {
                Result r;
                switch (reader.Path)
                {
                case "statusCode":
                    r = reader.ReadInt32(out int statusCode);
                    if (r != Result.Success)
                    {
                        return(r);
                    }

                    batchOperationResult.StatusCode = (HttpStatusCode)statusCode;
                    break;

                case "subStatusCode":
                    r = reader.ReadInt32(out int subStatusCode);
                    if (r != Result.Success)
                    {
                        return(r);
                    }

                    batchOperationResult.SubStatusCode = (SubStatusCodes)subStatusCode;
                    break;

                case "eTag":
                    r = reader.ReadString(out string eTag);
                    if (r != Result.Success)
                    {
                        return(r);
                    }

                    batchOperationResult.ETag = eTag;
                    break;

                case "resourceBody":
                    r = reader.ReadBinary(out byte[] resourceBody);
                    if (r != Result.Success)
                    {
                        return(r);
                    }

                    batchOperationResult.ResourceStream = new MemoryStream(
                        buffer: resourceBody, index: 0, count: resourceBody.Length, writable: false, publiclyVisible: true);
                    break;

                case "retryAfterMilliseconds":
                    r = reader.ReadUInt32(out uint retryAfterMilliseconds);
                    if (r != Result.Success)
                    {
                        return(r);
                    }

                    batchOperationResult.RetryAfter = TimeSpan.FromMilliseconds(retryAfterMilliseconds);
                    break;
                }
            }

            return(Result.Success);
        }
        private static Result ReadOperation(ref RowReader reader, int operationIndex, out ItemBatchOperation operation)
        {
            operation = null;

            OperationType operationType    = OperationType.Invalid;
            string        partitionKeyJson = null;

            byte[] effectivePartitionKey = null;
            string id = null;

            byte[] binaryId     = null;
            byte[] resourceBody = null;
            Cosmos.IndexingDirective?indexingDirective = null;
            string ifMatch      = null;
            string ifNoneMatch  = null;
            int?   ttlInSeconds = null;

            while (reader.Read())
            {
                Result r;
                switch (reader.Path)
                {
                case "operationType":
                    r = reader.ReadInt32(out int operationTypeInt);
                    if (r != Result.Success)
                    {
                        return(r);
                    }

                    operationType = (OperationType)operationTypeInt;
                    break;

                case "resourceType":
                    r = reader.ReadInt32(out int resourceType);
                    if (r != Result.Success)
                    {
                        return(r);
                    }

                    Assert.AreEqual(ResourceType.Document, (ResourceType)resourceType);
                    break;

                case "partitionKey":
                    r = reader.ReadString(out partitionKeyJson);
                    if (r != Result.Success)
                    {
                        return(r);
                    }

                    break;

                case "effectivePartitionKey":
                    r = reader.ReadBinary(out effectivePartitionKey);
                    if (r != Result.Success)
                    {
                        return(r);
                    }

                    break;

                case "id":
                    r = reader.ReadString(out id);
                    if (r != Result.Success)
                    {
                        return(r);
                    }

                    break;

                case "binaryId":
                    r = reader.ReadBinary(out binaryId);
                    if (r != Result.Success)
                    {
                        return(r);
                    }

                    break;

                case "resourceBody":
                    r = reader.ReadBinary(out resourceBody);
                    if (r != Result.Success)
                    {
                        return(r);
                    }

                    break;

                case "indexingDirective":
                    r = reader.ReadString(out string indexingDirectiveStr);
                    if (r != Result.Success)
                    {
                        return(r);
                    }

                    if (!Enum.TryParse <Cosmos.IndexingDirective>(indexingDirectiveStr, out Cosmos.IndexingDirective indexingDirectiveEnum))
                    {
                        return(Result.Failure);
                    }

                    indexingDirective = indexingDirectiveEnum;

                    break;

                case "ifMatch":
                    r = reader.ReadString(out ifMatch);
                    if (r != Result.Success)
                    {
                        return(r);
                    }

                    break;

                case "ifNoneMatch":
                    r = reader.ReadString(out ifNoneMatch);
                    if (r != Result.Success)
                    {
                        return(r);
                    }

                    break;

                case "timeToLiveInSeconds":
                    r = reader.ReadInt32(out int ttl);
                    if (r != Result.Success)
                    {
                        return(r);
                    }

                    ttlInSeconds = ttl;
                    break;
                }
            }

            // Ensure the mandatory fields were populated
            if (operationType == OperationType.Invalid)
            {
                return(Result.Failure);
            }

            TransactionalBatchItemRequestOptions requestOptions = null;

            if (indexingDirective.HasValue || ifMatch != null || ifNoneMatch != null || binaryId != null || effectivePartitionKey != null || ttlInSeconds.HasValue)
            {
                requestOptions = new TransactionalBatchItemRequestOptions();
                if (indexingDirective.HasValue)
                {
                    requestOptions.IndexingDirective = indexingDirective;
                }

                if (ifMatch != null)
                {
                    requestOptions.IfMatchEtag = ifMatch;
                }
                else if (ifNoneMatch != null)
                {
                    requestOptions.IfNoneMatchEtag = ifNoneMatch;
                }

                if (binaryId != null || effectivePartitionKey != null || ttlInSeconds.HasValue)
                {
                    requestOptions.Properties = new Dictionary <string, object>();

                    if (binaryId != null)
                    {
                        requestOptions.Properties.Add(WFConstants.BackendHeaders.BinaryId, binaryId);
                    }

                    if (effectivePartitionKey != null)
                    {
                        requestOptions.Properties.Add(WFConstants.BackendHeaders.EffectivePartitionKey, effectivePartitionKey);
                    }

                    if (ttlInSeconds.HasValue)
                    {
                        requestOptions.Properties.Add(WFConstants.BackendHeaders.TimeToLiveInSeconds, ttlInSeconds.ToString());
                    }
                }
            }

            Documents.PartitionKey parsedPartitionKey = null;
            if (partitionKeyJson != null)
            {
                parsedPartitionKey = Documents.PartitionKey.FromJsonString(partitionKeyJson);
            }

            operation = new ItemBatchOperation(
                operationType: operationType,
                operationIndex: operationIndex,
                id: id,
                requestOptions: requestOptions)
            {
                ParsedPartitionKey = parsedPartitionKey,
                ResourceBody       = resourceBody
            };

            return(Result.Success);
        }
Example #27
0
 private static Func <TdsColumnReader, T> GetRowReader <T>(TdsPackageReader reader, string key) where T : class, new()
 {
     return((Func <TdsColumnReader, T>)Readers.GetOrAdd(key, x => RowReader.GetComplexReader <T>(reader)));
 }
Example #28
0
        private static Result ReadOperationResult(ref RowReader reader, out TransactionalBatchOperationResult batchOperationResult)
        {
            batchOperationResult = new TransactionalBatchOperationResult();
            while (reader.Read())
            {
                Result r;
                switch (reader.Path)
                {
                case "statusCode":
                    r = reader.ReadInt32(out int statusCode);
                    if (r != Result.Success)
                    {
                        return(r);
                    }

                    batchOperationResult.StatusCode = (HttpStatusCode)statusCode;
                    break;

                case "subStatusCode":
                    r = reader.ReadInt32(out int subStatusCode);
                    if (r != Result.Success)
                    {
                        return(r);
                    }

                    batchOperationResult.SubStatusCode = (SubStatusCodes)subStatusCode;
                    break;

                case "eTag":
                    r = reader.ReadString(out string eTag);
                    if (r != Result.Success)
                    {
                        return(r);
                    }

                    batchOperationResult.ETag = eTag;
                    break;

                case "resourceBody":
                    r = reader.ReadBinary(out byte[] resourceBody);
                    if (r != Result.Success)
                    {
                        return(r);
                    }

                    batchOperationResult.ResourceStream = new MemoryStream(
                        buffer: resourceBody, index: 0, count: resourceBody.Length, writable: false, publiclyVisible: true);
                    break;

                case "requestCharge":
                    r = reader.ReadFloat64(out double requestCharge);
                    if (r != Result.Success)
                    {
                        return(r);
                    }

                    // Round request charge to 2 decimals on the operation results
                    // similar to how we round them for the full response.
                    batchOperationResult.RequestCharge = Math.Round(requestCharge, 2);
                    break;

                case "retryAfterMilliseconds":
                    r = reader.ReadUInt32(out uint retryAfterMilliseconds);
                    if (r != Result.Success)
                    {
                        return(r);
                    }

                    batchOperationResult.RetryAfter = TimeSpan.FromMilliseconds(retryAfterMilliseconds);
                    break;
                }
            }

            return(Result.Success);
        }
Example #29
0
        public IEnumerable <TModel> Read()
        {
            _errors.Clear();
            var rowIndex = 0;

            var firstRow = RowReader.Read();

            if (firstRow == null)
            {
                if (HasHeader)
                {
                    throw new TableIOException(new[] { new ErrorDetail
                                                       {
                                                           Type    = "NoTableHeader",
                                                           Message = "Table header is none."
                                                       } });
                }
                else
                {
                    yield break;
                }
            }

            // decide valid column size.
            // ** all row's column size must be valid column size. **
            var validColumnSize = ColumnSize ?? firstRow.Count;

            if (PropertyMapper.RequiredHeaderOnRead && !HasHeader)
            {
                throw new TableIOException(new[] { new ErrorDetail
                                                   {
                                                       Type     = "HeaderRequired",
                                                       Message  = $"Header is required on read.",
                                                       RowIndex = rowIndex
                                                   } });
            }

            var propertyMaps = PropertyMapper.CreatePropertyMaps(typeof(TModel), HasHeader ? firstRow.Select(f => $"{f}").ToArray() : null);
            var propertyMapMaxColumnIndex = propertyMaps.Any() ? propertyMaps.Max(m => m.ColumnIndex) : -1;

            if (propertyMapMaxColumnIndex >= validColumnSize)
            {
                throw new TableIOException(new[] { new ErrorDetail
                                                   {
                                                       Type     = "OutOfRangeColumnIndexMapping",
                                                       Message  = $"Max column index({propertyMapMaxColumnIndex}) of property mapping is greater than or equal to valid column size({validColumnSize}).",
                                                       RowIndex = rowIndex
                                                   } });
            }

            var row = firstRow;

            if (HasHeader)
            {
                rowIndex++;
                row = RowReader.Read();
            }

            while (row != null)
            {
                if (row.Count != validColumnSize)
                {
                    _errors.Add(new ErrorDetail
                    {
                        Type     = "InvalidColumnSize",
                        Message  = "Column size is invalid.",
                        RowIndex = rowIndex
                    });
                    if (_errors.Count >= ErrorLimit)
                    {
                        throw new TableIOException(_errors);
                    }
                }
                else
                {
                    yield return(ConvertFromRow(row, rowIndex, propertyMaps));
                }

                rowIndex++;
                row = RowReader.Read();
            }

            if (_errors.Any())
            {
                throw new TableIOException(_errors);
            }
        }
Example #30
0
 protected abstract IEnumerable <TRow> Read(RowReader read);
Example #31
0
        public void GoodFileNoSeparatorCharUSEnglish()
        {
            // Arrange

            CsvFileDescription fileDescription_namesUs = new CsvFileDescription
            {
                NoSeparatorChar = true,
                UseOutputFormatForParsingCsvValue = false,
                FirstLineHasColumnNames           = false,
                EnforceCsvColumnAttribute         = true, // default is false
                FileCultureName = "en-US"                 // default is the current culture
            };

            const string testInput = @"AAAAAAAA34.18405/23/08
BBBBBBBB10.31105/12/12
CCCCCCCC12.00012/23/08";

            var expected = new[] {
                new ProductDataCharLength
                {
                    Name = "AAAAAAAA", Weight = 34.184, StartDate = new DateTime(2008, 5, 23),
                },
                new ProductDataCharLength {
                    Name = "BBBBBBBB", Weight = 10.311, StartDate = new DateTime(2012, 5, 12),
                },
                new ProductDataCharLength {
                    Name = "CCCCCCCC", Weight = 12.000, StartDate = new DateTime(2008, 12, 23),
                }
            };

            // Act and Assert
            FileDataAccess dataAccess = new FileDataAccess(testInput.GetStreamReader(), fileDescription_namesUs);
            RowReader <ProductDataCharLength> reader = dataAccess.ReadDataPreparation <ProductDataCharLength>(null);

            dataAccess.Row = new DataRow();
            FieldMapperReading <ProductDataCharLength> fm = new FieldMapperReading <ProductDataCharLength>(
                fileDescription_namesUs, null, false);
            List <int> charLengths = fm.GetCharLengths();

            Assert.AreEqual(charLengths.Count, 3);
            bool firstRow = true;
            List <ProductDataCharLength> actual = new List <ProductDataCharLength>();

            while (dataAccess.Cs.ReadRow(dataAccess.Row, charLengths))
            {
                if ((dataAccess.Row.Count == 1) && ((dataAccess.Row[0].Value == null) ||
                                                    (string.IsNullOrEmpty(dataAccess.Row[0].Value.Trim()))))
                {
                    continue;
                }

                bool readingResult = reader.ReadingOneFieldRow(fm, dataAccess.Row, firstRow);

                if (readingResult)
                {
                    actual.Add(reader.Obj);
                }
                firstRow = false;
            }
            AssertCollectionsEqual(actual, expected);
        }