Skip to content

Commit

Permalink
Merge pull request #60 from joakimhew/develop
Browse files Browse the repository at this point in the history
Add missing using directive and extension method for converting ubiquitous series to a strongly typed collection
  • Loading branch information
tihomir-kit authored Oct 2, 2017
2 parents ffac4ef + 77c406b commit 03b34f9
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 0 deletions.
8 changes: 8 additions & 0 deletions InfluxData.Net.Common/RequestClients/RequestClientBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
using System.Diagnostics;
using InfluxData.Net.Common.Helpers;


#if DEBUG

using InfluxData.Net.Common.Helpers;
using System.Diagnostics;

#endif

namespace InfluxData.Net.Common.RequestClients
{
public abstract class RequestClientBase
Expand Down
47 changes: 47 additions & 0 deletions InfluxData.Net.InfluxDb/Helpers/SerieExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using InfluxData.Net.Common.Helpers;
using InfluxData.Net.InfluxDb.Models.Responses;
using System.Reflection;

namespace InfluxData.Net.InfluxDb.Helpers
{
Expand All @@ -25,6 +26,52 @@ public static T FirstRecordValueAs<T>(this Serie serie, string columnKey)
return (T)(firstValue[serie.Columns.IndexOf(columnKey)]);
}

/// <summary>
/// Converts an ubiquitous enumeration of series to a strongly typed enumeration by matching property names.
/// </summary>
/// <typeparam name="T">Type to convert the enumeration of series to</typeparam>
/// <param name="series">Series to convert</param>
/// <returns>Strongly typed enumeration representing the series</returns>
public static IEnumerable<T> RecordsAs<T>(this IEnumerable<Serie> series)
where T : new()
{
if (series == null)
yield return default(T);

Type t = typeof(T);

foreach (var serie in series)
{
var properties = t.GetProperties(BindingFlags.Instance | BindingFlags.Public).ToList();

var matchedProperties = serie.Columns
.Select(columnName => properties.FirstOrDefault(
property => string.Compare(property.Name, columnName, StringComparison.InvariantCultureIgnoreCase) == 0))
.ToList();

foreach (var value in serie.Values)
{
var instance = new T();

for (var columnIndex = 0; columnIndex < serie.Columns.Count(); columnIndex++)
{
var prop = matchedProperties[columnIndex];

if (prop == null)
continue;

Type propType = prop.PropertyType;

var convertedValue = Convert.ChangeType(value[columnIndex], prop.PropertyType);

prop.SetValue(instance, convertedValue);
}

yield return instance;
}
}
}

/// <summary>
/// Extracts a serie object from a serie collection by serie/measurement name.
/// </summary>
Expand Down
3 changes: 3 additions & 0 deletions InfluxData.Net.Tests/InfluxData.Net.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,7 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<Folder Include="InfluxDb\Helpers\" />
</ItemGroup>
</Project>
126 changes: 126 additions & 0 deletions InfluxData.Net.Tests/InfluxDb/Helpers/SerieExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
using InfluxData.Net.InfluxDb.Models.Responses;
using System.Collections.Generic;
using InfluxData.Net.InfluxDb.Helpers;
using System;
using System.Linq;
using Xunit;

namespace InfluxData.Net.Tests.InfluxDb.Helpers
{
[Trait("InfluxDb SerieExtensions", "Serie extensions")]
public class SerieExtensionsTests
{

[Fact]
public void Can_Convert_Ubiquitous_Series_To_Strongly_Typed_Collection()
{
var firstSerialNumber = "F6A2B2C5E1A6";
var secondSerialNumber = "A2B4C2F5D3D1";

var firstCpuTemp = 54.14;
var secondCpuTemp = 23.54;

IList<IList<object>> firstValues = new List<IList<object>>
{
new List<object>
{
firstSerialNumber,
firstCpuTemp
}
};

Serie firstSerie = new Serie
{
Name = "Example serie",
Columns = new List<string> { "serialNumber", "cpuTemp" },
Values = firstValues
};


IList<IList<object>> secondValues = new List<IList<object>>
{
new List<object>
{
secondSerialNumber,
secondCpuTemp
}
};

Serie secondSerie = new Serie
{
Name = "Example serie",
Columns = new List<string> { "serialNumber", "cpuTemp" },
Values = secondValues
};


List<Serie> series = new List<Serie>
{
firstSerie,
secondSerie
};


var stronglyTyped = series.RecordsAs<StronglyTypedSerie>();

var expectedNumberOfConvertedValues = 2;
var actualNumberOfConvertedValues = stronglyTyped.Count();

var expectedSerialNumberOfFirstStronglyTypedObject = firstSerialNumber;
var actualSerialNumberOfFirstStronglyTypedObject = stronglyTyped.First().SerialNumber;

var expectedSerialNumberOfLastStronglyTypedObject = secondSerialNumber;
var actualSerialNumberOfLastStronglyTypedOject = stronglyTyped.Last().SerialNumber;

var expectedCpuTempOfFirstStronglyTypedObject = firstCpuTemp;
var actualCpuTempOfFirstStronglyTypedObject = stronglyTyped.First().CpuTemp;

var expectedCpuTempOfLastStronglyTypedObject = secondCpuTemp;
var actualCpuTempOfLastStronglyTypedOject = stronglyTyped.Last().CpuTemp;

Assert.Equal(expectedNumberOfConvertedValues, actualNumberOfConvertedValues);
Assert.Equal(expectedSerialNumberOfFirstStronglyTypedObject, actualSerialNumberOfFirstStronglyTypedObject);
Assert.Equal(expectedSerialNumberOfLastStronglyTypedObject, actualSerialNumberOfLastStronglyTypedOject);
Assert.Equal(expectedCpuTempOfFirstStronglyTypedObject, actualCpuTempOfFirstStronglyTypedObject);
Assert.Equal(expectedCpuTempOfLastStronglyTypedObject, actualCpuTempOfLastStronglyTypedOject);
}

[Fact]
public void Trying_To_Convert_With_Invalid_Format_Throws_Exception()
{
var firstSerialNumber = "F6A2B2C5E1A6";

var firstCpuTemp = "THIS CANNOT BE CONVERTED TO A DOUBLE";

IList<IList<object>> firstValues = new List<IList<object>>
{
new List<object>
{
firstSerialNumber,
firstCpuTemp
}
};

Serie firstSerie = new Serie
{
Name = "Example serie",
Columns = new List<string> { "serialNumber", "cpuTemp" },
Values = firstValues
};


List<Serie> series = new List<Serie>
{
firstSerie
};

Assert.Throws(typeof(FormatException), () => series.RecordsAs<StronglyTypedSerie>().ToList());
}

class StronglyTypedSerie
{
public string SerialNumber { get; set; }
public double CpuTemp { get; set; }
}
}
}

0 comments on commit 03b34f9

Please sign in to comment.