/// <summary> /// Checks that the given POCO type is valid and doesn't contain repeated column names /// </summary> public static void ValidatePocoType <A>() { var colNames = ReportParserUtils.GetColumnNamesFromPocoType <A>(); var uniqueNames = colNames.ToDictionary(name => name); if (colNames.Count() != uniqueNames.Count()) { throw new System.ArgumentException("The given row type contains duplicate column names."); } }
/// <summary> /// Constructor. /// </summary> /// <param name="reader">Provides the text of the report.</param> /// <param name="reportName">An arbitrary name given to this report.</param> /// <param name="onError">A callback action that is used when there is /// an error in parsing an attribute from the report.</param> public AwReport(InputTextReader reader, string reportName, Action <ColumnValuePair, A> onError) { if (reader == null) { throw new ArgumentNullException("The input text reader cannot be null."); } ReportParserUtils.ValidatePocoType <A>(); this.ReportName = reportName; this.reader = reader; this.OnError = onError; ReportParserUtils.GetColumnNamesFromPocoType <A>() .ToList().ForEach(name => colNames.Add(name)); }
/// <summary> /// Parses the current row from the input reader. /// </summary> /// <returns>The row that was just parsed</returns> /// <exception cref="AdWordsReportsException"> /// Throws an error if the current row could not be parsed because the reader has ended /// or there is invalid input. /// </exception> private A ParseCurrentRow() { var record = new A(); var foundColumns = new HashSet <string>(); IEnumerable <ColumnValuePair> rowData = reader.GetAttributes(); foreach (ColumnValuePair attribute in rowData) { if (colNames.Contains(attribute.ColName)) { foundColumns.Add(attribute.ColName); ReportParserUtils.SetColumnValue <A>(attribute, record, OnError); } } if (foundColumns.Count == 0 && colNames.Count != 0) { throw new AdWordsReportsException("None of the required columns " + "were found. The stream might not have any of the required columns " + "or moveNextRow() was not called before requesting the first row."); } return(record); }