Skip to content

Commit 9bf6e5d

Browse files
authored
feat: add possibility to put generic type object as a value for PointData and PointData.Builder (#295)
1 parent 7e410e3 commit 9bf6e5d

File tree

5 files changed

+59
-1
lines changed

5 files changed

+59
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## 4.0.0-rc3 [unreleased]
22

3+
### Features
4+
1. [#295](https://github.com/influxdata/influxdb-client-csharp/pull/295): Add possibility to put generic type object as a value for `PointData` and `PointData.Builder`
5+
36
## 4.0.0-rc2 [2022-02-25]
47

58
### Migration Notice

Client.Test/PointDataBuilderTest.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,5 +163,15 @@ public void HasFields()
163163
Assert.IsTrue(
164164
PointData.Builder.Measurement("h2o").Tag("location", "europe").Field("level", "2").HasFields());
165165
}
166+
167+
[Test]
168+
public void UseGenericObjectAsFieldValue()
169+
{
170+
var point = PointData.Builder.Measurement("h2o")
171+
.Tag("location", "europe")
172+
.Field("custom-object", new GenericObject { Value1 = "test", Value2 = 10 });
173+
174+
Assert.AreEqual("h2o,location=europe custom-object=\"test-10\"", point.ToPointData().ToLineProtocol());
175+
}
166176
}
167177
}

Client.Test/PointDataTest.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,5 +418,26 @@ public void OnlyInfinityValues()
418418

419419
Assert.AreEqual("", point.ToLineProtocol());
420420
}
421+
422+
[Test]
423+
public void UseGenericObjectAsFieldValue()
424+
{
425+
var point = PointData.Measurement("h2o")
426+
.Tag("location", "europe")
427+
.Field("custom-object", new GenericObject { Value1 = "test", Value2 = 10 });
428+
429+
Assert.AreEqual("h2o,location=europe custom-object=\"test-10\"", point.ToLineProtocol());
430+
}
431+
}
432+
433+
internal class GenericObject
434+
{
435+
internal string Value1 { get; set; }
436+
internal int Value2 { get; set; }
437+
438+
public override string ToString()
439+
{
440+
return $"{Value1}-{Value2}";
441+
}
421442
}
422443
}

Client/Writes/PointData.Builder.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,17 @@ public Builder Field(string name, bool value)
167167
return PutField(name, value);
168168
}
169169

170+
/// <summary>
171+
/// Add a field with an <see cref="object"/> value.
172+
/// </summary>
173+
/// <param name="name">the field name</param>
174+
/// <param name="value">the field value</param>
175+
/// <returns>this</returns>
176+
public Builder Field(string name, object value)
177+
{
178+
return PutField(name, value);
179+
}
180+
170181
/// <summary>
171182
/// Updates the timestamp for the point.
172183
/// </summary>

Client/Writes/PointData.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,17 @@ public PointData Field(string name, bool value)
202202
return PutField(name, value);
203203
}
204204

205+
/// <summary>
206+
/// Add a field with an <see cref="object"/> value.
207+
/// </summary>
208+
/// <param name="name">the field name</param>
209+
/// <param name="value">the field value</param>
210+
/// <returns>this</returns>
211+
public PointData Field(string name, object value)
212+
{
213+
return PutField(name, value);
214+
}
215+
205216
/// <summary>
206217
/// Updates the timestamp for the point.
207218
/// </summary>
@@ -489,7 +500,9 @@ private bool AppendFields(StringBuilder sb)
489500
}
490501
else
491502
{
492-
sb.Append(value);
503+
sb.Append('"');
504+
EscapeValue(sb, value.ToString());
505+
sb.Append('"');
493506
}
494507

495508
sb.Append(',');

0 commit comments

Comments
 (0)