private List <IConverter> CreateConverters(string key, SourceTargetMapping mapping) { var converters = new List <IConverter>(); foreach (var targetConverter in mapping.TargetConverters) { if (targetConverter.LeftSideMap != null && targetConverter.LeftSideMap.ContainsKey(key)) { var config = new ConverterConfiguration() { Id = targetConverter.Id, CombineInputOutput = targetConverter.CombineInputOutput, NestOutput = targetConverter.NestOutput, LeftSideMap = targetConverter.LeftSideMap, Properties = (targetConverter.Properties.Count > 0) ? targetConverter.Properties : mapping.Properties, PipedConverters = converters, Mapping = mapping }; IConverter converter = CollectorFactory.CloneConverter(Converters[targetConverter.Id]); if (converter != null) { converter.Configure(config); converters.Add(converter); } } } return(converters); }
/// <summary> /// Convert an entire row of data. /// </summary> /// <param name="dataRow">The row to convert</param> /// <returns>The converted row.</returns> public Dictionary <string, object> ConvertDataRow(SourceTargetMapping mapping, IEntityCollection dataRow) { var mappedRow = new Dictionary <string, object>(); // Convert the data points... foreach (var dataPoint in dataRow.Entities) { var convertedDataPoints = ConvertDataPoint(mapping, dataPoint, dataRow); if (convertedDataPoints != null) { foreach (var convertedDataPoint in convertedDataPoints) { // Last conversion wins if (mappedRow.ContainsKey(convertedDataPoint.Key)) { mappedRow[convertedDataPoint.Key] = convertedDataPoint.Value; } else { mappedRow.Add(convertedDataPoint.Key, convertedDataPoint.Value); } } } } return(mappedRow); }
/// <summary> /// Convert a single data point. /// </summary> /// <param name="dataPoint">The data point to convert</param> /// <param name="dataRow">The associated row data points</param> /// <returns>A dictionary of converted data.</returns> public Dictionary <string, object> ConvertDataPoint(SourceTargetMapping mapping, KeyValuePair <string, object> dataPoint, IEntityCollection dataRowIn) { Dictionary <string, object> result = null; if (mapping != null) { // create any converters that are targeting this data point. var converters = CreateConverters(dataPoint.Key, mapping); // make our local copy of the data row, we may need to manipulate it for nested conversions. var dataRow = new EntityCollection(); foreach (var e in dataRowIn.Entities) { dataRow.Entities.Add(e.Key, e.Value); } Dictionary <string, object> convertedDataPoints = null; foreach (var converter in converters) { var config = GetConverterConfiguration(converter.Id, mapping.TargetConverters); var convertedDataPointsOut = converter.Convert(dataPoint, dataRow); convertedDataPoints = CombineData(config, convertedDataPoints, convertedDataPointsOut); result = convertedDataPoints; if (!config.Pipe) { break; } } } return(result); }
/// <summary> /// Configure the converter. /// </summary> /// <param name="config">The converter configuration.</param> public void Configure(ConverterConfiguration config) { _id = config.Id; _combineInputOutput = config.CombineInputOutput; _nestOutput = config.NestOutput; _leftSideMap = config.LeftSideMap; _properties = config.Properties; _mapping = config.Mapping; _pipedConverters = config.PipedConverters; }
public void PipedConverters_PipedArray_Success() { // Use Case: A data point contains an array of objects (software items). // The PipedArrayConverter uses reflection to get each field, convert the field based on // the configured converters for the associated source target mapping. // The converter returns a dictionary of dictionaries based on an id. // The id is determined in the SourceTargetConverter property "ArrayKey". // The property "PrefixId" means to prefix the id with the "ArrayKey", i.e. "Id_XXX" // where XXX is the Id field of the software. var items = CreateMockSoftwareArray(); var row = new EntityCollection(); row.Entities.Add("Items", items); var properties = new Dictionary <string, string>(); properties.Add("ArrayKey", "Id"); properties.Add("PrefixId", "true"); var targetConverters = new List <SourceTargetConverter>(); var mapping = new SourceTargetMapping() { //PipeData = true, Properties = properties, PrimaryKey = "Items", TargetConverters = targetConverters }; var noOpConverter = new NoOpConverter(); var leftSideMap = new Dictionary <string, List <string> >(); leftSideMap.Add("Id", new List <string>() { "Id" }); leftSideMap.Add("Title", new List <string>() { "Title" }); leftSideMap.Add("DateTime", new List <string>() { "DateTime" }); var noOpConfig = new ConverterConfiguration() { Id = "1", LeftSideMap = leftSideMap }; noOpConverter.Configure(noOpConfig); var converters = new List <IConverter>(); converters.Add(noOpConverter); var converter = new PipedArrayConverter(); converters.Add(converter); var config = new ConverterConfiguration() { Id = "2", Mapping = mapping, PipedConverters = converters, Properties = properties }; converter.Configure(config); var convertedData = converter.Convert(new KeyValuePair <string, object>("Items", items), row); convertedData.Count.Should().Be(2); foreach (var id in convertedData.Keys) { if (id.Contains("_pkAttrName")) { continue; } var software = convertedData[id] as List <object>; var software0 = software[0] as Dictionary <string, object>; software0["Id"].Should().Be("1"); software0["Title"].Should().Be("Some Software Title #1"); software0["DateTime"].Should().NotBeNull(); var software1 = software[1] as Dictionary <string, object>; software1["Id"].Should().Be("2"); software1["Title"].Should().Be("Some Software Title #2"); software1["DateTime"].Should().NotBeNull(); } }