Skip to content

Commit 8ae7ac3

Browse files
authored
fix: parsing infinite numbers (influxdata#221)
1 parent 466c9fe commit 8ae7ac3

File tree

4 files changed

+47
-5
lines changed

4 files changed

+47
-5
lines changed

CHANGELOG.md

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

3+
### Bug Fixes
4+
1. [#221](https://github.com/influxdata/influxdb-client-csharp/pull/221): Parsing infinite numbers
5+
36
### Dependencies
47
1. [#222](https://github.com/influxdata/influxdb-client-csharp/pull/222): Update dependencies:
58
- RestSharp to 106.12.0

Client.Core/Flux/Exceptions/FluxCsvParserException.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using InfluxDB.Client.Core.Exceptions;
23
using InfluxDB.Client.Core.Flux.Domain;
34

@@ -11,5 +12,9 @@ public class FluxCsvParserException : InfluxException
1112
public FluxCsvParserException(string message) : base(message)
1213
{
1314
}
15+
16+
public FluxCsvParserException(string message, Exception exception = null) : base(message, exception)
17+
{
18+
}
1419
}
1520
}

Client.Core/Flux/Internal/FluxCsvParser.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,9 @@ private class ParseFluxResponseState
209209
{
210210
currentId = Convert.ToInt32(state.csv[1 + 1]);
211211
}
212-
catch (Exception)
212+
catch (Exception e)
213213
{
214-
throw new FluxCsvParserException("Unable to parse CSV response.");
214+
throw new FluxCsvParserException("Unable to parse CSV response.", e);
215215
}
216216
if (state.tableId == -1)
217217
{
@@ -273,7 +273,12 @@ private Object ToValue(string strValue, FluxColumn column)
273273
case "long":
274274
return Convert.ToInt64(strValue);
275275
case "double":
276-
return Convert.ToDouble(strValue, CultureInfo.InvariantCulture);
276+
return strValue switch
277+
{
278+
"+Inf" => double.PositiveInfinity,
279+
"-Inf" => double.NegativeInfinity,
280+
_ => Convert.ToDouble(strValue, CultureInfo.InvariantCulture)
281+
};
277282
case "base64Binary":
278283
return Convert.FromBase64String(strValue);
279284
case "dateTime:RFC3339":
@@ -285,9 +290,9 @@ private Object ToValue(string strValue, FluxColumn column)
285290
return strValue;
286291
}
287292
}
288-
catch (Exception)
293+
catch (Exception e)
289294
{
290-
throw new FluxCsvParserException("Unable to parse CSV response.");
295+
throw new FluxCsvParserException("Unable to parse CSV response.", e);
291296
}
292297
}
293298

Client.Legacy.Test/FluxCsvParserTest.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,35 @@ public void ResponseWithError()
705705
}
706706
}
707707

708+
[Test]
709+
public void ParseInfinite()
710+
{
711+
const string data = @"#group,false,false,true,true,true,true,true,true,true,true,false,false
712+
#datatype,string,long,dateTime:RFC3339,dateTime:RFC3339,string,string,string,string,string,string,double,double
713+
#default,_result,,,,,,,,,,,
714+
,result,table,_start,_stop,_field,_measurement,language,license,name,owner,le,_value
715+
,,0,2021-06-23T06:50:11.897825012Z,2021-06-25T06:50:11.897825012Z,stars,github_repository,C#,MIT License,influxdb-client-csharp,influxdata,0,0
716+
,,0,2021-06-23T06:50:11.897825012Z,2021-06-25T06:50:11.897825012Z,stars,github_repository,C#,MIT License,influxdb-client-csharp,influxdata,10,0
717+
,,0,2021-06-23T06:50:11.897825012Z,2021-06-25T06:50:11.897825012Z,stars,github_repository,C#,MIT License,influxdb-client-csharp,influxdata,20,0
718+
,,0,2021-06-23T06:50:11.897825012Z,2021-06-25T06:50:11.897825012Z,stars,github_repository,C#,MIT License,influxdb-client-csharp,influxdata,30,0
719+
,,0,2021-06-23T06:50:11.897825012Z,2021-06-25T06:50:11.897825012Z,stars,github_repository,C#,MIT License,influxdb-client-csharp,influxdata,40,0
720+
,,0,2021-06-23T06:50:11.897825012Z,2021-06-25T06:50:11.897825012Z,stars,github_repository,C#,MIT License,influxdb-client-csharp,influxdata,50,0
721+
,,0,2021-06-23T06:50:11.897825012Z,2021-06-25T06:50:11.897825012Z,stars,github_repository,C#,MIT License,influxdb-client-csharp,influxdata,60,0
722+
,,0,2021-06-23T06:50:11.897825012Z,2021-06-25T06:50:11.897825012Z,stars,github_repository,C#,MIT License,influxdb-client-csharp,influxdata,70,0
723+
,,0,2021-06-23T06:50:11.897825012Z,2021-06-25T06:50:11.897825012Z,stars,github_repository,C#,MIT License,influxdb-client-csharp,influxdata,80,0
724+
,,0,2021-06-23T06:50:11.897825012Z,2021-06-25T06:50:11.897825012Z,stars,github_repository,C#,MIT License,influxdb-client-csharp,influxdata,90,0
725+
,,0,2021-06-23T06:50:11.897825012Z,2021-06-25T06:50:11.897825012Z,stars,github_repository,C#,MIT License,influxdb-client-csharp,influxdata,+Inf,15
726+
,,0,2021-06-23T06:50:11.897825012Z,2021-06-25T06:50:11.897825012Z,stars,github_repository,C#,MIT License,influxdb-client-csharp,influxdata,-Inf,15
727+
728+
";
729+
730+
var tables = ParseFluxResponse(data);
731+
Assert.AreEqual(1, tables.Count);
732+
Assert.AreEqual(12, tables[0].Records.Count);
733+
Assert.AreEqual(double.PositiveInfinity, tables[0].Records[10].GetValueByKey("le"));
734+
Assert.AreEqual(double.NegativeInfinity, tables[0].Records[11].GetValueByKey("le"));
735+
}
736+
708737
private List<FluxTable> ParseFluxResponse(string data)
709738
{
710739
var consumer = new FluxCsvParser.FluxResponseConsumerTable();

0 commit comments

Comments
 (0)