Skip to content

feat: PointData Immutability #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 16, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Client.Test/MeasurementMapperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void ColumnWithoutName()

var lineProtocol = _mapper.ToPoint(poco, WritePrecision.S).ToLineProtocol();

Assert.AreEqual("poco,tag=tag\\ val ValueWithEmptyName=25,ValueWithoutDefaultName=20i,value=15.444 864000", lineProtocol);
Assert.AreEqual("poco,tag=tag\\ val value=15.444,ValueWithEmptyName=25,ValueWithoutDefaultName=20i 864000", lineProtocol);
}

[Test]
Expand Down
30 changes: 28 additions & 2 deletions Client.Test/PointDataTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,30 @@ namespace InfluxDB.Client.Test
[TestFixture]
public class PointDataTest
{
[Test]
public void Immutability()
{
var point = PointData.Measurement("h2 o")
.Tag("location", "europe");

var point1 = point
.Tag("TAG", "VALX")
.Field("level", 2);

var point2 = point
.Tag("TAG", "VALX")
.Field("level", 2);

var point3 = point
.Tag("TAG", "VALY")
.Field("level", 2);

Assert.AreEqual(point1, point2);
Assert.AreNotEqual(point, point1);
Assert.False(ReferenceEquals(point1, point2));
Assert.AreNotEqual(point3, point1);
}

[Test]
public void MeasurementEscape()
{
Expand Down Expand Up @@ -273,7 +297,8 @@ public void DefaultTagsOverride()

var defaults = new PointSettings().AddDefaultTag("expensive", "true");

Assert.AreEqual("h2o,expensive=true,location=europe level=2i", point.ToLineProtocol(defaults));
string actual = point.ToLineProtocol(defaults);
Assert.AreEqual("h2o,expensive=true,location=europe level=2i", actual);
}

[Test]
Expand All @@ -299,7 +324,8 @@ public void DefaultTagsNotOverride()

var defaults = new PointSettings().AddDefaultTag("expensive", "true");

Assert.AreEqual("h2o,expensive=false,location=europe level=2i", point.ToLineProtocol(defaults));
string lineProtocol = point.ToLineProtocol(defaults);
Assert.AreEqual("h2o,expensive=false,location=europe level=2i", lineProtocol);
}

[Test]
Expand Down
13 changes: 7 additions & 6 deletions Client/Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@
</PropertyGroup>

<ItemGroup>
<None Include="..\Assets\influxdata.jpg" Pack="true" PackagePath=""/>
<ProjectReference Include="..\Client.Core\Client.Core.csproj"/>
<None Include="..\Assets\influxdata.jpg" Pack="true" PackagePath="" />
<ProjectReference Include="..\Client.Core\Client.Core.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="JsonSubTypes" Version="1.5.2"/>
<PackageReference Include="Microsoft.Net.Http.Headers" Version="2.1.1"/>
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.5.0"/>
<PackageReference Include="System.Reactive" Version="4.1.2"/>
<PackageReference Include="JsonSubTypes" Version="1.5.2" />
<PackageReference Include="Microsoft.Net.Http.Headers" Version="2.1.1" />
<PackageReference Include="System.Collections.Immutable" Version="1.7.1" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.5.0" />
<PackageReference Include="System.Reactive" Version="4.1.2" />
</ItemGroup>

</Project>
38 changes: 19 additions & 19 deletions Client/Internal/MeasurementMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,29 @@ internal PointData ToPoint<TM>(TM measurement, WritePrecision precision)
var name = !string.IsNullOrEmpty(propertyInfo.Column.Name) ? propertyInfo.Column.Name : propertyInfo.Property.Name;
if (propertyInfo.Column.IsTag)
{
point.Tag(name, value.ToString());
point = point.Tag(name, value.ToString());
}
else if (propertyInfo.Column.IsTimestamp)
{
if (value is long l)
{
point.Timestamp(l, precision);
point = point.Timestamp(l, precision);
}
else if (value is TimeSpan span)
{
point.Timestamp(span, precision);
point = point.Timestamp(span, precision);
}
else if (value is DateTime date)
{
point.Timestamp(date, precision);
point = point.Timestamp(date, precision);
}
else if (value is DateTimeOffset offset)
{
point.Timestamp(offset, precision);
point = point.Timestamp(offset, precision);
}
else if (value is Instant instant)
{
point.Timestamp(instant, precision);
point = point.Timestamp(instant, precision);
}
else
{
Expand All @@ -89,55 +89,55 @@ internal PointData ToPoint<TM>(TM measurement, WritePrecision precision)
{
if (value is bool b)
{
point.Field(name, b);
point = point.Field(name, b);
}
else if (value is double d)
{
point.Field(name, d);
point = point.Field(name, d);
}
else if (value is float f)
{
point.Field(name, f);
point = point.Field(name, f);
}
else if (value is decimal dec)
{
point.Field(name, dec);
point = point.Field(name, dec);
}
else if (value is long lng)
{
point.Field(name, lng);
point = point.Field(name, lng);
}
else if (value is ulong ulng)
{
point.Field(name, ulng);
point = point.Field(name, ulng);
}
else if (value is int i)
{
point.Field(name, i);
point = point.Field(name, i);
}
else if (value is byte bt)
{
point.Field(name, bt);
point = point.Field(name, bt);
}
else if (value is sbyte sb)
{
point.Field(name, sb);
point = point.Field(name, sb);
}
else if (value is short sh)
{
point.Field(name, sh);
point = point.Field(name, sh);
}
else if (value is uint ui)
{
point.Field(name, ui);
point = point.Field(name, ui);
}
else if (value is ushort us)
{
point.Field(name, us);
point = point.Field(name, us);
}
else
{
point.Field(name, value.ToString());
point = point.Field(name, value.ToString());
}
}
}
Expand Down
Loading